Skip to content

Commit b599928

Browse files
authored
sign out: logout of registry and unregister sub-man (#51)
When signing out of the Red Hat account make sure to undo the actions of signing in: * Logging out of `registry.redhat.io` * Running `subscription-manager unregister` inside the podman machine Fixes: #41 Signed-off-by: Valentin Rothberg <[email protected]>
1 parent 0736811 commit b599928

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/extension.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**********************************************************************
2-
* Copyright (C) 2022 - 2023 Red Hat, Inc.
2+
* Copyright (C) 2022 - 2024 Red Hat, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ import {
2525
import { ServiceAccountV1, ContainerRegistryAuthorizerClient } from '@redhat-developer/rhcra-client';
2626
import path from 'node:path';
2727
import { accessSync, constants, existsSync, readFileSync } from 'node:fs';
28-
import { restartPodmanMachine, runRpmInstallSubscriptionManager, runSubscriptionManager, runSubscriptionManagerActivationStatus, runSubscriptionManagerRegister } from './podman-cli';
28+
import { restartPodmanMachine, runRpmInstallSubscriptionManager, runSubscriptionManager, runSubscriptionManagerActivationStatus, runSubscriptionManagerRegister, runSubscriptionManagerUnregister } from './podman-cli';
2929
import { SubscriptionManagerClient } from '@redhat-developer/rhsm-client';
3030
import { isLinux } from './util';
3131

@@ -80,6 +80,15 @@ async function createRegistry(username: string, secret: string, serverUrl: strin
8080
});
8181
}
8282

83+
async function removeRegistry(serverUrl: string = REGISTRY_REDHAT_IO): Promise<void> {
84+
extensionApi.registry.unregisterRegistry({
85+
serverUrl,
86+
username:'',
87+
secret: '',
88+
source: ''
89+
});
90+
}
91+
8392
// TODO: add listRegistries to registry API to allow search by
8493
// registry URL
8594
function isRedHatRegistryConfigured(): boolean {
@@ -207,6 +216,8 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
207216
return service.getSessions(scopes);
208217
},
209218
removeSession: async function (sessionId: string): Promise<void> {
219+
await runSubscriptionManagerUnregister()
220+
removeRegistry();
210221
const service = await getAuthService();
211222
const session = await service.removeSession(sessionId);
212223
onDidChangeSessions.fire({ removed: [session!] });

src/podman-cli.ts

+13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const PODMAN_COMMANDS = {
2626
SM_ACTIVATION_STATUS: () => 'machine ssh sudo subscription-manager status'.split(' '),
2727
SM_ACTIVATE_SUBS: (activationKeyName: string, orgId: string) =>
2828
`machine ssh sudo subscription-manager register --activationkey ${activationKeyName} --org ${orgId}`.split(' '),
29+
SM_DEACTIVATE_SUBS: () =>
30+
`machine ssh sudo subscription-manager unregister`.split(' '),
2931
MACHINE_STOP: () => 'machine stop'.split(' '),
3032
MACHINE_START: () => 'machine start'.split(' '),
3133
}
@@ -112,6 +114,17 @@ export async function runSubscriptionManagerRegister(activationKeyName: string,
112114
}
113115
}
114116

117+
export async function runSubscriptionManagerUnregister(): Promise<number | undefined> {
118+
try {
119+
const result = await extensionApi.process.exec(getPodmanCli(), PODMAN_COMMANDS.SM_DEACTIVATE_SUBS());
120+
return 0;
121+
} catch (err) {
122+
const exitCode = (err as extensionApi.RunError).exitCode;
123+
console.error(`Subscription manager registration returned exit code: ${exitCode}`);
124+
return exitCode;
125+
}
126+
}
127+
115128
export async function restartPodmanMachine() {
116129
await extensionApi.process.exec(getPodmanCli(), PODMAN_COMMANDS.MACHINE_STOP());
117130
await extensionApi.process.exec(getPodmanCli(), PODMAN_COMMANDS.MACHINE_START());

0 commit comments

Comments
 (0)