private async void SaveProfileForm_ProfileEdited(object sender, ProfileEventArgs e) { // If no changes were made then just log a message and return string msg; string name = e.ProfileName; var profile = ComboProfiles.SelectedItem as AWSCredentialsProfile; var creds = await profile.Credentials.GetCredentialsAsync(); if (e.AccessKeyId == creds.AccessKey && e.SecretAccessKey == creds.SecretKey) { msg = string.Format(ProfileUnchanged, name); addLog(msg); return; } // Persist changes to this AWS credentials profile using (new WaitCursor()) { AWSCredentialsProfile.Persist(name, e.AccessKeyId, e.SecretAccessKey); } msg = string.Format(ProfileEdited, name); addLog(msg); // Reset the data source for the profiles combobox // Keep selected the profile that was just edited ProfileSettingsBase[] profiles = await bindProfilesAsync(); profile = profiles.Single(p => p.Name == name) as AWSCredentialsProfile; ComboProfiles.SelectedIndex = Array.IndexOf(profiles, profile); }
private async Task <List <S3Bucket> > bindBucketsAsync(AWSCredentialsProfile profile, RegionEndpoint region, TreeNode logNode = null) { // Set up for listing buckets if (logNode != null) { string msg = string.Format(ListingBuckets, profile.Name); logNode.Nodes.Add(msg); } resetBuckets(); _ctsListBuckets.Cancel(); _ctsListBuckets = new CancellationTokenSource(); // Asynchronously list buckets available to this profile var s3 = new AmazonS3Client(profile.Credentials, region); List <S3Bucket> buckets = new List <S3Bucket>(); using (new WaitCursor()) { try { buckets = (await s3.ListBucketsAsync(_ctsListBuckets.Token)).Buckets; } catch (OperationCanceledException) { logNode?.Remove(); return(buckets); } // If an exception occurs then log its details... catch (AmazonServiceException ex) { if (logNode != null) { var errNode = new TreeNode(ListingBucketsFailed); var stackNode = new TreeNode(ExceptionStackTrace); TreeNode[] stackTraceNodes = ex.StackTrace.Split('\n').Select(m => new TreeNode(m)).ToArray(); stackNode.Nodes.AddRange(stackTraceNodes); errNode.Nodes.AddRange( new string[3] { string.Format(ExceptionMessage, ex.Message), string.Format(ExceptionSource, ex.Source), string.Format(ExceptionTargetSite, ex.TargetSite), } .Select(m => new TreeNode(m)).ToArray() ); errNode.Nodes.Add(stackNode); logNode.Nodes.Add(errNode); } return(buckets); } } // Adjust controls based on listed buckets if (logNode != null) { string msg = string.Format(BucketsListed, buckets.Count); logNode.Nodes.Add(msg); } resetBuckets(buckets); return(buckets); }
/// <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 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)) { AWSCredentialsProfile.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 (!string.IsNullOrEmpty(credentialsFilePath) && 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 properties or the AWSProfileName property."); } }
public SaveProfileForm(AWSCredentialsProfile profile = null) { InitializeComponent(); // Initialize the Form depending on whether we adding a new Profile or editing an existing one if (profile == null) { cvSave.MarkValidity(TxtProfileName, false); cvSave.MarkValidity(TxtAccessKeyID, false); cvSave.MarkValidity(TxtSecretAccessKey, false); } else { initFields(profile); } }
/// <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."); } }
private async void initFields(AWSCredentialsProfile profile) { toggleFields(false); // Get this profile's credentials asynchronously _origProfile = profile; using (new WaitCursor()) { _origCreds = await profile.Credentials.GetCredentialsAsync(); } // Initialize controls with these credentials this.Text = string.Format(EditProfileTitle, _origProfile.Name); TxtProfileName.Text = _origProfile.Name; TxtAccessKeyID.Text = _origCreds.AccessKey; TxtSecretAccessKey.Text = _origCreds.SecretKey; toggleFields(true); }
/// <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); }