Skip to content

Commit fdfb2b7

Browse files
authored
fix: unpack IMS_OAUTH_S2S env vars (#901)
* fix: unpack IMS_OAUTH_S2S env vars * comment fix
1 parent 2365703 commit fdfb2b7

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

src/lib/import.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,21 @@ async function importConsoleConfig (consoleConfigFileOrBuffer, flags) {
2222
const loadFunc = skipValidation ? loadConfigFile : loadAndValidateConfigFile
2323
const config = loadFunc(consoleConfigFileOrBuffer).values
2424

25-
const serviceClientId = getServiceApiKey(config, useJwt)
25+
const serviceClientId = getServiceApiKey(config, useJwt) // = client_id, legacy
2626
const oauthS2SCredential = getOAuthS2SCredential(config)
2727

2828
let extraEnvVars
2929
if (typeof oauthS2SCredential === 'object') {
30-
extraEnvVars = { [SERVICE_API_KEY_ENV]: serviceClientId, [IMS_OAUTH_S2S_ENV]: JSON.stringify(oauthS2SCredential) }
30+
// unpack oauthS2S json into IMS_OAUTH_S2S_* env vars
31+
const oauthS2SEnv = Object.entries(oauthS2SCredential).reduce((acc, [key, value]) => {
32+
if (Array.isArray(value)) {
33+
value = JSON.stringify(value) // stringify arrays e.g. scopes to be consistent with AIO_* vars behavior
34+
}
35+
acc[`${IMS_OAUTH_S2S_ENV}_${key.toUpperCase()}`] = value
36+
return acc
37+
}, {})
38+
39+
extraEnvVars = { [SERVICE_API_KEY_ENV]: serviceClientId, ...oauthS2SEnv }
3140
} else {
3241
extraEnvVars = { [SERVICE_API_KEY_ENV]: serviceClientId }
3342
}

test/commands/lib/import.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test('exports', () => {
3434
})
3535

3636
describe('importConsoleConfig', () => {
37-
test('with oauth_server_to_server credentials, adds IMS_OAUTH_S2S to env vars', async () => {
37+
test('with oauth_server_to_server credentials, unpacks IMS_OAUTH_S2S_* env vars', async () => {
3838
const configContent = fixtureFile('oauths2s/valid.config.json')
3939
// The file is read twice: once by importConsoleConfig (loadFunc) and once by importConfigJson
4040
fs.readFileSync.mockReturnValue(configContent)
@@ -44,17 +44,17 @@ describe('importConsoleConfig', () => {
4444
expect(config).toBeDefined()
4545
expect(config.project.name).toEqual('TestProject123')
4646

47-
// Check that writeFile was called with the IMS_OAUTH_S2S_ENV variable
4847
const envWriteCall = fs.writeFile.mock.calls.find(call => call[0].endsWith('.env'))
4948
expect(envWriteCall).toBeDefined()
50-
expect(envWriteCall[1]).toContain(SERVICE_API_KEY_ENV)
51-
expect(envWriteCall[1]).toContain(IMS_OAUTH_S2S_ENV)
52-
53-
// Verify the IMS_OAUTH_S2S value contains expected credential data
5449
const envContent = envWriteCall[1]
55-
expect(envContent).toContain('"client_id":"CXCXCXCXCXCXCXCXC"')
56-
expect(envContent).toContain('"client_secret":"SFSFSFSFSFSFSFSFSFSFSFSFSFS"')
57-
expect(envContent).toContain('"org_id":"XOXOXOXOXOXOX@AdobeOrg"')
50+
expect(envContent).toContain(SERVICE_API_KEY_ENV)
51+
expect(envContent).toContain(IMS_OAUTH_S2S_ENV)
52+
53+
// Credential is unpacked into IMS_OAUTH_S2S_* vars
54+
expect(envContent).toContain('IMS_OAUTH_S2S_CLIENT_ID=CXCXCXCXCXCXCXCXC')
55+
expect(envContent).toContain('IMS_OAUTH_S2S_CLIENT_SECRET=SFSFSFSFSFSFSFSFSFSFSFSFSFS')
56+
expect(envContent).toContain('IMS_OAUTH_S2S_ORG_ID=XOXOXOXOXOXOX@AdobeOrg')
57+
expect(envContent).toContain('IMS_OAUTH_S2S_SCOPES=["openid","AdobeID"]') // stringified array
5858
})
5959

6060
test('with jwt credentials only, does not add IMS_OAUTH_S2S to env vars', async () => {

0 commit comments

Comments
 (0)