Exemplo n.º 1
0
 public SSMEncryptionKey(
     AmazonSimpleSystemsManagementClient amazonSimpleSystemsManagementClient,
     IConfiguration configuration)
 {
     _amazonSimpleSystemsManagementClient = amazonSimpleSystemsManagementClient;
     _configuration = configuration;
 }
Exemplo n.º 2
0
        public override void Invoke(AWSCredentials creds, RegionEndpoint region, int maxItems)
        {
            AmazonSimpleSystemsManagementConfig config = new AmazonSimpleSystemsManagementConfig();

            config.RegionEndpoint = region;
            ConfigureClient(config);
            AmazonSimpleSystemsManagementClient client = new AmazonSimpleSystemsManagementClient(creds, config);

            DescribeParametersResponse resp = new DescribeParametersResponse();

            do
            {
                DescribeParametersRequest req = new DescribeParametersRequest
                {
                    NextToken = resp.NextToken
                    ,
                    MaxResults = maxItems
                };

                resp = client.DescribeParameters(req);
                CheckError(resp.HttpStatusCode, "200");

                foreach (var obj in resp.Parameters)
                {
                    AddObject(obj);
                }
            }while (!string.IsNullOrEmpty(resp.NextToken));
        }
Exemplo n.º 3
0
 public async Task <GetParameterResponse> GetParameter(GetParameterRequest getParameterRequest)
 {
     using (var client = new AmazonSimpleSystemsManagementClient())
     {
         return(await client.GetParameterAsync(getParameterRequest));
     }
 }
Exemplo n.º 4
0
        static async Task GetConfiguration()
        {
            // NOTE: set the region here to match the region used when you created
            // the parameter
            var region = Amazon.RegionEndpoint.USEast1;

            var request = new GetParameterRequest()
            {
                Name = "/TestParameterStore/EnvironmentName"
            };

            using (var client = new AmazonSimpleSystemsManagementClient(region))
            {
                try
                {
                    var response = await client.GetParameterAsync(request);

                    Console.WriteLine($"Parameter {request.Name} has value: {response.Parameter.Value}");
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine($"Error occurred: {ex.Message}");
                }
            }
        }
Exemplo n.º 5
0
        public static async Task <string> GetParameterValue(string parameterKey)
        {
            var request = new GetParameterRequest()
            {
                Name = parameterKey
            };

            var extensionDownloadCount = String.Empty;

            try
            {
                using (var client = new AmazonSimpleSystemsManagementClient())
                {
                    var response = await client.GetParameterAsync(request);

                    extensionDownloadCount = response.Parameter.Value;

                    Console.WriteLine($"Parameter {request.Name} value is: {extensionDownloadCount}");
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"Error occurred: {ex.Message}");
            }

            return(extensionDownloadCount);
        }
Exemplo n.º 6
0
        static async Task GetConfiguration()
        {
            var awsAccessKeyId     = "..."; //ConfigurationManager.AppSettings["AWSAccessKeyId"];
            var awsSecretAccessKey = "..."; //ConfigurationManager.AppSettings["AWSSecretAccessKey"];

            var            region      = Amazon.RegionEndpoint.USEast1;
            AWSCredentials credentials = new BasicAWSCredentials(
                awsAccessKeyId,
                awsSecretAccessKey
                );

            var request = new GetParameterRequest()
            {
                Name = "/internal/api/access-logs"
            };

            using (var client = new AmazonSimpleSystemsManagementClient(credentials, region))
            {
                try
                {
                    GetParameterResponse response = await client.GetParameterAsync(request);

                    Console.WriteLine($"Parameter {request.Name} value is: {response.Parameter.Value}");
                    JObject json = JObject.Parse(response.Parameter.Value);

                    Console.WriteLine(json.GetValue("url").ToString());
                    Console.WriteLine(json.GetValue("apiId").ToString());
                    Console.WriteLine(json.GetValue("headerName").ToString());
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine($"Error occurred: {ex.Message}");
                }
            }
        }
        public async Task <BillingResponse> Handle(BillingRequest request, CancellationToken cancellationToken)
        {
            var stripeChargeRequest = request.MapToStripeRequest();

            var client     = new AmazonSimpleSystemsManagementClient();
            var ssmRequest = new GetParameterRequest
            {
                //differentiate between dev and test later
                Name           = "/stripeSecretKey/test",
                WithDecryption = true
            };
            var ssmResponse = client.GetParameterAsync(ssmRequest).Result;
            var stripeKey   = ssmResponse.Parameter.Value;

            StripeConfiguration.ApiKey = stripeKey;
            var stripeParameters = new ChargeCreateOptions
            {
                Amount      = stripeChargeRequest.Amount,
                Currency    = "usd",
                Source      = stripeChargeRequest.Source,
                Description = "Scratch Charge",
            };
            var service  = new ChargeService();
            var response = service.Create(stripeParameters);

            return(response == null ? new BillingResponse {
                Status = false
            } : new BillingResponse {
                Status = true
            });
        }
