Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/oauth-access-tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ Access tokens follow the JWT format of [RFC 9068](https://www.rfc-editor.org/rfc
A token is at most 16 KB. Its lifetime is the authorization server's to choose.

A token without a `scope` has no permissions. This differs from a token signed with `jwt_secret`,
where a missing `permissions` claim means full access. As for every API token, the registry under
`/v2` does not check permissions.
where a missing `permissions` claim means full access. The registry under `/v2` checks an access
token's permissions too: `image:read` to pull, `image:read` and `image:write` to push, and
`image:delete` for a DELETE request. A token signed with `jwt_secret` can do all of it whatever
its permissions.

`revoked_token_hashes` applies to access tokens as it does to the others. Revoking a token signed
with ES256, ES384 or ES512 also revokes the second token string that carries the same signature.
Expand Down
8 changes: 7 additions & 1 deletion lib/jwtauth/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (v *Verifier) ParseWithClaims(ctx context.Context, tokenString string, clai
// A token that declares itself an access token is never checked against
// the HMAC keys, and no other token is checked against an authorization
// server's keys.
if v.accessTokens.Enabled() && accesstoken.Typed(tokenString) {
if v.IsAccessToken(tokenString) {
return v.parseAccessToken(ctx, tokenString, claims)
}
if len(v.keys) == 0 {
Expand Down Expand Up @@ -86,6 +86,12 @@ func (v *Verifier) ParseWithClaims(ctx context.Context, tokenString string, clai
return nil, err
}

// IsAccessToken reports whether ParseWithClaims checks tokenString against an
// authorization server's keys rather than the HMAC keys.
func (v *Verifier) IsAccessToken(tokenString string) bool {
return v.accessTokens.Enabled() && accesstoken.Typed(tokenString)
}

// parseAccessToken verifies an authorization server's access token and hands
// its claims to the caller in the shape of an HMAC token's: the scope becomes
// the permissions list. The list is always present, so such a token is never
Expand Down
15 changes: 15 additions & 0 deletions lib/middleware/oapi_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,21 @@ func JwtAuth(verifier *jwtauth.Verifier, revoked *tokenrevocation.List) func(htt
"auth_type", authType,
"subject", userClaims["sub"])
ctx := contextWithUserClaims(r.Context(), userClaims)
// Tokens signed with the server's secret have always been able
// to push and pull whatever their permissions, and tokens in
// use rely on it, so only access tokens are held to theirs.
required := scopes.ImageRead
if r.Method == http.MethodDelete {
required = scopes.ImageDelete
} else if isWriteOperation(r.Method) {
required = scopes.ImageWrite
}
if verifier.IsAccessToken(token) && !scopes.HasScope(ctx, required) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, `{"errors":[{"code":"DENIED","message":"missing required scope: %s"}]}`, required)
return
}
next.ServeHTTP(w, requestWithoutAuthorization(r, ctx))
return
}
Expand Down
39 changes: 38 additions & 1 deletion lib/middleware/oapi_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,43 @@ func TestJwtAuth_RegistryPathAcceptsUserTokens(t *testing.T) {
})
}

// The registry holds an access token to its permissions. A token signed with
// the server's secret pushes and pulls whatever its permissions, as before.
func TestJwtAuth_RegistryPermissions(t *testing.T) {
server := accesstokentest.NewServer(t)
verifier := newTestVerifier(t, testJWTSecret).WithAccessTokens(server.Verifier(t))
handler := JwtAuth(verifier, nil)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
accessToken := func(scope string) string {
return server.Token(t, accesstokentest.Claims("federation:ci", scope), nil)
}
const ok, denied = http.StatusOK, http.StatusForbidden
for _, tt := range []struct {
name, token string
pull, push, drop int
}{
{"secret token with full access", generateUserToken(t, "user"), ok, ok, ok},
{"secret token scoped to other resources", generateScopedToken(t, "user", []string{"instance:read"}), ok, ok, ok},
{"access token with image:read", accessToken("image:read"), ok, denied, denied},
{"access token with image:write", accessToken("image:write"), denied, ok, denied},
{"access token with image:delete", accessToken("image:delete"), denied, denied, ok},
{"access token with the wildcard", accessToken("*"), ok, ok, ok},
{"access token scoped to other resources", accessToken("instance:read"), denied, denied, denied},
} {
for method, want := range map[string]int{http.MethodGet: tt.pull, http.MethodPut: tt.push, http.MethodDelete: tt.drop} {
req := httptest.NewRequest(method, "/v2/test-image/manifests/latest", nil)
req.Header.Set("Authorization", "Bearer "+tt.token)
rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)
assert.Equal(t, want, rr.Code, "%s: %s", tt.name, method)
if want == denied {
assert.Contains(t, rr.Body.String(), `"code":"DENIED"`)
}
}
}
}

func TestJwtAuth_RegistryUnauthorizedResponse(t *testing.T) {
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -670,7 +707,7 @@ func TestJwtAuth_AccessTokens(t *testing.T) {
assert.Equal(t, "false", rr.Header().Get("X-Full-Access"))
assert.Equal(t, "false", rr.Header().Get("X-Instance-Read"))

// The registry accepts it as it accepts any API token.
// The registry accepts it with the scope the request needs.
rr = serve("/v2/test-image/manifests/latest", server.Token(t, accesstokentest.Claims("federation:ci", "image:read"), nil))
require.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, "federation:ci", rr.Header().Get("X-User-ID"))
Expand Down
8 changes: 5 additions & 3 deletions lib/scopes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ Scopes follow the pattern `resource:action`. Each resource type supports `read`,

| Scope | Grants access to |
|---|---|
| `image:read` | List images, get image details |
| `image:write` | Pull/create images |
| `image:delete` | Delete images |
| `image:read` | List images, get image details; pull from the registry under `/v2` |
| `image:write` | Pull/create images; push to the registry under `/v2`, which needs `image:read` too |
| `image:delete` | Delete images; delete from the registry under `/v2` |

The registry holds only [access tokens from an OAuth authorization server](../../docs/oauth-access-tokens.md) to these scopes. A token signed with `jwt_secret`, such as one `hypeman-token` mints, could always use the registry whatever its scopes, and keeps that access.

### Volumes

Expand Down
Loading