Skip to content
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

Fix ACR JWT #290

Open
wants to merge 2 commits into
base: main
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
envACRUsername = "ACR_USERNAME"
envACRPassword = "ACR_PASSWORD"
envACRRefreshToken = "ACR_REFRESH_TOKEN"
envACRJWKSURI = "ACR_JWKS_URI"

envDockerUsername = "DOCKER_USERNAME"
envDockerPassword = "DOCKER_PASSWORD"
Expand Down Expand Up @@ -140,6 +141,12 @@ func (o *Options) addAuthFlags(fs *pflag.FlagSet) {
"username/password (%s_%s).",
envPrefix, envACRRefreshToken,
))
fs.StringVar(&o.Client.ACR.JWKSURI,
"acr-jwks-uri", "",
fmt.Sprintf(
"JWKS URI to verify the JWT access token received. If left blank, JWT token will not be verified. (%s_%s)",
envPrefix, envACRJWKSURI,
))
///

// Docker
Expand Down Expand Up @@ -278,6 +285,7 @@ func (o *Options) complete() {
{envACRUsername, &o.Client.ACR.Username},
{envACRPassword, &o.Client.ACR.Password},
{envACRRefreshToken, &o.Client.ACR.RefreshToken},
{envACRJWKSURI, &o.Client.ACR.JWKSURI},

{envDockerUsername, &o.Client.Docker.Username},
{envDockerPassword, &o.Client.Docker.Password},
Expand Down
4 changes: 4 additions & 0 deletions cmd/app/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestComplete(t *testing.T) {
{"VERSION_CHECKER_ACR_USERNAME", "acr-username"},
{"VERSION_CHECKER_ACR_PASSWORD", "acr-password"},
{"VERSION_CHECKER_ACR_REFRESH_TOKEN", "acr-token"},
{"VERSION_CHECKER_ACR_JWKS_URI", "acr-jwks-uri"},
{"VERSION_CHECKER_DOCKER_USERNAME", "docker-username"},
{"VERSION_CHECKER_DOCKER_PASSWORD", "docker-password"},
{"VERSION_CHECKER_DOCKER_TOKEN", "docker-token"},
Expand All @@ -51,6 +52,7 @@ func TestComplete(t *testing.T) {
Username: "acr-username",
Password: "acr-password",
RefreshToken: "acr-token",
JWKSURI: "acr-jwks-uri",
},
Docker: docker.Options{
Username: "docker-username",
Expand Down Expand Up @@ -92,6 +94,7 @@ func TestComplete(t *testing.T) {
{"VERSION_CHECKER_ACR_USERNAME", "acr-username"},
{"VERSION_CHECKER_ACR_PASSWORD", "acr-password"},
{"VERSION_CHECKER_ACR_REFRESH_TOKEN", "acr-token"},
{"VERSION_CHECKER_ACR_JWKS_URI", "acr-jwks-uri"},
{"VERSION_CHECKER_DOCKER_USERNAME", "docker-username"},
{"VERSION_CHECKER_DOCKER_PASSWORD", "docker-password"},
{"VERSION_CHECKER_DOCKER_TOKEN", "docker-token"},
Expand Down Expand Up @@ -119,6 +122,7 @@ func TestComplete(t *testing.T) {
Username: "acr-username",
Password: "acr-password",
RefreshToken: "acr-token",
JWKSURI: "acr-jwks-uri",
},
Docker: docker.Options{
Username: "docker-username",
Expand Down
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
)

require (
github.com/MicahParks/keyfunc/v3 v3.3.5
github.com/aws/aws-sdk-go-v2/config v1.27.39
github.com/aws/aws-sdk-go-v2/credentials v1.17.37
github.com/aws/aws-sdk-go-v2/service/ecr v1.35.3
Expand All @@ -43,6 +44,7 @@ require (
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/MicahParks/jwkset v0.5.19 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.14 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.18 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.18 // indirect
Expand Down Expand Up @@ -106,12 +108,12 @@ require (
github.com/x448/float16 v0.8.4 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20240725214946-42030a7cedce // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
Expand Down
24 changes: 14 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ github.com/Azure/go-autorest/logger v0.2.1 h1:IG7i4p/mDa2Ce4TRyAO8IHnVhAVF3RFU+Z
github.com/Azure/go-autorest/logger v0.2.1/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo=
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/MicahParks/jwkset v0.5.19 h1:XZCsgJv05DBCvxEHYEHlSafqiuVn5ESG0VRB331Fxhw=
github.com/MicahParks/jwkset v0.5.19/go.mod h1:q8ptTGn/Z9c4MwbcfeCDssADeVQb3Pk7PnVxrvi+2QY=
github.com/MicahParks/keyfunc/v3 v3.3.5 h1:7ceAJLUAldnoueHDNzF8Bx06oVcQ5CfJnYwNt1U3YYo=
github.com/MicahParks/keyfunc/v3 v3.3.5/go.mod h1:SdCCyMJn/bYqWDvARspC6nCT8Sk74MjuAY22C7dCST8=
github.com/aws/aws-sdk-go-v2 v1.31.0 h1:3V05LbxTSItI5kUqNwhJrrrY1BAXxXt0sN0l72QmG5U=
github.com/aws/aws-sdk-go-v2 v1.31.0/go.mod h1:ztolYtaEUtdpf9Wftr31CJfLVjOnD/CVRkKOOYgF8hA=
github.com/aws/aws-sdk-go-v2/config v1.27.39 h1:FCylu78eTGzW1ynHcongXK9YHtoXD5AiiUqq3YfJYjU=
Expand Down Expand Up @@ -238,8 +242,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand All @@ -255,8 +259,8 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA=
golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -279,24 +283,24 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
82 changes: 59 additions & 23 deletions pkg/client/acr/acr.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import (
"sync"
"time"

"github.com/MicahParks/keyfunc/v3"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
jwt "github.com/golang-jwt/jwt/v5"
"github.com/golang-jwt/jwt/v5"

"github.com/jetstack/version-checker/pkg/api"
"github.com/jetstack/version-checker/pkg/client/util"
)

const (
userAgent = "jetstack/version-checker"
userAgent = "jetstack/version-checker"
requiredScope = "repository:*:metadata_read"
)

type Client struct {
Expand All @@ -39,6 +42,7 @@ type Options struct {
Username string
Password string
RefreshToken string
JWKSURI string
}

type AccessTokenResponse struct {
Expand Down Expand Up @@ -160,35 +164,52 @@ func (c *Client) getACRClient(ctx context.Context, host string) (*acrClient, err
}

var (
client *acrClient
err error
client *acrClient
accessTokenClient *autorest.Client
accessTokenReq *http.Request
err error
)

if len(c.RefreshToken) > 0 {
client, err = c.getAccessTokenClient(ctx, host)
accessTokenClient, accessTokenReq, err = c.getAccessTokenRequesterForRefreshToken(ctx, host)
} else {
client, err = c.getBasicAuthClient(host)
accessTokenClient, accessTokenReq, err = c.getAccessTokenRequesterForBasicAuth(ctx, host)
}
if err != nil {
return nil, err
}
if client, err = c.getAuthorizedClient(accessTokenClient, accessTokenReq, host); err != nil {
return nil, err
}

c.cachedACRClient[host] = client

return client, nil
}

func (c *Client) getBasicAuthClient(_ string) (*acrClient, error) {
func (c *Client) getAccessTokenRequesterForBasicAuth(ctx context.Context, host string) (*autorest.Client, *http.Request, error) {
client := autorest.NewClientWithUserAgent(userAgent)
client.Authorizer = autorest.NewBasicAuthorizer(c.Username, c.Password)
urlParameters := map[string]interface{}{
"url": "https://" + host,
}

return &acrClient{
Client: &client,
tokenExpiry: time.Unix(1<<63-1, 0),
}, nil
preparer := autorest.CreatePreparer(
autorest.WithCustomBaseURL("{url}", urlParameters),
autorest.WithPath("/oauth2/token"),
autorest.WithQueryParameters(map[string]interface{}{
"scope": requiredScope,
"service": host,
}),
)
req, err := preparer.Prepare((&http.Request{}).WithContext(ctx))
if err != nil {
return nil, nil, err
}

return &client, req, nil
}

func (c *Client) getAccessTokenClient(ctx context.Context, host string) (*acrClient, error) {
func (c *Client) getAccessTokenRequesterForRefreshToken(ctx context.Context, host string) (*autorest.Client, *http.Request, error) {
client := autorest.NewClientWithUserAgent(userAgent)
urlParameters := map[string]interface{}{
"url": "https://" + host,
Expand All @@ -197,7 +218,7 @@ func (c *Client) getAccessTokenClient(ctx context.Context, host string) (*acrCli
formDataParameters := map[string]interface{}{
"grant_type": "refresh_token",
"refresh_token": c.RefreshToken,
"scope": "repository:*:*",
"scope": requiredScope,
"service": host,
}

Expand All @@ -208,11 +229,14 @@ func (c *Client) getAccessTokenClient(ctx context.Context, host string) (*acrCli
autorest.WithFormData(autorest.MapToValues(formDataParameters)))
req, err := preparer.Prepare((&http.Request{}).WithContext(ctx))
if err != nil {
return nil, err
return nil, nil, err
}
return &client, req, nil
}

resp, err := autorest.SendWithSender(client, req,
autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
func (c *Client) getAuthorizedClient(accessTokenClient *autorest.Client, accessTokenReq *http.Request, host string) (*acrClient, error) {
resp, err := autorest.SendWithSender(accessTokenClient, accessTokenReq,
autorest.DoRetryForStatusCodes(accessTokenClient.RetryAttempts, accessTokenClient.RetryDuration, autorest.StatusCodesForRetry...))
if err != nil {
return nil, fmt.Errorf("%s: failed to request access token: %s",
host, err)
Expand All @@ -225,26 +249,38 @@ func (c *Client) getAccessTokenClient(ctx context.Context, host string) (*acrCli
host, err)
}

exp, err := getTokenExpiration(respToken.AccessToken)
exp, err := c.getTokenExpiration(respToken.AccessToken)
if err != nil {
return nil, fmt.Errorf("%s: %s", host, err)
}

token := &adal.Token{
RefreshToken: c.RefreshToken,
RefreshToken: "", // empty if access_token was retrieved with basic auth. but client is not reused after expiry anyway (see cachedACRClient)
AccessToken: respToken.AccessToken,
}

client.Authorizer = autorest.NewBearerAuthorizer(token)
accessTokenClient.Authorizer = autorest.NewBearerAuthorizer(token)

return &acrClient{
tokenExpiry: exp,
Client: &client,
Client: accessTokenClient,
}, nil
}

func getTokenExpiration(tokenString string) (time.Time, error) {
token, err := jwt.Parse(tokenString, nil, jwt.WithoutClaimsValidation())
func (c *Client) getTokenExpiration(tokenString string) (time.Time, error) {
jwtParser := jwt.NewParser(jwt.WithoutClaimsValidation())
var token *jwt.Token
var err error
if c.JWKSURI != "" {
var k keyfunc.Keyfunc
k, err = keyfunc.NewDefaultCtx(context.TODO(), []string{c.JWKSURI})
if err != nil {
return time.Time{}, err
}
token, err = jwtParser.Parse(tokenString, k.Keyfunc)
} else {
token, _, err = jwtParser.ParseUnverified(tokenString, jwt.MapClaims{})
}
if err != nil {
return time.Time{}, err
}
Expand Down