Пример #1
0
        /// <summary>
        ///     Reads the VPC for cacheing DNS-Records to overrides public IPAddresses
        ///     like SQS.[zone].amazonaws.com to 10.10.10.10
        /// </summary>
        /// <param name="awsCredentials">AwsCredintials for read the VPC</param>
        /// <param name="awsScanRules">Rules for Scaning</param>
        /// <param name="cancellationToken">Task Cancellation Toke</param>
        /// <returns>only a Task</returns>
        private async Task ReadVpcAsync(AWSCredentials awsCredentials, IAwsScanRules awsScanRules,
                                        CancellationToken cancellationToken)
        {
            try
            {
                var result = new List <DnsRecordBase>();

                var endpoints = await _awsVpcReader.ReadEndpoints(awsCredentials, awsScanRules, cancellationToken)
                                .ConfigureAwait(false);

                foreach (AwsAdapterBase adapter in _awsAdapter)
                {
                    var adapterResult = await adapter
                                        .GetAdapterResultAsync(awsCredentials, endpoints, cancellationToken)
                                        .ConfigureAwait(false);

                    result.AddRange(adapterResult.DnsRecords);
                    _proxyBypassList.AddRange(adapterResult.ProxyBypassList);
                }

                var groupedResult = (from record in result
                                     group record by record.Name.ToString()
                                     into newRecords
                                     orderby newRecords.Key
                                     select newRecords).ToList();
                foreach (var dnsRecordBases in groupedResult)
                {
                    var dnsQuestion = new DnsQuestion(DomainName.Parse(dnsRecordBases.Key), RecordType.A,
                                                      RecordClass.INet);
                    StoreInCache(dnsQuestion, dnsRecordBases.ToList());
                }
            }
            catch (AmazonEC2Exception aee)
            {
                if (aee.ErrorCode == "UnauthorizedOperation")
                {
                    _logger.LogError(aee, "AWS ErrorCode=[{0}] ==> {1}", aee.ErrorCode, aee.Message);
                }
                else
                {
                    throw;
                }
            }
            catch (Exception e)
            {
                _logger.LogCritical(e, e.Message);
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// Read the Apt-Gateway VpcEndpoint and map all ApiGateway Configurations/Urls to the IPAddress
        /// Create DNs-Records
        /// </summary>
        public override async Task <AwsAdapterResult> GetAdapterResultAsync(
            AWSCredentials awsCredentials,
            IEnumerable <Endpoint> endpoints,
            CancellationToken cancellationToken)
        {
            using (var amazonApiGatewayClient = new AmazonAPIGatewayClient(awsCredentials, AmazonApiGatewayConfig))
            {
                var result = new AwsAdapterResult();
                var apiGatewayNetworkInterfaces =
                    endpoints.Where(x =>
                                    x.VpcEndpoint.ServiceName.Contains(".execute-api", StringComparison.InvariantCulture)).ToList();
                var apis = await amazonApiGatewayClient.GetRestApisAsync(new GetRestApisRequest(), cancellationToken)
                           .ConfigureAwait(false);

                var orderedApis = apis.Items.Where(x =>
                                                   x.EndpointConfiguration.Types.SingleOrDefault(y => "PRIVATE".Equals(y, StringComparison.InvariantCulture)) !=
                                                   null).ToArray();

                foreach (var endpoint in apiGatewayNetworkInterfaces)
                {
                    for (var i = orderedApis.Length - 1; i >= 0; i--)
                    {
                        var item       = orderedApis[i];
                        var net        = endpoint.NetworkInterfaces.First();
                        var domainName = CreateApiGatewayDomainName(endpoint.VpcEndpoint.ServiceName, item.Id);

                        result.ProxyBypassList.Add(domainName);
                        result.DnsRecords.Add(new ARecord(
                                                  ARSoft.Tools.Net.DomainName.Parse(domainName),
                                                  Ttl,
                                                  IPAddress.Parse(net.PrivateIpAddress)));
                        result.DnsRecords.Add(new ARecord(
                                                  DomainName.Parse(net.PrivateDnsName),
                                                  Ttl,
                                                  IPAddress.Parse(net.PrivateIpAddress)));
                    }
                }
                return(result);
            }
        }