コード例 #1
0
        /// <summary>
        /// Constructs an instance of StoredProfileAWSCredentials. Credentials will be searched for using the profileName parameter.
        /// </summary>
        /// <param name="profileName">The profile name to search for credentials for</param>
        /// <param name="profilesLocation">Overrides the location to search for credentials</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, an ArgumentException
        /// is thrown.
        /// </remarks>
        public StoredProfileAWSCredentials(string profileName, string profilesLocation)
        {
            NameValueCollection appConfig = ConfigurationManager.AppSettings;

            var lookupName = string.IsNullOrEmpty(profileName) ? 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 (string.IsNullOrEmpty(profilesLocation) && ProfileManager.IsProfileKnown(lookupName))
            {
                ProfileManager.Validate(lookupName);
                AWSCredentials credentials;
                if (ProfileManager.TryGetAWSCredentials(lookupName, out credentials))
                {
                    this._wrappedCredentials = credentials.GetCredentials();
                    var logger = Logger.GetLogger(typeof(StoredProfileAWSCredentials));
                    logger.InfoFormat("Credentials found using account name {0} and looking in SDK account store.", lookupName);
                }
            }

            // If credentials weren't found in the SDK store then search the shared credentials file.
            if (this._wrappedCredentials == null)
            {
                var credentialsFilePath = DetermineCredentialsFilePath(profilesLocation);
                if (File.Exists(credentialsFilePath))
                {
                    var parser  = new CredentialsFileParser(credentialsFilePath);
                    var section = parser.FindSection(lookupName);
                    if (section != null)
                    {
                        section.Validate();
                        this._wrappedCredentials = section.Credentials;
                        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 or AWSProfileName.");
            }
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
0
        /// <summary>
        /// Tests if a profile has been registered in either the SDK store or the
        /// specified credential file. 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 a profile with the specified name has been registered.</returns>
        public static bool IsProfileKnown(string profileName, string profilesLocation)
        {
            if (string.IsNullOrEmpty(profilesLocation) && ProfileManager.IsProfileKnown(profileName))
            {
                return(true);
            }

            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);
                return(section != null);
            }

            return(false);
        }
コード例 #4
0
        /// <summary>
        /// Constructs an instance of StoredProfileAWSCredentials. Credentials will be searched for using the profileName parameter.
        /// </summary>
        /// <param name="profileName">The profile name to search for credentials for</param>
        /// <param name="profilesLocation">Overrides the location to search for credentials</param>
        public StoredProfileAWSCredentials(string profileName, string profilesLocation)
        {
            NameValueCollection appConfig = ConfigurationManager.AppSettings;

            var lookupName = string.IsNullOrEmpty(profileName) ? 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 (string.IsNullOrEmpty(profilesLocation))
            {
                AWSCredentials credentials;
                if (Amazon.Util.ProfileManager.TryGetAWSCredentials(lookupName, out credentials))
                {
                    this._wrappedCredentials = credentials.GetCredentials();
                    var logger = Logger.GetLogger(typeof(StoredProfileAWSCredentials));
                    logger.InfoFormat("Credentials found using account name {0} and looking in SDK account store.", lookupName);
                }
            }

            // If credentials weren't found in the SDK store then search the shared credentials file.
            if (this._wrappedCredentials == null)
            {
                var credentialsFilePath = DetermineCredentialsFilePath(profilesLocation);
                if (File.Exists(credentialsFilePath))
                {                    
                    var parser = new CredentialsFileParser(credentialsFilePath);
                    var section = parser.FindSection(lookupName);
                    if (section != null && section.HasValidCredentials)
                    {
                        this._wrappedCredentials = section.Credentials;
                        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 or AWSProfileName.");
            }
        }
コード例 #5
0
        /// <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;
        }
コード例 #6
0
        /// <summary>
        /// Tests if a profile has been registered in either the SDK store or the
        /// specified credential file. 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 a profile with the specified name has been registered.</returns>
        public static bool IsProfileKnown(string profileName, string profilesLocation)
        {
            if (string.IsNullOrEmpty(profilesLocation) && ProfileManager.IsProfileKnown(profileName))
                return true;

            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);
                return section != null;
            }

            return false;
        }