Skip to content

Commit e568606

Browse files
authored
feat: retrieve pull-secret using Red Hat SSO account (#216)
Signed-off-by: Denis Golovin <[email protected]>
1 parent fcf98df commit e568606

File tree

3 files changed

+33
-2249
lines changed

3 files changed

+33
-2249
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"test": "vitest run --coverage --passWithNoTests"
7171
},
7272
"dependencies": {
73+
"@redhat-developer/rhaccm-client": "^0.0.1"
7374
},
7475
"devDependencies": {
7576
"@podman-desktop/api": "next",
@@ -92,5 +93,8 @@
9293
"vitest": "^1.6.0",
9394
"which": "^3.0.0",
9495
"zip-local": "^0.3.5"
95-
}
96+
},
97+
"extensionDependencies": [
98+
"redhat.redhat-authentication"
99+
]
96100
}

src/crc-start.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { crcStatus } from './crc-status';
2222
import { commander } from './daemon-commander';
2323
import { crcLogProvider } from './log-provider';
2424
import { productName } from './util';
25+
import { AccountManagementClient } from '@redhat-developer/rhaccm-client';
2526

2627
interface ImagePullSecret {
2728
auths: Auths;
@@ -91,12 +92,33 @@ export async function startCrc(
9192
}
9293

9394
async function askAndStorePullSecret(logger: extensionApi.Logger): Promise<boolean> {
94-
const pullSecret = await extensionApi.window.showInputBox({
95-
prompt: 'Provide a pull secret',
96-
markdownDescription:
97-
'To pull container images from the registry, a *pull secret* is necessary. You can get a pull secret from the [Red Hat OpenShift Local download page](https://console.redhat.com/openshift/create/local?sc_cid=7013a000003SUmqAAG). Use the *"Copy pull secret"* option and paste the content into the field above',
98-
ignoreFocusOut: true,
99-
});
95+
let pullSecret: string;
96+
const authSession: extensionApi.AuthenticationSession | undefined = await extensionApi.authentication.getSession(
97+
'redhat.authentication-provider',
98+
[
99+
'api.iam.registry_service_accounts', //scope that gives access to hydra service accounts API
100+
'api.console', // scope that gives access to console.redhat.com APIs
101+
'id.username',
102+
], // adds claim to accessToken that used to render account label
103+
{ createIfNone: true }, // will request to login in browser if session does not exists
104+
);
105+
if (authSession) {
106+
const client = new AccountManagementClient({
107+
BASE: 'https://api.openshift.com',
108+
TOKEN: authSession.accessToken,
109+
});
110+
const accessTokenCfg = await client.default.postApiAccountsMgmtV1AccessToken();
111+
pullSecret = JSON.stringify(accessTokenCfg);
112+
}
113+
if (!pullSecret) {
114+
// ask for text in field
115+
pullSecret = await extensionApi.window.showInputBox({
116+
prompt: 'Provide a pull secret',
117+
markdownDescription:
118+
'To pull container images from the registry, a *pull secret* is necessary. You can get a pull secret from the [Red Hat OpenShift Local download page](https://console.redhat.com/openshift/create/local?sc_cid=7013a000003SUmqAAG). Use the *"Copy pull secret"* option and paste the content into the field above',
119+
ignoreFocusOut: true,
120+
});
121+
}
100122

101123
if (!pullSecret) {
102124
return false;

0 commit comments

Comments
 (0)