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 * 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.profile.internal;
16
17 import com.amazonaws.annotation.Immutable;
18 import com.amazonaws.annotation.SdkInternalApi;
19 import com.amazonaws.util.StringUtils;
20
21 /**
22 * Loads profile name from the usual places or uses the default profile name.
23 */
24 @SdkInternalApi
25 @Immutable
26 public class AwsProfileNameLoader {
27
28 /**
29 * Name of the default profile as specified in the configuration file.
30 */
31 public static final String DEFAULT_PROFILE_NAME = "default";
32
33 /**
34 * Environment variable name for overriding the default AWS profile
35 */
36 public static final String AWS_PROFILE_ENVIRONMENT_VARIABLE = "AWS_PROFILE";
37
38 /**
39 * System property name for overriding the default AWS profile
40 */
41 public static final String AWS_PROFILE_SYSTEM_PROPERTY = "aws.profile";
42
43 public static final AwsProfileNameLoader INSTANCE = new AwsProfileNameLoader();
44
45 private AwsProfileNameLoader() {
46 }
47
48 /**
49 * TODO The order would make more sense as System Property, Environment Variable, Default
50 * Profile name but we have to keep the current order for backwards compatiblity. Consider
51 * changing this in a future major version.
52 */
53 public final String loadProfileName() {
54 final String profileEnvVarOverride = getEnvProfileName();
55 if (!StringUtils.isNullOrEmpty(profileEnvVarOverride)) {
56 return profileEnvVarOverride;
57 } else {
58 final String profileSysPropOverride = getSysPropertyProfileName();
59 if (!StringUtils.isNullOrEmpty(profileSysPropOverride)) {
60 return profileSysPropOverride;
61 } else {
62 return DEFAULT_PROFILE_NAME;
63 }
64 }
65 }
66
67 private String getSysPropertyProfileName() {
68 return StringUtils.trim(System.getProperty(AWS_PROFILE_SYSTEM_PROPERTY));
69 }
70
71 private String getEnvProfileName() {
72 return StringUtils.trim(System.getenv(AWS_PROFILE_ENVIRONMENT_VARIABLE));
73 }
74 }
75