1 /*
2 * Copyright 2011-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 * You may obtain a copy of the License at:
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
11 * OR CONDITIONS OF ANY KIND, either express or implied. See the
12 * License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package com.amazonaws.auth;
16
17 import static com.amazonaws.auth.ContainerCredentialsProvider.CONTAINER_CREDENTIALS_FULL_URI;
18 import static com.amazonaws.auth.ContainerCredentialsProvider.ECS_CONTAINER_CREDENTIALS_PATH;
19
20 import com.amazonaws.auth.ContainerCredentialsProvider.ECSCredentialsEndpointProvider;
21 import com.amazonaws.auth.ContainerCredentialsProvider.FullUriCredentialsEndpointProvider;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /**
26 * <p>
27 * {@link AWSCredentialsProvider} that loads credentials from an Amazon Container (e.g. EC2)
28 *
29 * Credentials are solved in the following order:
30 * <ol>
31 * <li>
32 * If environment variable "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" is
33 * set (typically on EC2) it is used to hit the metadata service at the following endpoint: http://169.254.170.2
34 * </li>
35 * <li>
36 * If environment variable "AWS_CONTAINER_CREDENTIALS_FULL_URI" is
37 * set it is used to hit a metadata service at that URI. <br/> Optionally an authorization token can be included
38 * in the "Authorization" header of the request by setting the "AWS_CONTAINER_AUTHORIZATION_TOKEN" environment variable.
39 * </li>
40 * <li>
41 * If neither of the above environment variables are specified credentials are attempted to be loaded from Amazon EC2
42 * Instance Metadata Service using the {@link InstanceProfileCredentialsProvider}.
43 * </li>
44 * </ol>
45 */
46 public class EC2ContainerCredentialsProviderWrapper implements AWSCredentialsProvider {
47
48 private static final Log LOG = LogFactory.getLog(EC2ContainerCredentialsProviderWrapper.class);
49
50 private final AWSCredentialsProvider provider;
51
52 public EC2ContainerCredentialsProviderWrapper() {
53 provider = initializeProvider();
54 }
55
56 private AWSCredentialsProvider initializeProvider() {
57 try {
58 if (System.getenv(ECS_CONTAINER_CREDENTIALS_PATH) != null) {
59 return new ContainerCredentialsProvider(new ECSCredentialsEndpointProvider());
60 }
61 if (System.getenv(CONTAINER_CREDENTIALS_FULL_URI) != null) {
62 return new ContainerCredentialsProvider(new FullUriCredentialsEndpointProvider());
63 }
64 return InstanceProfileCredentialsProvider.getInstance();
65 } catch (SecurityException securityException) {
66 LOG.debug("Security manager did not allow access to the ECS credentials environment variable " + ECS_CONTAINER_CREDENTIALS_PATH +
67 "or the container full URI environment variable " + CONTAINER_CREDENTIALS_FULL_URI
68 + ". Please provide access to this environment variable if you want to load credentials from ECS Container.");
69 return InstanceProfileCredentialsProvider.getInstance();
70 }
71 }
72
73 @Override
74 public AWSCredentials getCredentials() {
75 return provider.getCredentials();
76 }
77
78 @Override
79 public void refresh() {
80 provider.refresh();
81 }
82 }
83