public static void InitializePlatformResources(PlatformResourcesContext context) { context.Database.EnsureCreated(); if (!context.Templates.Any()) { var defaultTemplates = new Template[] { new Template { Name = "Microsoft Windows Server 2019 Base Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("26/1/2019"), OperatingSystem = "Microsoft Windows Server 2019 Base", AWSAMIReference = "ami-0077b703cac9baa12", SpecificMinimumSize = true, MinimumStorage = 30 }, new Template { Name = "Amazon Linux 2 LTS Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Amazon Linux 2 LTS", AWSAMIReference = "ami-0b84d2c53ad5250c2", SpecificMinimumSize = true, MinimumStorage = 8 }, new Template { Name = "Amazon Linux 2018.3 Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Amazon Linux 2018.3", AWSAMIReference = "ami-05b3bcf7f311194b3", SpecificMinimumSize = true, MinimumStorage = 8 }, new Template { Name = "Red Hat Enterprise Linux Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Red Hat Enterprise Linux 7.5", AWSAMIReference = "ami-76144b0a", SpecificMinimumSize = true, MinimumStorage = 10 }, new Template { Name = "SUSE Linux Enterprise Server Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "SUSE Linux Enterprise Server 15", AWSAMIReference = "ami-0920c364049458e86", SpecificMinimumSize = true, MinimumStorage = 10 }, new Template { Name = "Ubuntu Server 18.04 LTS Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Ubuntu Server 18.04 LTS", AWSAMIReference = "ami-0c5199d385b432989", SpecificMinimumSize = true, MinimumStorage = 8 }, new Template { Name = "Ubuntu Server 14.04 LTS Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Ubuntu Server 14.04 LTS", AWSAMIReference = "ami-039950f07c4a0d878", SpecificMinimumSize = true, MinimumStorage = 8 }, new Template { Name = "Microsoft Windows Server 2016 Base Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Microsoft Windows Server 2016 Base", AWSAMIReference = "ami-04385f3f533c85af7", SpecificMinimumSize = true, MinimumStorage = 30 }, new Template { Name = "Microsoft Windows Server 2012 R2 Base Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Microsoft Windows Server 2012 R2 Base", AWSAMIReference = "ami-059b411d0e166914e", SpecificMinimumSize = true, MinimumStorage = 30 }, new Template { Name = "Microsoft Windows Server 2012 Base Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Microsoft Windows Server 2012 Base", AWSAMIReference = "ami-0caa1a0b06b16e0c0", SpecificMinimumSize = true, MinimumStorage = 30 }, new Template { Name = "Microsoft Windows Server 2008 R2 Base Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Microsoft Windows Server 2008 R2 Base", AWSAMIReference = "ami-0428d47da132d9763", SpecificMinimumSize = true, MinimumStorage = 30 }, new Template { Name = "Microsoft Windows Server 2008 SP2 Base Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Microsoft Windows Server 2008 SP2 Base", AWSAMIReference = "ami-07bae34c7bd0d31d0", SpecificMinimumSize = true, MinimumStorage = 30 }, new Template { Name = "Ubuntu Server 16.04 LTS Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Ubuntu Server 16.04 LTS", AWSAMIReference = "ami-0eb1f21bbd66347fe", SpecificMinimumSize = true, MinimumStorage = 8 }, new Template { Name = "Microsoft Windows Server 2003 R2 Base Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Microsoft Windows Server 2003 R2 Base", AWSAMIReference = "ami-0407dbd3ff0dca87c", SpecificMinimumSize = true, MinimumStorage = 30 }, new Template { Name = "Kali Linux Template", Type = TemplateType.Default, DateCreated = DateTime.Parse("1/1/2019"), OperatingSystem = "Kali Linux 2018.3", AWSAMIReference = "ami-0db99f1dca7fe5ee2", SpecificMinimumSize = true, MinimumStorage = 25, AWSSnapshotReference = "snap-0bd354666ed0d1609" }, new Template { Name = "Splunk Enterprise Template", Type = TemplateType.Custom, DateCreated = DateTime.Parse("5/1/2019"), OperatingSystem = "Amazon Linux 2 LTS", AWSAMIReference = "ami-04c4105e8f4bf70eb", SpecificMinimumSize = true, MinimumStorage = 15, TemplateDescription = "Default Amazon 2 LTS Template with Splunk Enterprise 7 Pre-loaded \nUser Name: eCTFAdmin \nPassword:eCTFP@ss", AWSSnapshotReference = "snap-0aaaa31ee20e240ea" } }; foreach (Template t in defaultTemplates) { context.Templates.Add(t); } context.SaveChanges(); } }
public async Task DoWorkAsync() { _logger.LogInformation("Update Background Service is running"); try { _logger.LogInformation("Update Background Service is checking VPCs"); if (context.VPCs.Any()) { DescribeVpcsResponse responseDescribeVPC = await ec2Client.DescribeVpcsAsync(); List <VPC> vpcs = await context.VPCs.ToListAsync(); foreach (VPC vpc in vpcs) { Boolean Flag = false; foreach (Vpc AWSVPC in responseDescribeVPC.Vpcs) { if (vpc.AWSVPCReference.Equals(AWSVPC.VpcId)) { Flag = true; break; } } if (Flag == false) { context.VPCs.Remove(vpc); } } context.SaveChanges(); _logger.LogInformation("Update Background Service completed checking of VPCs"); } if (context.VPCs.Any()) { VPC vpc = await context.VPCs.FindAsync(1); if (context.Servers.Any()) { _logger.LogInformation("Update Background Service is checking servers"); List <Server> servers = await context.Servers.ToListAsync(); DescribeInstancesResponse response = await ec2Client.DescribeInstancesAsync(new DescribeInstancesRequest { Filters = new List <Filter> { new Filter { Name = "vpc-id", Values = new List <string> { vpc.AWSVPCReference } } } }); foreach (var server in servers) { foreach (var reservation in response.Reservations) { foreach (var instance in reservation.Instances) { bool Flag = false; if (server.AWSEC2Reference.Equals(instance.InstanceId)) { if (instance.State.Code == 0 && server.State != State.Starting) { server.State = State.Starting; Flag = true; } else if (instance.State.Code == 16 && server.State != State.Running) { server.State = State.Running; Flag = true; } else if (instance.State.Code == 64 && server.State != State.Stopping) { server.State = State.Stopping; Flag = true; } else if (instance.State.Code == 80 && server.State != State.Stopped) { server.State = State.Stopped; Flag = true; } if (server.Visibility == Visibility.Internet && (server.IPAddress != instance.PublicIpAddress || server.DNSHostname != instance.PublicDnsName) && server.State != State.Stopped) { server.IPAddress = instance.PublicIpAddress; server.DNSHostname = instance.PublicDnsName; Flag = true; } else if (server.Visibility == Visibility.Internet && (server.IPAddress != instance.PublicIpAddress || server.DNSHostname != instance.PublicDnsName)) { server.IPAddress = "Public IP Address is not available when server is stopped"; server.DNSHostname = "Public DNS Hostname is not available when server is stopped"; Flag = true; } else if ((server.Visibility == Visibility.Extranet || server.Visibility == Visibility.Intranet) && (server.IPAddress != instance.PrivateIpAddress || server.DNSHostname != instance.PrivateDnsName)) { server.IPAddress = instance.PrivateIpAddress; server.DNSHostname = instance.PrivateDnsName; Flag = true; } if (Flag == true) { context.Servers.Update(server); } break; } } } } context.SaveChanges(); } if (context.CloudWatchLogGroups.Any() && context.CloudWatchLogStreams.Any()) { _logger.LogInformation("Update Background Service is checking cloudwatch groups"); List <CloudWatchLogGroup> allStreams = await context.CloudWatchLogGroups.ToListAsync(); foreach (CloudWatchLogGroup g in allStreams) { DescribeLogStreamsResponse response = await cwlClient.DescribeLogStreamsAsync(new DescribeLogStreamsRequest { LogGroupName = g.Name.Replace("@", "/") }); foreach (LogStream ls in response.LogStreams) { Boolean Flag = false; foreach (CloudWatchLogStream CWLS in g.LogStreams) { if (ls.Arn.Equals(CWLS.ARN)) { Flag = true; break; } } if (Flag == false) { CloudWatchLogStream newS = new CloudWatchLogStream { ARN = ls.Arn, CreationTime = ls.CreationTime, FirstEventTime = ls.FirstEventTimestamp, LastEventTime = ls.LastEventTimestamp, Name = ls.LogStreamName, LinkedGroupID = g.ID }; if (g.Name.Equals("VMVPCLogs")) { newS.DisplayName = "Network Flow Log For Challenge Network Interface (" + newS.Name.Substring(0, newS.Name.Length - 4) + ")"; } else if (g.Name.Equals("PlatformVPCLogs")) { newS.DisplayName = "Network Flow Log For Platform Network Interface (" + newS.Name.Substring(0, newS.Name.Length - 4) + ")"; } else if (g.Name.Equals("@aws@elasticbeanstalk@User-Side@IIS-Log")) { newS.DisplayName = "IIS Logs for User Side Web Server"; } else if (g.Name.Equals("@aws@elasticbeanstalk@Admin-Side@IIS-Log")) { newS.DisplayName = "IIS Logs for Admin Side Web Server"; } else if (g.Name.Equals("@aws@elasticbeanstalk@User-Side@EBDeploy-Log")) { newS.DisplayName = "Elastic Beanstalk Deployment Tool Logs for User Side"; } else if (g.Name.Equals("@aws@elasticbeanstalk@Admin-Side@EBDeploy-Log")) { newS.DisplayName = "Elastic Beanstalk Deployment Tool Logs for Admin Side"; } else if (g.Name.Equals("@aws@elasticbeanstalk@User-Side@EBHooks-Log")) { newS.DisplayName = "Elastic Beanstalk Deployment Hook Logs for User Side"; } else if (g.Name.Equals("@aws@elasticbeanstalk@Admin-Side@EBHooks-Log")) { newS.DisplayName = "Elastic Beanstalk Deployment Hook Logs for Admin Side"; } else { newS.DisplayName = newS.Name; } if (!g.Name.Equals("RDSOSMetrics") || !g.Name.Contains("sns")) { context.CloudWatchLogStreams.Add(newS); } } } foreach (CloudWatchLogStream CWLS in g.LogStreams) { Boolean Flag = false; foreach (LogStream ls in response.LogStreams) { if (CWLS.ARN.Equals(ls.Arn)) { Flag = true; break; } } if (Flag == false) { context.CloudWatchLogStreams.Remove(CWLS); } } } await context.SaveChangesAsync(); } if (context.Templates.Any()) { DescribeSnapshotsResponse response = await ec2Client.DescribeSnapshotsAsync(new DescribeSnapshotsRequest()); List <Template> templates = await context.Templates.FromSql("SELECT * FROM dbo.Templates WHERE AWSSnapshotReference = NULL").ToListAsync(); foreach (Template t in templates) { foreach (Snapshot s in response.Snapshots) { if (s.Description.Contains(t.AWSAMIReference)) { t.AWSSnapshotReference = s.SnapshotId; break; } } } await context.SaveChangesAsync(); } } _logger.LogInformation("Update Background Service has completed!"); } catch (SqlException e) { _logger.LogInformation("Update Background Service faced an SQL exception! " + e.Message + " | " + e.Source); return; } catch (Exception e) { _logger.LogInformation("Update Background Service faced an exception! " + e.Message + " | " + e.Source); return; } }