From cc17c7f98cadbb53091fd84c39499eed140f341e Mon Sep 17 00:00:00 2001 From: David Collom Date: Wed, 18 Nov 2026 13:16:10 +0000 Subject: [PATCH] Fix ACR token reuse and refuse cross-account ECR lookups by default ACR: the token cache check was inverted, so a cached token was only reused once it had expired and every lookup fetched a new one. Reuse it while it is valid instead. When --acr-jwks-uri is set, create the JWKS keyfunc once and share it rather than starting a new refresh goroutine (bound to a context that is never cancelled) for each token. ECR: the registry ID comes from the image host, so a pod could point version-checker's AWS credentials at another account's registry. Look up the caller's own account with STS GetCallerIdentity (cached per region) and refuse other accounts unless --ecr-allow-cross-account is set. Also stop dereferencing a nil ImageDigest or ImagePushedAt from DescribeImages. Setups that read a shared-services ECR in another account must now pass --ecr-allow-cross-account. Signed-off-by: David Collom --- cmd/app/options.go | 4 ++++ go.mod | 3 ++- pkg/client/acr/acr.go | 4 ++++ pkg/client/acr/auth.go | 19 +++++++++++++++++-- pkg/client/ecr/ecr.go | 35 +++++++++++++++++++++++++++++------ 5 files changed, 56 insertions(+), 9 deletions(-) diff --git a/cmd/app/options.go b/cmd/app/options.go index 3ef09bf2..f81ad9bd 100644 --- a/cmd/app/options.go +++ b/cmd/app/options.go @@ -125,6 +125,10 @@ func (o *Options) addAppFlags(fs *pflag.FlagSet) { "pprof-serving-address", "", "", "Address to serve pprof on for profiling.") + fs.BoolVar(&o.Client.ECR.AllowCrossAccount, + "ecr-allow-cross-account", false, + "Allow ECR lookups for registries outside the credentials' own AWS account. Disabled by default.") + fs.BoolVarP(&o.DefaultTestAll, "test-all-containers", "a", false, "If enabled, all containers will be tested, unless they have the "+ diff --git a/go.mod b/go.mod index 71653c08..779d1ed6 100644 --- a/go.mod +++ b/go.mod @@ -63,6 +63,8 @@ require ( github.com/stretchr/testify v1.12.1 ) +require github.com/aws/aws-sdk-go-v2/service/sts v1.51.6 + require ( github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect github.com/Azure/go-autorest v14.2.0+incompatible // indirect @@ -78,7 +80,6 @@ require ( github.com/aws/aws-sdk-go-v2/service/signin v1.12.0 // indirect github.com/aws/aws-sdk-go-v2/service/sso v1.39.0 // indirect github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.0 // indirect - github.com/aws/aws-sdk-go-v2/service/sts v1.51.6 // indirect github.com/aws/smithy-go v1.28.7 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/blang/semver/v4 v4.0.0 // indirect diff --git a/pkg/client/acr/acr.go b/pkg/client/acr/acr.go index fd357dd4..2247b637 100644 --- a/pkg/client/acr/acr.go +++ b/pkg/client/acr/acr.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/MicahParks/keyfunc/v3" "io" "net/http" "sync" @@ -28,6 +29,9 @@ type Client struct { cachedACRClient map[string]*acrClient cacheMu sync.Mutex + + jwksMu sync.Mutex + jwks keyfunc.Keyfunc } type Options struct { diff --git a/pkg/client/acr/auth.go b/pkg/client/acr/auth.go index 5461c3fd..c826cdde 100644 --- a/pkg/client/acr/auth.go +++ b/pkg/client/acr/auth.go @@ -22,7 +22,7 @@ func (c *Client) getACRClient(ctx context.Context, host string) (*acrClient, err c.cacheMu.Lock() defer c.cacheMu.Unlock() - if client, ok := c.cachedACRClient[host]; ok && time.Now().After(client.tokenExpiry) { + if client, ok := c.cachedACRClient[host]; ok && time.Now().Before(client.tokenExpiry) { return client, nil } @@ -137,7 +137,7 @@ func (c *Client) getTokenExpiration(tokenString string) (time.Time, error) { var err error if c.JWKSURI != "" { var k keyfunc.Keyfunc - k, err = keyfunc.NewDefaultCtx(context.TODO(), []string{c.JWKSURI}) + k, err = c.keyfunc() if err != nil { return time.Time{}, err } @@ -161,3 +161,18 @@ func (c *Client) getTokenExpiration(tokenString string) (time.Time, error) { return time.Time{}, fmt.Errorf("failed to find 'exp' claim in access token") } + +// keyfunc returns a single shared JWKS keyfunc, so only one refresh goroutine ever runs. +func (c *Client) keyfunc() (keyfunc.Keyfunc, error) { + c.jwksMu.Lock() + defer c.jwksMu.Unlock() + if c.jwks != nil { + return c.jwks, nil + } + k, err := keyfunc.NewDefaultCtx(context.Background(), []string{c.JWKSURI}) + if err != nil { + return nil, err + } + c.jwks = k + return k, nil +} diff --git a/pkg/client/ecr/ecr.go b/pkg/client/ecr/ecr.go index 54c60327..1942cb94 100644 --- a/pkg/client/ecr/ecr.go +++ b/pkg/client/ecr/ecr.go @@ -4,11 +4,13 @@ import ( "context" "fmt" "net/http" + "sync" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/credentials" "github.com/aws/aws-sdk-go-v2/service/ecr" + "github.com/aws/aws-sdk-go-v2/service/sts" "github.com/jetstack/version-checker/pkg/api" "github.com/jetstack/version-checker/pkg/client/util" @@ -20,6 +22,9 @@ var _ api.ImageClient = (*Client)(nil) type Client struct { Options Config aws.Config + + // callerAccounts caches the STS caller account per region. + callerAccounts sync.Map } type Options struct { @@ -28,6 +33,9 @@ type Options struct { AccessKeyID string SecretAccessKey string SessionToken string + + // AllowCrossAccount permits lookups in registries owned by other AWS accounts. + AllowCrossAccount bool } func New(opts Options) *Client { @@ -49,12 +57,27 @@ func (c *Client) Tags(ctx context.Context, host, repo, image string) ([]api.Imag id := matches[1] region := matches[3] - client, err := c.createClient(ctx, region) + client, cfg, err := c.createClient(ctx, region) if err != nil { return nil, fmt.Errorf("failed to construct ecr client for image host %s: %s", host, err) } + if !c.AllowCrossAccount { + account, ok := c.callerAccounts.Load(region) + if !ok { + out, err := sts.NewFromConfig(cfg).GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}) + if err != nil { + return nil, fmt.Errorf("failed to determine own aws account: %w", err) + } + account = aws.ToString(out.Account) + c.callerAccounts.Store(region, account) + } + if account != id { + return nil, fmt.Errorf("refusing cross-account ecr lookup for account %s (set --ecr-allow-cross-account to permit)", id) + } + } + repoName := util.JoinRepoImage(repo, image) tags := map[string]api.ImageTag{} @@ -72,8 +95,8 @@ func (c *Client) Tags(ctx context.Context, host, repo, image string) ([]api.Imag for _, img := range images.ImageDetails { // Base data shared across tags base := api.ImageTag{ - SHA: *img.ImageDigest, - Timestamp: *img.ImagePushedAt, + SHA: aws.ToString(img.ImageDigest), + Timestamp: aws.ToTime(img.ImagePushedAt), } // Continue early if no tags available @@ -99,7 +122,7 @@ func (c *Client) Tags(ctx context.Context, host, repo, image string) ([]api.Imag return util.TagMaptoList(tags), nil } -func (c *Client) createClient(ctx context.Context, region string) (*ecr.Client, error) { +func (c *Client) createClient(ctx context.Context, region string) (*ecr.Client, aws.Config, error) { var cfg aws.Config var err error @@ -118,7 +141,7 @@ func (c *Client) createClient(ctx context.Context, region string) (*ecr.Client, ) } if err != nil { - return nil, fmt.Errorf("failed to construct aws credentials: %s", err) + return nil, cfg, fmt.Errorf("failed to construct aws credentials: %s", err) } - return ecr.NewFromConfig(cfg), nil + return ecr.NewFromConfig(cfg), cfg, nil }