Skip to content

Commit 2d8eccf

Browse files
author
Chris Gilmer
authored
Merge pull request #83 from trussworks/cg_overrides_for_profiles
Add overrides for source_profile and mfa_serial to add-profiles subcommand
2 parents 80b656a + 4518014 commit 2d8eccf

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

cmd/add_profile.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ func AddProfileInitFlags(flag *pflag.FlagSet) {
2222
flag.String(AWSProfileFlag, "", "The AWS profile used to get the source_profile and mfa_serial attributes")
2323
flag.String(AWSRegionFlag, endpoints.UsWest2RegionID, "The AWS region")
2424
flag.String(IAMRoleFlag, "", "The IAM role name assigned to the user being setup")
25+
flag.String(SourceProfileFlag, "", "The source_profile to use which overrides the one from the AWS profile being used")
26+
flag.String(MfaSerialFlag, "", "The mfa_serial to use which overrides the one from the AWS profile being used")
2527
flag.String(OutputFlag, "json", "The AWS CLI output format")
2628

2729
// Verbose
@@ -49,6 +51,15 @@ func AddProfileCheckConfig(v *viper.Viper) error {
4951
return fmt.Errorf("IAM Role check failed: %w", err)
5052
}
5153

54+
awsProfile := v.GetString(AWSProfileFlag)
55+
sourceProfile := v.GetString(SourceProfileFlag)
56+
mfaSerial := v.GetString(MfaSerialFlag)
57+
if len(awsProfile) == 0 {
58+
if len(sourceProfile) == 0 || len(mfaSerial) == 0 {
59+
return fmt.Errorf("No %q was provided. Must provide either %q or both %q and %q flags", AWSProfileFlag, AWSProfileFlag, SourceProfileFlag, MfaSerialFlag)
60+
}
61+
}
62+
5263
if err := checkOutput(v); err != nil {
5364
return fmt.Errorf("Output check failed: %w", err)
5465
}
@@ -84,20 +95,29 @@ func (apc *AddProfileConfig) AddProfile() error {
8495
return fmt.Errorf("unable to load aws config file: %w", err)
8596
}
8697

87-
roleProfileSection := iniFile.Section(fmt.Sprintf("profile %s", apc.AWSProfileName))
88-
// Get the source profile
89-
sourceProfileKey, err := roleProfileSection.GetKey("source_profile")
90-
if err != nil {
91-
return fmt.Errorf("Unable to get source profile from %q: %w", apc.AWSProfileName, err)
92-
}
93-
apc.BaseProfileName = sourceProfileKey.String()
98+
// Pull profile details from existing AWS profile provided by user
99+
if len(apc.AWSProfileName) != 0 {
94100

95-
// Get the MFA Serial
96-
mfaSerialKey, err := roleProfileSection.GetKey("mfa_serial")
97-
if err != nil {
98-
return err
101+
roleProfileSection := iniFile.Section(fmt.Sprintf("profile %s", apc.AWSProfileName))
102+
103+
// Get the source profile
104+
if len(apc.BaseProfileName) == 0 {
105+
sourceProfileKey, err := roleProfileSection.GetKey("source_profile")
106+
if err != nil {
107+
return fmt.Errorf("Unable to get source_profile from %q: %w", apc.AWSProfileName, err)
108+
}
109+
apc.BaseProfileName = sourceProfileKey.String()
110+
}
111+
112+
// Get the MFA Serial
113+
if len(apc.MFASerial) == 0 {
114+
mfaSerialKey, err := roleProfileSection.GetKey("mfa_serial")
115+
if err != nil {
116+
return fmt.Errorf("Unable to get mfa_serial from %q: %w", apc.AWSProfileName, err)
117+
}
118+
apc.MFASerial = mfaSerialKey.String()
119+
}
99120
}
100-
apc.MFASerial = mfaSerialKey.String()
101121

102122
// Add each of the remaining profiles
103123
for _, profileAccount := range apc.AWSProfileAccounts {
@@ -175,6 +195,8 @@ func addProfileFunction(cmd *cobra.Command, args []string) error {
175195
awsProfileAccount := v.GetStringSlice(AWSProfileAccountFlag)
176196
awsProfile := v.GetString(AWSProfileFlag)
177197
iamRole := v.GetString(IAMRoleFlag)
198+
sourceProfile := v.GetString(SourceProfileFlag)
199+
mfaSerial := v.GetString(MfaSerialFlag)
178200
output := v.GetString(OutputFlag)
179201

180202
// initialize things
@@ -203,7 +225,9 @@ func addProfileFunction(cmd *cobra.Command, args []string) error {
203225

204226
// Profiles
205227
addProfileConfig.AWSProfileAccounts = awsProfileAccount
228+
addProfileConfig.BaseProfileName = sourceProfile
206229
addProfileConfig.AWSProfileName = awsProfile
230+
addProfileConfig.MFASerial = mfaSerial
207231

208232
if err := addProfileConfig.Run(); err != nil {
209233
logger.Fatal(err)

cmd/cli.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ const (
3030
// IAMRoleFlag is the IAM Role name Flag
3131
IAMRoleFlag string = "iam-role"
3232

33+
// SourceProfileFlag is the Source Profile Flag
34+
SourceProfileFlag string = "source-profile"
35+
// MfaSerialFlag is the MFA Serial Flag
36+
MfaSerialFlag string = "mfa-serial"
37+
3338
// OutputFlag is the Output Flag
3439
OutputFlag string = "output"
3540

0 commit comments

Comments
 (0)