Skip to content

WRKLDS-1550: Add pkg/image/registryclient/v2 #1972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
20 changes: 20 additions & 0 deletions pkg/image/registryclient/v2/auth/api_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package auth

import (
"net/http"
"strings"
)

// APIVersions gets the API versions out of an HTTP response using the provided
// version header as the key for the HTTP header.
func APIVersions(resp *http.Response, versionHeader string) []string {
var versions []string
if versionHeader != "" {
for _, supportedVersions := range resp.Header[http.CanonicalHeaderKey(versionHeader)] {
for _, version := range strings.Fields(supportedVersions) {
versions = append(versions, version)
}
}
}
return versions
}
56 changes: 56 additions & 0 deletions pkg/image/registryclient/v2/auth/api_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package auth

import (
"net/http"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestAPIVersions(t *testing.T) {
const header = "Docker-Distribution-API-Version"

tests := []struct {
name string
headerName string
headerValue string
expectedVersions []string
}{{
name: "HeaderPresentExactName",
headerName: header,
headerValue: "registry/2.0",
expectedVersions: []string{"registry/2.0"},
}, {
name: "HeaderPresentLowerCaseName",
headerName: strings.ToLower(header),
headerValue: "registry/2.0",
expectedVersions: []string{"registry/2.0"},
}, {
name: "HeaderPresentMultipleValues",
headerName: header,
headerValue: "registry/2.0 registry/3.0",
expectedVersions: []string{"registry/2.0", "registry/3.0"},
}, {
name: "HeaderMissing",
headerName: header,
headerValue: "",
expectedVersions: nil,
}}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
resp := http.Response{
Header: make(map[string][]string),
}
if test.headerValue != "" {
resp.Header.Set(test.headerName, test.headerValue)
}

versions := APIVersions(&resp, header)
if !cmp.Equal(test.expectedVersions, versions) {
t.Error("Unexpected versions slice:\n", cmp.Diff(test.expectedVersions, versions))
}
})
}
}
17 changes: 17 additions & 0 deletions pkg/image/registryclient/v2/auth/credential_store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package auth

import "net/url"

// CredentialStore is an interface for getting credentials for a given URL.
type CredentialStore interface {
// Basic returns basic auth for the given URL
Basic(*url.URL) (string, string)

// RefreshToken returns a refresh token for the
// given URL and service
RefreshToken(*url.URL, string) string

// SetRefreshToken sets the refresh token if none
// is provided for the given url and service
SetRefreshToken(realm *url.URL, service, token string)
}
32 changes: 32 additions & 0 deletions pkg/image/registryclient/v2/auth/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package auth

import (
"fmt"
"strings"
)

// Scope is a type which is serializable to a string
// using the allow scope grammar.
type Scope interface {
String() string
}

// RepositoryScope represents a token scope for access
// to a repository.
type RepositoryScope struct {
Repository string
Class string
Actions []string
}

// String returns the string representation of the repository
// using the scope grammar
func (rs RepositoryScope) String() string {
repoType := "repository"
// Keep existing format for image class to maintain backwards compatibility
// with authorization servers which do not support the expanded grammar.
if rs.Class != "" && rs.Class != "image" {
repoType = fmt.Sprintf("%s(%s)", repoType, rs.Class)
}
return fmt.Sprintf("%s:%s:%s", repoType, rs.Repository, strings.Join(rs.Actions, ","))
}
Loading