Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S3 STS session authentication #182

Merged
merged 1 commit into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 78 additions & 24 deletions KeeAnywhere/StorageProviders/AmazonS3/AmazonS3AccountForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 34 additions & 2 deletions KeeAnywhere/StorageProviders/AmazonS3/AmazonS3AccountForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -29,6 +29,8 @@ private void OnFormLoad(object sender, EventArgs e)
PluginResources.KeeAnywhere_48x48, "Authorize to Amazon S3",
"Please enter your Amazon S3 credentials here.");

this.Size = new System.Drawing.Size(553, 400);

}

private void OnFormClosed(object sender, FormClosedEventArgs e)
Expand All @@ -53,12 +55,24 @@ private async Task TestConnection()
return;
}

if (m_chkUseSessionToken.Checked == true && string.IsNullOrEmpty(this.SessionToken))
{
m_lblTestResult.Text = "Please enter Session Token";
return;
}

this.Enabled = false;
m_lblTestResult.Text = "Testing Connection...";

this.TestResult = null;

var credentials = new BasicAWSCredentials(this.AccessKey, this.SecretKey);
AWSCredentials credentials;

if (m_chkUseSessionToken.Checked == false)
credentials = new BasicAWSCredentials(this.AccessKey, this.SecretKey);
else
credentials = new SessionAWSCredentials(this.AccessKey, this.SecretKey, this.SessionToken);

try
{
using (var api = AmazonS3Helper.GetApi(credentials, this.AWSRegion))
Expand Down Expand Up @@ -90,6 +104,8 @@ public void ClearTestResult()

public string AccessKey { get { return m_txtAccessKey.Text.Trim(); } }
public string SecretKey { get { return m_txtSecretKey.Text.Trim(); } }
public string SessionToken { get { return m_txtSessionToken.Text.Trim(); } }
public bool UseSessionToken { get { return m_chkUseSessionToken.Checked; } }
public RegionEndpoint AWSRegion { get { return (RegionEndpoint) m_cmbRegion.SelectedItem; } }

protected bool? TestResult { get; set; }
Expand All @@ -116,5 +132,21 @@ private async void OnOk(object sender, EventArgs e)
if (this.TestResult != null && this.TestResult.Value)
this.DialogResult = DialogResult.OK;
}

private void M_chkUseSessionToken_CheckedChanged(object sender, EventArgs e)
{
if (m_chkUseSessionToken.Checked == true)
{
m_lblSessionToken.Visible = true;
m_txtSessionToken.Visible = true;
this.Size = new System.Drawing.Size(553, 460);
}
else
{
m_lblSessionToken.Visible = false;
m_txtSessionToken.Visible = false;
this.Size = new System.Drawing.Size(553, 400);
}
}
}
}
8 changes: 6 additions & 2 deletions KeeAnywhere/StorageProviders/AmazonS3/AmazonS3Helper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
Expand All @@ -17,7 +17,11 @@ public static class AmazonS3Helper
{
public static AmazonS3Client GetApi(AccountConfiguration account)
{
var credentials = new BasicAWSCredentials(account.Id, account.Secret);
AWSCredentials credentials;
if (account.AdditionalSettings != null && account.AdditionalSettings.ContainsKey("UseSessionToken") && Convert.ToBoolean(account.AdditionalSettings["UseSessionToken"]) == true && account.AdditionalSettings.ContainsKey("SessionToken"))
credentials = new SessionAWSCredentials(account.Id, account.Secret, account.AdditionalSettings["SessionToken"]);
else
credentials = new BasicAWSCredentials(account.Id, account.Secret);

var region = RegionEndpoint.USWest1;
if (account.AdditionalSettings != null && account.AdditionalSettings.ContainsKey("AWSRegion"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -28,6 +28,8 @@ public async Task<AccountConfiguration> CreateAccount()
};

account.AdditionalSettings = new Dictionary<string, string>() {{"AWSRegion", dlg.AWSRegion.SystemName}};
account.AdditionalSettings.Add("UseSessionToken", Convert.ToString(dlg.UseSessionToken));
account.AdditionalSettings.Add("SessionToken", dlg.SessionToken);
return account;
}
}
Expand Down