Skip to content

Commit 2dae593

Browse files
committed
changes for multi env vars + throw on error
1 parent cd72d2f commit 2dae593

2 files changed

Lines changed: 297 additions & 42 deletions

File tree

src/utils.js

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,13 +1200,7 @@ function rewriteActionsWithAdobeIncludeIMSCredentialsAnnotation (packages) {
12001200
// avoid side effects, do not modify input packages
12011201
const newPackages = cloneDeep(packages)
12021202

1203-
// constants
1204-
const IMS_OAUTH_S2S_ENV_KEY = 'IMS_OAUTH_S2S'
1205-
1206-
let imsAuthObject = null
1207-
try {
1208-
imsAuthObject = JSON.parse(process.env[IMS_OAUTH_S2S_ENV_KEY])
1209-
} catch (e) {}
1203+
const imsAuthObject = loadIMSCredentialsFromEnv()
12101204

12111205
// traverse all actions in all packages
12121206
Object.keys(newPackages).forEach((key) => {
@@ -1224,12 +1218,37 @@ function rewriteActionsWithAdobeIncludeIMSCredentialsAnnotation (packages) {
12241218
return newPackages
12251219
}
12261220

1221+
/**
1222+
* Load the IMS credentials from the environment variables.
1223+
*
1224+
* @returns {object} the IMS auth object
1225+
*/
1226+
function loadIMSCredentialsFromEnv () {
1227+
// constants
1228+
const IMS_OAUTH_S2S_ENV_KEY = 'IMS_OAUTH_S2S'
1229+
1230+
const imsAuthObject = {
1231+
client_id: process.env[`${IMS_OAUTH_S2S_ENV_KEY}_CLIENT_ID`],
1232+
client_secret: process.env[`${IMS_OAUTH_S2S_ENV_KEY}_CLIENT_SECRET`],
1233+
org_id: process.env[`${IMS_OAUTH_S2S_ENV_KEY}_ORG_ID`],
1234+
scopes: (() => {
1235+
try {
1236+
return JSON.parse(process.env[`${IMS_OAUTH_S2S_ENV_KEY}_SCOPES`])
1237+
} catch (e) {
1238+
return process.env[`${IMS_OAUTH_S2S_ENV_KEY}_SCOPES`] // pass in string as is
1239+
}
1240+
})()
1241+
}
1242+
return imsAuthObject
1243+
}
1244+
12271245
/**
12281246
* Get the inputs for the include-ims-credentials annotation.
1247+
* Throws an error if the imsAuthObject is incomplete.
12291248
*
12301249
* @param {object} thisAction the action to process
12311250
* @param {object} imsAuthObject the IMS auth object
1232-
* @returns {object|undefined} the inputs
1251+
* @returns {object|undefined} the inputs or undefined with a warning
12331252
*/
12341253
function getIncludeIMSCredentialsAnnotationInputs (thisAction, imsAuthObject) {
12351254
const env = getCliEnv() || DEFAULT_ENV
@@ -1240,12 +1259,28 @@ function getIncludeIMSCredentialsAnnotationInputs (thisAction, imsAuthObject) {
12401259

12411260
// check if the annotation is defined
12421261
if (thisAction.annotations?.[ANNOTATION_INCLUDE_IMS_CREDENTIALS]) {
1243-
// check if the action is a web action
1244-
if (!imsAuthObject) {
1245-
aioLogger.warn(`The project has no credentials attached (missing the '${IMS_OAUTH_S2S_ENV_KEY}' environment variable). The annotation '${ANNOTATION_INCLUDE_IMS_CREDENTIALS}' will be ignored.`)
1246-
return
1262+
// check if the IMS credentials are loaded properly, if not emit a warning
1263+
if (Object.keys(imsAuthObject).length === 0) {
1264+
throw new Error(`Credentials for the project are missing, please ensure the Console Workspace is configured with an OAuth server to server credential.
1265+
Unset the annotation '${ANNOTATION_INCLUDE_IMS_CREDENTIALS}' if you don't want to include credentials.`)
1266+
}
1267+
const missingEnvVars = []
1268+
if (!imsAuthObject.client_id) {
1269+
missingEnvVars.push(`${IMS_OAUTH_S2S_ENV_KEY}_CLIENT_ID`)
1270+
}
1271+
if (!imsAuthObject.client_secret) {
1272+
missingEnvVars.push(`${IMS_OAUTH_S2S_ENV_KEY}_CLIENT_SECRET`)
1273+
}
1274+
if (!imsAuthObject.org_id) {
1275+
missingEnvVars.push(`${IMS_OAUTH_S2S_ENV_KEY}_ORG_ID`)
1276+
}
1277+
if (!imsAuthObject.scopes) {
1278+
missingEnvVars.push(`${IMS_OAUTH_S2S_ENV_KEY}_SCOPES`)
1279+
}
1280+
if (missingEnvVars.length > 0) {
1281+
throw new Error(`Credentials for the project are incomplete. Missing '${missingEnvVars.join('|')}' env variables.
1282+
Unset the annotation '${ANNOTATION_INCLUDE_IMS_CREDENTIALS}' if you don't want to include credentials.`)
12471283
}
1248-
12491284
return { [IMS_OAUTH_S2S_INPUT]: { ...imsAuthObject }, [IMS_ENV_INPUT]: env }
12501285
}
12511286
}
@@ -2253,5 +2288,6 @@ module.exports = {
22532288
safeParse,
22542289
isSupportedActionKind,
22552290
getIncludeIMSCredentialsAnnotationInputs,
2291+
loadIMSCredentialsFromEnv,
22562292
DEFAULT_PACKAGE_RESERVED_NAME
22572293
}

0 commit comments

Comments
 (0)