@@ -50,6 +50,14 @@ func fixtureServiceAccountKey(mods ...func(*clients.ServiceAccountKeyResponse))
5050 return serviceAccountKeyResponse
5151}
5252
53+ // helper function to create a credentials.json file with saKey and private key
54+ func createCredentialsKeyJson (serviceAccountKey , privateKey string ) ([]byte , error ) {
55+ tempMap := map [string ]interface {}{}
56+ tempMap ["STACKIT_SERVICE_ACCOUNT_KEY" ] = serviceAccountKey
57+ tempMap ["STACKIT_PRIVATE_KEY" ] = privateKey
58+ return json .Marshal (tempMap )
59+ }
60+
5361// Error cases are tested in the NoAuth, KeyAuth, TokenAuth and DefaultAuth functions
5462func TestSetupAuth (t * testing.T ) {
5563 privateKey , err := generatePrivateKey ()
@@ -111,57 +119,104 @@ func TestSetupAuth(t *testing.T) {
111119 }
112120 }()
113121
122+ // create a credentials file with saKey and private key
123+ credentialsKeyFile , errs := os .CreateTemp ("" , "temp-*.txt" )
124+ if errs != nil {
125+ t .Fatalf ("Creating temporary file: %s" , err )
126+ }
127+ defer func () {
128+ err := os .Remove (credentialsKeyFile .Name ())
129+ if err != nil {
130+ t .Fatalf ("Removing temporary file: %s" , err )
131+ }
132+ }()
133+
134+ credKeyJson , err := createCredentialsKeyJson (string (saKey ), privateKey )
135+ if err != nil {
136+ t .Fatalf ("createCredentialsKeyJson: %s" , err )
137+ }
138+ _ , errs = credentialsKeyFile .WriteString (string (credKeyJson ))
139+ if errs != nil {
140+ t .Fatalf ("Writing credentials json to temporary file: %s" , err )
141+ }
142+
114143 for _ , test := range []struct {
115- desc string
116- config * config.Configuration
117- setToken bool
118- setKeys bool
119- setPath bool
120- isValid bool
144+ desc string
145+ config * config.Configuration
146+ setToken bool
147+ setKeys bool
148+ setKeyPaths bool
149+ setCredentialsFilePathToken bool
150+ setCredentialsFilePathKey bool
151+ isValid bool
121152 }{
122153 {
123- desc : "token_config" ,
124- config : nil ,
125- setToken : true ,
126- setPath : false ,
127- isValid : true ,
154+ desc : "token_config" ,
155+ config : nil ,
156+ setToken : true ,
157+ setCredentialsFilePathToken : false ,
158+ isValid : true ,
128159 },
129160 {
130- desc : "key_config" ,
131- config : nil ,
132- setKeys : true ,
133- setPath : false ,
134- isValid : true ,
161+ desc : "key_config" ,
162+ config : nil ,
163+ setKeys : true ,
164+ setCredentialsFilePathToken : false ,
165+ isValid : true ,
135166 },
136167 {
137- desc : "valid_path_to_file" ,
138- config : nil ,
139- setToken : false ,
140- setPath : true ,
141- isValid : true ,
168+ desc : "key_config_path" ,
169+ config : nil ,
170+ setKeys : false ,
171+ setKeyPaths : true ,
172+ setCredentialsFilePathToken : false ,
173+ isValid : true ,
174+ },
175+ {
176+ desc : "key_config_credentials_path" ,
177+ config : nil ,
178+ setKeys : false ,
179+ setKeyPaths : false ,
180+ setCredentialsFilePathKey : true ,
181+ isValid : true ,
182+ },
183+ {
184+ desc : "valid_path_to_file" ,
185+ config : nil ,
186+ setToken : false ,
187+ setCredentialsFilePathToken : true ,
188+ isValid : true ,
142189 },
143190 {
144191 desc : "custom_config_token" ,
145192 config : & config.Configuration {
146193 Token : "token" ,
147194 },
148- setToken : false ,
149- setPath : false ,
150- isValid : true ,
195+ setToken : false ,
196+ setCredentialsFilePathToken : false ,
197+ isValid : true ,
151198 },
152199 {
153200 desc : "custom_config_path" ,
154201 config : & config.Configuration {
155202 CredentialsFilePath : "test_resources/test_credentials_bar.json" ,
156203 },
157- setToken : false ,
158- setPath : false ,
159- isValid : true ,
204+ setToken : false ,
205+ setCredentialsFilePathToken : false ,
206+ isValid : true ,
160207 },
161208 } {
162209 t .Run (test .desc , func (t * testing.T ) {
163210 setTemporaryHome (t )
164211 if test .setKeys {
212+ t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY" , string (saKey ))
213+ t .Setenv ("STACKIT_PRIVATE_KEY" , privateKey )
214+ } else {
215+ t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY" , "" )
216+ t .Setenv ("STACKIT_PRIVATE_KEY" , "" )
217+ }
218+
219+ if test .setKeyPaths {
165220 t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY_PATH" , saKeyFile .Name ())
166221 t .Setenv ("STACKIT_PRIVATE_KEY_PATH" , privateKeyFile .Name ())
167222 } else {
@@ -175,8 +230,10 @@ func TestSetupAuth(t *testing.T) {
175230 t .Setenv ("STACKIT_SERVICE_ACCOUNT_TOKEN" , "" )
176231 }
177232
178- if test .setPath {
233+ if test .setCredentialsFilePathToken {
179234 t .Setenv ("STACKIT_CREDENTIALS_PATH" , "test_resources/test_credentials_bar.json" )
235+ } else if test .setCredentialsFilePathKey {
236+ t .Setenv ("STACKIT_CREDENTIALS_PATH" , credentialsKeyFile .Name ())
180237 } else {
181238 t .Setenv ("STACKIT_CREDENTIALS_PATH" , "" )
182239 }
@@ -327,12 +384,35 @@ func TestDefaultAuth(t *testing.T) {
327384 }
328385 }()
329386
387+ // create a credentials file with saKey and private key
388+ credentialsKeyFile , errs := os .CreateTemp ("" , "temp-*.txt" )
389+ if errs != nil {
390+ t .Fatalf ("Creating temporary file: %s" , err )
391+ }
392+ defer func () {
393+ err := os .Remove (credentialsKeyFile .Name ())
394+ if err != nil {
395+ t .Fatalf ("Removing temporary file: %s" , err )
396+ }
397+ }()
398+
399+ credKeyJson , err := createCredentialsKeyJson (string (saKey ), privateKey )
400+ if err != nil {
401+ t .Fatalf ("createCredentialsKeyJson: %s" , err )
402+ }
403+ _ , errs = credentialsKeyFile .WriteString (string (credKeyJson ))
404+ if errs != nil {
405+ t .Fatalf ("Writing credentials json to temporary file: %s" , err )
406+ }
407+
330408 for _ , test := range []struct {
331- desc string
332- setToken bool
333- setKeys bool
334- isValid bool
335- expectedFlow string
409+ desc string
410+ setToken bool
411+ setKeyPaths bool
412+ setKeys bool
413+ setCredentialsFilePathKey bool
414+ isValid bool
415+ expectedFlow string
336416 }{
337417 {
338418 desc : "token" ,
@@ -343,7 +423,7 @@ func TestDefaultAuth(t *testing.T) {
343423 {
344424 desc : "key_precedes_token" ,
345425 setToken : true ,
346- setKeys : true ,
426+ setKeyPaths : true ,
347427 isValid : true ,
348428 expectedFlow : "key" ,
349429 },
@@ -352,23 +432,51 @@ func TestDefaultAuth(t *testing.T) {
352432 setToken : false ,
353433 isValid : false ,
354434 },
435+ {
436+ desc : "use keys via environment" ,
437+ setKeys : true ,
438+ setToken : false ,
439+ isValid : true ,
440+ expectedFlow : "key" ,
441+ },
442+ {
443+ desc : "use keys via credentials file" ,
444+ setKeys : false ,
445+ setToken : false ,
446+ setCredentialsFilePathKey : true ,
447+ isValid : true ,
448+ expectedFlow : "key" ,
449+ },
355450 } {
356451 t .Run (test .desc , func (t * testing.T ) {
357452 setTemporaryHome (t )
358- if test .setKeys {
453+ if test .setKeyPaths {
359454 t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY_PATH" , saKeyFile .Name ())
360455 t .Setenv ("STACKIT_PRIVATE_KEY_PATH" , privateKeyFile .Name ())
361456 } else {
362457 t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY_PATH" , "" )
363458 t .Setenv ("STACKIT_PRIVATE_KEY_PATH" , "" )
364459 }
365460
461+ if test .setKeys {
462+ t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY" , string (saKey ))
463+ t .Setenv ("STACKIT_PRIVATE_KEY" , privateKey )
464+ } else {
465+ t .Setenv ("STACKIT_SERVICE_ACCOUNT_KEY" , "" )
466+ t .Setenv ("STACKIT_PRIVATE_KEY" , "" )
467+ }
468+
469+ if test .setCredentialsFilePathKey {
470+ t .Setenv ("STACKIT_CREDENTIALS_PATH" , credentialsKeyFile .Name ())
471+ } else {
472+ t .Setenv ("STACKIT_CREDENTIALS_PATH" , "test-path" )
473+ }
474+
366475 if test .setToken {
367476 t .Setenv ("STACKIT_SERVICE_ACCOUNT_TOKEN" , "test-token" )
368477 } else {
369478 t .Setenv ("STACKIT_SERVICE_ACCOUNT_TOKEN" , "" )
370479 }
371- t .Setenv ("STACKIT_CREDENTIALS_PATH" , "test-path" )
372480 t .Setenv ("STACKIT_SERVICE_ACCOUNT_EMAIL" , "test-email" )
373481
374482 // Get the default authentication client and ensure that it's not nil
0 commit comments