|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package repository |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "io" |
| 22 | + "net/http" |
| 23 | + "net/url" |
| 24 | + "path" |
| 25 | + "path/filepath" |
| 26 | + "sort" |
| 27 | + "strings" |
| 28 | + |
| 29 | + "github.com/blang/semver" |
| 30 | + "github.com/pkg/errors" |
| 31 | + "k8s.io/apimachinery/pkg/util/wait" |
| 32 | +) |
| 33 | + |
| 34 | +const ( |
| 35 | + defaultGoProxyHost = "proxy.golang.org" |
| 36 | +) |
| 37 | + |
| 38 | +type goproxyClient struct { |
| 39 | + scheme string |
| 40 | + host string |
| 41 | +} |
| 42 | + |
| 43 | +func newGoproxyClient(scheme, host string) *goproxyClient { |
| 44 | + return &goproxyClient{ |
| 45 | + scheme: scheme, |
| 46 | + host: host, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (g *goproxyClient) getVersions(ctx context.Context, base, owner, repository string) ([]string, error) { |
| 51 | + // A goproxy is also able to handle the github repository path instead of the actual go module name. |
| 52 | + gomodulePath := path.Join(base, owner, repository) |
| 53 | + |
| 54 | + rawURL := url.URL{ |
| 55 | + Scheme: g.scheme, |
| 56 | + Host: g.host, |
| 57 | + Path: path.Join(gomodulePath, "@v", "/list"), |
| 58 | + } |
| 59 | + |
| 60 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL.String(), http.NoBody) |
| 61 | + if err != nil { |
| 62 | + return nil, errors.Wrapf(err, "failed to get versions: failed to create request") |
| 63 | + } |
| 64 | + |
| 65 | + var rawResponse []byte |
| 66 | + var retryError error |
| 67 | + _ = wait.PollImmediateWithContext(ctx, retryableOperationInterval, retryableOperationTimeout, func(ctx context.Context) (bool, error) { |
| 68 | + retryError = nil |
| 69 | + |
| 70 | + resp, err := http.DefaultClient.Do(req) |
| 71 | + if err != nil { |
| 72 | + retryError = errors.Wrapf(err, "failed to get versions: failed to do request") |
| 73 | + return false, nil |
| 74 | + } |
| 75 | + defer resp.Body.Close() |
| 76 | + |
| 77 | + if resp.StatusCode != 200 { |
| 78 | + retryError = errors.Errorf("failed to get versions: response status code %d", resp.StatusCode) |
| 79 | + return false, nil |
| 80 | + } |
| 81 | + |
| 82 | + rawResponse, err = io.ReadAll(resp.Body) |
| 83 | + if err != nil { |
| 84 | + retryError = errors.Wrap(err, "failed to get versions: error reading goproxy response body") |
| 85 | + return false, nil |
| 86 | + } |
| 87 | + return true, nil |
| 88 | + }) |
| 89 | + if retryError != nil { |
| 90 | + return nil, retryError |
| 91 | + } |
| 92 | + |
| 93 | + parsedVersions := semver.Versions{} |
| 94 | + for _, s := range strings.Split(string(rawResponse), "\n") { |
| 95 | + if s == "" { |
| 96 | + continue |
| 97 | + } |
| 98 | + parsedVersion, err := semver.ParseTolerant(s) |
| 99 | + if err != nil { |
| 100 | + // Discard releases with tags that are not a valid semantic versions (the user can point explicitly to such releases). |
| 101 | + continue |
| 102 | + } |
| 103 | + parsedVersions = append(parsedVersions, parsedVersion) |
| 104 | + } |
| 105 | + |
| 106 | + sort.Sort(parsedVersions) |
| 107 | + |
| 108 | + versions := []string{} |
| 109 | + for _, v := range parsedVersions { |
| 110 | + versions = append(versions, "v"+v.String()) |
| 111 | + } |
| 112 | + |
| 113 | + return versions, nil |
| 114 | +} |
| 115 | + |
| 116 | +// getGoproxyHost detects and returns the scheme and host for goproxy requests. |
| 117 | +// It returns empty strings if goproxy is disabled via `off` or `direct` values. |
| 118 | +func getGoproxyHost(goproxy string) (string, string, error) { |
| 119 | + // Fallback to default |
| 120 | + if goproxy == "" { |
| 121 | + return "https", defaultGoProxyHost, nil |
| 122 | + } |
| 123 | + |
| 124 | + var goproxyHost, goproxyScheme string |
| 125 | + // xref https://github.com/golang/go/blob/master/src/cmd/go/internal/modfetch/proxy.go |
| 126 | + for goproxy != "" { |
| 127 | + var rawURL string |
| 128 | + if i := strings.IndexAny(goproxy, ",|"); i >= 0 { |
| 129 | + rawURL = goproxy[:i] |
| 130 | + goproxy = goproxy[i+1:] |
| 131 | + } else { |
| 132 | + rawURL = goproxy |
| 133 | + goproxy = "" |
| 134 | + } |
| 135 | + |
| 136 | + rawURL = strings.TrimSpace(rawURL) |
| 137 | + if rawURL == "" { |
| 138 | + continue |
| 139 | + } |
| 140 | + if rawURL == "off" || rawURL == "direct" { |
| 141 | + // Return nothing to fallback to github repository client without an error. |
| 142 | + return "", "", nil |
| 143 | + } |
| 144 | + |
| 145 | + // Single-word tokens are reserved for built-in behaviors, and anything |
| 146 | + // containing the string ":/" or matching an absolute file path must be a |
| 147 | + // complete URL. For all other paths, implicitly add "https://". |
| 148 | + if strings.ContainsAny(rawURL, ".:/") && !strings.Contains(rawURL, ":/") && !filepath.IsAbs(rawURL) && !path.IsAbs(rawURL) { |
| 149 | + rawURL = "https://" + rawURL |
| 150 | + } |
| 151 | + |
| 152 | + parsedURL, err := url.Parse(rawURL) |
| 153 | + if err != nil { |
| 154 | + return "", "", errors.Wrapf(err, "parse GOPROXY url %q", rawURL) |
| 155 | + } |
| 156 | + goproxyHost = parsedURL.Host |
| 157 | + goproxyScheme = parsedURL.Scheme |
| 158 | + // A host was found so no need to continue. |
| 159 | + break |
| 160 | + } |
| 161 | + |
| 162 | + return goproxyScheme, goproxyHost, nil |
| 163 | +} |
0 commit comments