@@ -32,6 +32,7 @@ import (
3232
3333 "github.com/fluxcd/pkg/auth"
3434 "github.com/fluxcd/pkg/auth/aws"
35+ "github.com/fluxcd/pkg/auth/generic"
3536)
3637
3738func TestProvider_NewControllerToken (t * testing.T ) {
@@ -82,7 +83,7 @@ func TestProvider_NewControllerToken(t *testing.T) {
8283 }
8384
8485 provider := aws.Provider {Implementation : impl }
85- token , err := provider .NewControllerToken (context . Background (), opts ... )
86+ token , err := provider .NewControllerToken (t . Context (), opts ... )
8687
8788 if tt .err == "" {
8889 g .Expect (err ).NotTo (HaveOccurred ())
@@ -536,3 +537,166 @@ func TestProvider_GetAccessTokenOptionsForCluster(t *testing.T) {
536537
537538 g .Expect (o .STSRegion ).To (Equal ("us-west-2" ))
538539}
540+
541+ func TestGetRegionFromCodeCommitURL (t * testing.T ) {
542+ for _ , tt := range []struct {
543+ name string
544+ gitURL string
545+ expectedRegion string
546+ err string
547+ }{
548+ {
549+ name : "valid CodeCommit URL" ,
550+ gitURL : "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/test-repo" ,
551+ expectedRegion : "us-east-1" ,
552+ },
553+ {
554+ name : "valid CodeCommit FIPS URL" ,
555+ gitURL : "https://git-codecommit-fips.us-west-2.amazonaws.com/v1/repos/test-repo" ,
556+ expectedRegion : "us-west-2" ,
557+ },
558+ {
559+ name : "valid CodeCommit China URL" ,
560+ gitURL : "https://git-codecommit.cn-north-1.amazonaws.com.cn/v1/repos/test-repo" ,
561+ expectedRegion : "cn-north-1" ,
562+ },
563+ {
564+ name : "nil URL" ,
565+ err : "Git URL must be specified for AWS CodeCommit authentication" ,
566+ },
567+ {
568+ name : "non-HTTPS URL" ,
569+ gitURL : "http://git-codecommit.us-east-1.amazonaws.com/v1/repos/test-repo" ,
570+ err : "AWS CodeCommit authentication requires an HTTPS Git URL" ,
571+ },
572+ {
573+ name : "invalid CodeCommit URL" ,
574+ gitURL : "https://github.com/org/repo" ,
575+ err : "invalid AWS CodeCommit Git URL: github.com" ,
576+ },
577+ } {
578+ t .Run (tt .name , func (t * testing.T ) {
579+ g := NewWithT (t )
580+ var parsedURL * url.URL
581+ if tt .gitURL != "" {
582+ var err error
583+ parsedURL , err = url .Parse (tt .gitURL )
584+ g .Expect (err ).NotTo (HaveOccurred ())
585+ }
586+ region , err := aws .GetRegionFromCodeCommitURL (parsedURL )
587+ if tt .err != "" {
588+ g .Expect (err ).To (HaveOccurred ())
589+ g .Expect (err .Error ()).To (Equal (tt .err ))
590+ } else {
591+ g .Expect (err ).NotTo (HaveOccurred ())
592+ g .Expect (region ).To (Equal (tt .expectedRegion ))
593+ }
594+ })
595+ }
596+ }
597+
598+ func TestProvider_NewCodeCommitGitCredentials (t * testing.T ) {
599+ invalidToken := & generic.Token {Token : "invalid" , ExpiresAt : time .Now ().Add (time .Hour )}
600+ proxyUrl := url.URL {Scheme : "http" , Host : "proxy.example.com" }
601+ awsRegion := "us-east-1"
602+ for _ , tt := range []struct {
603+ name string
604+ gitURL string
605+ getAccessToken bool
606+ accessTokens []auth.Token
607+ expectedUsername string
608+ err string
609+ }{
610+ {
611+ name : "valid CodeCommit URL" ,
612+ gitURL : "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/test-repo" ,
613+ getAccessToken : true ,
614+ expectedUsername : "access-key-id%session-token" ,
615+ },
616+ {
617+ name : "valid CodeCommit FIPS URL" ,
618+ gitURL : "https://git-codecommit-fips.us-east-1.amazonaws.com/v1/repos/test-repo" ,
619+ getAccessToken : true ,
620+ expectedUsername : "access-key-id%session-token" ,
621+ },
622+ {
623+ name : "valid CodeCommit China URL" ,
624+ gitURL : "https://git-codecommit.cn-north-1.amazonaws.com.cn/v1/repos/test-repo" ,
625+ getAccessToken : true ,
626+ expectedUsername : "access-key-id%session-token" ,
627+ },
628+ {
629+ name : "missing Git URL" ,
630+ getAccessToken : true ,
631+ err : "Git URL must be specified for AWS CodeCommit authentication" ,
632+ },
633+ {
634+ name : "non HTTPS URL" ,
635+ gitURL : "http://git-codecommit.us-east-1.amazonaws.com/v1/repos/test-repo" ,
636+ getAccessToken : true ,
637+ err : "AWS CodeCommit authentication requires an HTTPS Git URL" ,
638+ },
639+ {
640+ name : "invalid CodeCommit URL" ,
641+ gitURL : "https://github.com/org/repo" ,
642+ getAccessToken : true ,
643+ err : "invalid AWS CodeCommit Git URL: github.com" ,
644+ },
645+ {
646+ name : "missing access token" ,
647+ gitURL : "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/test-repo" ,
648+ getAccessToken : false ,
649+ accessTokens : []auth.Token {},
650+ err : `AWS access token is required for region "us-east-1"` ,
651+ },
652+ {
653+ name : "invalid access token type" ,
654+ gitURL : "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/test-repo" ,
655+ getAccessToken : false ,
656+ accessTokens : []auth.Token {invalidToken },
657+ err : "failed to cast token to AWS token: *generic.Token" ,
658+ },
659+ } {
660+ t .Run (tt .name , func (t * testing.T ) {
661+ g := NewWithT (t )
662+
663+ impl := & mockImplementation {
664+ t : t ,
665+ argRegion : awsRegion ,
666+ argProxyURL : & proxyUrl ,
667+ returnCreds : awssdk.Credentials {AccessKeyID : "access-key-id" , SecretAccessKey : "secret-access-key" , SessionToken : "session-token" },
668+ }
669+
670+ opts := []auth.Option {}
671+ if tt .gitURL != "" {
672+ gitURL , err := url .Parse (tt .gitURL )
673+ g .Expect (err ).NotTo (HaveOccurred ())
674+ opts = append (opts , auth .WithGitURL (* gitURL ))
675+ }
676+
677+ provider := aws.Provider {Implementation : impl }
678+ accessTokens := tt .accessTokens
679+ if tt .getAccessToken {
680+ accessToken , err := auth .GetAccessToken (t .Context (), provider ,
681+ auth .WithSTSRegion (awsRegion ),
682+ auth .WithProxyURL (proxyUrl ),
683+ )
684+ g .Expect (err ).NotTo (HaveOccurred ())
685+ accessTokens = []auth.Token {accessToken }
686+ }
687+
688+ username , password , err := provider .NewCodeCommitGitCredentials (t .Context (), accessTokens , opts ... )
689+
690+ if tt .err == "" {
691+ g .Expect (err ).NotTo (HaveOccurred ())
692+ g .Expect (username ).To (Equal (tt .expectedUsername ))
693+ g .Expect (password ).To (MatchRegexp (`^[0-9]{8}T[0-9]{6}Z[0-9a-f]{64}$` ))
694+ } else {
695+ g .Expect (err ).To (HaveOccurred ())
696+ g .Expect (err .Error ()).To (Equal (tt .err ))
697+ g .Expect (username ).To (BeEmpty ())
698+ g .Expect (password ).To (BeEmpty ())
699+ }
700+ })
701+ }
702+ }
0 commit comments