|
| 1 | +package loader |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/tls" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "github.com/hashicorp/go-retryablehttp" |
| 9 | + "github.com/santhosh-tekuri/jsonschema/v6" |
| 10 | + "github.com/yannh/kubeconform/pkg/cache" |
| 11 | + "io" |
| 12 | + "net/http" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +type HTTPURLLoader struct { |
| 17 | + client http.Client |
| 18 | + cache cache.Cache |
| 19 | +} |
| 20 | + |
| 21 | +func (l *HTTPURLLoader) Load(url string) (any, error) { |
| 22 | + if l.cache != nil { |
| 23 | + if cached, err := l.cache.Get(url); err == nil { |
| 24 | + return jsonschema.UnmarshalJSON(bytes.NewReader(cached.([]byte))) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + resp, err := l.client.Get(url) |
| 29 | + if err != nil { |
| 30 | + msg := fmt.Sprintf("failed downloading schema at %s: %s", url, err) |
| 31 | + return nil, errors.New(msg) |
| 32 | + } |
| 33 | + defer resp.Body.Close() |
| 34 | + |
| 35 | + if resp.StatusCode == http.StatusNotFound { |
| 36 | + msg := fmt.Sprintf("could not find schema at %s", url) |
| 37 | + return nil, NewNotFoundError(errors.New(msg)) |
| 38 | + } |
| 39 | + |
| 40 | + if resp.StatusCode != http.StatusOK { |
| 41 | + msg := fmt.Sprintf("error while downloading schema at %s - received HTTP status %d", url, resp.StatusCode) |
| 42 | + return nil, fmt.Errorf("%s", msg) |
| 43 | + } |
| 44 | + |
| 45 | + body, err := io.ReadAll(resp.Body) |
| 46 | + if err != nil { |
| 47 | + msg := fmt.Sprintf("failed parsing schema from %s: %s", url, err) |
| 48 | + return nil, errors.New(msg) |
| 49 | + } |
| 50 | + |
| 51 | + if l.cache != nil { |
| 52 | + if err = l.cache.Set(url, body); err != nil { |
| 53 | + return nil, fmt.Errorf("failed to write cache to disk: %s", err) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + s, err := jsonschema.UnmarshalJSON(bytes.NewReader(body)) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + return s, nil |
| 63 | +} |
| 64 | + |
| 65 | +func NewHTTPURLLoader(skipTLS bool, cache cache.Cache) (*HTTPURLLoader, error) { |
| 66 | + transport := &http.Transport{ |
| 67 | + MaxIdleConns: 100, |
| 68 | + IdleConnTimeout: 3 * time.Second, |
| 69 | + DisableCompression: true, |
| 70 | + Proxy: http.ProxyFromEnvironment, |
| 71 | + } |
| 72 | + |
| 73 | + if skipTLS { |
| 74 | + transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} |
| 75 | + } |
| 76 | + |
| 77 | + // retriable http client |
| 78 | + retryClient := retryablehttp.NewClient() |
| 79 | + retryClient.RetryMax = 2 |
| 80 | + retryClient.HTTPClient = &http.Client{Transport: transport} |
| 81 | + retryClient.Logger = nil |
| 82 | + |
| 83 | + httpLoader := HTTPURLLoader{client: *retryClient.StandardClient(), cache: cache} |
| 84 | + return &httpLoader, nil |
| 85 | +} |
0 commit comments