Skip to content

fix:Removing env var credentials provider as default. #1161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates.
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package software.amazon.lambda.powertools.core.internal;

public class LambdaConstants {
public static final String LAMBDA_FUNCTION_NAME_ENV = "AWS_LAMBDA_FUNCTION_NAME";
public static final String AWS_REGION_ENV = "AWS_REGION";
public static final String AWS_LAMBDA_INITIALIZATION_TYPE = "AWS_LAMBDA_INITIALIZATION_TYPE";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@
package software.amazon.lambda.powertools.idempotency;

public class Constants {
public static final String LAMBDA_FUNCTION_NAME_ENV = "AWS_LAMBDA_FUNCTION_NAME";
public static final String AWS_REGION_ENV = "AWS_REGION";
public static final String IDEMPOTENCY_DISABLED_ENV = "POWERTOOLS_IDEMPOTENCY_DISABLED";
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static software.amazon.lambda.powertools.idempotency.Constants.AWS_REGION_ENV;
import static software.amazon.lambda.powertools.core.internal.LambdaConstants.AWS_LAMBDA_INITIALIZATION_TYPE;
import static software.amazon.lambda.powertools.core.internal.LambdaConstants.AWS_REGION_ENV;
import static software.amazon.lambda.powertools.core.internal.LambdaConstants.LAMBDA_FUNCTION_NAME_ENV;
import static software.amazon.lambda.powertools.idempotency.persistence.DataRecord.Status.INPROGRESS;

/**
Expand Down Expand Up @@ -86,9 +88,17 @@ private DynamoDBPersistenceStore(String tableName,
String idempotencyDisabledEnv = System.getenv().get(Constants.IDEMPOTENCY_DISABLED_ENV);
if (idempotencyDisabledEnv == null || idempotencyDisabledEnv.equalsIgnoreCase("false")) {
DynamoDbClientBuilder ddbBuilder = DynamoDbClient.builder()
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.httpClient(UrlConnectionHttpClient.builder().build())
.region(Region.of(System.getenv(AWS_REGION_ENV)));

// AWS_LAMBDA_INITIALIZATION_TYPE has two values on-demand and snap-start
// when using snap-start mode, the env var creds provider isn't used and causes a fatal error if set
// fall back to the default provider chain if the mode is anything other than on-demand.
String initializationType = System.getenv().get(AWS_LAMBDA_INITIALIZATION_TYPE);
if (initializationType != null && initializationType.equals("on-demand")) {
ddbBuilder.credentialsProvider(EnvironmentVariableCredentialsProvider.create());
}

this.dynamoDbClient = ddbBuilder.build();
} else {
// we do not want to create a DynamoDbClient if idempotency is disabled
Expand Down Expand Up @@ -249,7 +259,7 @@ public static Builder builder() {
* You can also set a custom {@link DynamoDbClient} for further tuning.
*/
public static class Builder {
private static final String funcEnv = System.getenv(Constants.LAMBDA_FUNCTION_NAME_ENV);
private static final String funcEnv = System.getenv(LAMBDA_FUNCTION_NAME_ENV);

private String tableName;
private String keyAttr = "id";
Expand Down
4 changes: 4 additions & 0 deletions powertools-parameters/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
</distributionManagement>

<dependencies>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-core</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ssm</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ssm.SsmClient;
import software.amazon.awssdk.services.ssm.SsmClientBuilder;
import software.amazon.awssdk.services.ssm.model.GetParameterRequest;
import software.amazon.awssdk.services.ssm.model.GetParametersByPathRequest;
import software.amazon.awssdk.services.ssm.model.GetParametersByPathResponse;
Expand All @@ -30,6 +31,8 @@
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;
import software.amazon.lambda.powertools.parameters.transform.Transformer;

import static software.amazon.lambda.powertools.core.internal.LambdaConstants.AWS_LAMBDA_INITIALIZATION_TYPE;

/**
* AWS System Manager Parameter Store Provider <br/><br/>
*
Expand Down Expand Up @@ -75,20 +78,6 @@ public class SSMProvider extends BaseProvider {
private boolean decrypt = false;
private boolean recursive = false;

/**
* Default constructor with default {@link SsmClient}. <br/>
* Use when you don't need to customize region or any other attribute of the client.<br/><br/>
* <p>
* Use the {@link SSMProvider.Builder} to create an instance of it.
*/
SSMProvider(CacheManager cacheManager) {
this(cacheManager, SsmClient.builder()
.httpClientBuilder(UrlConnectionHttpClient.builder())
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())))
.build());
}