Exemplo n.º 8
0
            static async Task <string> GetFileValueFromSsmUsingAmazonSdk(string filename)
            {
                // NOTE: set the region here to match the region used when you created
                // the parameter
                var region  = Amazon.RegionEndpoint.USEast1;
                var request = new GetParameterRequest()
                {
                    Name           = filename,
                    WithDecryption = true
                };

                using (var client = new AmazonSimpleSystemsManagementClient(region))
                {
                    try
                    {
                        var response = await client.GetParameterAsync(request);

                        Logger.Info($"Retrieved {filename} from SSM");
                        return(response.Parameter.Value);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("Error fetching {0} from SSM: {1}", filename, ex);
                        throw;
                    }
                }
            }
Exemplo n.º 9
0
        public async Task <string> GetKeyParameterValue(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            try
            {
                using (var client = new AmazonSimpleSystemsManagementClient(RegionEndpoint.EUWest1))
                {
                    var response =
                        await client.GetParameterAsync(
                            new GetParameterRequest
                    {
                        Name           = key,
                        WithDecryption = false
                    }).ConfigureAwait(true);

                    return(response.Parameter.Value);
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Failed to get parameter value");
                return(string.Empty);
            }
        }
Exemplo n.º 10
0
        static async Task <string> GetUserPassword()
        {
            string retVal = "";
            var    region = Amazon.RegionEndpoint.USEast1;

            var request = new GetParameterRequest()
            {
                Name = "MQBrokerUserPassword"
            };

            using (var client = new AmazonSimpleSystemsManagementClient(region))
            {
                try
                {
                    var response = await client.GetParameterAsync(request);

                    retVal = response.Parameter.Value;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine($"Error occurred: {ex.Message}");
                }
            }
            return(retVal);
        }
Exemplo n.º 11
0
 private SSMClient()
 {
     MaxAge              = TimeSpan.FromDays(1);
     m_Parameters        = new Dictionary <string, SSMParameter>(StringComparer.OrdinalIgnoreCase);
     m_InvalidParameters = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
     m_Logger            = new LoggerFactory().CreateLogger <SSMClient>();
     try
     {
         string aws_ps_region = Environment.GetEnvironmentVariable("AWS_Region");
         if (string.IsNullOrWhiteSpace(aws_ps_region))
         {
             m_Client = new AmazonSimpleSystemsManagementClient();
         }
         else
         {
             RegionEndpoint parameterStoreRegion = RegionEndpoint.GetBySystemName(aws_ps_region);
             m_Client = new AmazonSimpleSystemsManagementClient(parameterStoreRegion);
         }
     }
     catch (Exception ex)
     {
         m_Logger.LogInformation("Exception info", ex.StackTrace);
         m_Logger.LogDebug("SSMClient", "Cannot create SSM Client: {0}", ex.Message);
         m_Client = null;
     }
 }
Exemplo n.º 12
0
 public async Task <PutParameterResponse> CreateParameter(PutParameterRequest parameterToCreate)
 {
     using (var client = new AmazonSimpleSystemsManagementClient())
     {
         return(await client.PutParameterAsync(parameterToCreate));
     }
 }
Exemplo n.º 13
0
        public async Task AddApiCredentials(Capability capability, ApiCredentials apiCredentials, string clusterId)
        {
            var ssmClient = new AmazonSimpleSystemsManagementClient(RegionEndpoint.EUCentral1);

            await ssmClient.PutParameterAsync(new PutParameterRequest
            {
                Type  = ParameterType.SecureString,
                Name  = $"/capabilities/{capability.RootId}/kafka/{clusterId}/credentials",
                Tier  = ParameterTier.Standard,
                Value = JsonConvert.SerializeObject(new
                {
                    Key    = apiCredentials.Key,
                    Secret = apiCredentials.Secret
                }),
                Tags = new List <Tag>
                {
                    new Tag {
                        Key = "capabilityName", Value = capability.Name
                    },
                    new Tag {
                        Key = "capabilityId", Value = capability.Id
                    },
                    new Tag {
                        Key = "capabilityRootId", Value = capability.RootId
                    },
                    new Tag {
                        Key = "createdBy", Value = "Kafka-Janitor"
                    }
                }
            });
        }
Exemplo n.º 14
0
 public static IConfigurationBuilder AddSsmParameterStore(
     this IConfigurationBuilder configuration,
     string BasePath = "/",
     AmazonSimpleSystemsManagementClient client = null)
 {
     configuration.Add(new SsmParameterStoreConfigurationSource(BasePath, client ?? new AmazonSimpleSystemsManagementClient()));
     return(configuration);
 }
Exemplo n.º 15
0
        public async Task <Stream> FunctionHandler(Stream input, ILambdaContext context)
        {
            var ssm = new AmazonSimpleSystemsManagementClient();

            const string googleComputeParameter      = "/estranged/google/compute";
            const string communityWebhookParameter   = "/estranged/discord/webhooks/community";
            const string reviewsWebhookParameter     = "/estranged/discord/webhooks/reviews";
            const string gamingWebhookParameter      = "/estranged/discord/webhooks/gaming";
            const string syndicationWebhookParameter = "/estranged/discord/webhooks/syndication";

            var parameters = (await ssm.GetParametersAsync(new GetParametersRequest
            {
                Names = new List <string>
                {
                    googleComputeParameter,
                    communityWebhookParameter,
                    reviewsWebhookParameter,
                    gamingWebhookParameter,
                    syndicationWebhookParameter
                }
            })).Parameters.ToDictionary(x => x.Name, x => x.Value);

            var config = new FunctionConfig
            {
                EstrangedDiscordCommunityWebhook   = parameters[communityWebhookParameter],
                EstrangedDiscordReviewsWebhook     = parameters[reviewsWebhookParameter],
                EstrangedDiscordGamingWebhook      = parameters[gamingWebhookParameter],
                EstrangedDiscordSyndicationWebhook = parameters[syndicationWebhookParameter]
            };

            var httpClient = new HttpClient();

            var services = new ServiceCollection()
                           .AddLogging(options =>
            {
                options.AddConsole();
                options.SetMinimumLevel(LogLevel.Warning);
            })
                           .AddSingleton <IRunnable, CommunityRunnable>()
                           .AddSingleton <IRunnable, ReviewsRunnable>()
                           .AddSingleton <IRunnable, SyndicationRunnable>()
                           .AddSingleton(TranslationClient.Create(GoogleCredential.FromJson(parameters[googleComputeParameter])))
                           .AddSteam(new SteamConfig {
                HttpClient = httpClient
            })
                           .AddSingleton <ISeenItemRepository, SeenItemRepository>()
                           .AddSingleton <IAmazonDynamoDB, AmazonDynamoDBClient>()
                           .AddSingleton <Scraper>()
                           .AddSingleton(httpClient)
                           .AddSingleton(config);

            var provider = services.BuildServiceProvider();

            await Task.WhenAll(provider.GetServices <IRunnable>().SelectMany(x => x.RunAsync(CancellationToken.None)));

            return(input);
        }
Exemplo n.º 16
0
        private string FetchGitHubPersonalAuthToken()
        {
            var client   = new AmazonSimpleSystemsManagementClient();
            var response = client.GetParameterAsync(new GetParameterRequest
            {
                Name           = DEFAULT_OAUTH_PARAMETER_STORE_KEY,
                WithDecryption = true
            }).GetAwaiter().GetResult();

            return(response.Parameter.Value);
        }
 public ElectionConfigurationSource(IOptions <AppConfig> config, IHostingEnvironment hostingEnvironment)
 {
     _hostingEnvironment   = hostingEnvironment;
     _config               = config.Value;
     _amazonSettingsClient = new AmazonSimpleSystemsManagementClient();
     _parameterStoreName   = Consts.PARAMETER_STORE_NAME;
     if (hostingEnvironment.IsDevelopment())
     {
         _parameterStoreName += "-dev";
     }
 }
Exemplo n.º 18
0
        public string GetValue(string parameter)
        {
            var ssmClient = new AmazonSimpleSystemsManagementClient(_region);

            var response = ssmClient.GetParameterAsync(new GetParameterRequest
            {
                Name           = parameter,
                WithDecryption = true
            }).Result;

            return(response.Parameter.Value);
        }
Exemplo n.º 19
0
        public async Task <string> GetValueAsync(string parameter)
        {
            var ssmClient = new AmazonSimpleSystemsManagementClient(_region);

            var response = await ssmClient.GetParameterAsync(new GetParameterRequest
            {
                Name           = parameter,
                WithDecryption = true
            });

            return(response.Parameter.Value);
        }
Exemplo n.º 20
0
 async Task <GetParametersByPathResponse> IParameterStoreService.GetAllParameters(string parameterPath)
 {
     using (var client = new AmazonSimpleSystemsManagementClient())
     {
         return(await client.GetParametersByPathAsync(new GetParametersByPathRequest()
         {
             Path = parameterPath,
             WithDecryption = true,
             Recursive = true
         }));
     }
 }
Exemplo n.º 21
0
 public SettingsController(IHostEnvironment hostingEnvironment)
 {
     if (hostingEnvironment.IsDevelopment())
     {
         var systemsManagementConfig = new AmazonSimpleSystemsManagementConfig
         {
             ServiceURL = Consts.SSMServiceUrl,
             UseHttp    = true
         };
         _amazonSettingsClient = new AmazonSimpleSystemsManagementClient(new BasicAWSCredentials("abc", "def"), systemsManagementConfig);
     }
 }
Exemplo n.º 22
0
        /// <summary>
        /// Retrieve PAT/Password from AWS SSM(Simple Systems Manager)
        /// </summary>
        /// <param name="passwordParameter"></param>
        async Task <string> GetSourcePassword(string passwordParameter)
        {
            AmazonSimpleSystemsManagementClient ssmClient = new AmazonSimpleSystemsManagementClient();
            GetParameterRequest request = new GetParameterRequest();

            // TODO: revisit hardcoding parameter names/if these are actually passed into container as ENV variables
            request.Name           = passwordParameter;
            request.WithDecryption = true;
            GetParameterResponse parameterResponse = await ssmClient.GetParameterAsync(request);

            return(parameterResponse.Parameter.Value);
        }
        public static void Handler(SQSEvent sqsEvent, ILambdaContext context)
        {
            context.Logger.LogLine($"Received SQSEvent with {sqsEvent.Records.Count} SQSMessage record(s)");

            context.Logger.LogLine("Retrieving ApiKey");
            string apiKey = String.Empty;

            using (var ssmClient = new AmazonSimpleSystemsManagementClient())
            {
                var response = ssmClient.GetParameterAsync(
                    new Amazon.SimpleSystemsManagement.Model.GetParameterRequest
                {
                    Name           = Environment.GetEnvironmentVariable("Email__ServiceApiKeyName"),
                    WithDecryption = true
                }
                    ).GetAwaiter().GetResult();
                apiKey = response.Parameter.Value;
            }

            context.Logger.LogLine("Creating EmailService object");
            var emailService = new MailgunEmailService(new MailgunEmailService.Options
            {
                FromAddress   = Environment.GetEnvironmentVariable("Email__FromAddress"),
                MailDomain    = Environment.GetEnvironmentVariable("Email__MailDomain"),
                ServiceUrl    = Environment.GetEnvironmentVariable("Email__ServiceUrl"),
                ServiceApiKey = apiKey
            });

            foreach (var sqsMessage in sqsEvent.Records)
            {
                context.Logger.LogLine($"Processing message {sqsMessage.MessageId}");

                // Parse out recipient, subject, and email body
                var strArray  = sqsMessage.Body.Split(MessageSeparator, 3, StringSplitOptions.None);
                var recipient = strArray[0];
                context.Logger.LogLine($"({sqsMessage.MessageId}) To: {recipient}");
                var subject = strArray[1];
                context.Logger.LogLine($"({sqsMessage.MessageId}) Subject: {subject}");
                var body = strArray[2];
                context.Logger.LogLine($"({sqsMessage.MessageId}) Subject: {body}");

                // Send the e-mail
                var emailMessage = new Email.EmailMessage
                {
                    EmailAddress = recipient,
                    Subject      = subject,
                    Body         = body
                };
                var result = emailService.SendEmailAsync(emailMessage).GetAwaiter().GetResult();
                context.Logger.LogLine($"({sqsMessage.MessageId}) Email service returned success: {result.Success} details: {result.Details}");
            }
        }
Exemplo n.º 24
0
        /// <summary>
        /// パラメータストアにあるWebhook URLを取得
        /// </summary>
        /// <returns>Webhook URL</returns>
        private async Task <string> GetWebhookUrl()
        {
            var request = new GetParameterRequest()
            {
                Name           = WEBHOOK_URL,
                WithDecryption = true
            };

            using var ssmClient = new AmazonSimpleSystemsManagementClient(region);
            var response = await ssmClient.GetParameterAsync(request);

            return(response.Parameter.Value);
        }
Exemplo n.º 25
0
        public static IConfigurationBuilder AddParameterStoreConfig(
            this IConfigurationBuilder builder,
            List <string> paramNames)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var systemManagementClient = new AmazonSimpleSystemsManagementClient(RegionEndpoint.USWest2);

            return(builder.Add((IConfigurationSource) new ParameterStoreConfigurationSource(systemManagementClient, paramNames)));
        }
