/// <summary> /// Tests if an AWSCredentialsProfile instance could be instantiated from /// the persisted settings data. /// </summary> /// <param name="os">The persisted settings.</param> /// <returns>True if the settings are compatible with an AWSCredentialsProfile type.</returns> public static bool CanCreateFrom(SettingsCollection.ObjectSettings os) { var osProfileType = os.GetValueOrDefault(SettingsConstants.ProfileTypeField, null); // legacy AWS profiles will not have the type key present return(osProfileType == null || osProfileType.Equals(ProfileManager.AWSCredentialsProfileType, StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// Copies the contents of the source profile to the destination. If the destination /// profile does not exist a new profile is created. Note that if the destination /// profile exists, all keys it contains are removed and replaced with keys from the /// source profile. /// </summary> /// <param name="source">The source profile to copy keys and values from.</param> /// <param name="destinationProfileName">The name of the profile to create or update.</param> /// <returns>The unique id assigned to the destination settings.</returns> public static string CopyProfileSettings(SettingsCollection.ObjectSettings source, string destinationProfileName) { var allSettings = PersistenceManager.Instance.GetSettings(SettingsConstants.RegisteredProfiles); var destination = ReadProfileSettings(allSettings, destinationProfileName); // overwrite with new object if dest exists, not merge, otherwise we can potentially mix credential // profile types if (destination == null) { destination = allSettings.NewObjectSettings(Guid.NewGuid().ToString()); } else { destination = allSettings.NewObjectSettings(destination.UniqueKey); } destination[SettingsConstants.DisplayNameField] = destinationProfileName; foreach (var k in source.Keys) { if (!k.Equals(SettingsConstants.DisplayNameField)) { destination[k] = source[k]; } } PersistenceManager.Instance.SaveSettings(SettingsConstants.RegisteredProfiles, allSettings); return(destination.UniqueKey); }
/// <summary> /// Unregister an object from the store. /// </summary> /// <param name="uniqueKey">The unique key for the object.</param> public void UnregisterObject(string uniqueKey) { var settings = GetSettings(); SettingsCollection.ObjectSettings objectSettings = null; if (TryGetObjectSettings(uniqueKey, settings, out objectSettings)) { settings.Remove(objectSettings.UniqueKey); SaveSettings(settings); } }
/// <summary> /// Verifies that the persisted settings contains the minimal viable data to /// instantiate an AWSCredentialsProfile instance. /// </summary> /// <param name="os">The persisted settings.</param> /// <exception cref="InvalidDataException">Thrown if the profile settings fail to validate.</exception> private static void Validate(SettingsCollection.ObjectSettings os) { var accessKeyId = os.GetValueOrDefault(SettingsConstants.AccessKeyField, null); if (accessKeyId == null) { throw new InvalidDataException("Missing or invalid access key value in the profile settings."); } var secretkey = os.GetValueOrDefault(SettingsConstants.SecretKeyField, null); if (secretkey == null) { throw new InvalidDataException("Missing or invalid secret key value in the profile settings."); } }
/// <summary> /// Register a profile that can later be referenced by the profileName. /// This profile will only be visible for the current user. /// </summary> /// <param name="profileName">Name given to the AWS credentials.</param> /// <param name="accessKeyId">The AWS access key id</param> /// <param name="secretKey">The AWS secret key</param> public static void RegisterProfile(string profileName, string accessKeyId, string secretKey) { var settings = PersistenceManager.Instance.GetSettings(SettingsConstants.RegisteredProfiles); SettingsCollection.ObjectSettings os = null; os = settings.FirstOrDefault(x => string.Equals(x[SettingsConstants.DisplayNameField], profileName, StringComparison.OrdinalIgnoreCase)); if (os == null) { os = settings.NewObjectSettings(Guid.NewGuid().ToString()); } os[SettingsConstants.DisplayNameField] = profileName; os[SettingsConstants.AccessKeyField] = accessKeyId; os[SettingsConstants.SecretKeyField] = secretKey; PersistenceManager.Instance.SaveSettings(SettingsConstants.RegisteredProfiles, settings); }
/// <summary> /// Instantiates an instance from the supplied settings. /// </summary> /// <param name="os">The persisted settings.</param> /// <returns>Profile instance or an exception if the profile data is invalid.</returns> public static SAMLEndpointSettings LoadFrom(SettingsCollection.ObjectSettings os) { if (os == null) { throw new ArgumentNullException("os"); } if (!CanCreateFrom(os)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Endpoint settings '{0}' does not contain SAML endpoint materials", os[SettingsConstants.DisplayNameField])); } Validate(os); var endpoint = os.GetValueOrDefault(SettingsConstants.EndpointField, null); string authenticationType = os.GetValueOrDefault(SettingsConstants.AuthenticationType, null); return(new SAMLEndpointSettings(os[SettingsConstants.DisplayNameField], new Uri(endpoint, UriKind.RelativeOrAbsolute), authenticationType)); }
/// <summary> /// Instantiates an AWSCredentialsProfile instance from the supplied settings collection. /// </summary> /// <param name="os">The settings representing the stored profile.</param> /// <returns>New credentials profile instance. An exception is thrown if the profile data is invalid.</returns> public static AWSCredentialsProfile LoadFrom(SettingsCollection.ObjectSettings os) { if (os == null) { throw new ArgumentNullException("os"); } if (!CanCreateFrom(os)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Profile '{0}' does not contain AWS credential materials", os[SettingsConstants.DisplayNameField])); } Validate(os); var accessKeyId = os.GetValueOrDefault(SettingsConstants.AccessKeyField, null); var secretkey = os.GetValueOrDefault(SettingsConstants.SecretKeyField, null); return(new AWSCredentialsProfile(os[SettingsConstants.DisplayNameField], accessKeyId, secretkey)); }
/// <summary> /// Validates that the presented settings would result in a valid role profile /// instance. /// </summary> /// <param name="os">The persisted settings.</param> /// <exception cref="InvalidDataException">Thrown if the profile settings fail to validate.</exception> private static void Validate(SettingsCollection.ObjectSettings os) { var endpointName = os.GetValueOrDefault(SettingsConstants.EndpointNameField, null); if (endpointName == null) { throw new InvalidDataException("Missing EndpointName data."); } SAMLEndpointSettings endpointSettings; if (!ProfileManager.TryGetSAMLEndpoint(endpointName, out endpointSettings)) { throw new InvalidDataException(string.Format(CultureInfo.InvariantCulture, "Endpoint settings with the name '{0}' could not be found.", endpointName)); } if (string.IsNullOrEmpty(os[SettingsConstants.RoleArnField])) { throw new InvalidDataException("Missing role ARN data."); } }
/// <summary> /// Instantiates an instance from the supplied settings. /// </summary> /// <param name="os">The persisted settings.</param> /// <returns>Profile instance or an exception if the profile data is invalid.</returns> public static SAMLRoleProfile LoadFrom(SettingsCollection.ObjectSettings os) { if (os == null) { throw new ArgumentNullException("os"); } if (!CanCreateFrom(os)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Profile '{0}' does not contain SAML role materials", os[SettingsConstants.DisplayNameField])); } Validate(os); var endpointName = os[SettingsConstants.EndpointNameField]; var endpointSettings = ProfileManager.GetSAMLEndpoint(endpointName); var roleArn = os[SettingsConstants.RoleArnField]; var userIdentity = os.GetValueOrDefault(SettingsConstants.UserIdentityField, null); return(new SAMLRoleProfile(os[SettingsConstants.DisplayNameField], endpointSettings, roleArn, userIdentity)); }
/// <summary> /// Verifies that the persisted settings contains the minimal viable data to /// instantiate a SAMLEndpointSettings instance. /// </summary> /// <param name="os">The persisted settings.</param> /// <exception cref="InvalidDataException">Thrown if the settings fail to validate.</exception> private static void Validate(SettingsCollection.ObjectSettings os) { var endpoint = os.GetValueOrDefault(SettingsConstants.EndpointField, null); if (endpoint == null) { throw new InvalidDataException("Missing endpoint value in the profile settings."); } try { var u = new Uri(endpoint); if (u.Scheme != Uri.UriSchemeHttps) { throw new InvalidDataException("The scheme of the endpoint must be HTTPS."); } } catch (UriFormatException e) { throw new InvalidDataException("The configured endpoint is not valid.", e); } }
private static bool TryGetObjectSettings(string uniqueKey, SettingsCollection settings, out SettingsCollection.ObjectSettings objectSettings) { objectSettings = settings.FirstOrDefault(x => string.Equals(x.UniqueKey, uniqueKey, StringComparison.OrdinalIgnoreCase)); return(objectSettings != null); }
private static bool TryGetObjectSettings(string propertyName, string value, SettingsCollection settings, out SettingsCollection.ObjectSettings objectSettings) { objectSettings = settings.FirstOrDefault(x => string.Equals(x[propertyName], value, StringComparison.OrdinalIgnoreCase)); return(objectSettings != null); }
/// <summary> /// Tests if a SAMLRoleProfile instance could be instantiated from /// the persisted settings data. /// </summary> /// <param name="os">The persisted settings.</param> /// <returns>True if the settings are compatible with a SAMLRoleProfile type.</returns> public static bool CanCreateFrom(SettingsCollection.ObjectSettings os) { var osProfileType = os.GetValueOrDefault(SettingsConstants.ProfileTypeField, null); return(osProfileType != null && osProfileType.Equals(ProfileManager.SAMLRoleProfileType, StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// Tests if a SAMLEndpointSettings instance could be instantiated from /// the persisted settings data. /// </summary> /// <param name="os">The persisted settings.</param> /// <returns>True if the settings are compatible.</returns> public static bool CanCreateFrom(SettingsCollection.ObjectSettings os) { var endpoint = os.GetValueOrDefault(SettingsConstants.EndpointField, null); return(!string.IsNullOrEmpty(endpoint)); }