github.com/argoproj/argo-events@v1.9.1/sensors/artifacts/url.go (about) 1 package artifacts 2 3 import ( 4 "crypto/tls" 5 "fmt" 6 "io" 7 "net/http" 8 9 "github.com/argoproj/argo-events/common/logging" 10 "github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" 11 ) 12 13 // URLReader implements the ArtifactReader interface for urls 14 type URLReader struct { 15 urlArtifact *v1alpha1.URLArtifact 16 } 17 18 // NewURLReader creates a new ArtifactReader for workflows at URL endpoints. 19 func NewURLReader(urlArtifact *v1alpha1.URLArtifact) (ArtifactReader, error) { 20 if urlArtifact == nil { 21 return nil, fmt.Errorf("URLArtifact cannot be empty") 22 } 23 return &URLReader{urlArtifact}, nil 24 } 25 26 func (reader *URLReader) Read() ([]byte, error) { 27 log := logging.NewArgoEventsLogger() 28 log.Debugf("reading urlArtifact from %s", reader.urlArtifact.Path) 29 insecureSkipVerify := !reader.urlArtifact.VerifyCert 30 client := &http.Client{ 31 Transport: &http.Transport{ 32 TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify}, 33 }, 34 } 35 resp, err := client.Get(reader.urlArtifact.Path) 36 if err != nil { 37 log.Warnf("failed to read url %s: %s", reader.urlArtifact.Path, err) 38 return nil, err 39 } 40 defer resp.Body.Close() 41 42 if resp.StatusCode != http.StatusOK { 43 log.Warnf("failed to read %s. status code: %d", reader.urlArtifact.Path, resp.StatusCode) 44 return nil, fmt.Errorf("status code %v", resp.StatusCode) 45 } 46 47 content, err := io.ReadAll(io.LimitReader(resp.Body, 512*1024)) 48 if err != nil { 49 log.Warnf("failed to read url body for %s: %s", reader.urlArtifact.Path, err) 50 return nil, err 51 } 52 return content, nil 53 }