/**
* Constructor with custom {@link SsmClient}. <br/>
* Use when you need to customize region or any other attribute of the client.<br/><br/>
Expand Down Expand Up @@ -253,11 +242,24 @@ public SSMProvider build() {
throw new IllegalStateException("No CacheManager provided, please provide one");
}
SSMProvider provider;
if (client != null) {
provider = new SSMProvider(cacheManager, client);
} else {
provider = new SSMProvider(cacheManager);
if (client == null) {
SsmClientBuilder ssmClientBuilder = SsmClient.builder()
.httpClientBuilder(UrlConnectionHttpClient.builder())
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())));

// AWS_LAMBDA_INITIALIZATION_TYPE has two values on-demand and snap-start
// when using snap-start mode, the env var creds provider isn't used and causes a fatal error if set
// fall back to the default provider chain if the mode is anything other than on-demand.
String initializationType = System.getenv().get(AWS_LAMBDA_INITIALIZATION_TYPE);
if (initializationType != null && initializationType.equals("on-demand")) {
ssmClientBuilder.credentialsProvider(EnvironmentVariableCredentialsProvider.create());
}

client = ssmClientBuilder.build();
}

provider = new SSMProvider(cacheManager, client);

if (transformationManager != null) {
provider.setTransformationManager(transformationManager);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder;
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;
import software.amazon.lambda.powertools.parameters.transform.Transformer;

import static java.nio.charset.StandardCharsets.UTF_8;
import static software.amazon.lambda.powertools.core.internal.LambdaConstants.AWS_LAMBDA_INITIALIZATION_TYPE;

/**
* AWS Secrets Manager Parameter Provider<br/><br/>
Expand Down Expand Up @@ -57,20 +59,6 @@ public class SecretsProvider extends BaseProvider {

private final SecretsManagerClient client;

/**
* Default constructor with default {@link SecretsManagerClient}. <br/>
* Use when you don't need to customize region or any other attribute of the client.<br/><br/>
*
* Use the {@link Builder} to create an instance of it.
*/
SecretsProvider(CacheManager cacheManager) {
this(cacheManager, SecretsManagerClient.builder()
.httpClientBuilder(UrlConnectionHttpClient.builder())
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())))
.build());
}

/**
* Constructor with custom {@link SecretsManagerClient}. <br/>
* Use when you need to customize region or any other attribute of the client.<br/><br/>
Expand Down Expand Up @@ -162,11 +150,24 @@ public SecretsProvider build() {
throw new IllegalStateException("No CacheManager provided, please provide one");
}
SecretsProvider provider;
if (client != null) {
provider = new SecretsProvider(cacheManager, client);
} else {
provider = new SecretsProvider(cacheManager);
if (client == null) {
SecretsManagerClientBuilder secretsManagerClientBuilder = SecretsManagerClient.builder()
.httpClientBuilder(UrlConnectionHttpClient.builder())
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())));

// AWS_LAMBDA_INITIALIZATION_TYPE has two values on-demand and snap-start
// when using snap-start mode, the env var creds provider isn't used and causes a fatal error if set
// fall back to the default provider chain if the mode is anything other than on-demand.
String initializationType = System.getenv().get(AWS_LAMBDA_INITIALIZATION_TYPE);
if (initializationType != null && initializationType.equals("on-demand")) {
secretsManagerClientBuilder.credentialsProvider(EnvironmentVariableCredentialsProvider.create());
}

client = secretsManagerClientBuilder.build();
}

provider = new SecretsProvider(cacheManager, client);

if (transformationManager != null) {
provider.setTransformationManager(transformationManager);
}
Expand Down