1 /* 2 * Copyright 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 16 package software.amazon.awssdk.profiles; 17 18 import static software.amazon.awssdk.utils.UserHomeDirectoryUtils.userHomeDirectory; 19 20 import java.nio.file.FileSystems; 21 import java.nio.file.Files; 22 import java.nio.file.Path; 23 import java.nio.file.Paths; 24 import java.util.Optional; 25 import java.util.regex.Pattern; 26 import software.amazon.awssdk.annotations.SdkPublicApi; 27 28 /** 29 * A collection of static methods for loading the location for configuration and credentials files. 30 */ 31 @SdkPublicApi 32 public final class ProfileFileLocation { 33 private static final Pattern HOME_DIRECTORY_PATTERN = 34 Pattern.compile("^~(/|" + Pattern.quote(FileSystems.getDefault().getSeparator()) + ").*$"); 35 ProfileFileLocation()36 private ProfileFileLocation() { 37 } 38 39 40 /** 41 * Resolve the path for the configuration file, regardless of whether it exists or not. 42 * 43 * @see #configurationFileLocation() 44 */ configurationFilePath()45 public static Path configurationFilePath() { 46 return resolveProfileFilePath( 47 ProfileFileSystemSetting.AWS_CONFIG_FILE.getStringValue() 48 .orElseGet(() -> Paths.get(userHomeDirectory(), 49 ".aws", "config").toString())); 50 } 51 52 /** 53 * Resolve the location for the credentials file, regardless of whether it exists or not. 54 * 55 * @see #credentialsFileLocation() 56 */ credentialsFilePath()57 public static Path credentialsFilePath() { 58 return resolveProfileFilePath( 59 ProfileFileSystemSetting.AWS_SHARED_CREDENTIALS_FILE.getStringValue() 60 .orElseGet(() -> Paths.get(userHomeDirectory(), 61 ".aws", "credentials").toString())); 62 } 63 64 /** 65 * Load the location for the configuration file, usually ~/.aws/config unless it's overridden using an environment variable 66 * or system property. 67 */ configurationFileLocation()68 public static Optional<Path> configurationFileLocation() { 69 return resolveIfExists(configurationFilePath()); 70 } 71 72 /** 73 * Load the location for the credentials file, usually ~/.aws/credentials unless it's overridden using an environment variable 74 * or system property. 75 */ credentialsFileLocation()76 public static Optional<Path> credentialsFileLocation() { 77 return resolveIfExists(credentialsFilePath()); 78 } 79 resolveProfileFilePath(String path)80 private static Path resolveProfileFilePath(String path) { 81 // Resolve ~ using the CLI's logic, not whatever Java decides to do with it. 82 if (HOME_DIRECTORY_PATTERN.matcher(path).matches()) { 83 path = userHomeDirectory() + path.substring(1); 84 } 85 86 return Paths.get(path); 87 } 88 resolveIfExists(Path path)89 private static Optional<Path> resolveIfExists(Path path) { 90 return Optional.ofNullable(path).filter(Files::isRegularFile).filter(Files::isReadable); 91 } 92 } 93