@@ -21,23 +21,25 @@ import { getAuthConfig } from './configuration';
21
21
import { onDidChangeSessions , RedHatAuthenticationService } from './authentication-service' ;
22
22
import { ServiceAccountV1 , ContainerRegistryAuthorizerClient } from '@redhat-developer/rhcra-client' ;
23
23
import path from 'node:path' ;
24
- import { homedir } from 'node:os' ;
25
- import { accessSync , constants , readFileSync } from 'node:fs' ;
24
+ import { readFileSync } from 'node:fs' ;
26
25
import {
27
26
runRpmInstallSubscriptionManager ,
28
27
runSubscriptionManager ,
29
28
runSubscriptionManagerActivationStatus ,
30
29
runSubscriptionManagerRegister ,
31
30
runSubscriptionManagerUnregister ,
32
31
runCreateFactsFile ,
33
- isPodmanMachineRunning ,
34
32
runStartPodmanMachine ,
35
33
runStopPodmanMachine ,
34
+ getRunningPodmanMachineName ,
36
35
} from './podman-cli' ;
37
36
import { SubscriptionManagerClient } from '@redhat-developer/rhsm-client' ;
38
37
import { isLinux } from './util' ;
39
38
import { SSOStatusBarItem } from './status-bar-item' ;
40
39
import { ExtensionTelemetryLogger as TelemetryLogger } from './telemetry' ;
40
+ import { signIntoRedHatDeveloperAccount } from './subscription' ;
41
+ import { isRedHatRegistryConfigured } from './subscription' ;
42
+ import { REGISTRY_REDHAT_IO } from './subscription' ;
41
43
42
44
let authenticationServicePromise : Promise < RedHatAuthenticationService > ;
43
45
let currentSession : extensionApi . AuthenticationSession | undefined ;
@@ -70,21 +72,6 @@ function parseJwt(token: string) {
70
72
return JSON . parse ( jsonPayload ) ;
71
73
}
72
74
73
- async function signIntoRedHatDeveloperAccount (
74
- createIfNone = true ,
75
- ) : Promise < extensionApi . AuthenticationSession | undefined > {
76
- return extensionApi . authentication . getSession (
77
- 'redhat.authentication-provider' ,
78
- [
79
- 'api.iam.registry_service_accounts' , //scope that gives access to hydra service accounts API
80
- 'api.console' ,
81
- ] , // scope that gives access to console.redhat.com APIs
82
- { createIfNone } , // will request to login in browser if session does not exists
83
- ) ;
84
- }
85
-
86
- const REGISTRY_REDHAT_IO = 'registry.redhat.io' ;
87
-
88
75
async function createRegistry (
89
76
username : string ,
90
77
secret : string ,
@@ -109,29 +96,6 @@ function removeRegistry(serverUrl: string = REGISTRY_REDHAT_IO): void {
109
96
} ) ;
110
97
}
111
98
112
- // TODO: add listRegistries to registry API to allow search by
113
- // registry URL
114
- function isRedHatRegistryConfigured ( ) : boolean {
115
- const pathToAuthJson = path . join ( homedir ( ) , '.config' , 'containers' , 'auth.json' ) ;
116
- let configured = false ;
117
- try {
118
- // TODO: handle all kind problems with file existence, accessibility and parsable content
119
- accessSync ( pathToAuthJson , constants . R_OK ) ;
120
- const authFileContent = readFileSync ( pathToAuthJson , { encoding : 'utf8' } ) ;
121
- const authFileJson : {
122
- auths ?: {
123
- [ registryUrl : string ] : {
124
- auth : string ;
125
- } ;
126
- } ;
127
- } = JSON . parse ( authFileContent ) ;
128
- configured = authFileJson ?. auths ?. hasOwnProperty ( REGISTRY_REDHAT_IO ) || false ;
129
- } catch ( _notAccessibleError ) {
130
- // if file is not there, ignore and return default value
131
- }
132
- return configured ;
133
- }
134
-
135
99
async function createOrReuseRegistryServiceAccount ( ) : Promise < void > {
136
100
const currentSession = await signIntoRedHatDeveloperAccount ( ) ;
137
101
const accessTokenJson = parseJwt ( currentSession ! . accessToken ) ;
@@ -162,7 +126,7 @@ async function createOrReuseRegistryServiceAccount(): Promise<void> {
162
126
) ;
163
127
}
164
128
165
- async function createOrReuseActivationKey ( ) {
129
+ async function createOrReuseActivationKey ( machineName : string ) {
166
130
const currentSession = await signIntoRedHatDeveloperAccount ( ) ;
167
131
const accessTokenJson = parseJwt ( currentSession ! . accessToken ) ;
168
132
const client = new SubscriptionManagerClient ( {
@@ -184,7 +148,7 @@ async function createOrReuseActivationKey() {
184
148
} ) ;
185
149
}
186
150
187
- await runSubscriptionManagerRegister ( 'podman-desktop' , accessTokenJson . organization . id ) ;
151
+ await runSubscriptionManagerRegister ( machineName , 'podman-desktop' , accessTokenJson . organization . id ) ;
188
152
}
189
153
190
154
async function isSimpleContentAccessEnabled ( ) : Promise < boolean > {
@@ -197,28 +161,31 @@ async function isSimpleContentAccessEnabled(): Promise<boolean> {
197
161
return response . body && response . body . simpleContentAccess === 'enabled' ;
198
162
}
199
163
200
- async function isSubscriptionManagerInstalled ( ) : Promise < boolean > {
201
- const exitCode = await runSubscriptionManager ( ) ;
164
+ async function isSubscriptionManagerInstalled ( machineName : string ) : Promise < boolean > {
165
+ const exitCode = await runSubscriptionManager ( machineName ) ;
202
166
return exitCode === 0 ;
203
167
}
204
168
205
- async function installSubscriptionManger ( ) {
169
+ async function installSubscriptionManger ( machineName : string ) {
206
170
try {
207
- return await runRpmInstallSubscriptionManager ( ) ;
171
+ return await runRpmInstallSubscriptionManager ( machineName ) ;
208
172
} catch ( err ) {
209
173
console . error ( `Subscription manager installation failed. ${ String ( err ) } ` ) ;
210
174
TelemetryLogger . logError ( 'subscriptionManagerInstallationError' , { error : String ( err ) } ) ;
211
175
throw err ;
212
176
}
213
177
}
214
178
215
- async function isPodmanVmSubscriptionActivated ( ) {
216
- const exitCode = await runSubscriptionManagerActivationStatus ( ) ;
179
+ async function isPodmanVmSubscriptionActivated ( machineName : string ) {
180
+ const exitCode = await runSubscriptionManagerActivationStatus ( machineName ) ;
217
181
return exitCode === 0 ;
218
182
}
219
183
220
184
async function removeSession ( sessionId : string ) : Promise < void > {
221
- runSubscriptionManagerUnregister ( ) . catch ( console . error ) ; // ignore error in case vm subscription activation failed on login
185
+ const machineName = getRunningPodmanMachineName ( ) ;
186
+ if ( machineName ) {
187
+ runSubscriptionManagerUnregister ( machineName ) . catch ( console . error ) ; // ignore error in case vm subscription activation failed on login
188
+ }
222
189
removeRegistry ( ) ; // never fails, even if registry does not exist
223
190
const service = await getAuthenticationService ( ) ;
224
191
const session = await service . removeSession ( sessionId ) ;
@@ -276,7 +243,8 @@ async function configureRegistryAndActivateSubscription() {
276
243
title : 'Activating Red Hat Subscription' ,
277
244
} ,
278
245
async progress => {
279
- if ( ! isPodmanMachineRunning ( ) ) {
246
+ const podmanRunningMachineName = getRunningPodmanMachineName ( ) ;
247
+ if ( ! podmanRunningMachineName ) {
280
248
if ( isLinux ( ) ) {
281
249
await extensionApi . window . showInformationMessage (
282
250
'Signing into a Red Hat account requires a running Podman machine, and is currently not supported on a Linux host. Please start a Podman machine and try again.' ,
@@ -301,18 +269,17 @@ async function configureRegistryAndActivateSubscription() {
301
269
}
302
270
throw new Error ( 'SCA is not enabled and message closed' ) ;
303
271
}
304
-
305
- if ( ! ( await isSubscriptionManagerInstalled ( ) ) ) {
306
- await installSubscriptionManger ( ) ;
307
- await runStopPodmanMachine ( ) ;
308
- await runStartPodmanMachine ( ) ;
272
+ if ( ! ( await isSubscriptionManagerInstalled ( podmanRunningMachineName ) ) ) {
273
+ await installSubscriptionManger ( podmanRunningMachineName ) ;
274
+ await runStopPodmanMachine ( podmanRunningMachineName ) ;
275
+ await runStartPodmanMachine ( podmanRunningMachineName ) ;
309
276
}
310
- if ( ! ( await isPodmanVmSubscriptionActivated ( ) ) ) {
277
+ if ( ! ( await isPodmanVmSubscriptionActivated ( podmanRunningMachineName ) ) ) {
311
278
const facts = {
312
279
supported_architectures : 'aarch64,x86_64' ,
313
280
} ;
314
- await runCreateFactsFile ( JSON . stringify ( facts , undefined , 2 ) ) ;
315
- await createOrReuseActivationKey ( ) ;
281
+ await runCreateFactsFile ( podmanRunningMachineName , JSON . stringify ( facts , undefined , 2 ) ) ;
282
+ await createOrReuseActivationKey ( podmanRunningMachineName ) ;
316
283
}
317
284
}
318
285
} ,
@@ -326,9 +293,8 @@ async function configureRegistryAndActivateSubscription() {
326
293
if ( ! telemetryData . successful && currentSession ?. id ) {
327
294
removeSession ( currentSession . id ) ; // if at least one fail, remove session
328
295
}
329
-
330
- TelemetryLogger . logUsage ( 'signin' , telemetryData ) ;
331
296
}
297
+ TelemetryLogger . logUsage ( 'signin' , telemetryData ) ;
332
298
}
333
299
334
300
export async function activate ( context : extensionApi . ExtensionContext ) : Promise < void > {
0 commit comments