Skip to content

Commit

Permalink
chore!: simplify environment configuration (#153)
Browse files Browse the repository at this point in the history
* chore: simplify environment configuration

* doc
  • Loading branch information
moritzraho authored Apr 29, 2024
1 parent c8d9c9c commit 5da6808
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 122 deletions.
3 changes: 1 addition & 2 deletions e2e/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ TEST_NAMESPACE_1='11111-test'
TEST_AUTH_1='testauth'
TEST_AUTH_2='testauth2'
TEST_NAMESPACE_2='12345-test-stage'
# do not use the scheme for endpoints
ADOBE_STATE_STORE_ENDPOINT_PROD='127.0.0.1:8080'
AIO_STATE_ENDPOINT='http://127.0.0.1:8080'
19 changes: 2 additions & 17 deletions e2e/e2e.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ Copy the `.env.example` to your own `.env` in this folder.
For local testing, add the environment variable:

```sh
# do not use the scheme for endpoints
ADOBE_STATE_STORE_ENDPOINT_PROD=127.0.0.1:8080
AIO_STATE_ENDPOINT='http://127.0.0.1:8080'
```

Substitute the host with `host.docker.internal` if you are testing with the Dockerized version of the e2e tests.
Expand All @@ -24,22 +23,8 @@ Substitute the host with `host.docker.internal` if you are testing with the Dock

You might have to connect to internal servers for your e2e testing.

For `prod`, use these two environment variables:

```sh
# do not use the scheme for endpoints
ADOBE_STATE_STORE_ENDPOINT_PROD=my-prod-server-here.com
# can be omitted as well, since it defaults to prod
AIO_CLI_ENV=prod
```

For `stage`, use these two environment variables:

```sh
# do not use the scheme for endpoints
ADOBE_STATE_STORE_ENDPOINT_STAGE=my-stage-server-here.com
# set the env
AIO_CLI_ENV=stage
AIO_STATE_ENDPOINT=https://my-server-here.com
```

## Local Run
Expand Down
36 changes: 9 additions & 27 deletions lib/AdobeState.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const logger = require('@adobe/aio-lib-core-logging')('@adobe/aio-lib-state', {
const { HttpExponentialBackoff } = require('@adobe/aio-lib-core-networking')
const url = require('node:url')
const { getCliEnv } = require('@adobe/aio-lib-env')
const { ADOBE_STATE_STORE_ENDPOINT, REGEX_PATTERN_STORE_KEY, API_VERSION, ADOBE_STATE_STORE_REGIONS, HEADER_KEY_EXPIRES } = require('./constants')
const { REGEX_PATTERN_STORE_KEY, API_VERSION, HEADER_KEY_EXPIRES, CUSTOM_ENDPOINT, ENDPOINTS, ALLOWED_REGIONS } = require('./constants')
const Ajv = require('ajv')

/* *********************************** typedefs *********************************** */
Expand Down Expand Up @@ -135,25 +135,11 @@ class AdobeState {
/** @private */
this.region = region
/** @private */
this.endpoint = this.getRegionalEndpoint(ADOBE_STATE_STORE_ENDPOINT[getCliEnv()], region)
this.endpoint = this.getRegionalEndpoint(ENDPOINTS[getCliEnv()], region)
/** @private */
this.fetchRetry = new HttpExponentialBackoff()
}

/**
* Tests if an endpoint is a local endpoint.
*
* @param {string} endpoint the endpoint to test
* @returns {boolean} true if it is a local endpoint
*/
isLocalEndpoint (endpoint) {
return (
endpoint.startsWith('localhost') ||
endpoint.startsWith('127.0.0.1') ||
endpoint.startsWith('host.docker.internal')
)
}

/**
* Gets the regional endpoint for an endpoint.
*
Expand All @@ -162,13 +148,11 @@ class AdobeState {
* @returns {string} the endpoint, with the correct region
*/
getRegionalEndpoint (endpoint, region) {
if (this.isLocalEndpoint(endpoint) || region === ADOBE_STATE_STORE_REGIONS[0]) {
return endpoint
if (CUSTOM_ENDPOINT) {
return CUSTOM_ENDPOINT
}

const pattern = /-amer/gi
const replacement = `-${region}`
return endpoint.replaceAll(pattern, replacement)
return endpoint.replaceAll(/<region>/gi, region)
}

/**
Expand All @@ -180,14 +164,12 @@ class AdobeState {
* @returns {string} the constructed request url
*/
createRequestUrl (key, queryObject = {}) {
const isLocal = this.isLocalEndpoint(this.endpoint)
const protocol = isLocal ? 'http' : 'https'
let urlString

if (key) {
urlString = `${protocol}://${this.endpoint}/${API_VERSION}/containers/${this.namespace}/data/${key}`
urlString = `${this.endpoint}/${API_VERSION}/containers/${this.namespace}/data/${key}`
} else {
urlString = `${protocol}://${this.endpoint}/${API_VERSION}/containers/${this.namespace}`
urlString = `${this.endpoint}/${API_VERSION}/containers/${this.namespace}`
}

logger.debug('requestUrl string', urlString)
Expand Down Expand Up @@ -230,15 +212,15 @@ class AdobeState {
logger.debug(`init AdobeState with ${JSON.stringify(cloned, null, 2)}`)

if (!credentials.region) {
credentials.region = ADOBE_STATE_STORE_REGIONS.at(0) // first item is the default
credentials.region = ALLOWED_REGIONS.at(0) // first item is the default
}

const schema = {
type: 'object',
properties: {
region: {
type: 'string',
enum: ADOBE_STATE_STORE_REGIONS
enum: ALLOWED_REGIONS
},
apikey: { type: 'string' },
namespace: { type: 'string' }
Expand Down
59 changes: 31 additions & 28 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,31 @@ governing permissions and limitations under the License.
const { PROD_ENV, STAGE_ENV } = require('@adobe/aio-lib-env')
const { isInternalToAdobeRuntime } = require('./utils')

// gets these values if the keys are set in the environment, if not it will use the defaults set
// omit the protocol (https)
// the endpoints must have the region encoded as '-region', in this case it is the default region 'amer'
// (see ADOBE_STATE_STORE_REGIONS first element default)
// Default endpoints with protocol
// the endpoints must have the region encoded as '-<region>'
const ENDPOINT_PROD = 'https://storage-state-<region>.app-builder.adp.adobe.io'
const ENDPOINT_PROD_INTERNAL = 'https://storage-state-<region>.app-builder.int.adp.adobe.io'
/// we always use the stage public endpoint, as Runtime Prod doesn't have access to State Stage internal
/// see https://jira.corp.adobe.com/browse/ACNA-2699
const ENDPOINT_STAGE = 'https://storage-state-<region>.stg.app-builder.adp.adobe.io'
const ENDPOINT_STAGE_INTERNAL = 'https://storage-state-<region>.stg.app-builder.adp.adobe.io'

const ALLOWED_REGIONS = [ // first region is the default region
'amer',
'apac',
'emea'
]

// can be overwritten by env
const {
ADOBE_STATE_STORE_ENDPOINT_PROD = 'storage-state-amer.app-builder.adp.adobe.io',
ADOBE_STATE_STORE_ENDPOINT_PROD_INTERNAL = 'storage-state-amer.app-builder.int.adp.adobe.io',

// we always use the stage public endpoint, as Runtime Prod doesn't have access to State Stage internal
// see https://jira.corp.adobe.com/browse/ACNA-2699
ADOBE_STATE_STORE_ENDPOINT_STAGE = 'storage-state-amer.stg.app-builder.adp.adobe.io',
ADOBE_STATE_STORE_ENDPOINT_STAGE_INTERNAL = 'storage-state-amer.stg.app-builder.adp.adobe.io',

API_VERSION = 'v1beta1',
ADOBE_STATE_STORE_REGIONS = [ // first region is the default region
'amer',
'apac',
'emea'
]
AIO_STATE_API_VERSION: API_VERSION = 'v1beta1',
// needs protocol
AIO_STATE_ENDPOINT: CUSTOM_ENDPOINT = null // make sure users can point to any instance (e.g. for testing)
} = process.env

const ADOBE_STATE_STORE_ENDPOINT = {
[PROD_ENV]: isInternalToAdobeRuntime() ? ADOBE_STATE_STORE_ENDPOINT_PROD_INTERNAL : ADOBE_STATE_STORE_ENDPOINT_PROD,
[STAGE_ENV]: isInternalToAdobeRuntime() ? ADOBE_STATE_STORE_ENDPOINT_STAGE_INTERNAL : ADOBE_STATE_STORE_ENDPOINT_STAGE
const ENDPOINTS = {
[PROD_ENV]: isInternalToAdobeRuntime() ? ENDPOINT_PROD_INTERNAL : ENDPOINT_PROD,
[STAGE_ENV]: isInternalToAdobeRuntime() ? ENDPOINT_STAGE_INTERNAL : ENDPOINT_STAGE
}

const MAX_KEY_SIZE = 1024 * 1 // 1KB
Expand All @@ -48,16 +49,18 @@ const REGEX_PATTERN_STORE_NAMESPACE = '^(development-)?([0-9]{3,10})-([a-z0-9]{1
const REGEX_PATTERN_STORE_KEY = `^[a-zA-Z0-9-_.]{1,${MAX_KEY_SIZE}}$`

module.exports = {
ADOBE_STATE_STORE_REGIONS,
ADOBE_STATE_STORE_ENDPOINT_PROD,
ADOBE_STATE_STORE_ENDPOINT_STAGE,
ADOBE_STATE_STORE_ENDPOINT_PROD_INTERNAL,
ADOBE_STATE_STORE_ENDPOINT_STAGE_INTERNAL,
ALLOWED_REGIONS,
API_VERSION,
ENDPOINTS,
CUSTOM_ENDPOINT,
MAX_KEY_SIZE,
MAX_TTL_SECONDS,
REGEX_PATTERN_STORE_NAMESPACE,
REGEX_PATTERN_STORE_KEY,
ADOBE_STATE_STORE_ENDPOINT,
HEADER_KEY_EXPIRES
HEADER_KEY_EXPIRES,
// for testing only
ENDPOINT_PROD,
ENDPOINT_PROD_INTERNAL,
ENDPOINT_STAGE,
ENDPOINT_STAGE_INTERNAL
}
Loading

0 comments on commit 5da6808

Please sign in to comment.