Documentation
¶
Overview ¶
Package dockerhub is a minimal Docker Hub client: public repository search and tag listing, plus an optional token (PAT or OAT) for private repositories, rate limit status and tag to digest resolution.
Index ¶
- Constants
- Variables
- type Access
- type CachedValidation
- type Capability
- type Client
- func (c *Client) CachedPullLimits(ctx context.Context, cred Credentials, ttl time.Duration, refresh bool) (lim PullLimits, checkedAt time.Time, cached bool, err error)
- func (c *Client) CachedValidate(ctx context.Context, cred Credentials, ttl time.Duration, refresh bool) (CachedValidation, error)
- func (c *Client) ListRepositories(ctx context.Context, cred Credentials, namespace, nameFilter string, ...) (RepositoryPage, error)
- func (c *Client) ListTags(ctx context.Context, namespace, repository string, pageSize int) ([]Tag, error)
- func (c *Client) ListTagsMatching(ctx context.Context, cred Credentials, ...) (TagPage, error)
- func (c *Client) ListTagsPage(ctx context.Context, cred Credentials, namespace, repository string, ...) (TagPage, error)
- func (c *Client) PullLimits(ctx context.Context, cred Credentials) (PullLimits, error)
- func (c *Client) ResolveDigest(ctx context.Context, cred Credentials, namespace, repository, tag string) (Manifest, error)
- func (c *Client) SearchRepositories(ctx context.Context, query string, pageSize int) ([]Repository, error)
- func (c *Client) Validate(ctx context.Context, cred Credentials) (Validation, error)
- type Credentials
- type Image
- type Manifest
- type PullLimits
- type RateLimitedError
- type RepoEntry
- type Repository
- type RepositoryPage
- type Tag
- type TagPage
- type Validation
Constants ¶
const ( // KindPersonal names a personal access token. KindPersonal = "personal" // KindOrganization names an organization access token. KindOrganization = "organization" )
const ( StatusOK = "ok" StatusDenied = "denied" StatusUnknown = "unknown" )
Capability states.
Variables ¶
var ErrForbidden = errors.New("dockerhub: token lacks the required scope")
ErrForbidden reports that the token is valid but lacks the scope or repository permission the call needs (HTTP 403).
var ErrNotFound = errors.New("dockerhub: repository not found")
ErrNotFound reports that Docker Hub has no such repository, the Hub API's own signal (a 404) for a namespace/name nothing was ever published under, distinct from an unreachable host or a rate limit.
var ErrRateLimited = errors.New("dockerhub: rate limited")
ErrRateLimited is matched by errors.Is for any 429 from Docker Hub or its registry, including the registry's "toomanyrequests" answer.
ErrUnauthorized reports that Docker Hub rejected the token (HTTP 401).
Functions ¶
This section is empty.
Types ¶
type Access ¶
type Access struct {
Type string `json:"type"`
Name string `json:"name"`
Actions []string `json:"actions"`
}
Access is one granted entry of a registry token.
type CachedValidation ¶
type CachedValidation struct {
Validation Validation
CheckedAt time.Time
Cached bool
}
CachedValidation holds a Validation and when it was read.
type Capability ¶
Capability is one checked thing the token can or cannot do.
type Client ¶
type Client struct {
HTTP *http.Client
// BaseURL, AuthURL and RegistryURL override the Hub API, token and
// registry hosts, for tests only.
BaseURL string
AuthURL string
RegistryURL string
// contains filtered or unexported fields
}
Client queries Docker Hub's public Hub API over plain HTTPS: no authentication, so no credentials to manage or leak.
func NewClient ¶
func NewClient() *Client
NewClient returns a Client with a bounded per-request timeout, the same shape registrycatalog.NewClient establishes for its own single small JSON requests.
func (*Client) CachedPullLimits ¶
func (c *Client) CachedPullLimits(ctx context.Context, cred Credentials, ttl time.Duration, refresh bool) (lim PullLimits, checkedAt time.Time, cached bool, err error)
CachedPullLimits is PullLimits with an in-memory cache. checkedAt is when the answer was read from Docker.
func (*Client) CachedValidate ¶
func (c *Client) CachedValidate(ctx context.Context, cred Credentials, ttl time.Duration, refresh bool) (CachedValidation, error)
CachedValidate is Validate with an in-memory cache. Failures are not cached.
func (*Client) ListRepositories ¶
func (c *Client) ListRepositories(ctx context.Context, cred Credentials, namespace, nameFilter string, page, pageSize int) (RepositoryPage, error)
ListRepositories lists a namespace through the namespace route. A token adds the private repositories it may see; for an organization token that needs the repository list scope, otherwise Docker omits them silently.
func (*Client) ListTags ¶
func (c *Client) ListTags(ctx context.Context, namespace, repository string, pageSize int) ([]Tag, error)
ListTags queries GET /v2/repositories/{namespace}/{repo}/tags. An empty namespace defaults to "library", the namespace Docker Hub's own search results omit for official images.
func (*Client) ListTagsMatching ¶
func (c *Client) ListTagsMatching(ctx context.Context, cred Credentials, namespace, repository, nameFilter string, page, pageSize int) (TagPage, error)
ListTagsMatching is ListTagsPage narrowed by Docker Hub's own name filter (a substring match, so callers wanting a prefix filter the result).
func (*Client) ListTagsPage ¶
func (c *Client) ListTagsPage(ctx context.Context, cred Credentials, namespace, repository string, page, pageSize int) (TagPage, error)
ListTagsPage lists tags with digest, size, architectures and push time. With a token it uses the namespace route that organization tokens need, and falls back to the older route for a personal token the namespace route refuses. Without a token only public repositories answer.
func (*Client) PullLimits ¶
func (c *Client) PullLimits(ctx context.Context, cred Credentials) (PullLimits, error)
PullLimits reads the current pull quota with a HEAD request on the documented preview repository, which does not count as a pull. A 429 that still carries limit headers is reported as remaining zero.
func (*Client) ResolveDigest ¶
func (c *Client) ResolveDigest(ctx context.Context, cred Credentials, namespace, repository, tag string) (Manifest, error)
ResolveDigest finds the digest of namespace/repository:tag with a HEAD on the manifest, which does not count against the pull limit. For a multi-architecture image this is the index digest, the one to pin.
func (*Client) SearchRepositories ¶
func (c *Client) SearchRepositories(ctx context.Context, query string, pageSize int) ([]Repository, error)
SearchRepositories queries GET /v2/search/repositories/?query=..., returning Docker Hub's own relevance-ranked order unchanged.
func (*Client) Validate ¶
func (c *Client) Validate(ctx context.Context, cred Credentials) (Validation, error)
Validate checks the token against auth.docker.io (a rejected token returns ErrUnauthorized), then probes what the Hub API lets it do. Probe failures other than a rejection are reported as unknown, not as an error.
type Credentials ¶
Credentials is a Docker Hub username and access token. Its formatting methods never print the token, so a stray log line cannot leak it.
func (Credentials) GoString ¶
func (c Credentials) GoString() string
GoString redacts the token for %#v.
func (Credentials) Kind ¶
func (c Credentials) Kind() string
Kind infers the token type from its documented prefix: organization access tokens start with dckr_oat_, everything else is treated as personal.
func (Credentials) LogValue ¶
func (c Credentials) LogValue() slog.Value
LogValue redacts the token for slog.
func (Credentials) Present ¶
func (c Credentials) Present() bool
Present reports whether both fields are set.
type Image ¶
type Image struct {
Architecture string `json:"architecture"`
OS string `json:"os"`
Variant string `json:"variant,omitempty"`
Digest string `json:"digest,omitempty"`
Size int64 `json:"size"`
}
Image is one platform entry of a multi-architecture tag.
type Manifest ¶
type Manifest struct {
Digest string `json:"digest"`
MediaType string `json:"media_type"`
Limits PullLimits `json:"limits"`
}
Manifest is the digest a tag points at right now.
type PullLimits ¶
type PullLimits struct {
// Known is false when the registry sent no limit headers, as for plans
// without a pull limit.
Known bool `json:"known"`
Limit int `json:"limit"`
Remaining int `json:"remaining"`
WindowSeconds int `json:"window_seconds"`
Source string `json:"source"`
// Authenticated is true when the preview request carried a token.
Authenticated bool `json:"authenticated"`
}
PullLimits is the pull quota Docker reports in registry response headers.
func ParseRateLimit ¶
func ParseRateLimit(h http.Header) PullLimits
ParseRateLimit reads ratelimit-limit, ratelimit-remaining and docker-ratelimit-source, for example "100;w=21600".
type RateLimitedError ¶
RateLimitedError carries the Retry-After wait when Docker Hub sent one.
func (*RateLimitedError) Error ¶
func (e *RateLimitedError) Error() string
func (*RateLimitedError) Is ¶
func (e *RateLimitedError) Is(target error) bool
Is makes errors.Is(err, ErrRateLimited) true.
type RepoEntry ¶
type RepoEntry struct {
FullName string `json:"full_name"`
Name string `json:"name"`
Namespace string `json:"namespace"`
IsPrivate bool `json:"is_private"`
Description string `json:"description"`
StarCount int `json:"star_count"`
PullCount int64 `json:"pull_count"`
LastUpdated string `json:"last_updated,omitempty"`
}
RepoEntry is one repository of a namespace.
type Repository ¶
type Repository struct {
Name string `json:"repo_name"`
ShortDescription string `json:"short_description"`
StarCount int `json:"star_count"`
IsOfficial bool `json:"is_official"`
IsAutomated bool `json:"is_automated"`
// IsPrivate is set only for repositories read with a token.
IsPrivate bool `json:"is_private"`
}
Repository is one GET /v2/search/repositories/ result: the fields the picker's search results list needs (name, description, popularity, official/automated flags), not the full upstream payload.
type RepositoryPage ¶
type RepositoryPage struct {
Count int `json:"count"`
Page int `json:"page"`
HasNext bool `json:"has_next"`
Repositories []RepoEntry `json:"repositories"`
}
RepositoryPage is one page of a namespace listing.
type Tag ¶
type Tag struct {
Name string `json:"name"`
Digest string `json:"digest,omitempty"`
FullSize int64 `json:"full_size"`
LastUpdated string `json:"last_updated,omitempty"`
LastPushed string `json:"last_pushed,omitempty"`
Architectures []string `json:"architectures"`
Images []Image `json:"images"`
}
Tag is one image tag. Anonymous listings fill Name only; credentialed listings add digest, size, architectures and push time.
type TagPage ¶
type TagPage struct {
Count int `json:"count"`
Page int `json:"page"`
HasNext bool `json:"has_next"`
Tags []Tag `json:"tags"`
}
TagPage is one page of tags.
type Validation ¶
type Validation struct {
Kind string `json:"kind"`
Username string `json:"username"`
Scopes []string `json:"scopes"`
Capabilities []Capability `json:"capabilities"`
PrivateReposVisible bool `json:"private_repos_visible"`
}
Validation is what Docker Hub confirmed about a token. Nothing here can prove the token is read-only: Docker offers no way to read a personal token's permission level and a write is never attempted.