Skip to content

Commit cf78af3

Browse files
committed
Refactor for clarity
1 parent f2527e2 commit cf78af3

File tree

9 files changed

+107
-107
lines changed

9 files changed

+107
-107
lines changed

cli/exec.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type ExecCommandInput struct {
2929
StartEcsServer bool
3030
Lazy bool
3131
JSONDeprecated bool
32-
Config vault.Config
32+
Config vault.ProfileConfig
3333
SessionDuration time.Duration
3434
NoSession bool
3535
UseStdout bool
@@ -167,7 +167,7 @@ func ExecCommand(input ExecCommandInput, f *vault.ConfigFile, keyring keyring.Ke
167167
return 0, err
168168
}
169169

170-
config, err := vault.NewConfigLoader(input.Config, f, input.ProfileName).LoadFromProfile(input.ProfileName)
170+
config, err := vault.NewConfigLoader(input.Config, f, input.ProfileName).GetProfileConfig(input.ProfileName)
171171
if err != nil {
172172
return 0, fmt.Errorf("Error loading config: %w", err)
173173
}
@@ -260,7 +260,7 @@ func createEnv(profileName string, region string) environ {
260260
return env
261261
}
262262

263-
func startEcsServerAndSetEnv(credsProvider aws.CredentialsProvider, config *vault.Config, lazy bool, cmdEnv *environ) error {
263+
func startEcsServerAndSetEnv(credsProvider aws.CredentialsProvider, config *vault.ProfileConfig, lazy bool, cmdEnv *environ) error {
264264
ecsServer, err := server.NewEcsServer(context.TODO(), credsProvider, config, "", 0, lazy)
265265
if err != nil {
266266
return err

cli/export.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
type ExportCommandInput struct {
2020
ProfileName string
2121
Format string
22-
Config vault.Config
22+
Config vault.ProfileConfig
2323
SessionDuration time.Duration
2424
NoSession bool
2525
UseStdout bool
@@ -90,7 +90,7 @@ func ExportCommand(input ExportCommandInput, f *vault.ConfigFile, keyring keyrin
9090
return fmt.Errorf("in an existing aws-vault subshell; 'exit' from the subshell or unset AWS_VAULT to force")
9191
}
9292

93-
config, err := vault.NewConfigLoader(input.Config, f, input.ProfileName).LoadFromProfile(input.ProfileName)
93+
config, err := vault.NewConfigLoader(input.Config, f, input.ProfileName).GetProfileConfig(input.ProfileName)
9494
if err != nil {
9595
return fmt.Errorf("Error loading config: %w", err)
9696
}

cli/login.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type LoginCommandInput struct {
2424
ProfileName string
2525
UseStdout bool
2626
Path string
27-
Config vault.Config
27+
Config vault.ProfileConfig
2828
SessionDuration time.Duration
2929
NoSession bool
3030
}
@@ -81,7 +81,7 @@ func ConfigureLoginCommand(app *kingpin.Application, a *AwsVault) {
8181
}
8282

8383
func LoginCommand(input LoginCommandInput, f *vault.ConfigFile, keyring keyring.Keyring) error {
84-
config, err := vault.NewConfigLoader(input.Config, f, input.ProfileName).LoadFromProfile(input.ProfileName)
84+
config, err := vault.NewConfigLoader(input.Config, f, input.ProfileName).GetProfileConfig(input.ProfileName)
8585
if err != nil {
8686
return fmt.Errorf("Error loading config: %w", err)
8787
}

cli/rotate.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
type RotateCommandInput struct {
1717
NoSession bool
1818
ProfileName string
19-
Config vault.Config
19+
Config vault.ProfileConfig
2020
}
2121

2222
func ConfigureRotateCommand(app *kingpin.Application, a *AwsVault) {
@@ -55,7 +55,7 @@ func RotateCommand(input RotateCommandInput, f *vault.ConfigFile, keyring keyrin
5555
vault.UseSessionCache = false
5656

5757
configLoader := vault.NewConfigLoader(input.Config, f, input.ProfileName)
58-
config, err := configLoader.LoadFromProfile(input.ProfileName)
58+
config, err := configLoader.GetProfileConfig(input.ProfileName)
5959
if err != nil {
6060
return fmt.Errorf("Error loading config: %w", err)
6161
}
@@ -170,7 +170,7 @@ func retry(maxTime time.Duration, sleep time.Duration, f func() error) (err erro
170170
}
171171
}
172172

173-
func getUsernameIfAssumingRole(ctx context.Context, awsCfg aws.Config, config *vault.Config) (*string, error) {
173+
func getUsernameIfAssumingRole(ctx context.Context, awsCfg aws.Config, config *vault.ProfileConfig) (*string, error) {
174174
if config.RoleARN != "" {
175175
n, err := vault.GetUsernameFromSession(ctx, awsCfg)
176176
if err != nil {
@@ -185,7 +185,7 @@ func getUsernameIfAssumingRole(ctx context.Context, awsCfg aws.Config, config *v
185185
func getProfilesInChain(profileName string, configLoader *vault.ConfigLoader) (profileNames []string, err error) {
186186
profileNames = append(profileNames, profileName)
187187

188-
config, err := configLoader.LoadFromProfile(profileName)
188+
config, err := configLoader.GetProfileConfig(profileName)
189189
if err != nil {
190190
return profileNames, err
191191
}

server/ecsserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ type EcsServer struct {
6363
server http.Server
6464
cache sync.Map
6565
baseCredsProvider aws.CredentialsProvider
66-
config *vault.Config
66+
config *vault.ProfileConfig
6767
}
6868

69-
func NewEcsServer(ctx context.Context, baseCredsProvider aws.CredentialsProvider, config *vault.Config, authToken string, port int, lazyLoadBaseCreds bool) (*EcsServer, error) {
69+
func NewEcsServer(ctx context.Context, baseCredsProvider aws.CredentialsProvider, config *vault.ProfileConfig, authToken string, port int, lazyLoadBaseCreds bool) (*EcsServer, error) {
7070
listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
7171
if err != nil {
7272
return nil, err

vault/config.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,14 @@ func (c *ConfigFile) ProfileNames() []string {
269269

270270
// ConfigLoader loads config from configfile and environment variables
271271
type ConfigLoader struct {
272-
BaseConfig Config
273-
File *ConfigFile
274-
ActiveProfile string
272+
BaseConfig ProfileConfig
273+
File *ConfigFile
274+
ActiveProfile string
275+
275276
visitedProfiles []string
276277
}
277278

278-
func NewConfigLoader(baseConfig Config, file *ConfigFile, activeProfile string) *ConfigLoader {
279+
func NewConfigLoader(baseConfig ProfileConfig, file *ConfigFile, activeProfile string) *ConfigLoader {
279280
return &ConfigLoader{
280281
BaseConfig: baseConfig,
281282
File: file,
@@ -297,7 +298,7 @@ func (cl *ConfigLoader) resetLoopDetection() {
297298
cl.visitedProfiles = []string{}
298299
}
299300

300-
func (cl *ConfigLoader) populateFromDefaults(config *Config) {
301+
func (cl *ConfigLoader) populateFromDefaults(config *ProfileConfig) {
301302
if config.AssumeRoleDuration == 0 {
302303
config.AssumeRoleDuration = DefaultSessionDuration
303304
}
@@ -312,7 +313,7 @@ func (cl *ConfigLoader) populateFromDefaults(config *Config) {
312313
}
313314
}
314315

315-
func (cl *ConfigLoader) populateFromConfigFile(config *Config, profileName string) error {
316+
func (cl *ConfigLoader) populateFromConfigFile(config *ProfileConfig, profileName string) error {
316317
if !cl.visitProfile(profileName) {
317318
return fmt.Errorf("Loop detected in config file for profile '%s'", profileName)
318319
}
@@ -419,7 +420,7 @@ func (cl *ConfigLoader) populateFromConfigFile(config *Config, profileName strin
419420
return nil
420421
}
421422

422-
func (cl *ConfigLoader) populateFromEnv(profile *Config) {
423+
func (cl *ConfigLoader) populateFromEnv(profile *ProfileConfig) {
423424
if region := os.Getenv("AWS_REGION"); region != "" && profile.Region == "" {
424425
log.Printf("Using region %q from AWS_REGION", region)
425426
profile.Region = region
@@ -501,9 +502,9 @@ func (cl *ConfigLoader) populateFromEnv(profile *Config) {
501502
}
502503
}
503504

504-
func (cl *ConfigLoader) hydrateSourceConfig(config *Config) error {
505+
func (cl *ConfigLoader) hydrateSourceConfig(config *ProfileConfig) error {
505506
if config.SourceProfileName != "" {
506-
sc, err := cl.LoadFromProfile(config.SourceProfileName)
507+
sc, err := cl.GetProfileConfig(config.SourceProfileName)
507508
if err != nil {
508509
return err
509510
}
@@ -513,8 +514,8 @@ func (cl *ConfigLoader) hydrateSourceConfig(config *Config) error {
513514
return nil
514515
}
515516

516-
// LoadFromProfile loads the profile from the config file and environment variables into config
517-
func (cl *ConfigLoader) LoadFromProfile(profileName string) (*Config, error) {
517+
// GetProfileConfig loads the profile from the config file and environment variables into config
518+
func (cl *ConfigLoader) GetProfileConfig(profileName string) (*ProfileConfig, error) {
518519
config := cl.BaseConfig
519520
config.ProfileName = profileName
520521
cl.populateFromEnv(&config)
@@ -535,19 +536,19 @@ func (cl *ConfigLoader) LoadFromProfile(profileName string) (*Config, error) {
535536
return &config, nil
536537
}
537538

538-
// Config is a collection of configuration options for creating temporary credentials
539-
type Config struct {
539+
// ProfileConfig is a collection of configuration options for creating temporary credentials
540+
type ProfileConfig struct {
540541
// ProfileName specifies the name of the profile config
541542
ProfileName string
542543

543544
// SourceProfile is the profile where credentials come from
544545
SourceProfileName string
545546

546547
// SourceProfile is the profile where credentials come from
547-
SourceProfile *Config
548+
SourceProfile *ProfileConfig
548549

549-
// ChainedFromProfile is the profile that used this profile as it's source profile
550-
ChainedFromProfile *Config
550+
// ChainedFromProfile is the profile that used this profile as its source profile
551+
ChainedFromProfile *ProfileConfig
551552

552553
// Region is the AWS region
553554
Region string
@@ -619,7 +620,7 @@ type Config struct {
619620
}
620621

621622
// SetSessionTags parses a comma separated key=vaue string and sets Config.SessionTags map
622-
func (c *Config) SetSessionTags(s string) error {
623+
func (c *ProfileConfig) SetSessionTags(s string) error {
623624
c.SessionTags = make(map[string]string)
624625
for _, tag := range strings.Split(s, ",") {
625626
kvPair := strings.SplitN(tag, "=", 2)
@@ -633,54 +634,54 @@ func (c *Config) SetSessionTags(s string) error {
633634
}
634635

635636
// SetTransitiveSessionTags parses a comma separated string and sets Config.TransitiveSessionTags
636-
func (c *Config) SetTransitiveSessionTags(s string) {
637+
func (c *ProfileConfig) SetTransitiveSessionTags(s string) {
637638
for _, tag := range strings.Split(s, ",") {
638639
if tag = strings.TrimSpace(tag); tag != "" {
639640
c.TransitiveSessionTags = append(c.TransitiveSessionTags, tag)
640641
}
641642
}
642643
}
643644

644-
func (c *Config) IsChained() bool {
645+
func (c *ProfileConfig) IsChained() bool {
645646
return c.ChainedFromProfile != nil
646647
}
647648

648-
func (c *Config) HasSourceProfile() bool {
649+
func (c *ProfileConfig) HasSourceProfile() bool {
649650
return c.SourceProfile != nil
650651
}
651652

652-
func (c *Config) HasMfaSerial() bool {
653+
func (c *ProfileConfig) HasMfaSerial() bool {
653654
return c.MfaSerial != ""
654655
}
655656

656-
func (c *Config) HasRole() bool {
657+
func (c *ProfileConfig) HasRole() bool {
657658
return c.RoleARN != ""
658659
}
659660

660-
func (c *Config) HasSSOSession() bool {
661+
func (c *ProfileConfig) HasSSOSession() bool {
661662
return c.SSOSession != ""
662663
}
663664

664-
func (c *Config) HasSSOStartURL() bool {
665+
func (c *ProfileConfig) HasSSOStartURL() bool {
665666
return c.SSOStartURL != ""
666667
}
667668

668-
func (c *Config) HasWebIdentity() bool {
669+
func (c *ProfileConfig) HasWebIdentity() bool {
669670
return c.WebIdentityTokenFile != "" || c.WebIdentityTokenProcess != ""
670671
}
671672

672-
func (c *Config) HasCredentialProcess() bool {
673+
func (c *ProfileConfig) HasCredentialProcess() bool {
673674
return c.CredentialProcess != ""
674675
}
675676

676-
func (c *Config) GetSessionTokenDuration() time.Duration {
677+
func (c *ProfileConfig) GetSessionTokenDuration() time.Duration {
677678
if c.IsChained() {
678679
return c.ChainedGetSessionTokenDuration
679680
}
680681
return c.NonChainedGetSessionTokenDuration
681682
}
682683

683-
func (c *Config) Validate() error {
684+
func (c *ProfileConfig) Validate() error {
684685
if c.HasSSOSession() && !c.HasSSOStartURL() {
685686
return fmt.Errorf("profile '%s' has sso_session but no sso_start_url", c.ProfileName)
686687
}
@@ -700,7 +701,6 @@ func (c *Config) Validate() error {
700701
} else if c.HasRole() {
701702
n++
702703
}
703-
704704
if n > 1 {
705705
return fmt.Errorf("profile '%s' has more than one source of credentials", c.ProfileName)
706706
}

vault/config_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func TestIncludeProfile(t *testing.T) {
254254
}
255255

256256
configLoader := &vault.ConfigLoader{File: configFile}
257-
config, err := configLoader.LoadFromProfile("testincludeprofile2")
257+
config, err := configLoader.GetProfileConfig("testincludeprofile2")
258258
if err != nil {
259259
t.Fatalf("Should have found a profile: %v", err)
260260
}
@@ -274,7 +274,7 @@ func TestIncludeSsoSession(t *testing.T) {
274274
}
275275

276276
configLoader := &vault.ConfigLoader{File: configFile}
277-
config, err := configLoader.LoadFromProfile("with-sso-session")
277+
config, err := configLoader.GetProfileConfig("with-sso-session")
278278
if err != nil {
279279
t.Fatalf("Should have found a profile: %v", err)
280280
}
@@ -369,7 +369,7 @@ source_profile=foo
369369
}
370370

371371
configLoader := &vault.ConfigLoader{File: configFile}
372-
config, err := configLoader.LoadFromProfile("foo")
372+
config, err := configLoader.GetProfileConfig("foo")
373373
if err != nil {
374374
t.Fatalf("Should have found a profile: %v", err)
375375
}
@@ -406,7 +406,7 @@ source_profile=root
406406
}
407407

408408
configLoader := &vault.ConfigLoader{File: configFile}
409-
config, err := configLoader.LoadFromProfile("foo")
409+
config, err := configLoader.GetProfileConfig("foo")
410410
if err != nil {
411411
t.Fatalf("Should have found a profile: %v", err)
412412
}
@@ -441,7 +441,7 @@ func TestSetSessionTags(t *testing.T) {
441441
}
442442

443443
for _, tc := range testCases {
444-
config := vault.Config{}
444+
config := vault.ProfileConfig{}
445445
err := config.SetSessionTags(tc.stringValue)
446446
if tc.ok {
447447
if err != nil {
@@ -473,7 +473,7 @@ func TestSetTransitiveSessionTags(t *testing.T) {
473473
}
474474

475475
for _, tc := range testCases {
476-
config := vault.Config{}
476+
config := vault.ProfileConfig{}
477477
config.SetTransitiveSessionTags(tc.stringValue)
478478
if !reflect.DeepEqual(tc.expected, config.TransitiveSessionTags) {
479479
t.Fatalf("Expected TransitiveSessionTags: %+v, got %+v", tc.expected, config.TransitiveSessionTags)
@@ -496,7 +496,7 @@ transitive_session_tags = tagOne ,tagTwo,tagThree
496496
t.Fatal(err)
497497
}
498498
configLoader := &vault.ConfigLoader{File: configFile, ActiveProfile: "tagged"}
499-
config, err := configLoader.LoadFromProfile("tagged")
499+
config, err := configLoader.GetProfileConfig("tagged")
500500
if err != nil {
501501
t.Fatalf("Should have found a profile: %v", err)
502502
}
@@ -533,7 +533,7 @@ transitive_session_tags = tagOne ,tagTwo,tagThree
533533
t.Fatal(err)
534534
}
535535
configLoader := &vault.ConfigLoader{File: configFile, ActiveProfile: "tagged"}
536-
config, err := configLoader.LoadFromProfile("tagged")
536+
config, err := configLoader.GetProfileConfig("tagged")
537537
if err != nil {
538538
t.Fatalf("Should have found a profile: %v", err)
539539
}
@@ -578,7 +578,7 @@ source_profile = interim
578578
t.Fatal(err)
579579
}
580580
configLoader := &vault.ConfigLoader{File: configFile, ActiveProfile: "target"}
581-
config, err := configLoader.LoadFromProfile("target")
581+
config, err := configLoader.GetProfileConfig("target")
582582
if err != nil {
583583
t.Fatalf("Should have found a profile: %v", err)
584584
}
@@ -640,13 +640,13 @@ credential_process = true
640640
configFile, _ := vault.LoadConfig(f)
641641
configLoader := &vault.ConfigLoader{File: configFile}
642642

643-
config, _ := configLoader.LoadFromProfile("foo:staging")
643+
config, _ := configLoader.GetProfileConfig("foo:staging")
644644
err := config.Validate()
645645
if err != nil {
646646
t.Fatalf("Should have validated: %v", err)
647647
}
648648

649-
config, _ = configLoader.LoadFromProfile("foo:production")
649+
config, _ = configLoader.GetProfileConfig("foo:production")
650650
err = config.Validate()
651651
if err == nil {
652652
t.Fatalf("Should have failed validation: %v", err)

vault/mfa.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (m *Mfa) GetMfaSerial() string {
3333
return m.mfaSerial
3434
}
3535

36-
func NewMfa(config *Config) *Mfa {
36+
func NewMfa(config *ProfileConfig) *Mfa {
3737
m := Mfa{
3838
mfaSerial: config.MfaSerial,
3939
}

0 commit comments

Comments
 (0)