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
4 changes: 4 additions & 0 deletions cmd/app/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "+
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pkg/client/acr/acr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/MicahParks/keyfunc/v3"
"io"
"net/http"
"sync"
Expand All @@ -28,6 +29,9 @@ type Client struct {

cachedACRClient map[string]*acrClient
cacheMu sync.Mutex

jwksMu sync.Mutex
jwks keyfunc.Keyfunc
}

type Options struct {
Expand Down
19 changes: 17 additions & 2 deletions pkg/client/acr/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
35 changes: 29 additions & 6 deletions pkg/client/ecr/ecr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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{}
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
}