1 /*
2  * Copyright 2012-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15 package com.amazonaws.auth;
16
17 import static com.amazonaws.SDKGlobalConfiguration.ACCESS_KEY_ENV_VAR;
18 import static com.amazonaws.SDKGlobalConfiguration.ALTERNATE_ACCESS_KEY_ENV_VAR;
19 import static com.amazonaws.SDKGlobalConfiguration.ALTERNATE_SECRET_KEY_ENV_VAR;
20 import static com.amazonaws.SDKGlobalConfiguration.AWS_SESSION_TOKEN_ENV_VAR;
21 import static com.amazonaws.SDKGlobalConfiguration.SECRET_KEY_ENV_VAR;
22
23 import com.amazonaws.SdkClientException;
24 import com.amazonaws.util.StringUtils;
25
26 /**
27  * {@link AWSCredentialsProvider} implementation that provides credentials by looking at the: <code>AWS_ACCESS_KEY_ID</code> (or
28  * <code>AWS_ACCESS_KEY</code>) and <code>AWS_SECRET_KEY</code> (or <code>AWS_SECRET_ACCESS_KEY</code>) environment variables. If
29  * the <code>AWS_SESSION_TOKEN</code> environment variable is also set then temporary credentials will be used.
30  */

31 public class EnvironmentVariableCredentialsProvider implements AWSCredentialsProvider {
32     @Override
33     public AWSCredentials getCredentials() {
34         String accessKey = System.getenv(ACCESS_KEY_ENV_VAR);
35         if (accessKey == null) {
36             accessKey = System.getenv(ALTERNATE_ACCESS_KEY_ENV_VAR);
37         }
38
39         String secretKey = System.getenv(SECRET_KEY_ENV_VAR);
40         if (secretKey == null) {
41             secretKey = System.getenv(ALTERNATE_SECRET_KEY_ENV_VAR);
42         }
43
44         accessKey = StringUtils.trim(accessKey);
45         secretKey = StringUtils.trim(secretKey);
46         String sessionToken = StringUtils.trim(System.getenv(AWS_SESSION_TOKEN_ENV_VAR));
47
48         if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey)) {
49
50             throw new SdkClientException(
51                     "Unable to load AWS credentials from environment variables " +
52                     "(" + ACCESS_KEY_ENV_VAR + " (or " + ALTERNATE_ACCESS_KEY_ENV_VAR + ") and " +
53                     SECRET_KEY_ENV_VAR + " (or " + ALTERNATE_SECRET_KEY_ENV_VAR + "))");
54         }
55
56         return sessionToken == null ?
57                 new BasicAWSCredentials(accessKey, secretKey)
58                 :
59                 new BasicSessionCredentials(accessKey, secretKey, sessionToken);
60     }
61
62     @Override
63     public void refresh() {
64     }
65
66     @Override
67     public String toString() {
68         return getClass().getSimpleName();
69     }
70 }