|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | + "github.com/aws/aws-sdk-go-v2/config" |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/codeartifact" |
| 12 | + "github.com/aws/aws-sdk-go-v2/service/codeartifact/types" |
| 13 | +) |
| 14 | + |
| 15 | +type CodeArtifactAuthInfoStruct struct { |
| 16 | + Url string |
| 17 | + AuthorizationToken string |
| 18 | + LastAuth time.Time |
| 19 | +} |
| 20 | + |
| 21 | +var CodeArtifactAuthInfo = &CodeArtifactAuthInfoStruct{} |
| 22 | + |
| 23 | +// Authenticate performs the authentication against CodeArtifact and caches the credentials |
| 24 | +func Authenticate() { |
| 25 | + log.Printf("Authenticating against CodeArtifact") |
| 26 | + |
| 27 | + // Authenticate against CodeArtifact |
| 28 | + cfg, cfgErr := config.LoadDefaultConfig(context.TODO()) |
| 29 | + if cfgErr != nil { |
| 30 | + log.Fatalf("unable to load SDK config, %v", cfgErr) |
| 31 | + } |
| 32 | + svc := codeartifact.NewFromConfig(cfg) |
| 33 | + |
| 34 | + // TODO: auto-resolve these via Environment variables |
| 35 | + codeArtDomain := aws.String(os.Getenv("CODEARTIFACT_DOMAIN")) |
| 36 | + codeArtOwner, codeArtOwnerFound := os.LookupEnv("CODEARTIFACT_OWNER") |
| 37 | + codeArtRepos := aws.String(os.Getenv("CODEARTIFACT_REPO")) |
| 38 | + |
| 39 | + // Resolve Package Format from the environment variable (defaults to pypi) |
| 40 | + codeArtTypeS, found := os.LookupEnv("CODEARTIFACT_TYPE") |
| 41 | + if !found || codeArtTypeS == "" { |
| 42 | + codeArtTypeS = "pypi" |
| 43 | + } |
| 44 | + var codeArtTypeT types.PackageFormat |
| 45 | + if codeArtTypeS == "pypi" { |
| 46 | + codeArtTypeT = types.PackageFormatPypi |
| 47 | + } else if codeArtTypeS == "maven" { |
| 48 | + codeArtTypeT = types.PackageFormatMaven |
| 49 | + } else if codeArtTypeS == "npm" { |
| 50 | + codeArtTypeT = types.PackageFormatNpm |
| 51 | + } else if codeArtTypeS == "nuget" { |
| 52 | + codeArtTypeT = types.PackageFormatNuget |
| 53 | + } |
| 54 | + |
| 55 | + // Create the input for the CodeArtifact API |
| 56 | + authInput := &codeartifact.GetAuthorizationTokenInput{ |
| 57 | + DurationSeconds: aws.Int64(3600), |
| 58 | + Domain: codeArtDomain, |
| 59 | + } |
| 60 | + if codeArtOwnerFound { |
| 61 | + authInput.DomainOwner = aws.String(codeArtOwner) |
| 62 | + } |
| 63 | + authResp, authErr := svc.GetAuthorizationToken(context.TODO(), authInput) |
| 64 | + if authErr != nil { |
| 65 | + log.Fatalf("unable to get authorization token, %v", authErr) |
| 66 | + } |
| 67 | + log.Printf("Authorization successful") |
| 68 | + CodeArtifactAuthInfo.AuthorizationToken = *authResp.AuthorizationToken |
| 69 | + CodeArtifactAuthInfo.LastAuth = time.Now() |
| 70 | + |
| 71 | + // Get the URL for the CodeArtifact Service |
| 72 | + urlInput := &codeartifact.GetRepositoryEndpointInput{ |
| 73 | + Domain: codeArtDomain, |
| 74 | + Format: codeArtTypeT, |
| 75 | + Repository: codeArtRepos, |
| 76 | + } |
| 77 | + if codeArtOwnerFound { |
| 78 | + urlInput.DomainOwner = aws.String(codeArtOwner) |
| 79 | + } |
| 80 | + |
| 81 | + urlResp, urlErr := svc.GetRepositoryEndpoint(context.TODO(), urlInput) |
| 82 | + if urlErr != nil { |
| 83 | + log.Fatalf("unable to get repository endpoint, %v", urlErr) |
| 84 | + } |
| 85 | + CodeArtifactAuthInfo.Url = *urlResp.RepositoryEndpoint |
| 86 | + |
| 87 | + log.Printf("Requests will now be proxied to %s", CodeArtifactAuthInfo.Url) |
| 88 | +} |
| 89 | + |
| 90 | +// CheckReauth checks if we have not yet authenticated, or need to authenticate within the next 15 minutes |
| 91 | +func CheckReauth() { |
| 92 | + for { |
| 93 | + if CodeArtifactAuthInfo.AuthorizationToken == "" || time.Now().Sub(CodeArtifactAuthInfo.LastAuth) > 45*time.Minute { |
| 94 | + log.Printf("%d minutes until the CodeArtifact token expires, attempting a reauth.", time.Now().Sub(CodeArtifactAuthInfo.LastAuth)/time.Minute) |
| 95 | + Authenticate() |
| 96 | + } |
| 97 | + // Sleep for 15 seconds for the next check |
| 98 | + time.Sleep(15 * time.Second) |
| 99 | + } |
| 100 | +} |
0 commit comments