loadFromURL loads the content of a URL and returns it as a byte slice.
(url string)
| 72 | |
| 73 | // loadFromURL loads the content of a URL and returns it as a byte slice. |
| 74 | func loadFromURL(url string) ([]byte, error) { |
| 75 | // As cosign does: https://github.com/sigstore/cosign/blob/beb9cf21bc6741bc6e6b9736bdf57abfb91599c0/pkg/blob/load.go#L47 |
| 76 | // By default it will attempt a maximum 10 redirects |
| 77 | // #nosec G107 |
| 78 | resp, err := http.Get(url) |
| 79 | if err != nil { |
| 80 | return nil, fmt.Errorf("requesting URL: %w", err) |
| 81 | } |
| 82 | defer resp.Body.Close() |
| 83 | |
| 84 | // Check if the response is OK |
| 85 | if resp.StatusCode != http.StatusOK { |
| 86 | return nil, fmt.Errorf("loading URL: %s", resp.Status) |
| 87 | } |
| 88 | |
| 89 | raw, err := io.ReadAll(resp.Body) |
| 90 | if err != nil { |
| 91 | return nil, fmt.Errorf("loading URL response: %w", err) |
| 92 | } |
| 93 | return raw, nil |
| 94 | } |
| 95 | |
| 96 | // loadFromEnv loads the content of an environment variable and returns it as a byte slice. |
| 97 | func loadFromEnv(envVar string) ([]byte, error) { |
no test coverage detected