@@ -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,31 @@ 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 : process . env [ `${ IMS_OAUTH_S2S_ENV_KEY } _SCOPES` ] // scopes should be a JSON array
1235+ }
1236+ return imsAuthObject
1237+ }
1238+
12271239/**
12281240 * Get the inputs for the include-ims-credentials annotation.
1241+ * Throws an error if the imsAuthObject is incomplete.
12291242 *
12301243 * @param {object } thisAction the action to process
12311244 * @param {object } imsAuthObject the IMS auth object
1232- * @returns {object|undefined } the inputs
1245+ * @returns {object|undefined } the inputs or undefined with a warning
12331246 */
12341247function getIncludeIMSCredentialsAnnotationInputs ( thisAction , imsAuthObject ) {
12351248 const env = getCliEnv ( ) || DEFAULT_ENV
@@ -1240,12 +1253,28 @@ function getIncludeIMSCredentialsAnnotationInputs (thisAction, imsAuthObject) {
12401253
12411254 // check if the annotation is defined
12421255 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
1256+ // check if the IMS credentials are loaded properly, if not emit a warning
1257+ if ( Object . keys ( imsAuthObject ) . length === 0 ) {
1258+ throw new Error ( `Credentials for the project are missing, please ensure the Console Workspace is configured with an OAuth server to server credential.
1259+ Unset the annotation '${ ANNOTATION_INCLUDE_IMS_CREDENTIALS } ' if you don't want to include credentials.` )
1260+ }
1261+ const missingEnvVars = [ ]
1262+ if ( ! imsAuthObject . client_id ) {
1263+ missingEnvVars . push ( `${ IMS_OAUTH_S2S_ENV_KEY } _CLIENT_ID` )
1264+ }
1265+ if ( ! imsAuthObject . client_secret ) {
1266+ missingEnvVars . push ( `${ IMS_OAUTH_S2S_ENV_KEY } _CLIENT_SECRET` )
1267+ }
1268+ if ( ! imsAuthObject . org_id ) {
1269+ missingEnvVars . push ( `${ IMS_OAUTH_S2S_ENV_KEY } _ORG_ID` )
1270+ }
1271+ if ( ! imsAuthObject . scopes ) {
1272+ missingEnvVars . push ( `${ IMS_OAUTH_S2S_ENV_KEY } _SCOPES` )
1273+ }
1274+ if ( missingEnvVars . length > 0 ) {
1275+ throw new Error ( `Credentials for the project are incomplete. Missing '${ missingEnvVars . join ( '|' ) } ' env variables.
1276+ Unset the annotation '${ ANNOTATION_INCLUDE_IMS_CREDENTIALS } ' if you don't want to include credentials.` )
12471277 }
1248-
12491278 return { [ IMS_OAUTH_S2S_INPUT ] : { ...imsAuthObject } , [ IMS_ENV_INPUT ] : env }
12501279 }
12511280}
@@ -2253,5 +2282,6 @@ module.exports = {
22532282 safeParse,
22542283 isSupportedActionKind,
22552284 getIncludeIMSCredentialsAnnotationInputs,
2285+ loadIMSCredentialsFromEnv,
22562286 DEFAULT_PACKAGE_RESERVED_NAME
22572287}
0 commit comments