public SqsActor () { Receive<string> (x => { var sqs_url = Environment.GetEnvironmentVariable("sqs_url", EnvironmentVariableTarget.Process); var config = new AmazonSQSConfig(); config.ServiceURL = sqs_url; var creds = new StoredProfileAWSCredentials(); var client = new AmazonSQSClient(creds, config); var msg = x + " and what " + Guid.NewGuid().ToString(); var queue_url = Environment.GetEnvironmentVariable("queue_url", EnvironmentVariableTarget.Process); var request = new Amazon.SQS.Model.SendMessageRequest(queue_url, msg); client.SendMessage(request); Sender.Tell(string.Format("done : [{0}]", msg )); }); }
public async System.Threading.Tasks.Task<IHttpActionResult> PostUpload(string folder, string filekey) { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var root = HttpContext.Current.Server.MapPath("~/App_Data/Temp/FileUploads"); if (!Directory.Exists(root)) { Directory.CreateDirectory(root); } var provider = new MultipartFormDataStreamProvider(root); try { var result = await Request.Content.ReadAsMultipartAsync(provider); } catch (Exception ex) { } try { string bucketName = "aws-yeon-test-fims-support"; var credentials = new StoredProfileAWSCredentials("s3"); IAmazonS3 client = new AmazonS3Client(credentials, Amazon.RegionEndpoint.APNortheast2); foreach (var file in provider.FileData) { PutObjectRequest putRequest = new PutObjectRequest { BucketName = bucketName, Key = file.Headers.ContentDisposition.FileName.Substring(1, file.Headers.ContentDisposition.FileName.Length - 2), FilePath = file.LocalFileName //ContentType = "text/plain" }; putRequest.Headers.ContentLength = 168059; PutObjectResponse response = client.PutObject(putRequest); } } catch (AmazonS3Exception amazonS3Exception) { return InternalServerError(amazonS3Exception); } catch (Exception ex) { return InternalServerError(ex); } finally { } return Ok(); }
public Ec2Terminator(string awsProfileName) { var creds = new StoredProfileAWSCredentials(awsProfileName); _client = AWSClientFactory.CreateAmazonEC2Client(creds); _instanceHandler = new Ec2InstanceHandler(_client); _tagHandler = new Ec2TagHandler(_client); _snapshotHandler = new Ec2SnapshotHandler(_client, _tagHandler); _securityGroupHandler = new Ec2SecurityGroupHandler(_client); }
//private Ec2SnapshotHandler _snapshotHandler; public Ec2Bootstrapper(string awsProfileName) { _awsProfileName = awsProfileName; var creds = new StoredProfileAWSCredentials(awsProfileName); _client = AWSClientFactory.CreateAmazonEC2Client(creds); _instanceHandler = new Ec2InstanceHandler(_client); _securityGroupHandler = new Ec2SecurityGroupHandler(_client); _tagHandler = new Ec2TagHandler(_client); _passwordHandler = new Ec2InstancePasswordHandler(_client); //_snapshotHandler = new Ec2SnapshotHandler(_client, _tagHandler); }
private static AmazonEC2Client GetEC2Client(string regionName) { AWSCredentials creds; //try the environment variables first try { creds = new EnvironmentVariablesAWSCredentials(); } //fall back to .aws folder / config catch (Exception) { creds = new StoredProfileAWSCredentials(); } return new AmazonEC2Client(creds, RegionEndpoint.GetBySystemName(regionName)); }
/// <summary> /// Constructor taking the landing zone /// </summary> public DataRouterReportQueue(string InQueueName, string InLandingZoneTempPath, int InDecimateWaitingCountStart, int InDecimateWaitingCountEnd) : base(InQueueName, InLandingZoneTempPath, InDecimateWaitingCountStart, InDecimateWaitingCountEnd) { AWSCredentials Credentials = new StoredProfileAWSCredentials(Config.Default.AWSProfileName, Config.Default.AWSCredentialsFilepath); AmazonSQSConfig SqsConfig = new AmazonSQSConfig { ServiceURL = Config.Default.AWSSQSServiceURL }; SqsClient = new AmazonSQSClient(Credentials, SqsConfig); AmazonS3Config S3Config = new AmazonS3Config { ServiceURL = Config.Default.AWSS3ServiceURL }; S3Client = new AmazonS3Client(Credentials, S3Config); }
/// <summary> /// Determines the type of the requested profile and returns the /// appropriate profile instance. /// </summary> /// <param name="profileName">The name of the profile (AWS or federated) to be loaded.</param> /// <param name="profileLocation"> /// The location of the shared credentials (.ini) file, for profiles that are not stored in the /// SDK credential store. /// </param> /// <returns>Instantiated profile type.</returns> public static AWSCredentials GetProfile(string profileName, string profileLocation) { if (StoredProfileAWSCredentials.CanCreateFrom(profileName, profileLocation)) { return(new StoredProfileAWSCredentials(profileName, profileLocation)); } #if !NETSTANDARD if (StoredProfileFederatedCredentials.CanCreateFrom(profileName, profileLocation)) { return(new StoredProfileFederatedCredentials(profileName, profileLocation)); } #endif var sb = new StringBuilder(); sb.AppendFormat(CultureInfo.InvariantCulture, "Profile {0} was not found in the SDK credential store", profileName); if (!string.IsNullOrEmpty(profileLocation)) { sb.AppendFormat(CultureInfo.InvariantCulture, " or at location '{0}'.", profileLocation); } throw new ArgumentException(sb.ToString()); }
private static void TestCredentialsFile(ImmutableCredentials ic) { var profileName = "testProfile"; var profilesLocation = WriteCreds(profileName, ic); var creds = new StoredProfileAWSCredentials(profileName, profilesLocation); var rc = creds.GetCredentials(); Assert.AreEqual(ic.SecretKey, rc.SecretKey); Assert.AreEqual(ic.AccessKey, rc.AccessKey); Assert.AreEqual(ic.UseToken, rc.UseToken); Assert.AreEqual(ic.Token, rc.Token); for (int i = 0; i < 4; i++) { creds = new StoredProfileAWSCredentials(profileName + i, profilesLocation); Assert.IsNotNull(creds); rc = creds.GetCredentials(); Assert.IsNotNull(rc.AccessKey); Assert.IsNotNull(rc.SecretKey); var shouldHaveToken = (i % 2 == 1); Assert.AreEqual(shouldHaveToken, rc.UseToken); } }
/// <summary> /// Resolves the set of <see cref="AWSCredentials">AWS Credentials</see> based on the /// combination of credential-related parameters that are specified. /// </summary> /// <remarks> /// The order of resolution is as follows: /// <list> /// <item> /// 1. If AccessKeyId is found /// <item>a. If Session Token is found, returns Session AWS Credential</item> /// <item>b. If no Session Token, returns a Base AWS Credential</item> /// </item> /// <item> /// 2. If Profile Name is found, return a Stored Profile AWS Credential, with /// an optional, overridden Profile Location /// </item> /// <item> /// 3. If an IAM Role Name is specified, get the credentials from the local /// EC2 instance IAM Role environment; if the special name '*' is used, /// it uses the first IAM Role found in the current EC2 environment /// </item> /// <item> /// 4. Otherwise, assume credentials are specified in environment variables /// accessible to the hosting process and retrieve them from the following /// variables: /// <item><code>AWS_ACCESS_KEY_ID</code></item> /// <item><code>AWS_SECRET_ACCESS_KEY</code></item> /// <item><code></code>AWS_SESSION_TOKEN</code> (optional)</code></item> /// </item> /// </list> /// </remarks> public AWSCredentials ResolveCredentials() { AWSCredentials cr; if (!string.IsNullOrEmpty(AwsAccessKeyId)) { if (!string.IsNullOrEmpty(AwsSessionToken)) { cr = new SessionAWSCredentials(AwsAccessKeyId, AwsSecretAccessKey, AwsSessionToken); } else { cr = new Amazon.Runtime.BasicAWSCredentials(AwsAccessKeyId, AwsSecretAccessKey); } } else if (!string.IsNullOrEmpty(AwsProfileName)) { cr = new StoredProfileAWSCredentials(AwsProfileName, AwsProfileLocation); } else if (!string.IsNullOrEmpty(AwsIamRole)) { if (AwsIamRole == IAM_ROLE_ANY) cr = new InstanceProfileAWSCredentials(); else cr = new InstanceProfileAWSCredentials(AwsIamRole); } else { cr = new EnvironmentVariablesAWSCredentials(); } return cr; }
/// <summary> /// Gets the data for EC2 Instances in a given Profile and Region. /// </summary> /// <param name="aprofile"></param> /// <param name="Region2Scan"></param> /// <returns></returns> public DataTable GetEC2Instances(string aprofile, string Region2Scan) { DataTable ToReturn = AWSTables.GetComponentTable("EC2"); RegionEndpoint Endpoint2scan = RegionEndpoint.USEast1; Amazon.Runtime.AWSCredentials credential; credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); //Convert the Region2Scan to an AWS Endpoint. foreach (var aregion in RegionEndpoint.EnumerableAllRegions) { if (aregion.DisplayName.Equals(Region2Scan)) { Endpoint2scan = aregion; continue; } } var ec2 = new Amazon.EC2.AmazonEC2Client(credential, Endpoint2scan); string accountid = GetAccountID(aprofile); var request = new DescribeInstanceStatusRequest(); request.IncludeAllInstances = true; DescribeInstanceStatusResponse instatresponse = new DescribeInstanceStatusResponse(); var indatarequest = new DescribeInstancesRequest(); try { instatresponse = ec2.DescribeInstanceStatus(request); } catch (Exception ex) { string test = "";//Quepaso? } //Get a list of the InstanceIDs. foreach (var instat in instatresponse.InstanceStatuses) { indatarequest.InstanceIds.Add(instat.InstanceId); indatarequest.InstanceIds.Sort(); } DescribeInstancesResponse DescResult = ec2.DescribeInstances(); int count = instatresponse.InstanceStatuses.Count(); //Build data dictionary of instances Dictionary<String, Instance> Bunchadata = new Dictionary<string, Instance>(); foreach (var urtburgle in DescResult.Reservations) { foreach (var instancedata in urtburgle.Instances) { try { Bunchadata.Add(instancedata.InstanceId, instancedata); } catch (Exception ex) { var ff ="";//a duplicate?? }; } } //Go through list of instances... foreach (var instat in instatresponse.InstanceStatuses) { string instanceid = instat.InstanceId; Instance thisinstance = new Instance(); try { thisinstance = Bunchadata[instanceid]; } catch(Exception ex) { continue; } DataRow thisinstancedatarow = ToReturn.NewRow(); //Collect the datases string instancename = ""; var status = instat.Status.Status; string AZ = instat.AvailabilityZone; var istate = instat.InstanceState.Name; string profile = aprofile; string myregion = Region2Scan; int eventnumber = instat.Events.Count(); List<string> eventlist = new List<string>(); var reservations = DescResult.Reservations; var myinstance = new Reservation(); var atreq = new DescribeInstanceAttributeRequest(); atreq.InstanceId = instanceid; atreq.Attribute = "disableApiTermination"; var atresp = ec2.DescribeInstanceAttribute(atreq).InstanceAttribute; string TerminationProtection = atresp.DisableApiTermination.ToString(); List<String> innies = new List<String>(); foreach (Reservation arez in DescResult.Reservations) { var checky = arez.Instances[0].InstanceId; innies.Add(checky); if (arez.Instances[0].InstanceId.Equals(instanceid)) { myinstance = arez; } } innies.Sort(); List<string> tags = new List<string>(); var loadtags = thisinstance.Tags.AsEnumerable(); foreach (var atag in loadtags) { tags.Add(atag.Key + ": " + atag.Value); if (atag.Key.Equals("Name")) instancename = atag.Value; } Dictionary<string, string> taglist = new Dictionary<string, string>(); foreach (var rekey in loadtags) { taglist.Add(rekey.Key, rekey.Value); } if (eventnumber > 0) { foreach (var anevent in instat.Events) { eventlist.Add(anevent.Description); } } String platform = ""; try { platform = thisinstance.Platform.Value; } catch { platform = "Linux"; } if (String.IsNullOrEmpty(platform)) platform = "Linux"; String Priv_IP = ""; try { Priv_IP = thisinstance.PrivateIpAddress; } catch { } if (String.IsNullOrEmpty(Priv_IP)) { Priv_IP = "?"; } String disinstance = thisinstance.InstanceId; String publicIP = ""; try { publicIP = thisinstance.PublicIpAddress; } catch { } if (String.IsNullOrEmpty(publicIP)) publicIP = ""; String publicDNS = ""; try { publicDNS = thisinstance.PublicDnsName; } catch { } if (String.IsNullOrEmpty(publicDNS)) publicDNS = ""; string myvpcid = ""; try { myvpcid = thisinstance.VpcId; } catch { } if (String.IsNullOrEmpty(myvpcid)) myvpcid = ""; string mysubnetid = ""; try { mysubnetid = thisinstance.SubnetId; } catch { } if (String.IsNullOrEmpty(mysubnetid)) mysubnetid = ""; //Virtualization type (HVM, Paravirtual) string ivirtType = ""; try { ivirtType = thisinstance.VirtualizationType; } catch { } if (String.IsNullOrEmpty(ivirtType)) ivirtType = "?"; // InstanceType (m3/Large etc) String instancetype = ""; try { instancetype = thisinstance.InstanceType.Value; } catch { } if (String.IsNullOrEmpty(instancetype)) instancetype = "?"; //Test section to try to pull out AMI data string AMI = ""; string AMIName = ""; string AMIDesc = ""; string AMILocation = ""; string AMIState = ""; try { AMI = thisinstance.ImageId; } catch { } if (string.IsNullOrEmpty(AMI)) AMI = ""; else { DescribeImagesRequest DIR = new DescribeImagesRequest(); DIR.ImageIds.Add(AMI); var imresp = ec2.DescribeImages(DIR); var idata = imresp.Images; try { if (idata.Count > 0) { AMIDesc = idata[0].Description; AMIName = idata[0].Name; AMILocation = idata[0].ImageLocation; AMIState = idata[0].State; } else { AMIDesc = "na"; AMIName = "na"; AMILocation = "na"; AMIState = "na"; } if (String.IsNullOrEmpty(AMIDesc)) { AMIDesc = ""; } if (String.IsNullOrEmpty(AMIName)) { AMIName = ""; } } catch(Exception ex) { string whyforerror = ex.Message; } } // var SGs = thisinstance.SecurityGroups; List<string> SGids = new List<string>(); List<String> SGNames = new List<string>(); foreach (var wabbit in SGs) { SGids.Add(wabbit.GroupId); SGNames.Add(wabbit.GroupName); } //Add to table if (SGids.Count < 1) SGids.Add("NullOrEmpty"); if (SGNames.Count < 1) SGNames.Add(""); if (String.IsNullOrEmpty(SGids[0])) SGids[0] = "NullOrEmpty"; if (String.IsNullOrEmpty(SGNames[0])) SGNames[0] = ""; if (String.IsNullOrEmpty(instancename)) instancename = ""; //EC2DetailsTable.Rows.Add(accountid, profile, myregion, instancename, instanceid, AMI, AMIDesc, AZ, platform, status, eventnumber, eventlist, tags, Priv_IP, publicIP, publicDNS, istate, ivirtType, instancetype, sglist); //Is list for Profile and Region, so can key off of InstanceID. In theory InstanceID is unique //Build our dictionary of values and keys for this instance This is dependent on the table created by GetEC2DetailsTable() Dictionary<string, string> datafields = new Dictionary<string, string>(); thisinstancedatarow["AccountID"] = accountid; thisinstancedatarow["Profile"] = profile; thisinstancedatarow["Region"] = myregion; thisinstancedatarow["InstanceName"] = instancename; thisinstancedatarow["InstanceID"] = instanceid; thisinstancedatarow["TerminationProtection"] = TerminationProtection; thisinstancedatarow["AMI"] = AMI; thisinstancedatarow["AMIState"] = AMIState; thisinstancedatarow["AMILocation"] = AMILocation; thisinstancedatarow["AMIDescription"] = AMIDesc; thisinstancedatarow["AvailabilityZone"] = AZ; thisinstancedatarow["Status"] = status; thisinstancedatarow["Events"] = eventnumber.ToString(); thisinstancedatarow["EventList"] = List2String(eventlist); thisinstancedatarow["Tags"] = List2String(tags); thisinstancedatarow["PrivateIP"] = Priv_IP; thisinstancedatarow["PublicIP"] = publicIP; thisinstancedatarow["PublicDNS"] = publicDNS; thisinstancedatarow["PublicDNS"] = publicDNS; thisinstancedatarow["VPC"] = myvpcid; thisinstancedatarow["SubnetID"] = mysubnetid; thisinstancedatarow["InstanceState"] = istate.Value; thisinstancedatarow["VirtualizationType"] = ivirtType; thisinstancedatarow["InstanceType"] = instancetype; thisinstancedatarow["SecurityGroups"] = List2String(SGids); thisinstancedatarow["SGNames"] = List2String(SGNames); //Add this instance to the data returned. ToReturn.Rows.Add(thisinstancedatarow); }//End for of instances return ToReturn; }//EndGetEC2
public DataTable GetVPCs(String aprofile, string Region2Scan) { string accountid = GetAccountID(aprofile); DataTable ToReturn = AWSTables.GetVPCDetailsTable(); Amazon.Runtime.AWSCredentials credential; 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; } } try { credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var ec2 = new Amazon.EC2.AmazonEC2Client(credential, Endpoint2scan); var vippies = ec2.DescribeVpcs().Vpcs; foreach (var avpc in vippies) { DataRow thisvpc = ToReturn.NewRow(); thisvpc["AccountID"] = accountid; thisvpc["Profile"] = aprofile; thisvpc["Region"] = Region2Scan; thisvpc["VpcID"] = avpc.VpcId; thisvpc["CidrBlock"] = avpc.CidrBlock; thisvpc["IsDefault"] = avpc.IsDefault.ToString(); thisvpc["DHCPOptionsID"] = avpc.DhcpOptionsId; thisvpc["InstanceTenancy"] = avpc.InstanceTenancy; thisvpc["State"] = avpc.State; var tagger = avpc.Tags; List<string> tlist = new List<string>(); foreach (var atag in tagger) { tlist.Add(atag.Key + ": " + atag.Value); } thisvpc["Tags"] = List2String(tlist); ToReturn.Rows.Add(thisvpc); } }//End of the big Try catch (Exception ex) { WriteToEventLog("VPC scan of " + aprofile + " failed:" + ex.Message.ToString(), EventLogEntryType.Error); } return ToReturn; }
public DataTable GetEBSDetails(string aprofile, string Region2Scan) { DataTable ToReturn = AWSTables.GetEBSDetailsTable(); string accountid = GetAccountID(aprofile); 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; } } Amazon.Runtime.AWSCredentials credential; try { credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var ec2 = new Amazon.EC2.AmazonEC2Client(credential, Endpoint2scan); // Describe volumes has a max limit, so we have to make sure we collect all the data we need. DescribeVolumesRequest requesty = new DescribeVolumesRequest(); requesty.MaxResults = 1000; var volres = ec2.DescribeVolumes(); var volyumes = volres.Volumes; List<Volume> vollist = new List<Volume>(); while (volres.NextToken != null) { foreach (var av in volyumes) { try { vollist.Add(av); } catch (Exception ex) { WriteToEventLog("EBS on " + aprofile + "/" + Region2Scan + " failed:\n" + ex.Message,EventLogEntryType.Error); } } requesty.NextToken = volres.NextToken; volres = ec2.DescribeVolumes(requesty); } foreach (var av in volyumes) vollist.Add(av); foreach (var onevol in vollist) { var arow = ToReturn.NewRow(); arow["AccountID"] = accountid; arow["Profile"] = aprofile; arow["Region"] = Region2Scan; arow["AZ"] = onevol.AvailabilityZone; arow["CreateTime"] = onevol.CreateTime.ToString(); arow["Encrypted"] = onevol.Encrypted.ToString(); arow["IOPS"] = onevol.Iops; arow["KMSKeyID"] = onevol.KmsKeyId; arow["Size-G"] = onevol.Size; arow["SnapshotID"] = onevol.SnapshotId; arow["State"] = onevol.State.Value; arow["VolumeID"] = onevol.VolumeId; arow["VolumeType"] = onevol.VolumeType.Value; //********** Some extra handling required**************/// List<string> taglist = new List<string>(); foreach (var atag in onevol.Tags) { taglist.Add(atag.Key + ": " + atag.Value); } arow["Tags"] = List2String(taglist); var atachs = onevol.Attachments; arow["Attachments"] = onevol.Attachments.Count.ToString(); if (onevol.Attachments.Count > 0) { arow["AttachTime"] = atachs[0].AttachTime; arow["DeleteonTerm"] = atachs[0].DeleteOnTermination; arow["Device"] = atachs[0].Device; arow["InstanceID"] = atachs[0].InstanceId; arow["AttachState"] = atachs[0].State; } ToReturn.Rows.Add(arow); } } catch (Exception ex) { WriteToEventLog("EBS on " + aprofile + " failed:\n" + ex.Message, EventLogEntryType.Error); } return ToReturn; }
public DataTable GetSNSSubscriptions(string aprofile, string Region2Scan) { DataTable ToReturn = AWSTables.GetSNSSubsTable(); string accountid = GetAccountID(aprofile); 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; } } Amazon.Runtime.AWSCredentials credential; try { credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); //SNS testing here var snsclient = new AmazonSimpleNotificationServiceClient(credential,Endpoint2scan); var subbies = snsclient.ListSubscriptions().Subscriptions; //var toppies = snsclient.ListTopics().Topics; foreach(var asub in subbies) { var myrow = ToReturn.NewRow(); myrow["AccountID"] = accountid ; myrow["Profile"] = aprofile ; myrow["Region"] = Region2Scan ; myrow["Endpoint"] = asub.Endpoint ; myrow["Owner"] = asub.Owner ; myrow["Protocol"] = asub.Protocol ; myrow["SubscriptionARN"] = asub.SubscriptionArn ; myrow["TopicARN"] = asub.TopicArn ; if (asub.SubscriptionArn.Contains(accountid)) { myrow["CrossAccount"] = "No" ; } else { myrow["CrossAccount"] = "Yup"; } var checkker = myrow["CrossAccount"]; ToReturn.Rows.Add(myrow); } } catch(Exception ex) { WriteToEventLog("SNS scan of " + aprofile + " failed\n" + ex.Message.ToString(), EventLogEntryType.Error); } return ToReturn; }
}//EndIamUserScan /// <summary> /// Given a profile and user, collect additional information. /// </summary> /// <param name="aprofile">An AWS Profile name stored in Windows Credential Store</param> /// <param name="auser">The Name of a User</param> /// <returns>Dictionary containing keys for each type of data[AccessKeys], [Groups], [Policies]</returns> public Dictionary<string, string> GetUserDetails(string aprofile, string username) { var credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var iam = new AmazonIdentityManagementServiceClient(credential); Dictionary<string, string> ToReturn = new Dictionary<string, string>(); string policylist = ""; string aklist = ""; string groups = ""; try { ListAccessKeysRequest LAKREQ = new ListAccessKeysRequest(); LAKREQ.UserName = username; var LAKRES = iam.ListAccessKeys(LAKREQ); foreach (var blivet in LAKRES.AccessKeyMetadata) { if (aklist.Length > 1) aklist += "\n"; aklist += blivet.AccessKeyId + " : " + blivet.Status; } } catch { aklist = ""; } try { ListAttachedUserPoliciesRequest LAUPREQ = new ListAttachedUserPoliciesRequest(); LAUPREQ.UserName = username; var LAUPRES = iam.ListAttachedUserPolicies(LAUPREQ); foreach (var apol in LAUPRES.AttachedPolicies) { if (policylist.Length > 1) policylist += "\n"; policylist += apol.PolicyName; } } catch { policylist = ""; } try { var groopsreq = new ListGroupsForUserRequest(); groopsreq.UserName = username; var LG = iam.ListGroupsForUser(groopsreq); foreach (var agroup in LG.Groups) { if (groups.Length > 1) groups += "\n"; groups += agroup.GroupName; } } catch { groups = ""; } ToReturn.Add("Groups", groups); ToReturn.Add("Policies", policylist); ToReturn.Add("AccessKeys", aklist); return ToReturn; }
public DataTable GetSnapshotDetails(string aprofile, string Region2Scan) { DataTable ToReturn = AWSTables.GetSnapshotDetailsTable(); string accountid = GetAccountID(aprofile); 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; } } Amazon.Runtime.AWSCredentials credential; try { credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var ec2 = new Amazon.EC2.AmazonEC2Client(credential, Endpoint2scan); // Describe snapshots has a max limit, so we have to make sure we collect all the data we need. DescribeSnapshotsRequest requesty = new DescribeSnapshotsRequest(); requesty.MaxResults = 1000; //Ouch! It lists all snaps we have access to. We only want ones we own and pay for.. //And it doesnt seem to return the ones we own. WTF???? requesty.OwnerIds.Add("self"); var snapres = ec2.DescribeSnapshots(requesty); var snappies = snapres.Snapshots; int nummie = snappies.Count; Dictionary<string, Snapshot> snaplist = new Dictionary<string, Snapshot>(); while (snapres.NextToken != null) { foreach (var av in snappies) { try { if (!snaplist.Keys.Contains(av.SnapshotId)) snaplist.Add(av.SnapshotId, av); else { var goob = snaplist[av.SnapshotId]; if (goob.Equals(av)) { string itsadupe = "Yar"; } }//Eliminate dupes } catch (Exception ex) { WriteToEventLog("Snapshots on " + aprofile + "/" + Region2Scan + " failed:\n" + ex.Message, EventLogEntryType.Error); } } requesty.NextToken = snapres.NextToken; snapres = ec2.DescribeSnapshots(requesty); } foreach (var av in snappies) { if (!snaplist.Keys.Contains(av.SnapshotId)) snaplist.Add(av.SnapshotId, av); else { var goob = snaplist[av.SnapshotId]; if (goob.Equals(av)) { string itsadupe = "Yar"; } }//Eliminate dupes. } foreach (var onesnap in snaplist.Values) { var arow = ToReturn.NewRow(); if (!accountid.Equals(onesnap.OwnerId)) continue; arow["AccountID"] = accountid; var rr = onesnap.GetType(); arow["Profile"] = aprofile; arow["Region"] = Region2Scan; arow["SnapshotID"] = onesnap.SnapshotId; arow["Description"] = onesnap.Description; arow["VolumeID"] = onesnap.VolumeId; arow["VolumeSize-GB"] = onesnap.VolumeSize; arow["Encrypted"] = onesnap.Encrypted.ToString(); arow["KMSKeyID"] = onesnap.KmsKeyId; arow["OwnerAlias"] = onesnap.OwnerAlias; arow["OwnerID"] = onesnap.OwnerId; arow["Progress"] = onesnap.Progress; arow["StartTime"] = onesnap.StartTime.ToString(); arow["State"] = onesnap.State.Value; arow["StateMessage"] = onesnap.StateMessage; var DKI = onesnap.DataEncryptionKeyId; if (String.IsNullOrEmpty(DKI)) { } else { arow["DataEncryptionKeyID"] = onesnap.DataEncryptionKeyId.ToString(); } //********** Some extra handling required**************/// List<string> taglist = new List<string>(); foreach (var atag in onesnap.Tags) { taglist.Add(atag.Key + ": " + atag.Value); } arow["Tags"] = List2String(taglist); ToReturn.Rows.Add(arow); } } catch (Exception ex) { WriteToEventLog("Snapshots on " + aprofile + " failed:\n" + ex.Message, EventLogEntryType.Error); } return ToReturn; }
/// <summary> /// Given a Profile and Region, return a table with data about bucket sizes, /// collected from CloudWatch. /// </summary> /// <param name="aprofile"></param> /// <param name="Region2Scan"></param> /// <returns></returns> public DataTable S3SizeCloudWatch(string aprofile, string Region2Scan) { DataTable ToReturn =AWSTables.GetS3SizesTable(); string accountid = GetAccountID(aprofile); 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; } } DataTable metlist = new DataTable(); try { var credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); AmazonCloudWatchClient CWClient = new AmazonCloudWatchClient(credential, Endpoint2scan); Amazon.CloudWatch.Model.ListMetricsRequest LMReq = new Amazon.CloudWatch.Model.ListMetricsRequest(); //Using to explore the metrics LMReq.Namespace = "AWS/S3"; LMReq.MetricName = "BucketSizeBytes"; var getmetrics = CWClient.ListMetrics(LMReq).Metrics; //This is just stuff I used to view data from the List metlist.Columns.Add("MetricName"); metlist.Columns.Add("NameSpace"); //These are the dimensions for S3. metlist.Columns.Add("Bucketname"); metlist.Columns.Add("StorageType"); foreach (var ametric in getmetrics) { var DR = metlist.NewRow(); try { DR["MetricName"] = ametric.MetricName; DR["NameSpace"] = ametric.Namespace; var dim = ametric.Dimensions; //These are the dimensions for S3. DR["BucketName"] = dim[0].Value; DR["StorageType"] = dim[1].Value; metlist.Rows.Add(DR); } catch(Exception ex) { } } // Okay, collect the daters for these here buckets foreach (var abucket in metlist.AsEnumerable()) { Amazon.CloudWatch.Model.GetMetricStatisticsRequest GMReq = new Amazon.CloudWatch.Model.GetMetricStatisticsRequest(); string bucketname = abucket[2].ToString(); string storagetype = abucket[3].ToString(); try { Amazon.CloudWatch.Model.Dimension dimbo = new Amazon.CloudWatch.Model.Dimension(); dimbo.Name = "BucketName"; dimbo.Value = bucketname; GMReq.Dimensions.Add(dimbo); Amazon.CloudWatch.Model.Dimension dimbo2 = new Amazon.CloudWatch.Model.Dimension(); dimbo2.Name = "StorageType"; dimbo2.Value = storagetype; GMReq.Dimensions.Add(dimbo2); //Build the request: GMReq.Namespace = "AWS/S3"; GMReq.EndTime = DateTime.Now; GMReq.StartTime = DateTime.Now - TimeSpan.FromDays(21); GMReq.Period = (60 * 60 * 24 * 7);//Seconds in a week. GMReq.Statistics.Add("Minimum"); GMReq.Statistics.Add("Maximum"); GMReq.Statistics.Add("Average"); GMReq.MetricName = "BucketSizeBytes"; //Execute request: var metricresponse = CWClient.GetMetricStatistics(GMReq); //Process Return var dp = metricresponse.Datapoints; if(dp.Count==0) { //none } var arow = ToReturn.NewRow(); Dictionary<DateTime, Amazon.CloudWatch.Model.Datapoint> sortem = new Dictionary<DateTime, Amazon.CloudWatch.Model.Datapoint>(); foreach(var ap in dp) { sortem.Add(ap.Timestamp, ap); } var sorteddates = sortem.Keys.ToList(); sorteddates.Sort(); var firstpass = true; foreach(var key in sorteddates) { var ap = sortem[key]; var min = ap.Minimum; var max = ap.Maximum; var av = ap.Average; var ts = ap.Timestamp; if (firstpass) { firstpass = false; arow["AccountID"] = accountid; arow["Profile"] = aprofile; arow["Bucket"] = bucketname; arow["Region"] = Region2Scan; arow["StartDate"] = ts.ToShortDateString(); arow["EndDate"] = ts.ToShortDateString(); arow["StartSizeMin"] =GetFileSize( min); arow["StartSizeMax"] =GetFileSize( max); arow["StartSizeAVG"]= GetFileSize(av); } else { arow["EndDate"] = ts.ToShortDateString(); arow["EndSizeMin"] = GetFileSize(min); arow["EndSizeAVG"] = GetFileSize(av); arow["EndSizeMax"] = GetFileSize(max); arow["EndSizeMaxBytes"] = Math.Round(av); } } ToReturn.Rows.Add(arow.ItemArray); } catch(Exception ex) { } } } catch(Exception ex) { } int buckets = metlist.Rows.Count; int returning = ToReturn.Rows.Count; return ToReturn; }
public DataTable GetSubnets(string aprofile, string Region2Scan) { string accountid = GetAccountID(aprofile); 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; } } Amazon.Runtime.AWSCredentials credential; DataTable ToReturn = AWSTables.GetComponentTable("Subnets"); try { credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var ec2 = new Amazon.EC2.AmazonEC2Client(credential, Endpoint2scan); var subbies = ec2.DescribeSubnets().Subnets; foreach (var asubnet in subbies) { DataRow disone = ToReturn.NewRow(); disone["AccountID"] = accountid; disone["Profile"] = aprofile; disone["AvailabilityZone"] = asubnet.AvailabilityZone; disone["AvailableIPCount"] = asubnet.AvailableIpAddressCount.ToString(); disone["Cidr"] = asubnet.CidrBlock; //Trickybits. Cidr to IP //var dater = Network2IpRange(asubnet.CidrBlock); System.Net.IPNetwork danetwork = System.Net.IPNetwork.Parse(asubnet.CidrBlock); disone["[Network]"] = danetwork.Network; disone["[Netmask]"] = danetwork.Netmask; disone["[Broadcast]"] = danetwork.Broadcast; disone["[FirstUsable]"] = danetwork.FirstUsable; disone["[LastUsable]"] = danetwork.LastUsable; /// disone["DefaultForAZ"] = asubnet.DefaultForAz.ToString(); disone["MapPubIPonLaunch"] = asubnet.MapPublicIpOnLaunch.ToString(); disone["State"] = asubnet.State; disone["SubnetID"] = asubnet.SubnetId; var tagger = asubnet.Tags; List<string> taglist = new List<string>(); foreach (var atag in tagger) { taglist.Add(atag.Key + ": " + atag.Value); if (atag.Key.Equals("Name")) disone["SubnetName"] = atag.Value; } disone["Tags"] = List2String(taglist); disone["VpcID"] = asubnet.VpcId; ToReturn.Rows.Add(disone); } } catch (Exception ex) { string rabbit = ""; } return ToReturn; }
/// <summary> /// Initializes the provider by pulling the config info from the web.config and validate/create the DynamoDB table. /// If the table is being created this method will block until the table is active. /// </summary> /// <param name="name"></param> /// <param name="config"></param> public override void Initialize(string name, NameValueCollection config) { _logger.InfoFormat("Initialize : Initializing Session provider {0}", name); if (config == null) throw new ArgumentNullException("config"); base.Initialize(name, config); GetConfigSettings(config); RegionEndpoint region = null; if(!string.IsNullOrEmpty(this._regionName)) region = RegionEndpoint.GetBySystemName(this._regionName); AWSCredentials credentials = null; if (!string.IsNullOrEmpty(this._accessKey)) { credentials = new BasicAWSCredentials(this._accessKey, this._secretKey); } else if (!string.IsNullOrEmpty(this._profileName)) { if (string.IsNullOrEmpty(this._profilesLocation)) credentials = new StoredProfileAWSCredentials(this._profileName); else credentials = new StoredProfileAWSCredentials(this._profileName, this._profilesLocation); } AmazonDynamoDBConfig ddbConfig = new AmazonDynamoDBConfig(); if (region != null) ddbConfig.RegionEndpoint = region; if (!string.IsNullOrEmpty(this._serviceURL)) ddbConfig.ServiceURL = this._serviceURL; if (credentials != null) { this._ddbClient = new AmazonDynamoDBClient(credentials, ddbConfig); } else { this._ddbClient = new AmazonDynamoDBClient(ddbConfig); } ((AmazonDynamoDBClient)this._ddbClient).BeforeRequestEvent += DynamoDBSessionStateStore_BeforeRequestEvent; SetupTable(); }
public DataTable GetCertDetails(string aprofile) { string accountid = GetAccountID(aprofile); DataTable ToReturn = AWSTables.GetCertDetailsTable(); Amazon.Runtime.AWSCredentials credential; RegionEndpoint Endpoint2scan = RegionEndpoint.USEast1; try { credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var iam = new AmazonIdentityManagementServiceClient(credential); var cervix = iam.ListServerCertificates(); //How to get certificate details???? foreach (var acert in cervix.ServerCertificateMetadataList) { DataRow disone = ToReturn.NewRow(); disone["AccountID"] = accountid; disone["Profile"] = aprofile; disone["CertName"] = acert.ServerCertificateName; //Cert Details //disone["Status"] = acert.; //disone["InUse"] = acert.xxx; //disone["DomainName"] = acert.xxx; //disone["AdditionalNames"] = acert.xxx; disone["Identifier"] = acert.ServerCertificateId; //disone["SerialNumber"] = acert.xxx; //disone["AssociatedResources"] = acert.xxx; //disone["RequestedAt"] = acert.xxx; //disone["IssuedAt"] = acert.xxx; //disone["NotBefore"] = acert.xxx; disone["NotAfter"] = acert.Expiration; //disone["PublicKeyInfo"] = acert.xxx; //disone["SignatureAlgorithm"] = acert.xxx; disone["ARN"] = acert.Arn; ToReturn.Rows.Add(disone); } }//End of the big Try catch (Exception ex) { //Whyfor did it fail? string w = ""; } return ToReturn; }
/// <summary> /// Just playing with this for now. /// </summary> /// <param name="aprofile"></param> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> public string CreateIAMAccount(string aprofile, string username, string password) { string IRReturning = "Yop"; var credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var iam = new AmazonIdentityManagementServiceClient(credential); CreateUserRequest request = new CreateUserRequest(); request.UserName = username; var repo = iam.CreateUser(request); var det = repo.ResponseMetadata; CreateAccessKeyRequest KeyRequest = new CreateAccessKeyRequest(); KeyRequest.UserName = username; return IRReturning; }
public List<string> GetGroups(string aprofile) { List<string> ToReturn = new List<string>(); Amazon.Runtime.AWSCredentials credential; try { string accountid = GetAccountID(aprofile); credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var iam = new AmazonIdentityManagementServiceClient(credential); ListGroupsRequest req = new ListGroupsRequest(); req.MaxItems = 100; var GROOPLIST = iam.ListGroups(req).Groups; foreach(var agroup in GROOPLIST) { ToReturn.Add(agroup.GroupName); } } catch(Exception ex) { } return ToReturn; }
/// <summary> /// Given a profile name, get the AccountID the profile is associated with. /// </summary> /// <param name="aprofile"></param> /// <returns></returns> public string GetAccountID(string aprofile) { List<User> myUserList = new List<User>(); string accountid = ""; var credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var iam = new AmazonIdentityManagementServiceClient(credential); try { myUserList = iam.ListUsers().Users; } catch (Exception ex) { return "Error: " + ex.Message; } try { accountid = myUserList[0].Arn.Split(':')[4];//Get the ARN and extract the AccountID ID } catch { accountid = "?"; } return accountid; }
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 Ec2AmiLocator(string awsProfileName) { var creds = new StoredProfileAWSCredentials(awsProfileName); _client = AWSClientFactory.CreateAmazonEC2Client(creds); }
public DataTable GetSQSQ(string aprofile, string Region2Scan) { DataTable ToReturn = new DataTable(); string accountid = GetAccountID(aprofile); 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; } } Amazon.Runtime.AWSCredentials credential; try { credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var sqsq = new Amazon.SQS.AmazonSQSClient(credential, Endpoint2scan); var daqs = sqsq.ListQueues("").QueueUrls; } catch { } return ToReturn; }
public DataTable GetRDS(string aprofile, string Region2Scan) { DataTable ToReturn = AWSTables.GetRDSDetailsTable(); string accountid = GetAccountID(aprofile); 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; } } Amazon.Runtime.AWSCredentials credential; try { credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var RDS = new Amazon.RDS.AmazonRDSClient(credential, Endpoint2scan); var RDSi = RDS.DescribeDBInstances(); foreach (var anRDS in RDSi.DBInstances) { DataRow disone = ToReturn.NewRow(); //Handle the List Breakdowns var sgs = anRDS.DBSecurityGroups; List<string> sglist = new List<string>(); foreach (var sg in sgs) { sglist.Add(sg.DBSecurityGroupName + ": " + sg.Status); } var DBSecurityGroups = List2String(sglist); List<string> vsg = new List<string>(); var w = anRDS.VpcSecurityGroups; foreach (var sg in w) { vsg.Add(sg.VpcSecurityGroupId + ": " + sg.Status); } var VPCSecurityGroups = List2String(vsg); //StraightMappings + Mappings of breakdowns. disone["AccountID"] = GetAccountID(aprofile); disone["Profile"] = aprofile; disone["AvailabilityZone"] = anRDS.AvailabilityZone; disone["InstanceID"] = anRDS.DBInstanceIdentifier; disone["Name"] = anRDS.DBName; disone["Status"] = anRDS.DBInstanceStatus; disone["EndPoint"] = anRDS.Endpoint.Address + ":" + anRDS.Endpoint.Port; disone["InstanceClass"] = anRDS.DBInstanceClass; disone["IOPS"] = anRDS.Iops.ToString(); disone["StorageType"] = anRDS.StorageType; disone["AllocatedStorage"] = anRDS.AllocatedStorage; disone["Engine"] = anRDS.StorageType; disone["EngineVersion"] = anRDS.AllocatedStorage; disone["Created"] = anRDS.InstanceCreateTime.ToString(); ToReturn.Rows.Add(disone); } } catch (Exception ex) { string rabbit = ""; } return ToReturn; }
public DataTable GetIAMUsers(string aprofile) { DataTable IAMTable = AWSTables.GetComponentTable("IAM"); //Blank table to fill out. Dictionary<string, string> UserNameIdMap = new Dictionary<string, string>();//Usernames to UserIDs to fill in row later. Amazon.Runtime.AWSCredentials credential; try { string accountid = GetAccountID(aprofile); credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var iam = new AmazonIdentityManagementServiceClient(credential); Dictionary<string, string> unamelookup = new Dictionary<string, string>(); var myUserList = iam.ListUsers().Users; foreach (var rabbit in myUserList) { unamelookup.Add(rabbit.UserId, rabbit.UserName); } var createcredreport = iam.GenerateCredentialReport(); bool notdone = true; var genstart = DateTime.Now; while (notdone) { var status = createcredreport.State; if (status == ReportStateType.COMPLETE) notdone = false; else { if (DateTime.Now > genstart + TimeSpan.FromMinutes(2)) { DataRow auserdata = IAMTable.NewRow(); auserdata["AccountID"] = accountid; auserdata["Profile"] = aprofile; auserdata["UserID"] = "Credential Report"; auserdata["UserName"] = "******"; IAMTable.Rows.Add(auserdata); return IAMTable; } //Sometimes reports take a LOOOOONG time. } } foreach (var auser in myUserList) { UserNameIdMap.Add(auser.UserName, auser.UserId); } Amazon.IdentityManagement.Model.GetCredentialReportResponse credreport = new GetCredentialReportResponse(); DateTime getreportstart = DateTime.Now; DateTime getreportfinish = DateTime.Now; try { credreport = iam.GetCredentialReport(); //Wait for report to finish... how? var goombah = credreport.ResponseMetadata.Metadata; //while(credreport.ResponseMetadata.Metadata) 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(); var headers = myStringRow.Split(",".ToCharArray()[0]); if (myStringRow != null) myStringRow = sr.ReadLine();//Dump the header line Dictionary<string, string> mydata = new Dictionary<string, string>(); while (myStringRow != null) { DataRow auserdata = IAMTable.NewRow(); var arow = myStringRow.Split(",".ToCharArray()[0]); //Letsa dumpa da data... auserdata["AccountID"] = accountid; auserdata["Profile"] = aprofile; string thisid = ""; string username = ""; try { thisid = UserNameIdMap[arow[0]]; auserdata["UserID"] = thisid; auserdata["UserName"] = unamelookup[thisid]; if (unamelookup[thisid] == "<root_account>") { auserdata["UserID"] = "*-" + accountid + "-* root"; } username = unamelookup[thisid]; } catch { auserdata["UserID"] = "*-" + accountid + "-* root"; auserdata["UserName"] = "******"; } auserdata["ARN"] = arow[1]; auserdata["CreateDate"] = arow[2]; auserdata["PwdEnabled"] = arow[3]; auserdata["PwdLastUsed"] = arow[4]; auserdata["PwdLastChanged"] = arow[5]; auserdata["PwdNxtRotation"] = arow[6].ToString(); auserdata["MFA Active"] = arow[7]; auserdata["AccessKey1-Active"] = arow[8];//access_key_1_active auserdata["AccessKey1-Rotated"] = arow[9];//access_key_1_last_rotated auserdata["AccessKey1-LastUsedDate"] = arow[10];//access_key_1_last_used_date auserdata["AccessKey1-LastUsedRegion"] = arow[11];//access_key_1_last_used_region auserdata["AccessKey1-LastUsedService"] = arow[12];//access_key_1_last_used_service auserdata["AccessKey2-Active"] = arow[13];//access_key_2_active auserdata["AccessKey2-Rotated"] = arow[14];//access_key_2_last_rotated auserdata["AccessKey2-LastUsedDate"] = arow[15];//access_key_2_last_used_date auserdata["AccessKey2-LastUsedRegion"] = arow[16];//access_key_2_last_used_region auserdata["AccessKey2-LastUsedService"] = arow[17];//access_key_2_last_used_service auserdata["Cert1-Active"] = arow[18];//cert_1_active auserdata["Cert1-Rotated"] = arow[19];//cert_1_last_rotated auserdata["Cert2-Active"] = arow[20];//cert_2_active auserdata["Cert2-Rotated"] = arow[21];//cert_2_last_rotated var extradata = GetUserDetails(aprofile, username); auserdata["User-Policies"] = extradata["Policies"]; auserdata["Access-Keys"] = extradata["AccessKeys"]; auserdata["Groups"] = extradata["Groups"]; IAMTable.Rows.Add(auserdata); myStringRow = sr.ReadLine(); } sr.Close(); sr.Dispose(); } catch (Exception ex) { WriteToEventLog("IAM scan of " + aprofile + " failed\n" + ex.Message.ToString(), EventLogEntryType.Error); //Deal with this later if necessary. } //Done stream, now to fill in the blanks... } catch//The final catch { string btest = ""; //Deal with this later if necessary. } return IAMTable; }//EndIamUserScan
public DataTable GetELBs(string aprofile, string Region2Scan) { DataTable ToReturn = AWSTables.GetELBsDetailTable(); string accountid = GetAccountID(aprofile); 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; } } Amazon.Runtime.AWSCredentials credential; try { credential = new Amazon.Runtime.StoredProfileAWSCredentials(aprofile); var ELBClient = new Amazon.ElasticLoadBalancing.AmazonElasticLoadBalancingClient(credential, Endpoint2scan); var ELBs = ELBClient.DescribeLoadBalancers().LoadBalancerDescriptions; foreach (var anELB in ELBs) { DataRow disone = ToReturn.NewRow(); //Handle the List Breakdowns var AZs = List2String( anELB.AvailabilityZones); var sgs = List2String(anELB.SecurityGroups); //The tricky part. Trying to figure out ELB listeners with Certificates associated. List<string> Listeners = new List<string>(); List<string> CertListen = new List<string>(); var SiteCert = new X509Certificate2(); string whyforfail = ""; string CheckDaHealth = ""; foreach (var alistener in anELB.ListenerDescriptions) { var protocol = alistener.Listener.Protocol; var externalport = alistener.Listener.LoadBalancerPort; var host = anELB.DNSName; var health = anELB.HealthCheck.Target; //Try to convert the health check string to an actual URL to test. string[] banana = health.Split(':'); string urlpath = banana[1]; int index = urlpath.IndexOf('/'); if (index >= 0) { urlpath= urlpath.Substring(index + 1); } string chekkit= protocol + @"://" + host + ":"+externalport + @"/" + urlpath; //Try a test try { WebRequest request = WebRequest.Create(chekkit); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response == null ) { CheckDaHealth += "Null Response"; } if(response.StatusCode != HttpStatusCode.OK) { CheckDaHealth += response.StatusDescription; } CheckDaHealth += "Ok "; } catch(Exception ex) { CheckDaHealth += chekkit +": "+ex.Message; } string connection = protocol + @":\\" + host + ":" + externalport; Listeners.Add(connection); if (!String.IsNullOrEmpty(alistener.Listener.SSLCertificateId))//If it has a certificate. { CertListen.Add(connection); try { //SiteCert = GetCertificate(connection); SiteCert = GetSSLCertificate2(host,externalport); } catch(Exception ex) { whyforfail = ex.Message; var humph = ""; } } } List<string> instances = new List<string>(); foreach(var aninstance in anELB.Instances) { instances.Add(aninstance.InstanceId); } //StraightMappings + Mappings of breakdowns. disone["AccountID"] = GetAccountID(aprofile); disone["Profile"] = aprofile; disone["AvailabilityZone"] = AZs; disone["Name"] = anELB.LoadBalancerName; disone["DNSName"] = anELB.DNSName; disone["Scheme"] = anELB.Scheme; disone["InstanceCount"] = anELB.Instances.Count; disone["Instances"] = List2String(instances); disone["Listeners"] = List2String(Listeners); disone["CertListeners"] = List2String(CertListen); disone["HealthCheck"] = anELB.HealthCheck.Target; disone["Status"] = CheckDaHealth; disone["SecurityGroups"] = sgs; //Disabling for now. try { if (!(SiteCert == null)) { disone["NotBefore"] = SiteCert.NotBefore; disone["NotAfter"] = SiteCert.NotAfter; disone["Issuer"] = SiteCert.Issuer; disone["Subject"] = SiteCert.Subject; disone["Thumbprint"] = SiteCert.Thumbprint; } else { disone["Subject"] = whyforfail; } } catch(Exception ex) { var e = 1; } ToReturn.Rows.Add(disone); } } catch (Exception ex) { string rabbit = ""; } return ToReturn; }
public DocumentService() { log4net.Config.XmlConfigurator.Configure(); var container = new TinyIoCContainer(); var handlerFactory = new TinyIocHandlerFactory(container); var messageMapperFactory = new TinyIoCMessageMapperFactory(container); container.Register<IHandleRequests<DocumentCreatedEvent>, DocumentCreatedEventHandler>(); container.Register<IHandleRequests<DocumentUpdatedEvent>, DocumentUpdatedEventHandler>(); container.Register<IHandleRequests<FolderCreatedEvent>, FolderCreatedEventHandler>(); var subscriberRegistry = new SubscriberRegistry(); subscriberRegistry.Register<DocumentCreatedEvent, DocumentCreatedEventHandler>(); subscriberRegistry.Register<DocumentUpdatedEvent, DocumentUpdatedEventHandler>(); subscriberRegistry.Register<FolderCreatedEvent, FolderCreatedEventHandler>(); //create policies var retryPolicy = Policy .Handle<Exception>() .WaitAndRetry(new[] { TimeSpan.FromMilliseconds(5000), TimeSpan.FromMilliseconds(10000), TimeSpan.FromMilliseconds(10000) }); var circuitBreakerPolicy = Policy .Handle<Exception>() .CircuitBreaker(1, TimeSpan.FromMilliseconds(500)); var policyRegistry = new PolicyRegistry() { {CommandProcessor.RETRYPOLICY, retryPolicy}, {CommandProcessor.CIRCUITBREAKER, circuitBreakerPolicy} }; //create message mappers var messageMapperRegistry = new MessageMapperRegistry(messageMapperFactory) { {typeof(FolderCreatedEvent), typeof(FolderCreatedEventMessageMapper)}, {typeof(DocumentCreatedEvent), typeof(DocumentCreatedEventMessageMapper)}, {typeof(DocumentUpdatedEvent), typeof(DocumentUpdatedEventMessageMapper)} }; var awsCredentials = new StoredProfileAWSCredentials(); var sqsMessageConsumerFactory = new SqsMessageConsumerFactory(awsCredentials ); var sqsMessageProducerFactory = new SqsMessageProducerFactory(awsCredentials ); var connections = new List<paramore.brighter.serviceactivator.Connection> { new paramore.brighter.serviceactivator.Connection( new ConnectionName("paramore.example.documentsandfolders.documentcreatedevent"), new InputChannelFactory(sqsMessageConsumerFactory, sqsMessageProducerFactory), typeof(DocumentCreatedEvent), new ChannelName("https://sqs.eu-west-1.amazonaws.com/027649620536/DocumentCreatedEvent"), "DocumentCreatedEvent", timeoutInMilliseconds: 5000, noOfPerformers: 10), new paramore.brighter.serviceactivator.Connection( new ConnectionName("paramore.example.documentsandfolders.documentupdatedevent"), new InputChannelFactory(sqsMessageConsumerFactory, sqsMessageProducerFactory), typeof(DocumentUpdatedEvent), new ChannelName("https://sqs.eu-west-1.amazonaws.com/027649620536/DocumentUpdatedEvent"), "DocumentUpdatedEvent", timeoutInMilliseconds: 5000, noOfPerformers: 10), new paramore.brighter.serviceactivator.Connection( new ConnectionName("paramore.example.documentsandfolders.foldercreateddevent"), new InputChannelFactory(sqsMessageConsumerFactory, sqsMessageProducerFactory), typeof(FolderCreatedEvent), new ChannelName("https://sqs.eu-west-1.amazonaws.com/027649620536/FolderCreatedEvent"), "FolderCreatedEvent", timeoutInMilliseconds: 5000, noOfPerformers: 10) }; var builder = DispatchBuilder .With() .CommandProcessor(CommandProcessorBuilder.With() .Handlers(new HandlerConfiguration(subscriberRegistry, handlerFactory)) .Policies(policyRegistry) .NoTaskQueues() .RequestContextFactory(new InMemoryRequestContextFactory()) .Build() ) .MessageMappers(messageMapperRegistry) .ChannelFactory(new InputChannelFactory(sqsMessageConsumerFactory, sqsMessageProducerFactory)) .Connections(connections); _dispatcher = builder.Build(); }