@@ -22,6 +22,8 @@ func AddProfileInitFlags(flag *pflag.FlagSet) {
22
22
flag .String (AWSProfileFlag , "" , "The AWS profile used to get the source_profile and mfa_serial attributes" )
23
23
flag .String (AWSRegionFlag , endpoints .UsWest2RegionID , "The AWS region" )
24
24
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" )
25
27
flag .String (OutputFlag , "json" , "The AWS CLI output format" )
26
28
27
29
// Verbose
@@ -49,6 +51,15 @@ func AddProfileCheckConfig(v *viper.Viper) error {
49
51
return fmt .Errorf ("IAM Role check failed: %w" , err )
50
52
}
51
53
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
+
52
63
if err := checkOutput (v ); err != nil {
53
64
return fmt .Errorf ("Output check failed: %w" , err )
54
65
}
@@ -84,20 +95,29 @@ func (apc *AddProfileConfig) AddProfile() error {
84
95
return fmt .Errorf ("unable to load aws config file: %w" , err )
85
96
}
86
97
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 {
94
100
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
+ }
99
120
}
100
- apc .MFASerial = mfaSerialKey .String ()
101
121
102
122
// Add each of the remaining profiles
103
123
for _ , profileAccount := range apc .AWSProfileAccounts {
@@ -175,6 +195,8 @@ func addProfileFunction(cmd *cobra.Command, args []string) error {
175
195
awsProfileAccount := v .GetStringSlice (AWSProfileAccountFlag )
176
196
awsProfile := v .GetString (AWSProfileFlag )
177
197
iamRole := v .GetString (IAMRoleFlag )
198
+ sourceProfile := v .GetString (SourceProfileFlag )
199
+ mfaSerial := v .GetString (MfaSerialFlag )
178
200
output := v .GetString (OutputFlag )
179
201
180
202
// initialize things
@@ -203,7 +225,9 @@ func addProfileFunction(cmd *cobra.Command, args []string) error {
203
225
204
226
// Profiles
205
227
addProfileConfig .AWSProfileAccounts = awsProfileAccount
228
+ addProfileConfig .BaseProfileName = sourceProfile
206
229
addProfileConfig .AWSProfileName = awsProfile
230
+ addProfileConfig .MFASerial = mfaSerial
207
231
208
232
if err := addProfileConfig .Run (); err != nil {
209
233
logger .Fatal (err )
0 commit comments