func (bs *blobStatter) Clear(ctx context.Context, dgst digest.Digest) error { ref, err := reference.WithDigest(bs.name, dgst) if err != nil { return err } blobURL, err := bs.ub.BuildBlobURL(ref) if err != nil { return err } req, err := http.NewRequest("DELETE", blobURL, nil) if err != nil { return err } resp, err := bs.client.Do(req) if err != nil { return err } defer resp.Body.Close() if client.SuccessStatus(resp.StatusCode) { return nil } return client.HandleErrorResponse(resp) }
func (r *registry) getRepoTags(ref reference.Named) (tags []string, err error) { u, err := r.ub.BuildTagsURL(ref) if err != nil { return tags, err } req, err := r.getRequest(u, "") if err != nil { return nil, err } resp, err := r.client.Do(req) defer safeCloseBody(resp) if err != nil { return nil, err } if !client.SuccessStatus(resp.StatusCode) { log.Printf("Error response to %#v %v", req, req.URL) return tags, client.HandleErrorResponse(resp) } b, err := ioutil.ReadAll(resp.Body) if err != nil { return tags, err } var tr tagsResponse if err := json.Unmarshal(b, &tr); err != nil { return tags, err } tags = tr.Tags return tags, nil }
// Get issues a HEAD request for a Manifest against its named endpoint in order // to construct a descriptor for the tag. If the registry doesn't support HEADing // a manifest, fallback to GET. func (t *tags) Get(ctx context.Context, tag string) (distribution.Descriptor, error) { ref, err := reference.WithTag(t.name, tag) if err != nil { return distribution.Descriptor{}, err } u, err := t.ub.BuildManifestURL(ref) if err != nil { return distribution.Descriptor{}, err } req, err := http.NewRequest("HEAD", u, nil) if err != nil { return distribution.Descriptor{}, err } for _, t := range distribution.ManifestMediaTypes() { req.Header.Add("Accept", t) } var attempts int resp, err := t.client.Do(req) check: if err != nil { return distribution.Descriptor{}, err } defer resp.Body.Close() switch { case resp.StatusCode >= 200 && resp.StatusCode < 400: return descriptorFromResponse(resp) case resp.StatusCode == http.StatusMethodNotAllowed: req, err = http.NewRequest("GET", u, nil) if err != nil { return distribution.Descriptor{}, err } for _, t := range distribution.ManifestMediaTypes() { req.Header.Add("Accept", t) } resp, err = t.client.Do(req) attempts++ if attempts > 1 { return distribution.Descriptor{}, err } goto check default: return distribution.Descriptor{}, client.HandleErrorResponse(resp) } }
func (bs *blobs) Open(ctx context.Context, dgst digest.Digest) (distribution.ReadSeekCloser, error) { ref, err := reference.WithDigest(bs.name, dgst) if err != nil { return nil, err } blobURL, err := bs.ub.BuildBlobURL(ref) if err != nil { return nil, err } return transport.NewHTTPReadSeeker(bs.client, blobURL, func(resp *http.Response) error { if resp.StatusCode == http.StatusNotFound { return distribution.ErrBlobUnknown } return client.HandleErrorResponse(resp) }), nil }
// Repositories returns a lexigraphically sorted catalog given a base URL. The 'entries' slice will be filled up to the size // of the slice, starting at the value provided in 'last'. The number of entries will be returned along with io.EOF if there // are no more entries func (r *oldRegistry) Repositories(ctx context.Context, entries []string, last string) (int, error) { var numFilled int var returnErr error values := buildCatalogValues(len(entries), last) u, err := r.ub.BuildCatalogURL(values) if err != nil { return 0, err } resp, err := r.client.Get(u) if err != nil { return 0, err } defer resp.Body.Close() if client.SuccessStatus(resp.StatusCode) { var ctlg struct { Repositories []string `json:"repositories"` } decoder := json.NewDecoder(resp.Body) if err := decoder.Decode(&ctlg); err != nil { return 0, err } for cnt := range ctlg.Repositories { entries[cnt] = ctlg.Repositories[cnt] } numFilled = len(ctlg.Repositories) link := resp.Header.Get("Link") if link == "" { returnErr = io.EOF } } else { return 0, client.HandleErrorResponse(resp) } return numFilled, returnErr }
func (r *registry) getBlob(ctx context.Context, name reference.Named, dgst digest.Digest) ([]byte, error) { ref, err := reference.WithDigest(name, dgst) if err != nil { return nil, err } blobURL, err := r.ub.BuildBlobURL(ref) if err != nil { return nil, err } reader := transport.NewHTTPReadSeeker(r.client, blobURL, func(resp *http.Response) error { if resp.StatusCode == http.StatusNotFound { return distribution.ErrBlobUnknown } return client.HandleErrorResponse(resp) }) defer reader.Close() return ioutil.ReadAll(reader) }
func (r *registry) manifestFromResponse(resp *http.Response) (distribution.Manifest, digest.Digest, error) { if resp.StatusCode == http.StatusNotModified { return nil, "", distribution.ErrManifestNotModified } else if client.SuccessStatus(resp.StatusCode) { mt := resp.Header.Get("Content-Type") body, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, "", err } m, _, err := distribution.UnmarshalManifest(mt, body) if err != nil { return nil, "", err } var d digest.Digest switch v := m.(type) { case *schema1.SignedManifest: //log.Print(string(v.Canonical)) d = digest.FromBytes(v.Canonical) case *schema2.DeserializedManifest: _, pl, err := m.Payload() if err != nil { return nil, "", err } //log.Print(string(pl)) d = digest.FromBytes(pl) default: return nil, "", fmt.Errorf("unsupported manifest format") } // log.Printf("%T", m) // log.Print("Calced: ", d) // log.Print("Header: ", resp.Header.Get("Docker-Content-Digest")) // log.Print("Docker: sha256:d3d75a393555a8eb6bf1e94736b90b84712638e5f3dbd7728355310dbd4f1684") //docker pull return m, d, nil } return nil, "", client.HandleErrorResponse(resp) }
func (bs *blobStatter) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) { ref, err := reference.WithDigest(bs.name, dgst) if err != nil { return distribution.Descriptor{}, err } u, err := bs.ub.BuildBlobURL(ref) if err != nil { return distribution.Descriptor{}, err } resp, err := bs.client.Head(u) if err != nil { return distribution.Descriptor{}, err } defer resp.Body.Close() if client.SuccessStatus(resp.StatusCode) { lengthHeader := resp.Header.Get("Content-Length") if lengthHeader == "" { return distribution.Descriptor{}, fmt.Errorf("missing content-length header for request: %s", u) } length, err := strconv.ParseInt(lengthHeader, 10, 64) if err != nil { return distribution.Descriptor{}, fmt.Errorf("error parsing content-length: %v", err) } return distribution.Descriptor{ MediaType: resp.Header.Get("Content-Type"), Size: length, Digest: dgst, }, nil } else if resp.StatusCode == http.StatusNotFound { return distribution.Descriptor{}, distribution.ErrBlobUnknown } return distribution.Descriptor{}, client.HandleErrorResponse(resp) }
func (th *tokenHandler) fetchToken(params map[string]string) (token *tokenResponse, err error) { //log.Debugf("Getting bearer token with %s for %s", challenge.Parameters, ta.auth.Username) realm, ok := params["realm"] if !ok { return nil, errors.New("no realm specified for token auth challenge") } // TODO(dmcgowan): Handle empty scheme realmURL, err := url.Parse(realm) if err != nil { return nil, fmt.Errorf("invalid token auth challenge realm: %s", err) } req, err := http.NewRequest("GET", realmURL.String(), nil) if err != nil { return nil, err } reqParams := req.URL.Query() service := params["service"] scope := th.scope.String() if service != "" { reqParams.Add("service", service) } for _, scopeField := range strings.Fields(scope) { reqParams.Add("scope", scopeField) } for scope := range th.additionalScopes { reqParams.Add("scope", scope) } if th.creds != nil { username, password := th.creds.Basic(realmURL) if username != "" && password != "" { reqParams.Add("account", username) req.SetBasicAuth(username, password) } } req.URL.RawQuery = reqParams.Encode() resp, err := th.client().Do(req) if err != nil { return nil, err } defer resp.Body.Close() if !client.SuccessStatus(resp.StatusCode) { err := client.HandleErrorResponse(resp) return nil, err } decoder := json.NewDecoder(resp.Body) tr := new(tokenResponse) if err = decoder.Decode(tr); err != nil { return nil, fmt.Errorf("unable to decode token response: %s", err) } // `access_token` is equivalent to `token` and if both are specified // the choice is undefined. Canonicalize `access_token` by sticking // things in `token`. if tr.AccessToken != "" { tr.Token = tr.AccessToken } if tr.Token == "" { return nil, errors.New("authorization server did not include a token in the response") } if tr.ExpiresIn < minimumTokenLifetimeSeconds { // The default/minimum lifetime. tr.ExpiresIn = minimumTokenLifetimeSeconds logrus.Debugf("Increasing token expiration to: %d seconds", tr.ExpiresIn) } if tr.IssuedAt.IsZero() { // issued_at is optional in the token response. tr.IssuedAt = th.clock.Now() } return tr, nil }
func (th *tokenHandler) fetchTokenWithBasicAuth(realm *url.URL, service string, scopes []string) (token string, expiration time.Time, err error) { req, err := http.NewRequest("GET", realm.String(), nil) if err != nil { return "", time.Time{}, err } reqParams := req.URL.Query() if service != "" { reqParams.Add("service", service) } for _, scope := range scopes { reqParams.Add("scope", scope) } if th.offlineAccess { reqParams.Add("offline_token", "true") clientID := th.clientID if clientID == "" { clientID = defaultClientID } reqParams.Add("client_id", clientID) } if th.creds != nil { username, password := th.creds.Basic(realm) if username != "" && password != "" { reqParams.Add("account", username) req.SetBasicAuth(username, password) } } req.URL.RawQuery = reqParams.Encode() resp, err := th.client().Do(req) if err != nil { return "", time.Time{}, err } defer resp.Body.Close() if !client.SuccessStatus(resp.StatusCode) { err := client.HandleErrorResponse(resp) return "", time.Time{}, err } decoder := json.NewDecoder(resp.Body) var tr getTokenResponse if err = decoder.Decode(&tr); err != nil { return "", time.Time{}, fmt.Errorf("unable to decode token response: %s", err) } if tr.RefreshToken != "" && th.creds != nil { th.creds.SetRefreshToken(realm, service, tr.RefreshToken) } // `access_token` is equivalent to `token` and if both are specified // the choice is undefined. Canonicalize `access_token` by sticking // things in `token`. if tr.AccessToken != "" { tr.Token = tr.AccessToken } if tr.Token == "" { return "", time.Time{}, ErrNoToken } if tr.ExpiresIn < minimumTokenLifetimeSeconds { // The default/minimum lifetime. tr.ExpiresIn = minimumTokenLifetimeSeconds logrus.Debugf("Increasing token expiration to: %d seconds", tr.ExpiresIn) } if tr.IssuedAt.IsZero() { // issued_at is optional in the token response. tr.IssuedAt = th.clock.Now().UTC() } return tr.Token, tr.IssuedAt.Add(time.Duration(tr.ExpiresIn) * time.Second), nil }
func (th *tokenHandler) fetchTokenWithOAuth(realm *url.URL, refreshToken, service string, scopes []string) (token string, expiration time.Time, err error) { form := url.Values{} form.Set("scope", strings.Join(scopes, " ")) form.Set("service", service) clientID := th.clientID if clientID == "" { // Use default client, this is a required field clientID = defaultClientID } form.Set("client_id", clientID) if refreshToken != "" { form.Set("grant_type", "refresh_token") form.Set("refresh_token", refreshToken) } else if th.creds != nil { form.Set("grant_type", "password") username, password := th.creds.Basic(realm) form.Set("username", username) form.Set("password", password) // attempt to get a refresh token form.Set("access_type", "offline") } else { // refuse to do oauth without a grant type return "", time.Time{}, fmt.Errorf("no supported grant type") } resp, err := th.client().PostForm(realm.String(), form) if err != nil { return "", time.Time{}, err } defer resp.Body.Close() if !client.SuccessStatus(resp.StatusCode) { err := client.HandleErrorResponse(resp) return "", time.Time{}, err } decoder := json.NewDecoder(resp.Body) var tr postTokenResponse if err = decoder.Decode(&tr); err != nil { return "", time.Time{}, fmt.Errorf("unable to decode token response: %s", err) } if tr.RefreshToken != "" && tr.RefreshToken != refreshToken { th.creds.SetRefreshToken(realm, service, tr.RefreshToken) } if tr.ExpiresIn < minimumTokenLifetimeSeconds { // The default/minimum lifetime. tr.ExpiresIn = minimumTokenLifetimeSeconds logrus.Debugf("Increasing token expiration to: %d seconds", tr.ExpiresIn) } if tr.IssuedAt.IsZero() { // issued_at is optional in the token response. tr.IssuedAt = th.clock.Now().UTC() } return tr.AccessToken, tr.IssuedAt.Add(time.Duration(tr.ExpiresIn) * time.Second), nil }