/// <summary> /// Tests if an instance can be created from the persisted profile data. /// If profilesLocation is null/empty, the SDK store is searched for the /// profile data before probing for the profile in the shared the ini-format /// credential file. /// </summary> /// <param name="profileName">The name of the profile to test</param> /// <param name="profilesLocation"> /// If null/empty, the SDK store is searched for the named profile otherwise /// the ini-format credential file at the specified location is inspected. /// </param> /// <returns>True if the persisted data would yield a valid credentials instance.</returns> public static bool CanCreateFrom(string profileName, string profilesLocation) { if (string.IsNullOrEmpty(profilesLocation) && ProfileManager.IsProfileKnown(profileName)) { return(AWSCredentialsProfile.CanCreateFrom(profileName)); } else { return(ValidCredentialsExistInSharedFile(profilesLocation, profileName)); } }
/// <summary> /// Constructs an instance for credentials stored in a profile with the specified name. /// </summary> /// <param name="profileName">The profile name to search for credentials for</param> /// <param name="profilesLocation"> /// Optional; instructs the SDK to check for the profile in the shared credentials file at the /// specified location. If not set, the SDK will inspect its own credential store file first before /// attempting to locate a shared credential file using either the default location beneath the user's /// home profile folder or the location specified in the AWS_SHARED_CREDENTIALS_FILE environment /// variable. /// </param> /// <remarks> /// If credential materials cannot be read or are invalid due to missing data an InvalidDataException /// is thrown. If no credentials can be located with the specified profile name, an ArgumentException /// is thrown. /// </remarks> public StoredProfileAWSCredentials(string profileName, string profilesLocation) { var lookupName = string.IsNullOrEmpty(profileName) ? StoredProfileCredentials.DEFAULT_PROFILE_NAME : profileName; ProfileName = lookupName; ProfilesLocation = null; // If not overriding the credentials lookup location check the SDK Store for credentials. If an override is being used then // assume the intent is to use the credentials file. #if BCL || NETSTANDARD if (string.IsNullOrEmpty(profilesLocation) && ProfileManager.IsProfileKnown(lookupName) && ProfileManager.IsAvailable) { if (ProfileManager.IsProfileKnown(lookupName) && AWSCredentialsProfile.CanCreateFrom(lookupName)) { _wrappedCredentials = ProfileManager.GetAWSCredentials(lookupName); var logger = Logger.GetLogger(typeof(StoredProfileAWSCredentials)); logger.InfoFormat("Credentials found using account name {0} and looking in SDK account store.", lookupName); } } #endif // If credentials weren't found in the SDK store then search the shared credentials file. if (this._wrappedCredentials == null) { var credentialsFilePath = StoredProfileCredentials.ResolveSharedCredentialFileLocation(profilesLocation); if (!string.IsNullOrEmpty(credentialsFilePath)) { var sharedCredentialsFile = new SharedCredentialsFile(credentialsFilePath); CredentialProfile profile; if (sharedCredentialsFile.TryGetProfile(lookupName, out profile) && AWSCredentialsFactory.TryGetAWSCredentials(profile, sharedCredentialsFile, out _wrappedCredentials)) { var logger = Logger.GetLogger(typeof(StoredProfileAWSCredentials)); logger.InfoFormat("Credentials found using account name {0} and looking in {1}.", lookupName, credentialsFilePath); } ProfilesLocation = credentialsFilePath; } } // No credentials found so error out. if (this._wrappedCredentials == null) { throw new ArgumentException("App.config does not contain credentials information. Either add the AWSAccessKey and AWSSecretKey properties or the AWSProfileName property."); } }
/// <summary> /// Tests if an instance can be created from the persisted profile data. /// If profilesLocation is null/empty, the SDK store is searched for the /// profile data first before testing the default location of the ini-format /// credential file. /// </summary> /// <param name="profileName">The name of the profile to test</param> /// <param name="profilesLocation"> /// If null/empty, the SDK store is searched for the named profile otherwise /// the ini-format credential file at the specified location is inspected. /// </param> /// <returns>True if the persisted data would yield a valid credentials instance.</returns> public static bool CanCreateFrom(string profileName, string profilesLocation) { if (string.IsNullOrEmpty(profilesLocation) && ProfileManager.IsProfileKnown(profileName)) { return(AWSCredentialsProfile.CanCreateFrom(profileName)); } var profileFile = string.IsNullOrEmpty(profilesLocation) ? AWSConfigs.AWSProfilesLocation : profilesLocation; var credentialsFilePath = DetermineCredentialsFilePath(profileFile); if (!string.IsNullOrEmpty(credentialsFilePath) && File.Exists(credentialsFilePath)) { var parser = new CredentialsFileParser(credentialsFilePath); var section = parser.FindSection(profileName); if (section != null) { try { section.Validate(); return(true); } catch (InvalidDataException) { } } else { var logger = Logger.GetLogger(typeof(StoredProfileAWSCredentials)); logger.InfoFormat("Credentials file {0} does not contain profile {1}.", credentialsFilePath, profileName); } } else { var logger = Logger.GetLogger(typeof(StoredProfileAWSCredentials)); logger.InfoFormat("Credentials file not found {0}.", credentialsFilePath); } return(false); }