com.amazonaws.auth.profile.internal.BasicProfileConfigLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-java-sdk-core Show documentation
Show all versions of aws-java-sdk-core Show documentation
The AWS SDK for Java - Core module holds the classes that are used by the individual service clients to interact with Amazon Web Services. Users need to depend on aws-java-sdk artifact for accessing individual client classes.
/*
* Copyright 2014-2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.auth.profile.internal;
import com.amazonaws.SdkClientException;
import com.amazonaws.annotation.SdkInternalApi;
import com.amazonaws.util.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
/**
* Class to load a CLI style config or credentials file. Performs only basic validation on
* properties and profiles.
*/
@SdkInternalApi
public class BasicProfileConfigLoader {
public static final BasicProfileConfigLoader INSTANCE = new BasicProfileConfigLoader();
private BasicProfileConfigLoader() {
}
public AllProfiles loadProfiles(File file) {
if (file == null) {
throw new IllegalArgumentException(
"Unable to load AWS profiles: specified file is null.");
}
if (!file.exists() || !file.isFile()) {
throw new IllegalArgumentException(
"AWS credential profiles file not found in the given path: " +
file.getAbsolutePath());
}
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
return loadProfiles(fis);
} catch (IOException ioe) {
throw new SdkClientException(
"Unable to load AWS credential profiles file at: " + file.getAbsolutePath(),
ioe);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException ioe) {
}
}
}
}
/**
* Loads the credential profiles from the given input stream.
*
* @param is input stream from where the profile details are read.
*/
private AllProfiles loadProfiles(InputStream is) throws IOException {
ProfilesConfigFileLoaderHelper helper = new ProfilesConfigFileLoaderHelper();
Map> allProfileProperties = helper
.parseProfileProperties(new Scanner(is, StringUtils.UTF8.name()));
// Convert the loaded property map to credential objects
Map profilesByName = new LinkedHashMap();
for (Entry> entry : allProfileProperties.entrySet()) {
String profileName = entry.getKey();
Map properties = entry.getValue();
assertParameterNotEmpty(profileName,
"Unable to load properties from profile: Profile name is empty.");
profilesByName.put(profileName, new BasicProfile(profileName, properties));
}
return new AllProfiles(profilesByName);
}
/**
* Asserts that the specified parameter value is neither empty
nor null, and if
* it is, throws a SdkClientException
with the specified error message.
*
* @param parameterValue The parameter value being checked.
* @param errorMessage The error message to include in the SdkClientException if the
* specified parameter value is empty.
*/
private void assertParameterNotEmpty(String parameterValue, String errorMessage) {
if (StringUtils.isNullOrEmpty(parameterValue)) {
throw new SdkClientException(errorMessage);
}
}
/**
* Implementation of AbstractProfilesConfigFileScanner that groups profile properties into a map
* while scanning through the credentials profile.
*/
private static class ProfilesConfigFileLoaderHelper extends AbstractProfilesConfigFileScanner {
/**
* Map from the parsed profile name to the map of all the property values included the
* specific profile
*/
protected final Map> allProfileProperties = new LinkedHashMap>();
/**
* Parses the input and returns a map of all the profile properties.
*/
public Map> parseProfileProperties(Scanner scanner) {
allProfileProperties.clear();
run(scanner);
return new LinkedHashMap>(allProfileProperties);
}
@Override
protected void onEmptyOrCommentLine(String profileName, String line) {
// Ignore empty or comment line
}
@Override
protected void onProfileStartingLine(String newProfileName, String line) {
// If the same profile name has already been declared, clobber the
// previous one
allProfileProperties.put(newProfileName, new HashMap());
}
@Override
protected void onProfileEndingLine(String prevProfileName) {
// No-op
}
@Override
protected void onProfileProperty(String profileName, String propertyKey,
String propertyValue, boolean isSupportedProperty,
String line) {
Map properties = allProfileProperties.get(profileName);
properties.put(propertyKey, propertyValue);
}
@Override
protected void onEndOfFile() {
// No-op
}
}
}