|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the |
| 4 | + * "License"); you may not use this file except in compliance |
| 5 | + * with the License. You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package software.amazon.lambda.powertools.core.internal; |
| 16 | + |
| 17 | +import static software.amazon.lambda.powertools.core.internal.SystemWrapper.getenv; |
| 18 | + |
| 19 | +import java.io.FileInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URL; |
| 22 | +import java.util.Properties; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Can be used to create a string that can server as a User-Agent suffix in requests made with the AWS SDK clients |
| 29 | + */ |
| 30 | +public class UserAgentConfigurator { |
| 31 | + |
| 32 | + public static final String NA = "NA"; |
| 33 | + public static final String VERSION_KEY = "powertools.version"; |
| 34 | + public static final String PT_FEATURE_VARIABLE = "${PT_FEATURE}"; |
| 35 | + public static final String PT_EXEC_ENV_VARIABLE = "${PT_EXEC_ENV}"; |
| 36 | + public static final String VERSION_PROPERTIES_FILENAME = "version.properties"; |
| 37 | + public static final String AWS_EXECUTION_ENV = "AWS_EXECUTION_ENV"; |
| 38 | + private static final Logger LOG = LoggerFactory.getLogger(UserAgentConfigurator.class); |
| 39 | + private static final String NO_OP = "no-op"; |
| 40 | + private static String ptVersion = getProjectVersion(); |
| 41 | + private static String userAgentPattern = "PT/" + PT_FEATURE_VARIABLE + "/" + ptVersion + " PTEnv/" |
| 42 | + + PT_EXEC_ENV_VARIABLE; |
| 43 | + |
| 44 | + private UserAgentConfigurator() { |
| 45 | + throw new IllegalStateException("Utility class. Not meant to be instantiated"); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Retrieves the project version from the version.properties file |
| 50 | + * |
| 51 | + * @return the project version |
| 52 | + */ |
| 53 | + static String getProjectVersion() { |
| 54 | + return getVersionFromProperties(VERSION_PROPERTIES_FILENAME, VERSION_KEY); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Retrieves the project version from a properties file. |
| 60 | + * The file should be in the resources folder. |
| 61 | + * The version is retrieved from the property with the given key. |
| 62 | + * |
| 63 | + * @param propertyFileName the name of the properties file |
| 64 | + * @param versionKey the key of the property that contains the version |
| 65 | + * @return the version of the project as configured in the given properties file |
| 66 | + */ |
| 67 | + static String getVersionFromProperties(String propertyFileName, String versionKey) { |
| 68 | + |
| 69 | + URL propertiesFileURI = Thread.currentThread().getContextClassLoader().getResource(propertyFileName); |
| 70 | + if (propertiesFileURI != null) { |
| 71 | + try (FileInputStream fis = new FileInputStream(propertiesFileURI.getPath())) { |
| 72 | + Properties properties = new Properties(); |
| 73 | + properties.load(fis); |
| 74 | + String version = properties.getProperty(versionKey); |
| 75 | + if (version != null && !version.isEmpty()) { |
| 76 | + return version; |
| 77 | + } |
| 78 | + } catch (IOException e) { |
| 79 | + LOG.warn("Unable to read {} file. Using default version.", propertyFileName); |
| 80 | + LOG.debug("Exception:", e); |
| 81 | + } |
| 82 | + } |
| 83 | + return NA; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Retrieves the user agent string for the Powertools for AWS Lambda. |
| 88 | + * It follows the pattern PT/{PT_FEATURE}/{PT_VERSION} PTEnv/{PT_EXEC_ENV} |
| 89 | + * The version of the project is automatically retrieved. |
| 90 | + * The PT_EXEC_ENV is automatically retrieved from the AWS_EXECUTION_ENV environment variable. |
| 91 | + * If it AWS_EXECUTION_ENV is not set, PT_EXEC_ENV defaults to "NA" |
| 92 | + * |
| 93 | + * @param ptFeature a custom feature to be added to the user agent string (e.g. idempotency). |
| 94 | + * If null or empty, the default PT_FEATURE is used. |
| 95 | + * The default PT_FEATURE is "no-op". |
| 96 | + * @return the user agent string |
| 97 | + */ |
| 98 | + public static String getUserAgent(String ptFeature) { |
| 99 | + |
| 100 | + String awsExecutionEnv = getenv(AWS_EXECUTION_ENV); |
| 101 | + String ptExecEnv = awsExecutionEnv != null ? awsExecutionEnv : NA; |
| 102 | + String userAgent = userAgentPattern.replace(PT_EXEC_ENV_VARIABLE, ptExecEnv); |
| 103 | + |
| 104 | + if (ptFeature == null || ptFeature.isEmpty()) { |
| 105 | + ptFeature = NO_OP; |
| 106 | + } |
| 107 | + return userAgent |
| 108 | + .replace(PT_FEATURE_VARIABLE, ptFeature) |
| 109 | + .replace(PT_EXEC_ENV_VARIABLE, ptExecEnv); |
| 110 | + } |
| 111 | +} |
0 commit comments