Skip to content

Commit 4f22b03

Browse files
committed
feat: expose oauth s2s env
1 parent 00f1333 commit 4f22b03

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

src/lib/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
defaultImageCacheDuration: '604800',
2323
AIO_CONFIG_IMS_ORG_ID: 'project.org.ims_org_id',
2424
SERVICE_API_KEY_ENV: 'SERVICE_API_KEY',
25+
IMS_OAUTH_S2S_ENV: 'IMS_OAUTH_S2S',
2526
ENTP_INT_CERTS_FOLDER: 'entp-int-certs',
2627
CONSOLE_API_KEYS: {
2728
prod: 'aio-cli-console-auth',

src/lib/import-helper.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -512,17 +512,19 @@ function transformRuntime (runtime) {
512512
}
513513

514514
/**
515-
* Gets the service credential from the credentials.
515+
* Gets the service credential from the credentials in the Console file.
516516
*
517517
* This is different if Jwt or OAuth Server to Server is available, and whether
518518
* there is a migration going on from Jwt -> OAuth Server to Server.
519519
*
520520
* @private
521521
* @param {object} credentials all the credentials for the workspace
522-
* @param {boolean} useJwt prefer jwt, if available.
523-
* @returns {object} the service credential object
522+
* @param {object} options
523+
* @param {boolean} options.useJwt prefer jwt, if available.
524+
* @param {boolean} options.forceOauthS2S force oauth_server_to_server credential or undefined
525+
* @returns {object|undefined} the service credential object
524526
*/
525-
function getServiceCredential (credentials, imsOrgId, useJwt) {
527+
function getServiceCredential (credentials, imsOrgId, { useJwt = false, forceOauthS2S = false }) {
526528
// find jwt / oauth_server_to_server credential
527529
const jwtCredential = credentials.find(credential => typeof credential.jwt === 'object')
528530
const oauthS2SCredential = credentials.find(credential => typeof credential.oauth_server_to_server === 'object')
@@ -538,6 +540,10 @@ function getServiceCredential (credentials, imsOrgId, useJwt) {
538540
oauthS2SCredential.oauth_server_to_server.ims_org_id = imsOrgId
539541
}
540542

543+
if (forceOauthS2S) {
544+
return oauthS2SCredential // force undefined if oauthS2SCredentials is not found
545+
}
546+
541547
if (jwtCredential && oauthS2SCredential) {
542548
if (useJwt) {
543549
return jwtCredential.jwt
@@ -584,7 +590,7 @@ function getServiceCredential (credentials, imsOrgId, useJwt) {
584590
*/
585591
function transformCredentials (credentials, imsOrgId, useJwt) {
586592
// get jwt / oauth_server_to_server credential
587-
const serviceCredential = getServiceCredential(credentials, imsOrgId, useJwt)
593+
const serviceCredential = getServiceCredential(credentials, imsOrgId, { useJwt })
588594

589595
return credentials.reduce((acc, credential) => {
590596
// the json schema enforces for oauth2 OR apiKey OR jwt OR oauth_server_to_server in a credential
@@ -736,6 +742,7 @@ const getProjectCredentialType = (projectConfig, flags) => {
736742

737743
module.exports = {
738744
getServiceApiKey,
745+
getServiceCredential,
739746
writeFile,
740747
loadConfigFile,
741748
loadAndValidateConfigFile,

src/lib/import.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { loadAndValidateConfigFile, importConfigJson, loadConfigFile, getServiceApiKey } = require('./import-helper')
2-
const { SERVICE_API_KEY_ENV } = require('./defaults')
1+
const { loadAndValidateConfigFile, importConfigJson, loadConfigFile, getServiceApiKey, getServiceCredential } = require('./import-helper')
2+
const { SERVICE_API_KEY_ENV, IMS_OAUTH_S2S_ENV } = require('./defaults')
33

44
/**
55
* Imports the project's console config to the local environment.
@@ -23,7 +23,14 @@ async function importConsoleConfig (consoleConfigFileOrBuffer, flags) {
2323
const config = loadFunc(consoleConfigFileOrBuffer).values
2424

2525
const serviceClientId = getServiceApiKey(config, useJwt)
26-
const extraEnvVars = { [SERVICE_API_KEY_ENV]: serviceClientId }
26+
const oauthS2SCredential = getServiceCredential(config.project?.workspace?.details?.credentials, config.project?.org?.ims_org_id, { forceOauthS2S: true })?.oauth_server_to_server
27+
28+
let extraEnvVars
29+
if (oauthS2SCredential) {
30+
extraEnvVars = { [SERVICE_API_KEY_ENV]: serviceClientId, [IMS_OAUTH_S2S_ENV]: JSON.stringify(oauthS2SCredential) }
31+
} else {
32+
extraEnvVars = { [SERVICE_API_KEY_ENV]: serviceClientId }
33+
}
2734

2835
await importConfigJson(consoleConfigFileOrBuffer, process.cwd(), { interactive, overwrite, merge, useJwt }, extraEnvVars)
2936
return config

0 commit comments

Comments
 (0)