Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions modules/AWSPowerShell/Common/Internal/SSOProfileMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static class SSOProfileMethods

private const string SsoSessionProfilePrefix = "sso-session";

private const string DefaultProfileName = "default";

/// <summary>
/// Regex:
/// - Starts with "sso-session"
Expand Down Expand Up @@ -94,11 +96,11 @@ private static string GetSharedConfigFilePath(this SharedCredentialsFile @this)
/// Returns a ProfileIniFile of the shared config file.
/// </summary>
/// <param name="this">The SharedCredentialsFile to retrieve the config file for.</param>
/// <param name="isDefaultProfile">When true, opens the file in permissive mode so bare [default] section headers are recognized.</param>
/// <returns>A ProfileIniFile instance of the config file.</returns>
private static ProfileIniFile GetSharedConfigFile(this SharedCredentialsFile @this)
private static ProfileIniFile GetSharedConfigFile(this SharedCredentialsFile @this, bool isDefaultProfile = false)
{
// Second parameter profileMarkerRequired is required to be true for config files, but not for general ini files.
return new ProfileIniFile(@this.GetSharedConfigFilePath(), true);
return new ProfileIniFile(@this.GetSharedConfigFilePath(), !isDefaultProfile);
}

private static void ThrowOnNullOrWhiteSpace(string name, string value)
Expand Down Expand Up @@ -191,8 +193,11 @@ public static void RegisterSsoProfileAndSession(this CredentialProfile profile)
profileProperties.Add(_regionPropertyName, profile.Region.SystemName);
}

var configFile = sharedCredentialsFile.GetSharedConfigFile();
configFile.EnsureSectionExists(SSOProfileMethods.CreateProfileName(profile.Name));
var isDefaultProfile = profile.Name == DefaultProfileName;
var profileSectionName = isDefaultProfile ? DefaultProfileName : SSOProfileMethods.CreateProfileName(profile.Name);

var configFile = sharedCredentialsFile.GetSharedConfigFile(isDefaultProfile);
configFile.EnsureSectionExists(profileSectionName);
configFile.EnsureSectionExists(SSOProfileMethods.CreateSsoSessionProfileName(options.SsoSession));

configFile.EditSection(profile.Name, false, profileProperties); // Section must already exist to edit sso-session
Expand Down
6 changes: 3 additions & 3 deletions modules/AWSPowerShell/Common/SSOCmdlets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ protected override void ProcessRecord()
if (!SettingsStore.TryGetProfile("default", null, out var profile))
{
this.ThrowTerminatingError(new ErrorRecord(
new ArgumentException($"profile {ProfileName} not found in the shared config (~/.aws/config) file."),
"ArgumentException", ErrorCategory.InvalidArgument, this.ProfileName));
new ArgumentException($"default profile not found in the shared config (~/.aws/config) file."),
"ArgumentException", ErrorCategory.InvalidArgument, this));
}

if (profile.Options.SsoSession != null && profile.Options.SsoStartUrl != null &&
Expand All @@ -132,7 +132,7 @@ protected override void ProcessRecord()
{
this.ThrowTerminatingError(new ErrorRecord(
new ArgumentException($"Either ProfileName or SessionName or a default profile with SSO configuration is required."),
"ArgumentException", ErrorCategory.InvalidArgument, this.ProfileName));
"ArgumentException", ErrorCategory.InvalidArgument, this));
}
}

Expand Down
Loading