Exemplo n.º 26
0
        public async Task <string> Fetch(string key)
        {
            using (var client = new AmazonSimpleSystemsManagementClient(RegionEndpoint.EUWest2))
            {
                var request = new GetParameterRequest {
                    Name = key, WithDecryption = true
                };

                var response = await client.GetParameterAsync(request);

                return(response.Parameter.Value);
            }
        }
Exemplo n.º 27
0
        //Function to get parameters from the AWS parameter store
        public string GetAWSParameter(string input)
        {
            var parameterName = input;

            var ssmClient = new AmazonSimpleSystemsManagementClient(Amazon.RegionEndpoint.USEast2);
            var response  = ssmClient.GetParameterAsync(new GetParameterRequest
            {
                Name           = parameterName,
                WithDecryption = true
            });

            return(response.Result.Parameter.Value);
        }
Exemplo n.º 28
0
        internal string ResolveToValidTopcArn(string topicArnOrParameterKey)
        {
            if (topicArnOrParameterKey.StartsWith("arn:"))
            {
                return(topicArnOrParameterKey);
            }

            if (DateTime.Now >= _ResolveCacheValidUntil)
            {
                _ResolveCache           = new Dictionary <string, string>();
                _ResolveCacheValidUntil = DateTime.Now.AddMinutes(5);
            }

            lock (_ResolveCache) {
                foreach (var phName in this.TopicParameterKeyPlaceholderResolvers.Keys)
                {
                    string pt = "{" + phName + "}";
                    if (topicArnOrParameterKey.Contains(pt))
                    {
                        string pv = this.TopicParameterKeyPlaceholderResolvers[phName].Invoke();
                        topicArnOrParameterKey = topicArnOrParameterKey.Replace(pt, pv);
                    }
                }

                if (_ResolveCache.ContainsKey(topicArnOrParameterKey))
                {
                    return(_ResolveCache[topicArnOrParameterKey]);
                }

                if (topicArnOrParameterKey.Contains("{"))
                {
                    throw new Exception($"There is a unresolvable Placeholder within ParameterKey '{topicArnOrParameterKey}'!");
                }

                using (var client = new AmazonSimpleSystemsManagementClient(this.AwsCredentials, this.AwsRegion)) {
                    var request = new GetParameterRequest()
                    {
                        Name = topicArnOrParameterKey
                    };
                    var t = client.GetParameterAsync(request);
                    t.Wait();
                    if (t.IsCompleted && t.Result is object && t.Result.Parameter is object)
                    {
                        _ResolveCache.Add(topicArnOrParameterKey, t.Result.Parameter.Value);
                        return(t.Result.Parameter.Value);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 29
0
        /// <summary>
        /// An alternative is using the AWS services directly instead of .net SDK.
        /// </summary>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        private async Task <string> GetSecureParameter(string parameterName)
        {
            var client  = new AmazonSimpleSystemsManagementClient(RegionEndpoint.APSoutheast2);
            var request = new GetParametersRequest
            {
                Names = new List <string> {
                    parameterName
                }
            };
            var response = await client.GetParametersAsync(request);

            tmpString = response.Parameters.SingleOrDefault().Value;
            return(response.ToString());
        }
Exemplo n.º 30
0
        private async Task <string> PutSecureParameter(string parameterName, string value)
        {
            var client  = new AmazonSimpleSystemsManagementClient(RegionEndpoint.APSoutheast2);
            var request = new PutParameterRequest
            {
                Name      = parameterName,
                Overwrite = true,
                Value     = value,
                Type      = ParameterType.SecureString
            };
            var response = await client.PutParameterAsync(request);

            return(response.ToString());
        }