Skip to content

Commit ca6f0dd

Browse files
committed
Revert "removing functionality of ServiceAccountEmail"
1 parent e5159a4 commit ca6f0dd

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

core/auth/auth.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const (
3232
func SetupAuth(cfg *config.Configuration) (rt http.RoundTripper, err error) {
3333
if cfg == nil {
3434
cfg = &config.Configuration{}
35+
email := getServiceAccountEmail(cfg)
36+
cfg.ServiceAccountEmail = email
3537
}
3638

3739
if cfg.CustomAuth != nil {
@@ -242,6 +244,27 @@ func readCredential(cred credentialType, credentials *Credentials) (string, erro
242244
return credentialValue, nil
243245
}
244246

247+
// getServiceAccountEmail searches for an email in the following order: client configuration, environment variable, credentials file.
248+
// is not required for authentication, so it can be empty.
249+
//
250+
// Deprecated: ServiceAccountEmail is not required and will be removed after 12th June 2025.
251+
func getServiceAccountEmail(cfg *config.Configuration) string {
252+
if cfg.ServiceAccountEmail != "" {
253+
return cfg.ServiceAccountEmail
254+
}
255+
256+
email, emailSet := os.LookupEnv("STACKIT_SERVICE_ACCOUNT_EMAIL")
257+
if !emailSet || email == "" {
258+
credentials, err := readCredentialsFile(cfg.CredentialsFilePath)
259+
if err != nil {
260+
// email is not required for authentication, so it shouldnt block it
261+
return ""
262+
}
263+
return credentials.STACKIT_SERVICE_ACCOUNT_EMAIL
264+
}
265+
return email
266+
}
267+
245268
// getKey searches for a key in the following order: client configuration, environment variable, credentials file.
246269
func getKey(cfgKey, cfgKeyPath *string, envVar, credType credentialType, cfgCredFilePath string) error {
247270
if *cfgKey != "" {

core/auth/auth_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ func TestSetupAuth(t *testing.T) {
170170
t.Setenv("STACKIT_CREDENTIALS_PATH", "")
171171
}
172172

173+
t.Setenv("STACKIT_SERVICE_ACCOUNT_EMAIL", "test-email")
174+
173175
authRoundTripper, err := SetupAuth(test.config)
174176

175177
if err != nil && test.isValid {
@@ -568,6 +570,78 @@ func TestNoAuth(t *testing.T) {
568570
}
569571
}
570572

573+
func TestGetServiceAccountEmail(t *testing.T) {
574+
for _, test := range []struct {
575+
description string
576+
cfg *config.Configuration
577+
envEmailSet bool
578+
path string
579+
expectedEmail string
580+
isValid bool
581+
}{
582+
{
583+
description: "custom_config",
584+
cfg: &config.Configuration{
585+
ServiceAccountEmail: "test_email",
586+
},
587+
path: "",
588+
expectedEmail: "test_email",
589+
isValid: true,
590+
},
591+
{
592+
description: "config_over_env_var",
593+
cfg: &config.Configuration{},
594+
envEmailSet: true,
595+
path: "",
596+
expectedEmail: "env_email",
597+
isValid: true,
598+
},
599+
{
600+
description: "env_variable",
601+
cfg: &config.Configuration{
602+
ServiceAccountEmail: "test_email",
603+
},
604+
envEmailSet: true,
605+
path: "",
606+
expectedEmail: "test_email",
607+
isValid: true,
608+
},
609+
{
610+
description: "path",
611+
cfg: &config.Configuration{},
612+
envEmailSet: false,
613+
path: "test_resources/test_credentials_bar.json",
614+
expectedEmail: "bar_email",
615+
isValid: true,
616+
},
617+
{
618+
description: "invalid_structure",
619+
cfg: &config.Configuration{},
620+
envEmailSet: false,
621+
path: "test_resources/test_invalid_structure.json",
622+
expectedEmail: "",
623+
isValid: false,
624+
},
625+
} {
626+
t.Run(test.description, func(t *testing.T) {
627+
if test.envEmailSet {
628+
t.Setenv("STACKIT_SERVICE_ACCOUNT_EMAIL", "env_email")
629+
} else {
630+
t.Setenv("STACKIT_SERVICE_ACCOUNT_EMAIL", "")
631+
}
632+
t.Setenv("STACKIT_CREDENTIALS_PATH", test.path)
633+
got := getServiceAccountEmail(test.cfg)
634+
if (got != "") && !test.isValid {
635+
t.Errorf("getServiceAccountEmail() did not return empty value for invalid test case")
636+
return
637+
}
638+
if got != test.expectedEmail {
639+
t.Errorf("getServiceAccountEmail() = %v, want %v", got, test.expectedEmail)
640+
}
641+
})
642+
}
643+
}
644+
571645
func generatePrivateKey() (string, error) {
572646
// Generate a new RSA key pair with a size of 2048 bits
573647
privKey, err := rsa.GenerateKey(rand.Reader, 2048)

core/config/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ func WithTokenEndpoint(url string) ConfigurationOption {
178178
// WithServiceAccountEmail returns a ConfigurationOption that sets the service account email
179179
//
180180
// Deprecated: WithServiceAccountEmail is not required and will be removed after 12th June 2025.
181-
func WithServiceAccountEmail(_ string) ConfigurationOption {
182-
return func(_ *Configuration) error {
181+
func WithServiceAccountEmail(serviceAccountEmail string) ConfigurationOption {
182+
return func(config *Configuration) error {
183+
config.ServiceAccountEmail = serviceAccountEmail
183184
return nil
184185
}
185186
}

0 commit comments

Comments
 (0)