Skip to content

Commit 991273c

Browse files
Merge pull request #18 from pluralsh/add-console-federated-login-support
Add Plural Console OIDC support
2 parents 4d344f7 + 535f652 commit 991273c

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

action.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
name: Setup Plural
22
description: Sets up the plural cli in your github repo
33
inputs:
4+
email:
5+
description: The user email to log in with via OIDC federated credential
6+
required: false
7+
consoleToken:
8+
description: 'Plural console authentication token'
9+
required: false
10+
consoleUrl:
11+
description: 'Plural console URL endpoint'
12+
required: false
413
config:
5-
description: Plural Config File
14+
description: Plural CLI Config File (you should prefer using OIDC federated credentials for authentication)
615
required: false
716
vsn:
817
description: the plural cli version to use
918
required: false
10-
default: '0.8.0'
19+
default: '0.12.14'
1120
plat:
1221
description: the cli platform to specify
1322
required: false
1423
default: 'amd64'
15-
email:
16-
description: the email to log in with via oidc
17-
required: false
18-
console_token:
19-
description: 'Plural console authentication token'
20-
required: false
21-
console_url:
22-
description: 'Plural console URL endpoint'
23-
required: false
2424
outputs: {}
2525
runs:
2626
using: 'node16'

index.js

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,12 @@ async function download(vsn, plat) {
4747
async function setupConfig(vsn) {
4848
let conf = core.getInput('config');
4949
if (!conf) {
50-
if (!core.getInput("email")) {
51-
if (!process.env.PLURAL_CONSOLE_TOKEN) {
52-
core.info("if you want to use `plural cd` commands you need to set $PLURAL_CONSOLE_TOKEN and $PLURAL_CONSOLE_URL")
53-
}
50+
if (!core.getInput("email") && core.getInput("consoleUrl")) {
5451
return
5552
}
5653

5754
if (cmp(vsn, '0.7.0') >= 0) {
5855
await setupTempConfig()
59-
setOutput()
6056
} else {
6157
core.setFailed("you must use versions greater than 0.7.0 with temporary credentials")
6258
}
@@ -76,25 +72,61 @@ async function setupConfig(vsn) {
7672
async function setupTempConfig() {
7773
const token = await core.getIDToken()
7874
const email = core.getInput('email')
75+
const consoleUrl = core.getInput('consoleUrl')
76+
if (consoleUrl) {
77+
return
78+
}
79+
7980
if (!email) {
8081
core.info("`email` is required when authenticating with oidc")
8182
return
8283
}
8384
const claims = jwt_decode(token)
8485
core.info(`logging in with jwt subject: ${claims.sub}`)
8586
await exec.exec(`plural auth oidc github_actions --token ${token} --email ${email}`)
87+
setOutput()
88+
}
89+
90+
async function getConsoleOIDCToken(url, email) {
91+
const token = await core.getIDToken()
92+
const claims = jwt_decode(token)
93+
core.info(`logging in with jwt subject: ${claims.sub}`)
94+
95+
const response = await fetch(path.join(url, 'v1/token/exchange'), {
96+
method: 'POST',
97+
headers: {'Content-Type': 'application/json'},
98+
body: JSON.stringify({ email, jwt: token }),
99+
})
100+
if (!response.ok) {
101+
const body = await response.text()
102+
core.setFailed(`failed to exchange token: ${response.status} ${body}`)
103+
return null
104+
}
105+
const data = await response.json()
106+
return data.access_token
86107
}
87108

88109
async function setupConsoleLogin() {
89-
const consoleToken = core.getInput('console_token');
90-
const consoleUrl = core.getInput('console_url');
110+
let consoleToken = core.getInput('consoleToken');
111+
const consoleUrl = core.getInput('consoleUrl');
112+
const email = core.getInput('email');
113+
114+
if (consoleUrl && email && !consoleToken) {
115+
consoleToken = await getConsoleOIDCToken(consoleUrl, email)
116+
if (!consoleToken) {
117+
return
118+
}
119+
}
91120

92121
if (consoleToken && consoleUrl) {
93122
core.exportVariable('PLURAL_CONSOLE_TOKEN', consoleToken);
94123
core.exportVariable('PLURAL_CONSOLE_URL', consoleUrl);
95-
core.info('Console authentication configured with provided token and URL');
96-
} else if (consoleToken || consoleUrl) {
97-
core.setFailed('Both console_token and console_url must be provided together');
124+
core.setSecret(consoleToken)
125+
core.setOutput('consoleToken', consoleToken)
126+
core.setOutput('consoleUrl', consoleUrl)
127+
core.info('Console authentication configured!');
128+
} else if (consoleUrl) {
129+
core.setFailed('Both consoleToken and consoleUrl must be provided together');
98130
}
99131
}
100132

@@ -104,9 +136,8 @@ function setOutput() {
104136
const token = config?.spec?.token || config?.token
105137
if (token) {
106138
core.setSecret(token)
139+
core.setOutput('token', config?.spec?.token || config?.token)
107140
}
108-
109-
core.setOutput('token', config?.spec?.token || config?.token)
110141
}
111142

112143
run();

0 commit comments

Comments
 (0)