コード例 #1
0
ファイル: Function.cs プロジェクト: rodboza/aws-sandbox
        /// <summary>
        /// A simple function that takes a string and does a ToUpper
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public string FunctionHandler(JsonElement input, ILambdaContext context)
        {
            try
            {
                //pegando das variáveis de ambiente a Region defaul
                var regionAws = Environment.GetEnvironmentVariable("AWS_REGION");
                //requisitando ao AWS Secrets Manager a chave de acesso no site open weather
                string apiId = AwsSecret.GetSecret("openweather", regionAws).GetProperty("apiid").ToString();
                string local = input.GetProperty("local").ToString();
                // faz request no openweather
                var climaJson = getOpenWeather(apiId, local);
                context.Logger.LogLine($" *** {JsonSerializer.Serialize(climaJson.ToModelClima())} ***");
                climaJson.ToModelClima().Salvar();
            }
            catch (Exception e)
            {
                context.Logger.LogLine($">>>>>>>>> inicio LOG");
                context.Logger.LogLine($">>>>>>>>> tipo {input.GetType().ToString()}");
                context.Logger.LogLine($">>>>>>>>> Conteudo {input.ToString()}");
                context.Logger.LogLine($">>>>>>>>> ENVIRONMENT VARIABLES: " + JsonSerializer.Serialize(System.Environment.GetEnvironmentVariables()));
                context.Logger.LogLine($">>>>>>>>> Contex: " + JsonSerializer.Serialize(context));
                context.Logger.LogLine($">>>>>>>>> Exception: " + JsonSerializer.Serialize(e.ToString()));
                context.Logger.LogLine($">>>>>>>>> Fim  LOG");

                throw;
            }
            return("OK");
        }
コード例 #2
0
        public void ConstructorHappyPath3()
        {
            var secret = new AwsSecret("configurationKey", "secretId", "secretKey");

            secret.ConfigurationKey.Should().Be("configurationKey");
            secret.SecretId.Should().Be("secretId");
            secret.SecretKey.Should().Be("secretKey");
            secret.SecretsManager.Should().BeSameAs(AwsSecret.DefaultSecretsManager);
        }
コード例 #3
0
        public void ConstructorHappyPath2()
        {
            var secretsManager = new Mock <IAmazonSecretsManager>().Object;

            var secret = new AwsSecret("configurationKey", "secretId", secretsManager: secretsManager);

            secret.ConfigurationKey.Should().Be("configurationKey");
            secret.SecretId.Should().Be("secretId");
            secret.SecretKey.Should().BeNull();
            secret.SecretsManager.Should().BeSameAs(secretsManager);
        }
コード例 #4
0
        public void GetValueMethodSadPath1()
        {
            var mockSecretsManager = new Mock <IAmazonSecretsManager>();

            mockSecretsManager.Setup(m => m.GetSecretValueAsync(It.IsAny <GetSecretValueRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync((GetSecretValueResponse)null);

            var secret = new AwsSecret("myConfigurationKey", "mySecretId", "myAwsSecretKey", mockSecretsManager.Object);

            Action act = () => secret.GetValue();

            act.Should().ThrowExactly <KeyNotFoundException>().WithMessage("*Response was null.*");
        }
コード例 #5
0
        public void GetValueMethodSadPath2()
        {
            var response = new GetSecretValueResponse
            {
                SecretString = "{}"
            };

            var mockSecretsManager = new Mock <IAmazonSecretsManager>();

            mockSecretsManager.Setup(m => m.GetSecretValueAsync(It.IsAny <GetSecretValueRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(response);

            var secret = new AwsSecret("myConfigurationKey", "mySecretId", "myAwsSecretKey", mockSecretsManager.Object);

            Action act = () => secret.GetValue();

            act.Should().ThrowExactly <KeyNotFoundException>().WithMessage("*Response did not contain item with the name 'myAwsSecretKey'.*");
        }
コード例 #6
0
        public void GetValueMethodHappyPath2()
        {
            var response = new GetSecretValueResponse
            {
                SecretString = "mySecretString"
            };

            var mockSecretsManager = new Mock <IAmazonSecretsManager>();

            mockSecretsManager.Setup(m => m.GetSecretValueAsync(It.IsAny <GetSecretValueRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(response);

            var secret = new AwsSecret("myConfigurationKey", "mySecretId", secretsManager: mockSecretsManager.Object);

            var value = secret.GetValue();

            value.Should().Be("mySecretString");

            mockSecretsManager.Verify(m => m.GetSecretValueAsync(
                                          It.Is <GetSecretValueRequest>(r => r.SecretId == "mySecretId"), It.IsAny <CancellationToken>()),
                                      Times.Once());
        }
コード例 #7
0
        public void GetValueMethodHappyPath3()
        {
            var buffer = Encoding.UTF8.GetBytes("Hello, world!");

            var response = new GetSecretValueResponse
            {
                SecretBinary = new MemoryStream(buffer)
            };

            var mockSecretsManager = new Mock <IAmazonSecretsManager>();

            mockSecretsManager.Setup(m => m.GetSecretValueAsync(It.IsAny <GetSecretValueRequest>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(response);

            var secret = new AwsSecret("myConfigurationKey", "mySecretId", secretsManager: mockSecretsManager.Object);

            var value = secret.GetValue();

            value.Should().Be(Convert.ToBase64String(buffer));

            mockSecretsManager.Verify(m => m.GetSecretValueAsync(
                                          It.Is <GetSecretValueRequest>(r => r.SecretId == "mySecretId"), It.IsAny <CancellationToken>()),
                                      Times.Once());
        }
コード例 #8
0
 public AwsSecretTest()
 {
     client          = repository.Create <IAmazonSecretsManager>();
     systemUnderTest = new AwsSecret(SecretId, RegionEndpoint.USEast2, client.Object);
 }