From 37d96faca57ee45b725c44471d69094d774658ad Mon Sep 17 00:00:00 2001 From: Pete Cornish Date: Sat, 23 Sep 2023 13:53:55 +0100 Subject: [PATCH] feat: supports IMPOSTER_CONFIG_DIR environment variable in all distributions. --- .../imposter/awslambda/AbstractHandler.kt | 13 ++++++------- .../imposter/awslambda/config/Settings.kt | 19 ++++++++++++++----- .../gatehill/imposter/cmd/ImposterLauncher.kt | 11 +++++++++-- .../imposter/config/util/ConfigUtil.kt | 9 +++++++++ docs/environment_variables.md | 1 + 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/AbstractHandler.kt b/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/AbstractHandler.kt index 6430d4b78..082864591 100644 --- a/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/AbstractHandler.kt +++ b/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/AbstractHandler.kt @@ -68,8 +68,6 @@ abstract class AbstractHandler( protected val logger: Logger = LogManager.getLogger(AbstractHandler::class.java) protected val server: LambdaServer - private val defaultLambdaBundleConfigDir = "/var/task/config" - init { // lambda functions are only allowed write access to /tmp System.setProperty("vertx.cacheDirBase", "/tmp/.vertx") @@ -80,14 +78,15 @@ abstract class AbstractHandler( LambdaServerFactory.eventType = eventType - @Suppress("DEPRECATION") - val configDir = Settings.configDir ?: Settings.s3ConfigUrl ?: defaultLambdaBundleConfigDir - ImposterBuilderKt() .withPluginClass(OpenApiPluginImpl::class.java) .withPluginClass(RestPluginImpl::class.java) - .apply { if (Settings.metaInfScan) withPluginClass(MetaInfPluginDetectorImpl::class.java) } - .withConfigurationDir(configDir) + .apply { + if (Settings.metaInfScan) { + withPluginClass(MetaInfPluginDetectorImpl::class.java) + } + Settings.configDirs.forEach { withConfigurationDir(it) } + } .withEngineOptions { options -> options.serverFactory = LambdaServerFactory::class.qualifiedName options.requestHandlingMode = RequestHandlingMode.SYNC diff --git a/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/config/Settings.kt b/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/config/Settings.kt index 732b2d907..1bdd6e013 100644 --- a/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/config/Settings.kt +++ b/adapter/awslambda/src/main/java/io/gatehill/imposter/awslambda/config/Settings.kt @@ -43,6 +43,7 @@ package io.gatehill.imposter.awslambda.config +import io.gatehill.imposter.config.util.ConfigUtil import io.gatehill.imposter.config.util.EnvVars import io.gatehill.imposter.util.splitOnCommaAndTrim @@ -50,7 +51,19 @@ import io.gatehill.imposter.util.splitOnCommaAndTrim * @author Pete Cornish */ object Settings { - val configDir: String? get() = EnvVars.getEnv("IMPOSTER_CONFIG_DIR") + private const val DEFAULT_BUNDLE_CONFIG_DIR = "/var/task/config" + + val configDirs: Array + get() { + ConfigUtil.parseConfigDirEnvVar().takeIf { it.isNotEmpty() }?.let { + return it + } + // deprecated environment variable + EnvVars.getEnv("IMPOSTER_S3_CONFIG_URL")?.let { + return arrayOf(it) + } + return arrayOf(DEFAULT_BUNDLE_CONFIG_DIR) + } /** * FQCN of [io.gatehill.imposter.plugin.PluginDiscoveryStrategy] implementation. @@ -58,10 +71,6 @@ object Settings { val pluginDiscoveryStrategyClass: String? get() = EnvVars.getEnv("IMPOSTER_PLUGIN_DISCOVERY_STRATEGY") - @Deprecated("Use configDir instead", ReplaceWith("Settings.configDir", "io.gatehill.imposter.awslambda.config.Settings")) - val s3ConfigUrl: String? get() = - EnvVars.getEnv("IMPOSTER_S3_CONFIG_URL") - val metaInfScan: Boolean get() = EnvVars.getEnv("IMPOSTER_METAINF_SCAN")?.toBoolean() ?: false diff --git a/cmd/src/main/java/io/gatehill/imposter/cmd/ImposterLauncher.kt b/cmd/src/main/java/io/gatehill/imposter/cmd/ImposterLauncher.kt index 574274608..188499bfc 100644 --- a/cmd/src/main/java/io/gatehill/imposter/cmd/ImposterLauncher.kt +++ b/cmd/src/main/java/io/gatehill/imposter/cmd/ImposterLauncher.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2021. + * Copyright (c) 2016-2023. * * This file is part of Imposter. * @@ -42,6 +42,7 @@ */ package io.gatehill.imposter.cmd +import io.gatehill.imposter.config.util.ConfigUtil import io.gatehill.imposter.config.util.MetaUtil.readVersion import io.gatehill.imposter.plugin.DynamicPluginDiscoveryStrategyImpl import io.gatehill.imposter.plugin.internal.MetaInfPluginDetectorImpl @@ -74,7 +75,11 @@ class ImposterLauncher(args: Array) { name = "--configDir", aliases = ["-c"], usage = "Directory containing mock configuration files", - required = true + + /** + * If not provided, [ConfigUtil.parseConfigDirEnvVar] will be used. + */ + required = false ) private var configDirs = arrayOf() @@ -180,6 +185,8 @@ class ImposterLauncher(args: Array) { if (tlsEnabled) DEFAULT_HTTPS_LISTEN_PORT else DEFAULT_HTTP_LISTEN_PORT } + configDirs += ConfigUtil.parseConfigDirEnvVar() + if (configDirs.isEmpty()) { LOGGER.error("No configuration directories were provided") exitProcess(0) diff --git a/core/config/src/main/java/io/gatehill/imposter/config/util/ConfigUtil.kt b/core/config/src/main/java/io/gatehill/imposter/config/util/ConfigUtil.kt index c407a4837..320e8d6e9 100644 --- a/core/config/src/main/java/io/gatehill/imposter/config/util/ConfigUtil.kt +++ b/core/config/src/main/java/io/gatehill/imposter/config/util/ConfigUtil.kt @@ -58,6 +58,7 @@ import io.gatehill.imposter.plugin.config.ResourcesHolder import io.gatehill.imposter.plugin.config.resource.BasePathHolder import io.gatehill.imposter.util.MapUtil import io.gatehill.imposter.util.ResourceUtil +import io.gatehill.imposter.util.splitOnCommaAndTrim import org.apache.logging.log4j.LogManager import java.io.File import java.io.IOException @@ -127,6 +128,14 @@ object ConfigUtil { }.toSet() } + /** + * Parses the `IMPOSTER_CONFIG_DIR` environment variable for a list of configuration directories. + * + * Note: this doesn't use `EnvVars.getEnv()` as it is not yet initialised. + */ + fun parseConfigDirEnvVar(): Array = + System.getenv("IMPOSTER_CONFIG_DIR")?.splitOnCommaAndTrim()?.toTypedArray() ?: emptyArray() + fun initInterpolators(environment: Map) { // reset the environment used by the expression evaluator expressionEvaluators = mapOf("env" to EnvEvaluator(environment)) diff --git a/docs/environment_variables.md b/docs/environment_variables.md index 5b614aec4..a617ee1ee 100644 --- a/docs/environment_variables.md +++ b/docs/environment_variables.md @@ -7,6 +7,7 @@ The following environment variables are supported: | IMPOSTER_ADD_ENGINE_RESPONSE_HEADERS | Whether to add response headers for `server` and unique request ID. | `true` | `false` | | IMPOSTER_AUTO_BASE_PATH | Whether to automatically set the base path for each configuration file, based on its relative path from the configuration root directory. | `false` | `true` | | IMPOSTER_CACHE_DIR | Path to a directory in which to store cached data, such as remote specifications. | `/imposter-cache` | `/path/to/dir` | +| IMPOSTER_CONFIG_DIR | The path to the configuration directory. Can be specified as a comma-separated list. See [configuration location](config_location.md). | `false` | `true` | | IMPOSTER_CONFIG_SCAN_RECURSIVE | Whether to scan for configuration files recursively within the configuration directories. | `false` | `true` | | IMPOSTER_CONFIG_DISCOVER_ENVFILES | Whether to discover envfiles. See below. | `true` | `false` | | IMPOSTER_FEATURES | Enables or disables features. See [Features](./features.md) documentation. | Per [default features](./features.md). | `metrics=false,stores=true` |