コード例 #1
0
        /// <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.");
            }
        }
コード例 #2
0
        private static bool ValidCredentialsExistInSharedFile(string profilesLocation, string profileName)
        {
            var credentialsFilePath = StoredProfileCredentials.ResolveSharedCredentialFileLocation(profilesLocation);

            if (!string.IsNullOrEmpty(credentialsFilePath))
            {
                var doLog = false;
                try
                {
                    var file = new SharedCredentialsFile(credentialsFilePath);
                    CredentialProfile profile = null;
                    if (file.TryGetProfile(profileName, out profile) && profile.CanCreateAWSCredentials)
                    {
                        return(true);
                    }
                    else
                    {
                        doLog = true;
                    }
                }
                catch (InvalidDataException)
                {
                    doLog = true;
                }

                if (doLog)
                {
                    var logger = Logger.GetLogger(typeof(StoredProfileAWSCredentials));
                    logger.InfoFormat("Credentials file {0} does not contain a valid profile named {1}.", credentialsFilePath, profileName);
                }
            }
            else
            {
                var logger = Logger.GetLogger(typeof(StoredProfileAWSCredentials));
                logger.InfoFormat("Credentials file not found {0}.", credentialsFilePath);
            }
            return(false);
        }