/** * <p>Removes control characters (char <= 32) from both * ends of this String returning {@code null} if the String is * empty ("") after the trim or if it is {@code null}. * * <p>The String is trimmed using {@link String#trim()}. * Trim removes start and end characters <= 32.</p> * * <pre> * StringUtils.trimToNull(null) = null * StringUtils.trimToNull("") = null * StringUtils.trimToNull(" ") = null * StringUtils.trimToNull("abc") = "abc" * StringUtils.trimToNull(" abc ") = "abc" * </pre> * * @param str the String to be trimmed, may be null * @return the trimmed String, * {@code null} if only chars <= 32, empty or null String input * @since 2.0 */ public static String trimToNull(final String str) { String ts = trim(str); return isEmpty(ts) ? null : ts; }
/** * <p>Removes control characters (char <= 32) from both * ends of this String returning {@code null} if the String is * empty ("") after the trim or if it is {@code null}. * * <p>The String is trimmed using {@link String#trim()}. * Trim removes start and end characters <= 32.</p> * * <pre> * StringUtils.trimToNull(null) = null * StringUtils.trimToNull("") = null * StringUtils.trimToNull(" ") = null * StringUtils.trimToNull("abc") = "abc" * StringUtils.trimToNull(" abc ") = "abc" * </pre> * * @param str the String to be trimmed, may be null * @return the trimmed String, * {@code null} if only chars <= 32, empty or null String input * @since 2.0 */ public static String trimToNull(final String str) { String ts = trim(str); return isEmpty(ts) ? null : ts; }
/** * Given a property line, load the property key and value. If the property line is invalid and should be ignored, this will * return empty. */ private static Optional<Pair<String, String>> parsePropertyDefinition(ParserState state, String line) { int firstEqualsLocation = line.indexOf('='); Validate.isTrue(firstEqualsLocation != -1, "Expected an '=' sign defining a property on line " + state.currentLineNumber); String propertyKey = StringUtils.trim(line.substring(0, firstEqualsLocation)); String propertyValue = StringUtils.trim(line.substring(firstEqualsLocation + 1)); Validate.isTrue(!propertyKey.isEmpty(), "Property did not have a name on line " + state.currentLineNumber); // If the profile name includes invalid characters, it should be ignored. if (!isValidIdentifier(propertyKey)) { log.warn(() -> "Ignoring property '" + propertyKey + "' on line " + state.currentLineNumber + " because " + "its name was not alphanumeric with dashes or underscores."); return Optional.empty(); } return Optional.of(Pair.of(propertyKey, propertyValue)); }
/** * Given a property line, load the property key and value. If the property line is invalid and should be ignored, this will * return empty. */ private static Optional<Pair<String, String>> parsePropertyDefinition(ParserState state, String line) { int firstEqualsLocation = line.indexOf('='); Validate.isTrue(firstEqualsLocation != -1, "Expected an '=' sign defining a property on line " + state.currentLineNumber); String propertyKey = StringUtils.trim(line.substring(0, firstEqualsLocation)); String propertyValue = StringUtils.trim(line.substring(firstEqualsLocation + 1)); Validate.isTrue(!propertyKey.isEmpty(), "Property did not have a name on line " + state.currentLineNumber); // If the profile name includes invalid characters, it should be ignored. if (!isValidIdentifier(propertyKey)) { log.warn(() -> "Ignoring property '" + propertyKey + "' on line " + state.currentLineNumber + " because " + "its name was not alphanumeric with dashes or underscores."); return Optional.empty(); } return Optional.of(Pair.of(propertyKey, propertyValue)); }
/** * Read a property continuation line and update the parser state with the results. This adds the value in the continuation * to the current property, prefixed with a newline. * * Non-Blank Parent Property: Whitespace Value Whitespace? * Blank Parent Property (Sub-Property): Whitespace Identifier Whitespace? = Whitespace? Value Whitespace? */ private static void readPropertyContinuationLine(ParserState state, String line) { // If we're in an invalid profile or property, ignore its continuations if (state.ignoringCurrentProfile || state.ignoringCurrentProperty) { return; } Validate.isTrue(state.currentProfileBeingRead != null && state.currentPropertyBeingRead != null, "Expected a profile or property definition on line " + state.currentLineNumber); // Comments are not removed on property continuation lines. They're considered part of the value. line = StringUtils.trim(line); Map<String, String> profileProperties = state.profiles.get(state.currentProfileBeingRead); String currentPropertyValue = profileProperties.get(state.currentPropertyBeingRead); String newPropertyValue = currentPropertyValue + "\n" + line; // If this is a sub-property, make sure it can be parsed correctly by the CLI. if (state.validatingContinuationsAsSubProperties) { parsePropertyDefinition(state, line); } profileProperties.put(state.currentPropertyBeingRead, newPropertyValue); }
/** * Read a property continuation line and update the parser state with the results. This adds the value in the continuation * to the current property, prefixed with a newline. * * Non-Blank Parent Property: Whitespace Value Whitespace? * Blank Parent Property (Sub-Property): Whitespace Identifier Whitespace? = Whitespace? Value Whitespace? */ private static void readPropertyContinuationLine(ParserState state, String line) { // If we're in an invalid profile or property, ignore its continuations if (state.ignoringCurrentProfile || state.ignoringCurrentProperty) { return; } Validate.isTrue(state.currentProfileBeingRead != null && state.currentPropertyBeingRead != null, "Expected a profile or property definition on line " + state.currentLineNumber); // Comments are not removed on property continuation lines. They're considered part of the value. line = StringUtils.trim(line); Map<String, String> profileProperties = state.profiles.get(state.currentProfileBeingRead); String currentPropertyValue = profileProperties.get(state.currentPropertyBeingRead); String newPropertyValue = currentPropertyValue + "\n" + line; // If this is a sub-property, make sure it can be parsed correctly by the CLI. if (state.validatingContinuationsAsSubProperties) { parsePropertyDefinition(state, line); } profileProperties.put(state.currentPropertyBeingRead, newPropertyValue); }
/** * Loads the individual access key ID and secret key from the specified credentials, trimming any extra whitespace from the * credentials. * * <p>Returns either a {@link AwsSessionCredentials} or a {@link AwsBasicCredentials} object, depending on the input type. * * @return A new credentials object with the sanitized credentials. */ protected AwsCredentials sanitizeCredentials(AwsCredentials credentials) { String accessKeyId = StringUtils.trim(credentials.accessKeyId()); String secretKey = StringUtils.trim(credentials.secretAccessKey()); if (credentials instanceof AwsSessionCredentials) { AwsSessionCredentials sessionCredentials = (AwsSessionCredentials) credentials; return AwsSessionCredentials.create(accessKeyId, secretKey, StringUtils.trim(sessionCredentials.sessionToken())); } return AwsBasicCredentials.create(accessKeyId, secretKey); }
/** * Read a profile line and update the parser state with the results. This marks future properties as being in this profile. * * Configuration Files: [ Whitespace? profile Whitespace Identifier Whitespace? ] Whitespace? CommentLine? * Credentials Files: [ Whitespace? Identifier Whitespace? ] Whitespace? CommentLine? */ private static void readProfileDefinitionLine(ParserState state, String line) { // Profile definitions do not require a space between the closing bracket and the comment delimiter String lineWithoutComments = removeTrailingComments(line, "#", ";"); String lineWithoutWhitespace = StringUtils.trim(lineWithoutComments); Validate.isTrue(lineWithoutWhitespace.endsWith("]"), "Profile definition must end with ']' on line " + state.currentLineNumber); Optional<String> profileName = parseProfileDefinition(state, lineWithoutWhitespace); // If we couldn't get the profile name, ignore this entire profile. if (!profileName.isPresent()) { state.ignoringCurrentProfile = true; return; } state.currentProfileBeingRead = profileName.get(); state.currentPropertyBeingRead = null; state.ignoringCurrentProfile = false; state.ignoringCurrentProperty = false; // If we've seen this profile before, don't override the existing properties. We'll be merging them. state.profiles.computeIfAbsent(profileName.get(), i -> new LinkedHashMap<>()); }
/** * Loads the individual access key ID and secret key from the specified credentials, trimming any extra whitespace from the * credentials. * * <p>Returns either a {@link AwsSessionCredentials} or a {@link AwsBasicCredentials} object, depending on the input type. * * @return A new credentials object with the sanitized credentials. */ protected AwsCredentials sanitizeCredentials(AwsCredentials credentials) { String accessKeyId = StringUtils.trim(credentials.accessKeyId()); String secretKey = StringUtils.trim(credentials.secretAccessKey()); if (credentials instanceof AwsSessionCredentials) { AwsSessionCredentials sessionCredentials = (AwsSessionCredentials) credentials; return AwsSessionCredentials.create(accessKeyId, secretKey, StringUtils.trim(sessionCredentials.sessionToken())); } return AwsBasicCredentials.create(accessKeyId, secretKey); }
/** * Read a profile line and update the parser state with the results. This marks future properties as being in this profile. * * Configuration Files: [ Whitespace? profile Whitespace Identifier Whitespace? ] Whitespace? CommentLine? * Credentials Files: [ Whitespace? Identifier Whitespace? ] Whitespace? CommentLine? */ private static void readProfileDefinitionLine(ParserState state, String line) { // Profile definitions do not require a space between the closing bracket and the comment delimiter String lineWithoutComments = removeTrailingComments(line, "#", ";"); String lineWithoutWhitespace = StringUtils.trim(lineWithoutComments); Validate.isTrue(lineWithoutWhitespace.endsWith("]"), "Profile definition must end with ']' on line " + state.currentLineNumber); Optional<String> profileName = parseProfileDefinition(state, lineWithoutWhitespace); // If we couldn't get the profile name, ignore this entire profile. if (!profileName.isPresent()) { state.ignoringCurrentProfile = true; return; } state.currentProfileBeingRead = profileName.get(); state.currentPropertyBeingRead = null; state.ignoringCurrentProfile = false; state.ignoringCurrentProperty = false; // If we've seen this profile before, don't override the existing properties. We'll be merging them. state.profiles.computeIfAbsent(profileName.get(), i -> new LinkedHashMap<>()); }
String rawProfileName = StringUtils.trim(lineWithoutBrackets); boolean hasProfilePrefix = rawProfileName.startsWith("profile ") || rawProfileName.startsWith("profile\t"); if (state.fileType == ProfileFile.Type.CONFIGURATION) { if (hasProfilePrefix) { standardizedProfileName = StringUtils.trim(rawProfileName.substring("profile".length())); } else if (rawProfileName.equals("default")) { standardizedProfileName = "default"; String profileName = StringUtils.trim(standardizedProfileName);
@Override public AwsCredentials resolveCredentials() { String accessKey = trim(loadSetting(SdkSystemSetting.AWS_ACCESS_KEY_ID).orElse(null)); String secretKey = trim(loadSetting(SdkSystemSetting.AWS_SECRET_ACCESS_KEY).orElse(null)); String sessionToken = trim(loadSetting(SdkSystemSetting.AWS_SESSION_TOKEN).orElse(null)); if (StringUtils.isEmpty(accessKey)) { throw SdkClientException.builder() .message(String.format("Unable to load credentials from system settings. Access key must be" + " specified either via environment variable (%s) or system property (%s).", SdkSystemSetting.AWS_ACCESS_KEY_ID.environmentVariable(), SdkSystemSetting.AWS_ACCESS_KEY_ID.property())) .build(); } if (StringUtils.isEmpty(secretKey)) { throw SdkClientException.builder() .message(String.format("Unable to load credentials from system settings. Secret key must be" + " specified either via environment variable (%s) or system property (%s).", SdkSystemSetting.AWS_SECRET_ACCESS_KEY.environmentVariable(), SdkSystemSetting.AWS_SECRET_ACCESS_KEY.property())) .build(); } return sessionToken == null ? AwsBasicCredentials.create(accessKey, secretKey) : AwsSessionCredentials.create(accessKey, secretKey, sessionToken); }
String rawProfileName = StringUtils.trim(lineWithoutBrackets); boolean hasProfilePrefix = rawProfileName.startsWith("profile ") || rawProfileName.startsWith("profile\t"); if (state.fileType == ProfileFile.Type.CONFIGURATION) { if (hasProfilePrefix) { standardizedProfileName = StringUtils.trim(rawProfileName.substring("profile".length())); } else if (rawProfileName.equals("default")) { standardizedProfileName = "default"; String profileName = StringUtils.trim(standardizedProfileName);
@Override public AwsCredentials resolveCredentials() { String accessKey = trim(loadSetting(SdkSystemSetting.AWS_ACCESS_KEY_ID).orElse(null)); String secretKey = trim(loadSetting(SdkSystemSetting.AWS_SECRET_ACCESS_KEY).orElse(null)); String sessionToken = trim(loadSetting(SdkSystemSetting.AWS_SESSION_TOKEN).orElse(null)); if (StringUtils.isEmpty(accessKey)) { throw SdkClientException.builder() .message(String.format("Unable to load credentials from system settings. Access key must be" + " specified either via environment variable (%s) or system property (%s).", SdkSystemSetting.AWS_ACCESS_KEY_ID.environmentVariable(), SdkSystemSetting.AWS_ACCESS_KEY_ID.property())) .build(); } if (StringUtils.isEmpty(secretKey)) { throw SdkClientException.builder() .message(String.format("Unable to load credentials from system settings. Secret key must be" + " specified either via environment variable (%s) or system property (%s).", SdkSystemSetting.AWS_SECRET_ACCESS_KEY.environmentVariable(), SdkSystemSetting.AWS_SECRET_ACCESS_KEY.property())) .build(); } return sessionToken == null ? AwsBasicCredentials.create(accessKey, secretKey) : AwsSessionCredentials.create(accessKey, secretKey, sessionToken); }
String lineWithoutWhitespace = StringUtils.trim(lineWithoutComments);
String lineWithoutWhitespace = StringUtils.trim(lineWithoutComments);