protected override void ProcessRecord() { AWSPSCredentials currentCredentials = null; if (this.TryGetCredentials(Host, out currentCredentials, SessionState)) { WriteVerbose(InitializeDefaultConfigurationCmdlet.CredentialsSourceMessage(currentCredentials)); if (string.IsNullOrEmpty(StoreAs)) { SetUpIfFederatedCredentials(currentCredentials); string scope = MyInvocation.BoundParameters.ContainsKey("Scope") ? Scope.ToString() + ":" : ""; this.SessionState.PSVariable.Set(scope + SessionKeys.AWSCredentialsVariableName, currentCredentials); } else { if (MyInvocation.BoundParameters.ContainsKey("Scope")) { this.ThrowTerminatingError(new ErrorRecord( new ArgumentException("Parameters Scope and StoreAs cannot be used together."), "ArgumentException", ErrorCategory.InvalidArgument, this)); } if (string.Equals(AWSCredentialsArgumentsFullCmdlet.AWSCredentialsObjectSet, ParameterSetName, StringComparison.Ordinal)) { // We're storing from the -Credential parameter to a credentials file. // Only some types of AWSCredentials are supported. var options = CredentialProfileOptionsExtractor.ExtractProfileOptions(currentCredentials.Credentials); if (options != null) { SettingsStore.RegisterProfile(options, StoreAs, ProfileLocation, null); } } else { if (string.Equals(AWSCredentialsArgumentsFullCmdlet.StoredProfileSet, ParameterSetName, StringComparison.Ordinal)) { // We're copying from one profile to another. var chain = new CredentialProfileStoreChain(ProfileLocation); CredentialProfile profile; if (chain.TryGetProfile(ProfileName, out profile)) { profile.CredentialProfileStore.CopyProfile(ProfileName, StoreAs, true); } else { // Parameters.TryGetCredentials has already tested for this but... this.ThrowTerminatingError(new ErrorRecord( new ArgumentException("Cannot determine credentials from supplied parameters"), "ArgumentException", ErrorCategory.InvalidArgument, this)); } } else { // We're storing from individual command line values to a credentials file. SettingsStore.RegisterProfile(GetCredentialProfileOptions(), StoreAs, ProfileLocation, null); } } if (string.IsNullOrEmpty(ProfileLocation)) { WriteVerbose("Updated .NET credentials file."); } else { WriteVerbose("Updated shared credentials file at " + ProfileLocation); } } } else { this.ThrowTerminatingError(new ErrorRecord(new ArgumentException("Cannot determine credentials from supplied parameters"), "ArgumentException", ErrorCategory.InvalidArgument, this)); } }
protected override void ProcessRecord() { AWSPSCredentials awsPSCredentialsFromCommandLine; RegionEndpoint regionFromCommandLine; AWSPSCredentials awsPSCredentialsFromDefaultProfile; RegionEndpoint regionFromDefaultProfile; AWSPSCredentials awsPSCredentialsFromPromptingUser = null; RegionEndpoint regionFromPromptingUser = null; // get what was passed in on the command line, and what's already in defaults GetFromCommandLine(out awsPSCredentialsFromCommandLine, out regionFromCommandLine); GetFromDefaultProfile(out awsPSCredentialsFromDefaultProfile, out regionFromDefaultProfile); // if the command line and defaults don't provide us what we need then prompt the user if (awsPSCredentialsFromCommandLine == null && awsPSCredentialsFromDefaultProfile == null) { awsPSCredentialsFromPromptingUser = PromptUserForCredentials(); } if (regionFromCommandLine == null && regionFromDefaultProfile == null) { regionFromPromptingUser = PromptUserForRegion(); } // figure out what needs to be saved to disk and what needs to get put in session var awsPSCredentialsToPersist = awsPSCredentialsFromCommandLine == null ? awsPSCredentialsFromPromptingUser : awsPSCredentialsFromCommandLine; var awsPSCredentialsToPutInSession = awsPSCredentialsToPersist == null ? awsPSCredentialsFromDefaultProfile : awsPSCredentialsToPersist; var regionToPersist = regionFromCommandLine == null ? regionFromPromptingUser : regionFromCommandLine; var regionToPutInSession = regionToPersist == null ? regionFromDefaultProfile : regionToPersist; // save credentials and region to disk if necessary if (awsPSCredentialsToPersist != null || regionToPersist != null) { if (string.Equals(AWSCredentialsArgumentsFullCmdlet.AWSCredentialsObjectSet, ParameterSetName, StringComparison.Ordinal)) { // We're storing from the -Credential parameter to a credentials file. // Only some types of AWSCredentials are supported. var options = CredentialProfileOptionsExtractor.ExtractProfileOptions(awsPSCredentialsToPersist.Credentials); if (options != null) { SettingsStore.RegisterProfile(options, SettingsStore.PSDefaultSettingName, ProfileLocation, regionToPersist); } } else { if (string.Equals(AWSCredentialsArgumentsFullCmdlet.StoredProfileSet, ParameterSetName, StringComparison.Ordinal)) { // We're copying from one profile to another. var chain = new CredentialProfileStoreChain(ProfileLocation); CredentialProfile profile; if (chain.TryGetProfile(ProfileName, out profile)) { profile.CredentialProfileStore.CopyProfile(ProfileName, SettingsStore.PSDefaultSettingName, true); SettingsStore.RegisterProfile(new CredentialProfileOptions(), SettingsStore.PSDefaultSettingName, ProfileLocation, regionToPersist); } else { // Parameters.TryGetCredentials has already tested for this but... this.ThrowTerminatingError(new ErrorRecord( new ArgumentException("Cannot determine credentials from supplied parameters"), "ArgumentException", ErrorCategory.InvalidArgument, this)); } } else { // We're storing from individual command line values to the default profile. SettingsStore.RegisterProfile(GetCredentialProfileOptions(), SettingsStore.PSDefaultSettingName, ProfileLocation, regionToPersist); } } if (string.IsNullOrEmpty(ProfileLocation)) { WriteVerbose("Updated SDK profile store."); } else { WriteVerbose("Updated credential file at " + ProfileLocation); } WriteVerbose(string.Format("Default credentials and/or region have been stored to credentials profile '{0}' and set active for this shell.", SettingsStore.PSDefaultSettingName)); } string scope = MyInvocation.BoundParameters.ContainsKey("Scope") ? Scope.ToString() + ":" : ""; // put credentials and region in session this.SessionState.PSVariable.Set(scope + SessionKeys.AWSCredentialsVariableName, awsPSCredentialsToPutInSession); this.SessionState.PSVariable.Set(scope + SessionKeys.AWSRegionVariableName, regionToPutInSession.SystemName); }