@@ -16,6 +16,7 @@ package cloud
1616
1717import (
1818 "context"
19+ "crypto/sha256"
1920 "encoding/base64"
2021 "fmt"
2122 "os"
@@ -25,7 +26,6 @@ import (
2526 "github.com/99designs/keyring"
2627 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
2728 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
28- "github.com/mitchellh/go-homedir"
2929 "github.com/streamnative/cloud-cli/pkg/auth"
3030 "github.com/streamnative/cloud-cli/pkg/auth/store"
3131 "github.com/streamnative/cloud-cli/pkg/cmd"
@@ -253,16 +253,6 @@ func Provider() *schema.Provider {
253253func providerConfigure (d * schema.ResourceData , terraformVersion string ) (interface {}, diag.Diagnostics ) {
254254 _ = terraformVersion
255255
256- home , err := homedir .Dir ()
257- if err != nil {
258- return nil , diag .FromErr (err )
259- }
260- configDir := filepath .Join (home , ".streamnative" )
261- if _ , err := os .Stat (configDir ); os .IsNotExist (err ) {
262- if err = os .MkdirAll (configDir , 0755 ); err != nil {
263- return nil , diag .FromErr (err )
264- }
265- }
266256 defaultIssuer := os .Getenv ("GLOBAL_DEFAULT_ISSUER" )
267257 if defaultIssuer == "" {
268258 defaultIssuer = GlobalDefaultIssuer
@@ -277,6 +267,12 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa
277267 }
278268 clientId := d .Get ("client_id" ).(string )
279269 clientSecret := d .Get ("client_secret" ).(string )
270+ keyFilePath := d .Get ("key_file_path" ).(string )
271+ configDir , err := getConfigDir (clientId , clientSecret , keyFilePath )
272+ if err != nil {
273+ return nil , diag .FromErr (err )
274+ }
275+
280276 var keyFile * auth.KeyFile
281277 var flow * auth.ClientCredentialsFlow
282278 var grant * auth.AuthorizationGrant
@@ -305,7 +301,6 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa
305301 return nil , diag .FromErr (err )
306302 }
307303 } else {
308- keyFilePath := d .Get ("key_file_path" ).(string )
309304 credsProvider := auth .NewClientCredentialsProviderFromKeyFile (keyFilePath )
310305 keyFile , err = credsProvider .GetClientCredentials ()
311306 if err != nil {
@@ -405,3 +400,23 @@ func makeKeyring(backendOverride string, configDir string) (keyring.Keyring, err
405400func keyringPrompt (prompt string ) (string , error ) {
406401 return "" , nil
407402}
403+
404+ // getConfigDir generate a unique configuration directory based on the provided arguments
405+ func getConfigDir (clientId , clientSecret , keyFilePath string ) (string , error ) {
406+ home , err := os .Getwd ()
407+ if err != nil {
408+ return "" , fmt .Errorf ("failed to get current working directory: %v" , err )
409+ }
410+ combined := fmt .Sprintf ("%s|%s|%s" , keyFilePath , clientId , clientSecret )
411+ hash := sha256 .Sum256 ([]byte (combined ))
412+ dirName := fmt .Sprintf (".streamnative_%x" , hash [:8 ])
413+
414+ configDir := filepath .Join (home , dirName )
415+
416+ if _ , err := os .Stat (configDir ); os .IsNotExist (err ) {
417+ if err = os .MkdirAll (configDir , 0755 ); err != nil {
418+ return "" , fmt .Errorf ("failed to create config directory: %v" , err )
419+ }
420+ }
421+ return configDir , nil
422+ }
0 commit comments