Skip to content

Commit 5de4ee1

Browse files
committed
fix other client usage of env var provider
1 parent ac4638c commit 5de4ee1

File tree

6 files changed

+69
-43
lines changed

6 files changed

+69
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

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

-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +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";
20-
public static final String AWS_LAMBDA_INITIALIZATION_TYPE = "AWS_LAMBDA_INITIALIZATION_TYPE";
2118
}

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
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;
3840
import static software.amazon.lambda.powertools.idempotency.persistence.DataRecord.Status.INPROGRESS;
3941

4042
/**
@@ -90,9 +92,9 @@ private DynamoDBPersistenceStore(String tableName,
9092
.region(Region.of(System.getenv(AWS_REGION_ENV)));
9193

9294
// AWS_LAMBDA_INITIALIZATION_TYPE has two values on-demand and snap-start
93-
// when using snap-start mode, the env var creds provider isn't used and causes a fatal error
95+
// when using snap-start mode, the env var creds provider isn't used and causes a fatal error if set
9496
// fall back to the default provider chain if the mode is anything other than on-demand.
95-
String initializationType = System.getenv().get(Constants.AWS_LAMBDA_INITIALIZATION_TYPE);
97+
String initializationType = System.getenv().get(AWS_LAMBDA_INITIALIZATION_TYPE);
9698
if (initializationType != null && initializationType.equals("on-demand")) {
9799
ddbBuilder.credentialsProvider(EnvironmentVariableCredentialsProvider.create());
98100
}
@@ -257,7 +259,7 @@ public static Builder builder() {
257259
* You can also set a custom {@link DynamoDbClient} for further tuning.
258260
*/
259261
public static class Builder {
260-
private static final String funcEnv = System.getenv(Constants.LAMBDA_FUNCTION_NAME_ENV);
262+
private static final String funcEnv = System.getenv(LAMBDA_FUNCTION_NAME_ENV);
261263

262264
private String tableName;
263265
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

+20-18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
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;
@@ -30,6 +31,8 @@
3031
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;
3132
import software.amazon.lambda.powertools.parameters.transform.Transformer;
3233

34+
import static software.amazon.lambda.powertools.core.internal.LambdaConstants.AWS_LAMBDA_INITIALIZATION_TYPE;
35+
3336
/**
3437
* AWS System Manager Parameter Store Provider <br/><br/>
3538
*
@@ -75,20 +78,6 @@ public class SSMProvider extends BaseProvider {
7578
private boolean decrypt = false;
7679
private boolean recursive = false;
7780

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-
9281
/**
9382
* Constructor with custom {@link SsmClient}. <br/>
9483
* Use when you need to customize region or any other attribute of the client.<br/><br/>
@@ -253,11 +242,24 @@ public SSMProvider build() {
253242
throw new IllegalStateException("No CacheManager provided, please provide one");
254243
}
255244
SSMProvider provider;
256-
if (client != null) {
257-
provider = new SSMProvider(cacheManager, client);
258-
} else {
259-
provider = new SSMProvider(cacheManager);
245+
if (client == null) {
246+
SsmClientBuilder ssmClientBuilder = SsmClient.builder()
247+
.httpClientBuilder(UrlConnectionHttpClient.builder())
248+
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())));
249+
250+
// AWS_LAMBDA_INITIALIZATION_TYPE has two values on-demand and snap-start
251+
// when using snap-start mode, the env var creds provider isn't used and causes a fatal error if set
252+
// fall back to the default provider chain if the mode is anything other than on-demand.
253+
String initializationType = System.getenv().get(AWS_LAMBDA_INITIALIZATION_TYPE);
254+
if (initializationType != null && initializationType.equals("on-demand")) {
255+
ssmClientBuilder.credentialsProvider(EnvironmentVariableCredentialsProvider.create());
256+
}
257+
258+
client = ssmClientBuilder.build();
260259
}
260+
261+
provider = new SSMProvider(cacheManager, client);
262+
261263
if (transformationManager != null) {
262264
provider.setTransformationManager(transformationManager);
263265
}

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

+19-18
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
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;
2627
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
2728
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;
2829
import software.amazon.lambda.powertools.parameters.transform.Transformer;
2930

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

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

5860
private final SecretsManagerClient client;
5961

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-
7462
/**
7563
* Constructor with custom {@link SecretsManagerClient}. <br/>
7664
* Use when you need to customize region or any other attribute of the client.<br/><br/>
@@ -162,11 +150,24 @@ public SecretsProvider build() {
162150
throw new IllegalStateException("No CacheManager provided, please provide one");
163151
}
164152
SecretsProvider provider;
165-
if (client != null) {
166-
provider = new SecretsProvider(cacheManager, client);
167-
} else {
168-
provider = new SecretsProvider(cacheManager);
153+
if (client == null) {
154+
SecretsManagerClientBuilder secretsManagerClientBuilder = SecretsManagerClient.builder()
155+
.httpClientBuilder(UrlConnectionHttpClient.builder())
156+
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())));
157+
158+
// AWS_LAMBDA_INITIALIZATION_TYPE has two values on-demand and snap-start
159+
// when using snap-start mode, the env var creds provider isn't used and causes a fatal error if set
160+
// fall back to the default provider chain if the mode is anything other than on-demand.
161+
String initializationType = System.getenv().get(AWS_LAMBDA_INITIALIZATION_TYPE);
162+
if (initializationType != null && initializationType.equals("on-demand")) {
163+
secretsManagerClientBuilder.credentialsProvider(EnvironmentVariableCredentialsProvider.create());
164+
}
165+
166+
client = secretsManagerClientBuilder.build();
169167
}
168+
169+
provider = new SecretsProvider(cacheManager, client);
170+
170171
if (transformationManager != null) {
171172
provider.setTransformationManager(transformationManager);
172173
}

0 commit comments

Comments
 (0)