Skip to content

Commit 6ce5359

Browse files
authored
fix:Removing env var credentials provider as default. (#1161)
SnapStart uses a different credentials provider so when it is hardcoded like this, the default will fail.
1 parent 0fd2ecf commit 6ce5359

File tree

7 files changed

+83
-43
lines changed

7 files changed

+83
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
package software.amazon.lambda.powertools.core.internal;
15+
16+
public class LambdaConstants {
17+
public static final String LAMBDA_FUNCTION_NAME_ENV = "AWS_LAMBDA_FUNCTION_NAME";
18+
public static final String AWS_REGION_ENV = "AWS_REGION";
19+
public static final String AWS_LAMBDA_INITIALIZATION_TYPE = "AWS_LAMBDA_INITIALIZATION_TYPE";
20+
public static final String ON_DEMAND = "on-demand";
21+
}

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/Constants.java

-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,5 @@
1414
package software.amazon.lambda.powertools.idempotency;
1515

1616
public class Constants {
17-
public static final String LAMBDA_FUNCTION_NAME_ENV = "AWS_LAMBDA_FUNCTION_NAME";
18-
public static final String AWS_REGION_ENV = "AWS_REGION";
1917
public static final String IDEMPOTENCY_DISABLED_ENV = "POWERTOOLS_IDEMPOTENCY_DISABLED";
2018
}

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/persistence/BasePersistenceStore.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.slf4j.Logger;
2121
import org.slf4j.LoggerFactory;
2222
import software.amazon.awssdk.utils.StringUtils;
23-
import software.amazon.lambda.powertools.idempotency.Constants;
2423
import software.amazon.lambda.powertools.idempotency.IdempotencyConfig;
2524
import software.amazon.lambda.powertools.idempotency.exceptions.IdempotencyItemAlreadyExistsException;
2625
import software.amazon.lambda.powertools.idempotency.exceptions.IdempotencyItemNotFoundException;
@@ -43,6 +42,8 @@
4342
import java.util.Spliterator;
4443
import java.util.stream.StreamSupport;
4544

45+
import static software.amazon.lambda.powertools.core.internal.LambdaConstants.LAMBDA_FUNCTION_NAME_ENV;
46+
4647
/**
4748
* Persistence layer that will store the idempotency result.
4849
* Base implementation. See {@link DynamoDBPersistenceStore} for an implementation (default one)
@@ -71,7 +72,7 @@ public abstract class BasePersistenceStore implements PersistenceStore {
7172
* @param functionName The name of the function being decorated
7273
*/
7374
public void configure(IdempotencyConfig config, String functionName) {
74-
String funcEnv = System.getenv(Constants.LAMBDA_FUNCTION_NAME_ENV);
75+
String funcEnv = System.getenv(LAMBDA_FUNCTION_NAME_ENV);
7576
this.functionName = funcEnv != null ? funcEnv : "testFunction";
7677
if (!StringUtils.isEmpty(functionName)) {
7778
this.functionName += "." + functionName;

powertools-idempotency/src/main/java/software/amazon/lambda/powertools/idempotency/persistence/DynamoDBPersistenceStore.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
import java.util.stream.Collectors;
3535
import java.util.stream.Stream;
3636

37-
import static software.amazon.lambda.powertools.idempotency.Constants.AWS_REGION_ENV;
37+
import static software.amazon.lambda.powertools.core.internal.LambdaConstants.AWS_LAMBDA_INITIALIZATION_TYPE;
38+
import static software.amazon.lambda.powertools.core.internal.LambdaConstants.AWS_REGION_ENV;
39+
import static software.amazon.lambda.powertools.core.internal.LambdaConstants.LAMBDA_FUNCTION_NAME_ENV;
40+
import static software.amazon.lambda.powertools.core.internal.LambdaConstants.ON_DEMAND;
3841
import static software.amazon.lambda.powertools.idempotency.persistence.DataRecord.Status.INPROGRESS;
3942

4043
/**
@@ -86,9 +89,17 @@ private DynamoDBPersistenceStore(String tableName,
8689
String idempotencyDisabledEnv = System.getenv().get(Constants.IDEMPOTENCY_DISABLED_ENV);
8790
if (idempotencyDisabledEnv == null || idempotencyDisabledEnv.equalsIgnoreCase("false")) {
8891
DynamoDbClientBuilder ddbBuilder = DynamoDbClient.builder()
89-
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
9092
.httpClient(UrlConnectionHttpClient.builder().build())
9193
.region(Region.of(System.getenv(AWS_REGION_ENV)));
94+
95+
// AWS_LAMBDA_INITIALIZATION_TYPE has two values on-demand and snap-start
96+
// when using snap-start mode, the env var creds provider isn't used and causes a fatal error if set
97+
// fall back to the default provider chain if the mode is anything other than on-demand.
98+
String initializationType = System.getenv().get(AWS_LAMBDA_INITIALIZATION_TYPE);
99+
if (initializationType != null && initializationType.equals(ON_DEMAND)) {
100+
ddbBuilder.credentialsProvider(EnvironmentVariableCredentialsProvider.create());
101+
}
102+
92103
this.dynamoDbClient = ddbBuilder.build();
93104
} else {
94105
// we do not want to create a DynamoDbClient if idempotency is disabled
@@ -249,7 +260,7 @@ public static Builder builder() {
249260
* You can also set a custom {@link DynamoDbClient} for further tuning.
250261
*/
251262
public static class Builder {
252-
private static final String funcEnv = System.getenv(Constants.LAMBDA_FUNCTION_NAME_ENV);
263+
private static final String funcEnv = System.getenv(LAMBDA_FUNCTION_NAME_ENV);
253264

254265
private String tableName;
255266
private String keyAttr = "id";

powertools-parameters/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
</distributionManagement>
4242

4343
<dependencies>
44+
<dependency>
45+
<groupId>software.amazon.lambda</groupId>
46+
<artifactId>powertools-core</artifactId>
47+
</dependency>
4448
<dependency>
4549
<groupId>software.amazon.awssdk</groupId>
4650
<artifactId>ssm</artifactId>

powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/SSMProvider.java

+21-18
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@
2222
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
2323
import software.amazon.awssdk.regions.Region;
2424
import software.amazon.awssdk.services.ssm.SsmClient;
25+
import software.amazon.awssdk.services.ssm.SsmClientBuilder;
2526
import software.amazon.awssdk.services.ssm.model.GetParameterRequest;
2627
import software.amazon.awssdk.services.ssm.model.GetParametersByPathRequest;
2728
import software.amazon.awssdk.services.ssm.model.GetParametersByPathResponse;
2829
import software.amazon.awssdk.utils.StringUtils;
30+
import software.amazon.lambda.powertools.core.internal.LambdaConstants;
2931
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
3032
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;
3133
import software.amazon.lambda.powertools.parameters.transform.Transformer;
3234

35+
import static software.amazon.lambda.powertools.core.internal.LambdaConstants.AWS_LAMBDA_INITIALIZATION_TYPE;
36+
3337
/**
3438
* AWS System Manager Parameter Store Provider <br/><br/>
3539
*
@@ -75,20 +79,6 @@ public class SSMProvider extends BaseProvider {
7579
private boolean decrypt = false;
7680
private boolean recursive = false;
7781

78-
/**
79-
* Default constructor with default {@link SsmClient}. <br/>
80-
* Use when you don't need to customize region or any other attribute of the client.<br/><br/>
81-
* <p>
82-
* Use the {@link SSMProvider.Builder} to create an instance of it.
83-
*/
84-
SSMProvider(CacheManager cacheManager) {
85-
this(cacheManager, SsmClient.builder()
86-
.httpClientBuilder(UrlConnectionHttpClient.builder())
87-
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
88-
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())))
89-
.build());
90-
}
91-
9282
/**
9383
* Constructor with custom {@link SsmClient}. <br/>
9484
* Use when you need to customize region or any other attribute of the client.<br/><br/>
@@ -253,11 +243,24 @@ public SSMProvider build() {
253243
throw new IllegalStateException("No CacheManager provided, please provide one");
254244
}
255245
SSMProvider provider;
256-
if (client != null) {
257-
provider = new SSMProvider(cacheManager, client);
258-
} else {
259-
provider = new SSMProvider(cacheManager);
246+
if (client == null) {
247+
SsmClientBuilder ssmClientBuilder = SsmClient.builder()
248+
.httpClientBuilder(UrlConnectionHttpClient.builder())
249+
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())));
250+
251+
// AWS_LAMBDA_INITIALIZATION_TYPE has two values on-demand and snap-start
252+
// when using snap-start mode, the env var creds provider isn't used and causes a fatal error if set
253+
// fall back to the default provider chain if the mode is anything other than on-demand.
254+
String initializationType = System.getenv().get(AWS_LAMBDA_INITIALIZATION_TYPE);
255+
if (initializationType != null && initializationType.equals(LambdaConstants.ON_DEMAND)) {
256+
ssmClientBuilder.credentialsProvider(EnvironmentVariableCredentialsProvider.create());
257+
}
258+
259+
client = ssmClientBuilder.build();
260260
}
261+
262+
provider = new SSMProvider(cacheManager, client);
263+
261264
if (transformationManager != null) {
262265
provider.setTransformationManager(transformationManager);
263266
}

powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/SecretsProvider.java

+20-18
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
2323
import software.amazon.awssdk.regions.Region;
2424
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
25+
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder;
2526
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
27+
import software.amazon.lambda.powertools.core.internal.LambdaConstants;
2628
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
2729
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;
2830
import software.amazon.lambda.powertools.parameters.transform.Transformer;
2931

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

3235
/**
3336
* AWS Secrets Manager Parameter Provider<br/><br/>
@@ -57,20 +60,6 @@ public class SecretsProvider extends BaseProvider {
5760

5861
private final SecretsManagerClient client;
5962

60-
/**
61-
* Default constructor with default {@link SecretsManagerClient}. <br/>
62-
* Use when you don't need to customize region or any other attribute of the client.<br/><br/>
63-
*
64-
* Use the {@link Builder} to create an instance of it.
65-
*/
66-
SecretsProvider(CacheManager cacheManager) {
67-
this(cacheManager, SecretsManagerClient.builder()
68-
.httpClientBuilder(UrlConnectionHttpClient.builder())
69-
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
70-
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())))
71-
.build());
72-
}
73-
7463
/**
7564
* Constructor with custom {@link SecretsManagerClient}. <br/>
7665
* Use when you need to customize region or any other attribute of the client.<br/><br/>
@@ -162,11 +151,24 @@ public SecretsProvider build() {
162151
throw new IllegalStateException("No CacheManager provided, please provide one");
163152
}
164153
SecretsProvider provider;
165-
if (client != null) {
166-
provider = new SecretsProvider(cacheManager, client);
167-
} else {
168-
provider = new SecretsProvider(cacheManager);
154+
if (client == null) {
155+
SecretsManagerClientBuilder secretsManagerClientBuilder = SecretsManagerClient.builder()
156+
.httpClientBuilder(UrlConnectionHttpClient.builder())
157+
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())));
158+
159+
// AWS_LAMBDA_INITIALIZATION_TYPE has two values on-demand and snap-start
160+
// when using snap-start mode, the env var creds provider isn't used and causes a fatal error if set
161+
// fall back to the default provider chain if the mode is anything other than on-demand.
162+
String initializationType = System.getenv().get(AWS_LAMBDA_INITIALIZATION_TYPE);
163+
if (initializationType != null && initializationType.equals(LambdaConstants.ON_DEMAND)) {
164+
secretsManagerClientBuilder.credentialsProvider(EnvironmentVariableCredentialsProvider.create());
165+
}
166+
167+
client = secretsManagerClientBuilder.build();
169168
}
169+
170+
provider = new SecretsProvider(cacheManager, client);
171+
170172
if (transformationManager != null) {
171173
provider.setTransformationManager(transformationManager);
172174
}

0 commit comments

Comments
 (0)