public GetBucketLocation ( GetBucketLocationRequest request ) : GetBucketLocationResponse | ||
request | GetBucketLocationRequest | Container for the necessary parameters to execute the GetBucketLocation service method. |
Résultat | GetBucketLocationResponse |
public void TestLocation() { foreach (var location in new S3Region[] { S3Region.USW1, S3Region.EUC1 }) { string bucketName = null; var region = RegionEndpoint.GetBySystemName(location.Value); using (var client = new AmazonS3Client(region)) { try { bucketName = UtilityMethods.SDK_TEST_PREFIX + DateTime.Now.Ticks; client.PutBucket(new PutBucketRequest { BucketName = bucketName, BucketRegion = location }); var returnedLocation = client.GetBucketLocation(new GetBucketLocationRequest { BucketName = bucketName }).Location; Assert.AreEqual(location, returnedLocation); } finally { if (bucketName != null) AmazonS3Util.DeleteS3BucketWithObjects(client, bucketName); } } } }
public void TestLocation() { // Disable EUW2 for now until we figure out why we are hitting the bucket number limit. foreach (var location in new S3Region[] { S3Region.USW1, S3Region.EUC1, S3Region.EUW1/*, S3Region.EUW2*/}) { string bucketName = null; var region = RegionEndpoint.GetBySystemName(location.Value); using (var client = new AmazonS3Client(region)) { try { bucketName = S3TestUtils.CreateBucketWithWait(client); var returnedLocation = client.GetBucketLocation(new GetBucketLocationRequest { BucketName = bucketName }).Location; //Map S3Region.EUW1 to S3Region.EU //S3 considers this as the same region. if (location == S3Region.EUW1) { Assert.AreEqual(S3Region.EU, returnedLocation); } else { Assert.AreEqual(location, returnedLocation); } } finally { if (bucketName != null) AmazonS3Util.DeleteS3BucketWithObjects(client, bucketName); } } } }
// Create one client to get region.. then create real one? // Seems dumb, but will see how it goes. public static IAmazonS3 GenerateS3Client( string accessKey, string secretKey, string bucketName = null) { IAmazonS3 client = new AmazonS3Client( accessKey, secretKey); if (!string.IsNullOrEmpty(bucketName)) { var regionForBucket = client.GetBucketLocation(bucketName); // if default US region then location will be null. Replace it with string.Empty for region lookup. client = new AmazonS3Client(accessKey, secretKey, RegionDict[regionForBucket.Location ?? string.Empty]); } return client; }
private void executeSomeBucketOperations(AmazonS3Config s3Config) { using (var s3Client = new AmazonS3Client(s3Config)) { // Call ListBuckets first to verify that AmazonS3PostMarshallHandler.ProcessRequestHandlers // correctly computes the endpoint when no bucket name is present. var listBucketsResponse = s3Client.ListBuckets(); Assert.IsNotNull(listBucketsResponse); Assert.IsFalse(string.IsNullOrEmpty(listBucketsResponse.ResponseMetadata.RequestId)); // Bonus call on ListObjects if we can find a bucket compatible with the test region (to avoid 301 // errors due to addressing bucket on wrong endpoint). This verifies that // AmazonS3PostMarshallHandler.ProcessRequestHandlers correctly computes the endpoint when // a bucket name is present. string bucketName = null; foreach (var bucket in listBucketsResponse.Buckets) { try { var bucketLocationResponse = s3Client.GetBucketLocation(bucket.BucketName); if (string.IsNullOrEmpty(bucketLocationResponse.Location) && s3Config.RegionEndpoint == RegionEndpoint.USEast1) bucketName = bucket.BucketName; else if (string.Equals(s3Config.RegionEndpoint.SystemName, bucketLocationResponse.Location, StringComparison.OrdinalIgnoreCase)) bucketName = bucket.BucketName; if (!string.IsNullOrEmpty(bucketName)) break; } catch(AmazonS3Exception e) { if (e.StatusCode != System.Net.HttpStatusCode.NotFound) throw; } } if (!string.IsNullOrEmpty(bucketName)) { var listObjectsResponse = s3Client.ListObjects(new ListObjectsRequest { BucketName = bucketName }); Assert.IsNotNull(listObjectsResponse); Assert.IsNotNull(listObjectsResponse.ResponseMetadata); } } }
public DataTable GetS3Buckets(string aprofile,string Region2Scan) { string accountid = GetAccountID(aprofile); Amazon.Runtime.AWSCredentials credential; DataTable ToReturn = AWSTables.GetComponentTable("S3"); RegionEndpoint Endpoint2scan = RegionEndpoint.USEast1; //Convert the Region2Scan to an AWS Endpoint. foreach (var aregion in RegionEndpoint.EnumerableAllRegions) { if (aregion.DisplayName.Equals(Region2Scan)) { Endpoint2scan = aregion; continue; } } //Query Cloudwatch to get list of buckets and sizes in this here region var Sizetable = S3SizeCloudWatch(aprofile, Region2Scan); int sizerows = Sizetable.Rows.Count; try { credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); AmazonS3Client S3Client = new AmazonS3Client(credential, Endpoint2scan); ListBucketsResponse response = S3Client.ListBuckets(); //Why are we getting USEast buckets? Because the Cloudwatch returns buckets by region, but list buckets does care about regions! foreach (S3Bucket abucket in response.Buckets) { DataRow abucketrow = ToReturn.NewRow(); var name = abucket.BucketName; DataRow bucketsizedata = AWSTables.GetS3SizesTable().NewRow(); Boolean havesize = true; try//Lookup the record in the Sizetable for this bucket. { //This is equivalent to the LINQ query. Boolean foundinsizetable = false; List<string> bn = new List<string>(); foreach(var rabbit in Sizetable.AsEnumerable()) { String thisun = rabbit["Bucket"].ToString(); bn.Add(thisun); if (thisun.Equals(name)) { bucketsizedata = rabbit; foundinsizetable = true; } } if(!foundinsizetable) { bn.Sort(); if (bn.Contains(name)) { string rabbit = "Yes it does!"; } //why not? } if (bucketsizedata == null) { abucketrow["EndDate"] = AWSTables.Shrug; havesize = false; } } catch { } try { GetBucketLocationRequest gbr = new GetBucketLocationRequest(); gbr.BucketName = name; GetBucketLocationResponse location = S3Client.GetBucketLocation(gbr); var region = location.Location.Value; if (region.Equals("")) { region = "us-east-1"; } if (!region.Contains(Endpoint2scan.SystemName)) { name = name;//Bucketname region = region;//BucketRegion string target = Endpoint2scan.SystemName; continue; } var pointy = RegionEndpoint.GetBySystemName(region); //Build a config that references the buckets region. AmazonS3Config S3C = new AmazonS3Config(); S3C.RegionEndpoint = pointy; AmazonS3Client BS3Client = new AmazonS3Client(credential, S3C); var authregion = ""; var EP = BS3Client.Config.RegionEndpoint.DisplayName; if (String.IsNullOrEmpty(BS3Client.Config.RegionEndpoint.DisplayName)) authregion = ""; else { authregion = BS3Client.Config.AuthenticationRegion; } string authservice = ""; if (string.IsNullOrEmpty(BS3Client.Config.AuthenticationServiceName)) authservice = ""; else { authservice = BS3Client.Config.AuthenticationServiceName; } var createddate = abucket.CreationDate; string owner = ""; string grants = ""; string tags = ""; string lastaccess = ""; string defaultpage = ""; string website = ""; //Now start pulling der einen data. GetACLRequest GACR = new GetACLRequest(); GACR.BucketName = name; var ACL = BS3Client.GetACL(GACR); var grantlist = ACL.AccessControlList; owner = grantlist.Owner.DisplayName; foreach (var agrant in grantlist.Grants) { if (grants.Length > 1) grants += "\n"; var gName = agrant.Grantee.DisplayName; var gType = agrant.Grantee.Type.Value; var aMail = agrant.Grantee.EmailAddress; if (gType.Equals("Group")) { grants += gType + " - " + agrant.Grantee.URI + " - " + agrant.Permission + " - " + aMail; } else { grants += gName + " - " + agrant.Permission + " - " + aMail; } } GetBucketWebsiteRequest GBWReq = new GetBucketWebsiteRequest(); GBWReq.BucketName = name; GetBucketWebsiteResponse GBWRes = BS3Client.GetBucketWebsite(GBWReq); defaultpage = GBWRes.WebsiteConfiguration.IndexDocumentSuffix; if (defaultpage != null) { website = @"http://" + name + @".s3-website-" + region + @".amazonaws.com/" + defaultpage; } abucketrow["AccountID"] = accountid; abucketrow["Profile"] = aprofile; abucketrow["Bucket"] = name; abucketrow["Region"] = region; abucketrow["RegionEndpoint"] = EP; abucketrow["AuthRegion"] = authregion; abucketrow["AuthService"] = authservice; abucketrow["CreationDate"] = createddate.ToString(); abucketrow["LastAccess"] = lastaccess; abucketrow["Owner"] = owner; abucketrow["Grants"] = grants; abucketrow["StartDate"] = "NA"; abucketrow["StartSizeMin"] = "NA"; abucketrow["StartSizeAVG"] = "NA"; abucketrow["StartSizeMax"] = "NA"; abucketrow["EndDate"] = "NA"; abucketrow["EndSizeMin"] = "NA"; abucketrow["EndSizeAVG"] = "NA"; abucketrow["EndSizeMax"] = "NA"; abucketrow["EndSizeMaxBytes"] = 0; if (havesize) { abucketrow["StartDate"] = bucketsizedata["StartDate"]; abucketrow["StartSizeMin"] = bucketsizedata["StartSizeMin"]; abucketrow["StartSizeAVG"] = bucketsizedata["StartSizeAVG"]; abucketrow["StartSizeMax"] = bucketsizedata["StartSizeMax"]; abucketrow["EndDate"] = bucketsizedata["EndDate"]; abucketrow["EndSizeMin"] = bucketsizedata["EndSizeMin"]; abucketrow["EndSizeAVG"] = bucketsizedata["EndSizeAVG"]; abucketrow["EndSizeMax"] = bucketsizedata["EndSizeMax"]; abucketrow["EndSizeMaxBytes"] = bucketsizedata["EndSizeMaxBytes"]; } abucketrow["WebsiteHosting"] = website; abucketrow["Logging"] = "X"; abucketrow["Events"] = "X"; abucketrow["Versioning"] = "X"; abucketrow["LifeCycle"] = "X"; abucketrow["Replication"] = "X"; abucketrow["Tags"] = "X"; abucketrow["RequesterPays"] = "X"; ToReturn.Rows.Add(abucketrow.ItemArray); } catch (Exception ex) { abucketrow["AccountID"] = accountid; abucketrow["Profile"] = aprofile; abucketrow["Bucket"] = name; abucketrow["Region"] = ex.InnerException.Message; if (havesize) { abucketrow["StartDate"] = bucketsizedata["StartDate"]; abucketrow["StartSizeMin"] = bucketsizedata["StartSizeMin"]; abucketrow["StartSizeAVG"] = bucketsizedata["StartSizeAVG"]; abucketrow["StartSizeMax"] = bucketsizedata["StartSizeMax"]; abucketrow["EndDate"] = bucketsizedata["EndDate"]; abucketrow["EndSizeMin"] = bucketsizedata["EndSizeMin"]; abucketrow["EndSizeAVG"] = bucketsizedata["EndSizeAVG"]; abucketrow["EndSizeMax"] = bucketsizedata["EndSizeMax"]; abucketrow["EndSizeMaxBytes"] = bucketsizedata["EndSizeMaxBytes"]; } else { abucketrow["StartDate"] = "NA"; abucketrow["StartSizeMin"] = "NA"; abucketrow["StartSizeAVG"] = "NA"; abucketrow["StartSizeMax"] = "NA"; abucketrow["EndDate"] = "NA"; abucketrow["EndSizeMin"] = "NA"; abucketrow["EndSizeAVG"] = "NA"; abucketrow["EndSizeMax"] = "NA"; abucketrow["EndSizeMaxBytes"] = 0; } ToReturn.Rows.Add(abucketrow.ItemArray); } } } catch(Exception ex) { //Croak } int sizesreturned = Sizetable.Rows.Count; int rowsreturned = ToReturn.Rows.Count; return ToReturn; }
public void BucketSamples() { { #region ListBuckets Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Issue call ListBucketsResponse response = client.ListBuckets(); // View response data Console.WriteLine("Buckets owner - {0}", response.Owner.DisplayName); foreach (S3Bucket bucket in response.Buckets) { Console.WriteLine("Bucket {0}, Created on {1}", bucket.BucketName, bucket.CreationDate); } #endregion } { #region BucketPolicy Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Put sample bucket policy (overwrite an existing policy) string newPolicy = @"{ ""Statement"":[{ ""Sid"":""BasicPerms"", ""Effect"":""Allow"", ""Principal"": ""*"", ""Action"":[""s3:PutObject"",""s3:GetObject""], ""Resource"":[""arn:aws:s3:::samplebucketname/*""] }]}"; PutBucketPolicyRequest putRequest = new PutBucketPolicyRequest { BucketName = "SampleBucket", Policy = newPolicy }; client.PutBucketPolicy(putRequest); // Retrieve current policy GetBucketPolicyRequest getRequest = new GetBucketPolicyRequest { BucketName = "SampleBucket" }; string policy = client.GetBucketPolicy(getRequest).Policy; Console.WriteLine(policy); Debug.Assert(policy.Contains("BasicPerms")); // Delete current policy DeleteBucketPolicyRequest deleteRequest = new DeleteBucketPolicyRequest { BucketName = "SampleBucket" }; client.DeleteBucketPolicy(deleteRequest); // Retrieve current policy and verify that it is null policy = client.GetBucketPolicy(getRequest).Policy; Debug.Assert(policy == null); #endregion } { #region GetBucketLocation Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Construct request GetBucketLocationRequest request = new GetBucketLocationRequest { BucketName = "SampleBucket" }; // Issue call GetBucketLocationResponse response = client.GetBucketLocation(request); // View response data Console.WriteLine("Bucket location - {0}", response.Location); #endregion } { #region PutBucket Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Construct request PutBucketRequest request = new PutBucketRequest { BucketName = "SampleBucket", BucketRegion = S3Region.EU, // set region to EU CannedACL = S3CannedACL.PublicRead // make bucket publicly readable }; // Issue call PutBucketResponse response = client.PutBucket(request); #endregion } { #region DeleteBucket Sample 1 // Create a client AmazonS3Client client = new AmazonS3Client(); // Construct request DeleteBucketRequest request = new DeleteBucketRequest { BucketName = "SampleBucket" }; // Issue call DeleteBucketResponse response = client.DeleteBucket(request); #endregion } { #region DeleteBucket Sample 2 // Create a client AmazonS3Client client = new AmazonS3Client(); // List and delete all objects ListObjectsRequest listRequest = new ListObjectsRequest { BucketName = "SampleBucket" }; ListObjectsResponse listResponse; do { // Get a list of objects listResponse = client.ListObjects(listRequest); foreach (S3Object obj in listResponse.S3Objects) { // Delete each object client.DeleteObject(new DeleteObjectRequest { BucketName = "SampleBucket", Key = obj.Key }); } // Set the marker property listRequest.Marker = listResponse.NextMarker; } while (listResponse.IsTruncated); // Construct DeleteBucket request DeleteBucketRequest request = new DeleteBucketRequest { BucketName = "SampleBucket" }; // Issue call DeleteBucketResponse response = client.DeleteBucket(request); #endregion } { #region LifecycleConfiguration Sample // Create a client AmazonS3Client client = new AmazonS3Client(); // Put sample lifecycle configuration (overwrite an existing configuration) LifecycleConfiguration newConfiguration = new LifecycleConfiguration { Rules = new List<LifecycleRule> { // Rule to delete keys with prefix "Test-" after 5 days new LifecycleRule { Prefix = "Test-", Expiration = new LifecycleRuleExpiration { Days = 5 } }, // Rule to delete keys in subdirectory "Logs" after 2 days new LifecycleRule { Prefix = "Logs/", Expiration = new LifecycleRuleExpiration { Days = 2 }, Id = "log-file-removal" } } }; PutLifecycleConfigurationRequest putRequest = new PutLifecycleConfigurationRequest { BucketName = "SampleBucket", Configuration = newConfiguration }; client.PutLifecycleConfiguration(putRequest); // Retrieve current configuration GetLifecycleConfigurationRequest getRequest = new GetLifecycleConfigurationRequest { BucketName = "SampleBucket" }; LifecycleConfiguration configuration = client.GetLifecycleConfiguration(getRequest).Configuration; Console.WriteLine("Configuration contains {0} rules", configuration.Rules.Count); foreach (LifecycleRule rule in configuration.Rules) { Console.WriteLine("Rule"); Console.WriteLine(" Prefix = " + rule.Prefix); Console.WriteLine(" Expiration (days) = " + rule.Expiration.Days); Console.WriteLine(" Id = " + rule.Id); Console.WriteLine(" Status = " + rule.Status); } // Put a new configuration and overwrite the existing configuration configuration.Rules.RemoveAt(0); // remove first rule client.PutLifecycleConfiguration(putRequest); // Delete current configuration DeleteLifecycleConfigurationRequest deleteRequest = new DeleteLifecycleConfigurationRequest { BucketName = "SampleBucket" }; client.DeleteLifecycleConfiguration(deleteRequest); // Retrieve current configuration and verify that it is null configuration = client.GetLifecycleConfiguration(getRequest).Configuration; Debug.Assert(configuration == null); #endregion } }
public Dictionary<string, DataTable> ScanProfile(ScanRequest Request) { Dictionary<string, DataTable> ScanResults = new Dictionary<string, DataTable>(); DataTable UserDetailsTable = GetUsersDetailsTable(); DataTable EC2DetailsTable = GetEC2DetailsTable(); DataTable S3DetailsTable = GetS3DetailsTable(); string accountid = ""; Amazon.Runtime.AWSCredentials credential; var aprofile = Request.Profile; var regions2process = Request.Regions; var SubmitResults = Request.ResultQueue; try { credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); //Try to get the AccountID ID// #region UserDetails var iam = new AmazonIdentityManagementServiceClient(credential); var myUserList = iam.ListUsers().Users; try { accountid = myUserList[0].Arn.Split(':')[4];//Get the ARN and extract the AccountID ID accountid = "ID: " + accountid;// Prefix added because Excel exsucks. } catch { accountid = "?"; } try // Send command to AWS to generate a Credential Report { var createcredreport = iam.GenerateCredentialReport(); } catch (Exception) { throw; } bool needreport = true; Amazon.IdentityManagement.Model.GetCredentialReportResponse credreport = new GetCredentialReportResponse(); DateTime getreportstart = DateTime.Now; DateTime getreportfinish = DateTime.Now; while (needreport) { try { credreport = iam.GetCredentialReport(); needreport = false; getreportfinish = DateTime.Now; var dif = getreportstart - getreportfinish; //Just a check on how long it takes. //Extract data from CSV Stream into DataTable var streambert = credreport.Content; streambert.Position = 0; StreamReader sr = new StreamReader(streambert); string myStringRow = sr.ReadLine(); if (myStringRow != null) myStringRow = sr.ReadLine();//Dump the header line while (myStringRow != null) { var arow = myStringRow.Split(",".ToCharArray()[0]); var newrow = new object[UserDetailsTable.Columns.Count]; newrow[0] = accountid; newrow[1] = aprofile; newrow[2] = ""; //UserID not in report. pull it later. newrow[3] = arow[0]; newrow[4] = arow[1]; newrow[5] = arow[2]; newrow[6] = arow[3]; newrow[7] = arow[4]; newrow[8] = arow[5]; newrow[9] = arow[6]; newrow[10] = arow[7]; newrow[11] = arow[8]; newrow[12] = arow[9]; newrow[13] = arow[10]; newrow[14] = arow[11]; newrow[15] = arow[12]; newrow[16] = arow[13]; newrow[17] = arow[14]; newrow[18] = arow[15]; newrow[19] = arow[16]; newrow[20] = arow[17]; newrow[21] = arow[18]; newrow[22] = arow[19]; newrow[23] = arow[20]; newrow[24] = arow[21]; RawUsers.Rows.Add(newrow); UserDetailsTable.Rows.Add(newrow); myStringRow = sr.ReadLine(); } sr.Close(); sr.Dispose(); } catch (Exception ex) { string test = ""; //Deal with this later if necessary. } } foreach (var auser in myUserList)//Fill in the userID. Why? because it exists. { string auserid = auser.UserId; string arn = auser.Arn; string username = auser.UserName; string policylist = ""; string aklist = ""; string groups = ""; ListAccessKeysRequest LAKREQ = new ListAccessKeysRequest(); LAKREQ.UserName = username; ListAccessKeysResult LAKRES = iam.ListAccessKeys(LAKREQ); foreach (var blivet in LAKRES.AccessKeyMetadata) { if (aklist.Length > 1) aklist += "\n"; aklist += blivet.AccessKeyId + " : " + blivet.Status; } ListAttachedUserPoliciesRequest LAUPREQ = new ListAttachedUserPoliciesRequest(); LAUPREQ.UserName = username; ListAttachedUserPoliciesResult LAUPRES = iam.ListAttachedUserPolicies(LAUPREQ); foreach (var apol in LAUPRES.AttachedPolicies) { if (policylist.Length > 1) policylist += "\n"; policylist += apol.PolicyName; } //Need to get policy and group info outta user var groopsreq = new ListGroupsForUserRequest(); groopsreq.UserName = username; ListGroupsForUserResult LG = iam.ListGroupsForUser(groopsreq); foreach (var agroup in LG.Groups) { if (groups.Length > 1) groups += "\n"; groups += agroup.GroupName; } foreach (DataRow myrow in UserDetailsTable.Rows) { if (myrow["ARN"].Equals(arn)) { myrow["UserID"] = auserid; myrow["User-Policies"] = policylist; myrow["Access-Keys"] = aklist; myrow["Groups"] = groups; } } } #endregion #region S3Details try { AmazonS3Client S3Client = new AmazonS3Client(credential,Amazon.RegionEndpoint.USEast1); ListBucketsResponse response = S3Client.ListBuckets(); foreach (S3Bucket abucket in response.Buckets) { DataRow abucketrow = GetS3DetailsTable().NewRow(); var name = abucket.BucketName; GetBucketLocationRequest gbr = new GetBucketLocationRequest(); gbr.BucketName=name; GetBucketLocationResponse location = S3Client.GetBucketLocation(gbr); var region = location.Location.Value; if (region.Equals(""))region="us-east-1"; var pointy = RegionEndpoint.GetBySystemName(region); //Build a config that references the buckets region. AmazonS3Config S3C = new AmazonS3Config(); S3C.RegionEndpoint=pointy; AmazonS3Client BS3Client = new AmazonS3Client(credential, S3C); var createddate = abucket.CreationDate; string owner = ""; string grants = ""; string tags = ""; string lastaccess = ""; string defaultpage = ""; string website = ""; //Now start pulling der einen data. GetACLRequest GACR = new GetACLRequest(); GACR.BucketName = name; var ACL = BS3Client.GetACL(GACR); var grantlist = ACL.AccessControlList; owner = grantlist.Owner.DisplayName; foreach (var agrant in grantlist.Grants) { if (grants.Length > 1) grants += "\n"; var gName = agrant.Grantee.DisplayName; var gType = agrant.Grantee.Type.Value; var aMail = agrant.Grantee.EmailAddress; if (gType.Equals("Group")) { grants += gType + " - " + agrant.Grantee.URI + " - " + agrant.Permission + " - " + aMail ; } else { grants += gName + " - "+ agrant.Permission + " - " + aMail; } } GetObjectMetadataRequest request = new GetObjectMetadataRequest(); request.BucketName = name; GetObjectMetadataResponse MDresponse = BS3Client.GetObjectMetadata(request); lastaccess = MDresponse.LastModified.ToString(); //defaultpage = MDresponse.WebsiteRedirectLocation; GetBucketWebsiteRequest GBWReq = new GetBucketWebsiteRequest(); GBWReq.BucketName = name; GetBucketWebsiteResponse GBWRes = BS3Client.GetBucketWebsite(GBWReq); defaultpage = GBWRes.WebsiteConfiguration.IndexDocumentSuffix; if (defaultpage != null) { website = @"http://" + name + @".s3-website-" + region + @".amazonaws.com/" + defaultpage; } //Amazon.S3.Model.req abucketrow["AccountID"] = accountid; abucketrow["Profile"] = aprofile; abucketrow["Bucket"] = name; abucketrow["Region"] = region; abucketrow["CreationDate"] = createddate.ToString(); abucketrow["LastAccess"] = lastaccess; abucketrow["Owner"] = owner; abucketrow["Grants"] = grants; abucketrow["WebsiteHosting"] = website; abucketrow["Logging"] = "X"; abucketrow["Events"] = "X"; abucketrow["Versioning"] = "X"; abucketrow["LifeCycle"] = "X"; abucketrow["Replication"] = "X"; abucketrow["Tags"] = "X"; abucketrow["RequesterPays"] = "X"; S3DetailsTable.Rows.Add(abucketrow.ItemArray); } } catch(Exception ex) { System.Windows.Forms.MessageBox.Show("S3 Failed!\n"+ex); } #endregion #region GetEC2Region ////////////////////////////////////////////////////////// //Foreach aregion foreach (var aregion in regions2process) { //Skip GovCloud and Beijing. They require special handling and I dont need em. if (aregion == Amazon.RegionEndpoint.USGovCloudWest1) continue; if (aregion == Amazon.RegionEndpoint.CNNorth1) continue; var region = aregion; regioncounter++; //Try to get scheduled events on my Profile/aregion var ec2 = AWSClientFactory.CreateAmazonEC2Client(credential, region); var request = new DescribeInstanceStatusRequest(); request.IncludeAllInstances = true; Dispatcher.Invoke(doupdatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, regioncounter }); var instatresponse = ec2.DescribeInstanceStatus(request); var indatarequest = new DescribeInstancesRequest(); foreach (var instat in instatresponse.InstanceStatuses) { indatarequest.InstanceIds.Add(instat.InstanceId); } DescribeInstancesResult DescResult = ec2.DescribeInstances(indatarequest); int count = instatresponse.InstanceStatuses.Count(); foreach (var instat in instatresponse.InstanceStatuses) { //Collect the datases string instanceid = instat.InstanceId; string instancename = ""; ProcessingLabel.Content = "Scanning -> Profile:" + aprofile + " Region: " + region + " Instance: " + instanceid; Dispatcher.Invoke(doupdatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { System.Windows.Controls.ProgressBar.ValueProperty, regioncounter }); var status = instat.Status.Status; string AZ = instat.AvailabilityZone; var istate = instat.InstanceState.Name; string profile = aprofile; string myregion = region.ToString(); int eventnumber = instat.Events.Count(); string eventlist = ""; var urtburgle = DescResult.Reservations; string tags = ""; // Holds the list of tags to print out. var loadtags = (from t in DescResult.Reservations where t.Instances[0].InstanceId.Equals(instanceid) select t.Instances[0].Tags).AsEnumerable(); Dictionary<string, string> taglist = new Dictionary<string, string>(); foreach (var rekey in loadtags) { foreach (var kvp in rekey) { taglist.Add(kvp.Key, kvp.Value); } } foreach (var atag in taglist)//Set instancename, and add value to combobox. { if (atag.Key.Equals("Name")) { instancename = atag.Value; } if (!TagFilterCombo.Items.Contains(atag.Key)) { TagFilterCombo.Items.Add(atag.Key); } if (tags.Length > 1) { tags += "\n" + atag.Key + ":" + atag.Value; } else { tags += atag.Key + ":" + atag.Value; } } if (eventnumber > 0) { foreach (var anevent in instat.Events) { eventlist += anevent.Description + "\n"; } } var platform = (from t in urtburgle where t.Instances[0].InstanceId.Equals(instanceid) select t.Instances[0].Platform).FirstOrDefault(); if (String.IsNullOrEmpty(platform)) platform = "Linux"; var Priv_IP = (from t in urtburgle where t.Instances[0].InstanceId.Equals(instanceid) select t.Instances[0].PrivateIpAddress).FirstOrDefault(); if (String.IsNullOrEmpty(Priv_IP)) Priv_IP = "?"; var publicIP = (from t in urtburgle where t.Instances[0].InstanceId.Equals(instanceid) select t.Instances[0].PublicIpAddress).FirstOrDefault(); if (String.IsNullOrEmpty(publicIP)) publicIP = ""; var publicDNS = (from t in urtburgle where t.Instances[0].InstanceId.Equals(instanceid) select t.Instances[0].PublicDnsName).FirstOrDefault(); if (String.IsNullOrEmpty(publicDNS)) publicDNS = ""; //Virtualization type (HVM, Paravirtual) var ivirtType = (from t in urtburgle where t.Instances[0].InstanceId.Equals(instanceid) select t.Instances[0].VirtualizationType).FirstOrDefault(); if (String.IsNullOrEmpty(ivirtType)) ivirtType = "?"; // InstanceType (m3/Large etc) var instancetype = (from t in urtburgle where t.Instances[0].InstanceId.Equals(instanceid) select t.Instances[0].InstanceType).FirstOrDefault(); if (String.IsNullOrEmpty(instancetype)) instancetype = "?"; var SGs = (from t in urtburgle where t.Instances[0].InstanceId.Equals(instanceid) select t.Instances[0].SecurityGroups); string sglist = ""; if (SGs.Count() > 0) { foreach (var ansg in SGs.FirstOrDefault()) { if (sglist.Length > 2) { sglist += "\n"; } sglist += ansg.GroupName; } } else { sglist = "_NONE!_"; } //Add to table if (String.IsNullOrEmpty(sglist)) sglist = "NullOrEmpty"; if (String.IsNullOrEmpty(instancename)) instancename = ""; string rabbit = accountid + profile + myregion + instancename + instanceid + AZ + status + eventnumber + eventlist + tags + Priv_IP + publicIP + publicDNS + istate + ivirtType + instancetype + sglist; if(instancename.Contains("p1-job")) { string yup = "y"; } EC2DetailsTable.Rows.Add(accountid, profile, myregion, instancename, instanceid, AZ, platform, status, eventnumber, eventlist, tags, Priv_IP, publicIP, publicDNS, istate, ivirtType, instancetype, sglist); } } #endregion ScanResults.Add("EC2", EC2DetailsTable); ScanResults.Add("Users", UserDetailsTable); ScanResults.Add("S3", S3DetailsTable); return ScanResults; } catch (Exception ex) { //If we failed to connect with creds. string error = new string(ex.ToString().TakeWhile(c => c != '\n').ToArray()); System.Windows.MessageBox.Show(error, Request.Profile.ToString() + " credentials failed to work.\n"); //Try to flag the menu item so it no longer selectable, and maybe make she red. System.Windows.Controls.MenuItem Proot = (System.Windows.Controls.MenuItem)this.MainMenu.Items[1]; foreach (System.Windows.Controls.MenuItem amenuitem in Proot.Items) { if (amenuitem.Header.ToString() == aprofile.ToString()) { amenuitem.IsCheckable = false; amenuitem.IsChecked = false; amenuitem.Background = Brushes.Red; amenuitem.ToolTip = Request.Profile.ToString() + " credentials failed to work.\n"; } } ScanResults.Add("EC2", GetEC2DetailsTable()); ScanResults.Add("Users", GetUsersDetailsTable()); ScanResults.Add("S3", GetS3DetailsTable()); return ScanResults; } }
private static void ValidateS3Settings(string accessKeyId, string secretAccessKey, string bucket, string region) { if (string.IsNullOrEmpty(accessKeyId)) { throw new ArgumentException("Empty access key id.", "accessKeyId"); } if (string.IsNullOrEmpty(secretAccessKey)) { throw new ArgumentException("Empty secret access key.", "secretAccessKey"); } if (string.IsNullOrEmpty(bucket)) { throw new ArgumentException("Empty s3 bucket.", "bucket"); } if (string.IsNullOrEmpty(region)) { throw new ArgumentException("Empty s3 region.", "region"); } try { using (var s3 = new AmazonS3Client(accessKeyId, secretAccessKey, new AmazonS3Config {RegionEndpoint = RegionEndpoint.GetBySystemName(region)})) { s3.GetBucketLocation(new GetBucketLocationRequest {BucketName = bucket}); } } catch(AmazonS3Exception error) { throw new Exception(error.ErrorCode); } }