This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This takes the essential workings of #1455 and creates a helper for using AWS authorization with ECR (the AWS container registry).
- Loading branch information
Showing
3 changed files
with
182 additions
and
42 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package registry | ||
|
||
// References: | ||
// - https://github.com/bzon/ecr-k8s-secret-creator | ||
// - https://github.com/kubernetes/kubernetes/blob/master/pkg/credentialprovider/aws/aws_credentials.go | ||
// - https://github.com/weaveworks/flux/pull/1455 | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ecr" | ||
"github.com/go-kit/kit/log" | ||
) | ||
|
||
const ( | ||
// For recognising ECR hosts | ||
ecrHostSuffix = ".amazonaws.com" | ||
// How long AWS tokens remain valid | ||
tokenValid = 12 * time.Hour | ||
) | ||
|
||
type AWSRegistryConfig struct { | ||
Region string | ||
RegistryIDs []string | ||
} | ||
|
||
func ImageCredsWithAWS(lookup func() ImageCreds, logger log.Logger, config AWSRegistryConfig) (func() ImageCreds, error) { | ||
awsCreds := NoCredentials() | ||
var credsExpire time.Time | ||
|
||
refresh := func(now time.Time) error { | ||
var err error | ||
awsCreds, err = fetchAWSCreds(config) | ||
if err != nil { | ||
// bump this along so we don't spam the log | ||
credsExpire = now.Add(time.Hour) | ||
return err | ||
} | ||
credsExpire = now.Add(tokenValid) | ||
return nil | ||
} | ||
|
||
// pre-flight check | ||
if err := refresh(time.Now()); err != nil { | ||
return nil, err | ||
} | ||
|
||
return func() ImageCreds { | ||
imageCreds := lookup() | ||
|
||
now := time.Now() | ||
if now.After(credsExpire) { | ||
if err := refresh(now); err != nil { | ||
logger.Log("warning", "AWS token not refreshed", "err", err) | ||
} | ||
} | ||
|
||
for name, creds := range imageCreds { | ||
if strings.HasSuffix(name.Domain, ecrHostSuffix) { | ||
newCreds := NoCredentials() | ||
newCreds.Merge(awsCreds) | ||
newCreds.Merge(creds) | ||
imageCreds[name] = newCreds | ||
} | ||
} | ||
return imageCreds | ||
}, nil | ||
} | ||
|
||
func fetchAWSCreds(config AWSRegistryConfig) (Credentials, error) { | ||
sess := session.Must(session.NewSession(&aws.Config{Region: &config.Region})) | ||
svc := ecr.New(sess) | ||
ecrToken, err := svc.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{ | ||
RegistryIds: aws.StringSlice(config.RegistryIDs), | ||
}) | ||
if err != nil { | ||
return Credentials{}, err | ||
} | ||
auths := make(map[string]creds) | ||
for _, v := range ecrToken.AuthorizationData { | ||
// Remove the https prefix | ||
host := strings.TrimPrefix(*v.ProxyEndpoint, "https://") | ||
creds, err := parseAuth(*v.AuthorizationToken) | ||
if err != nil { | ||
return Credentials{}, err | ||
} | ||
creds.provenance = "AWS API" | ||
creds.registry = host | ||
auths[host] = creds | ||
} | ||
return Credentials{m: auths}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters