@@ -13,6 +13,7 @@ import (
1313 "testing"
1414 "time"
1515
16+ "github.com/golang-jwt/jwt/v5"
1617 "github.com/google/uuid"
1718 "github.com/stackitcloud/stackit-sdk-go/core/clients"
1819 "github.com/stackitcloud/stackit-sdk-go/core/config"
@@ -121,6 +122,32 @@ func TestSetupAuth(t *testing.T) {
121122 }
122123 }()
123124
125+ // create a wif assertion file
126+ wifAssertionFile , errs := os .CreateTemp ("" , "temp-*.txt" )
127+ if errs != nil {
128+ t .Fatalf ("Creating temporary file: %s" , err )
129+ }
130+ defer func () {
131+ _ = wifAssertionFile .Close ()
132+ err := os .Remove (wifAssertionFile .Name ())
133+ if err != nil {
134+ t .Fatalf ("Removing temporary file: %s" , err )
135+ }
136+ }()
137+
138+ token , err := jwt .NewWithClaims (jwt .SigningMethodHS256 , jwt.RegisteredClaims {
139+ ExpiresAt : jwt .NewNumericDate (time .Now ().Add (time .Minute )),
140+ Subject : "sub" ,
141+ }).SignedString ([]byte ("test" ))
142+ if err != nil {
143+ t .Fatalf ("Removing temporary file: %s" , err )
144+ }
145+
146+ _ , errs = wifAssertionFile .WriteString (string (token ))
147+ if errs != nil {
148+ t .Fatalf ("Writing wif assertion to temporary file: %s" , err )
149+ }
150+
124151 // create a credentials file with saKey and private key
125152 credentialsKeyFile , errs := os .CreateTemp ("" , "temp-*.txt" )
126153 if errs != nil {
@@ -147,12 +174,19 @@ func TestSetupAuth(t *testing.T) {
147174 desc string
148175 config * config.Configuration
149176 setToken bool
177+ setWorkloadIdentity bool
150178 setKeys bool
151179 setKeyPaths bool
152180 setCredentialsFilePathToken bool
153181 setCredentialsFilePathKey bool
154182 isValid bool
155183 }{
184+ {
185+ desc : "wif_config" ,
186+ config : nil ,
187+ setWorkloadIdentity : true ,
188+ isValid : true ,
189+ },
156190 {
157191 desc : "token_config" ,
158192 config : nil ,
@@ -241,6 +275,12 @@ func TestSetupAuth(t *testing.T) {
241275 t .Setenv ("STACKIT_CREDENTIALS_PATH" , "" )
242276 }
243277
278+ if test .setWorkloadIdentity {
279+ t .Setenv ("STACKIT_FEDERATED_TOKEN_FILE" , wifAssertionFile .Name ())
280+ } else {
281+ t .Setenv ("STACKIT_FEDERATED_TOKEN_FILE" , "" )
282+ }
283+
244284 t .Setenv ("STACKIT_SERVICE_ACCOUNT_EMAIL" , "test-email" )
245285
246286 authRoundTripper , err := SetupAuth (test .config )
@@ -253,7 +293,7 @@ func TestSetupAuth(t *testing.T) {
253293 t .Fatalf ("Test didn't return error on invalid test case" )
254294 }
255295
256- if test . isValid && authRoundTripper == nil {
296+ if authRoundTripper == nil && test . isValid {
257297 t .Fatalf ("Roundtripper returned is nil for valid test case" )
258298 }
259299 })
@@ -381,6 +421,32 @@ func TestDefaultAuth(t *testing.T) {
381421 t .Fatalf ("Writing private key to temporary file: %s" , err )
382422 }
383423
424+ // create a wif assertion file
425+ wifAssertionFile , errs := os .CreateTemp ("" , "temp-*.txt" )
426+ if errs != nil {
427+ t .Fatalf ("Creating temporary file: %s" , err )
428+ }
429+ defer func () {
430+ _ = wifAssertionFile .Close ()
431+ err := os .Remove (wifAssertionFile .Name ())
432+ if err != nil {
433+ t .Fatalf ("Removing temporary file: %s" , err )
434+ }
435+ }()
436+
437+ token , err := jwt .NewWithClaims (jwt .SigningMethodHS256 , jwt.RegisteredClaims {
438+ ExpiresAt : jwt .NewNumericDate (time .Now ().Add (time .Minute )),
439+ Subject : "sub" ,
440+ }).SignedString ([]byte ("test" ))
441+ if err != nil {
442+ t .Fatalf ("Removing temporary file: %s" , err )
443+ }
444+
445+ _ , errs = wifAssertionFile .WriteString (string (token ))
446+ if errs != nil {
447+ t .Fatalf ("Writing wif assertion to temporary file: %s" , err )
448+ }
449+
384450 // create a credentials file with saKey and private key
385451 credentialsKeyFile , errs := os .CreateTemp ("" , "temp-*.txt" )
386452 if errs != nil {
@@ -409,6 +475,7 @@ func TestDefaultAuth(t *testing.T) {
409475 setKeyPaths bool
410476 setKeys bool
411477 setCredentialsFilePathKey bool
478+ setWorkloadIdentity bool
412479 isValid bool
413480 expectedFlow string
414481 }{
@@ -418,6 +485,14 @@ func TestDefaultAuth(t *testing.T) {
418485 isValid : true ,
419486 expectedFlow : "token" ,
420487 },
488+ {
489+ desc : "wif_precedes_key_precedes_token" ,
490+ setToken : true ,
491+ setKeyPaths : true ,
492+ setWorkloadIdentity : true ,
493+ isValid : true ,
494+ expectedFlow : "wif" ,
495+ },
421496 {
422497 desc : "key_precedes_token" ,
423498 setToken : true ,
@@ -475,6 +550,13 @@ func TestDefaultAuth(t *testing.T) {
475550 } else {
476551 t .Setenv ("STACKIT_SERVICE_ACCOUNT_TOKEN" , "" )
477552 }
553+
554+ if test .setWorkloadIdentity {
555+ t .Setenv ("STACKIT_FEDERATED_TOKEN_FILE" , wifAssertionFile .Name ())
556+ } else {
557+ t .Setenv ("STACKIT_FEDERATED_TOKEN_FILE" , "" )
558+ }
559+
478560 t .Setenv ("STACKIT_SERVICE_ACCOUNT_EMAIL" , "test-email" )
479561
480562 // Get the default authentication client and ensure that it's not nil
@@ -501,6 +583,10 @@ func TestDefaultAuth(t *testing.T) {
501583 if _ , ok := authClient .(* clients.KeyFlow ); ! ok {
502584 t .Fatalf ("Expected key flow, got %s" , reflect .TypeOf (authClient ))
503585 }
586+ case "wif" :
587+ if _ , ok := authClient .(* clients.WorkloadIdentityFederationFlow ); ! ok {
588+ t .Fatalf ("Expected key flow, got %s" , reflect .TypeOf (authClient ))
589+ }
504590 }
505591 }
506592 })
0 commit comments