Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migrate ot openapi-typescript and openapi-fetch #635

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ redhat-authentication.cdix
tests/**/output
test-resources
test-results

src/rh-api/gen
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
}
},
"scripts": {
"build": "vite build && node ./scripts/build.cjs",
"generate:registry-authorizer": "npx openapi-typescript src/rh-api/registry-authorizer-schema.json -o src/rh-api/gen/registry-authorizer.d.ts",
"generate:subscription": "npx openapi-typescript src/rh-api/subscription-schema.json -o src/rh-api/gen/subscription.d.ts",
"build": "pnpm generate:subscription && pnpm generate:registry-authorizer && vite build && node ./scripts/build.cjs",
"watch": "vite build -w",
"format:check": "prettier --end-of-line auto --cache --check \"{src,types,scripts}/**/*.{ts,js}\"",
"format:fix": "prettier --cache --write \"{src,types,scripts}/**/*.{ts,js}\"",
Expand All @@ -49,11 +51,9 @@
"dependencies": {
"@podman-desktop/api": "^1.14.1",
"@podman-desktop/podman-extension-api": "^1.14.2",
"@redhat-developer/rhcra-client": "^0.0.1",
"@redhat-developer/rhsm-client": "^0.0.4",
"axios": "^1.8.2",
"js-yaml": "^4.1.0",
"object-hash": "3.0.0",
"openapi-fetch": "^0.13.4",
"openid-client": "^5.7.0"
},
"devDependencies": {
Expand Down
383 changes: 144 additions & 239 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions src/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

import type { AuthenticationGetSessionOptions, AuthenticationSession, ExtensionContext } from '@podman-desktop/api';
import { authentication, commands } from '@podman-desktop/api';
import { OrganizationService } from '@redhat-developer/rhsm-client';
import { afterEach, beforeEach, expect, suite, test, vi } from 'vitest';

import * as extension from './extension';
import * as podmanCli from './podman-cli';
import * as subscription from './subscription';
import { Organization } from './rh-api/subscription';
import * as subscription from './rh-api/subscription';
import { ExtensionTelemetryLogger } from './telemetry';
import * as util from './util';

Expand Down Expand Up @@ -180,8 +180,18 @@ suite('signin command telemetry reports', () => {
},
);
vi.spyOn(subscription, 'isRedHatRegistryConfigured').mockReturnValue(true);
vi.spyOn(podmanCli, 'getConnectionForRunningPodmanMachine').mockReturnValue('machine1');
vi.spyOn(OrganizationService.prototype, 'checkOrgScaCapability').mockResolvedValue({ body: {} });
vi.spyOn(podmanCli, 'getConnectionForRunningPodmanMachine').mockReturnValue({
providerId: '1',
connection: {
name: 'machine1',
type: 'podman',
endpoint: {
socketPath: '/path/to/the/socket',
},
status: () => 'started',
},
});
vi.spyOn(Organization.prototype, 'checkOrgScaCapability').mockResolvedValue({ body: {} });
await extension.activate(createExtContext());
expect(commandFunctionCopy!).toBeDefined();
await commandFunctionCopy!();
Expand Down
55 changes: 28 additions & 27 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
***********************************************************************/

import * as extensionApi from '@podman-desktop/api';
import type { ServiceAccountV1 } from '@redhat-developer/rhcra-client';
import { ContainerRegistryAuthorizerClient } from '@redhat-developer/rhcra-client';
import { SubscriptionManagerClient } from '@redhat-developer/rhsm-client';

import icon from '../icon.png';
import { onDidChangeSessions, RedHatAuthenticationService } from './authentication-service';
Expand All @@ -35,10 +32,11 @@ import {
runSubscriptionManagerRegister,
runSubscriptionManagerUnregister,
} from './podman-cli';
import { ContainerRegistryAuthorizerClient } from './rh-api/registry-authorizer';
import { isRedHatRegistryConfigured, REGISTRY_REDHAT_IO, SubscriptionManagerClient } from './rh-api/subscription';
import { SSOStatusBarItem } from './status-bar-item';
import { isRedHatRegistryConfigured, REGISTRY_REDHAT_IO, signIntoRedHatDeveloperAccount } from './subscription';
import { ExtensionTelemetryLogger as TelemetryLogger } from './telemetry';
import { isLinux } from './util';
import { isLinux, signIntoRedHatDeveloperAccount } from './util';

interface JwtToken {
organization: {
Expand Down Expand Up @@ -96,29 +94,32 @@ function removeRegistry(serverUrl: string = REGISTRY_REDHAT_IO): void {
async function createOrReuseRegistryServiceAccount(): Promise<void> {
const currentSession = await signIntoRedHatDeveloperAccount();
const accessTokenJson = parseJwt(currentSession!.accessToken);
const client = new ContainerRegistryAuthorizerClient({
const { serviceAccountsApiV1: saApiV1 } = new ContainerRegistryAuthorizerClient({
BASE: 'https://access.redhat.com/hydra/rest/terms-based-registry',
TOKEN: currentSession!.accessToken,
});
const saApiV1 = client.serviceAccountsApiV1;
let selectedServiceAccount: ServiceAccountV1 | undefined;
try {
selectedServiceAccount = await saApiV1.serviceAccountByNameUsingGet1(
'podman-desktop',
accessTokenJson.organization.id,
);
} catch (err) {
let { data: serviceAccount } = await saApiV1.serviceAccountByNameUsingGet1(
'podman-desktop',
accessTokenJson.organization.id,
);

if (!serviceAccount) {
// ignore error when there is no podman-desktop service account yet
selectedServiceAccount = await saApiV1.createServiceAccountUsingPost1({
const { data: createdServiceAccount } = await saApiV1.createServiceAccountUsingPost1({
name: 'podman-desktop',
description: 'Service account to use from Podman Desktop',
redHatAccountId: accessTokenJson.organization.id,
});
if (createdServiceAccount) {
serviceAccount = createdServiceAccount;
} else {
throw new Error(`Can't create registry authorizer service account.`);
}
}

await createRegistry(
selectedServiceAccount!.credentials!.username!,
selectedServiceAccount!.credentials!.password!,
serviceAccount!.credentials!.username!,
serviceAccount!.credentials!.password!,
currentSession.account.label,
);
}
Expand All @@ -131,20 +132,20 @@ async function createOrReuseActivationKey(connection: extensionApi.ProviderConta
TOKEN: currentSession!.accessToken,
});

try {
await client.activationKey.showActivationKey('podman-desktop');
// podman-desktop activation key exists
} catch (err) {
// ignore and continue with activation key creation
// TODO: add check that used role and usage exists in organization
await client.activationKey.createActivationKeys({
const { error: showKeyErr } = await client.activationKey.showActivationKey('podman-desktop');

if (showKeyErr) {
// error is undefined when activation key already exists
const { error: createKeyErr } = await client.activationKey.createActivationKeys({
name: 'podman-desktop',
role: 'RHEl Workstation',
usage: 'Development/Test',
serviceLevel: 'Self-Support',
});
if (createKeyErr) {
throw new Error(createKeyErr.error?.message);
}
}

await runSubscriptionManagerRegister(connection, 'podman-desktop', accessTokenJson.organization.id);
}

Expand All @@ -154,8 +155,8 @@ async function isSimpleContentAccessEnabled(): Promise<boolean> {
BASE: 'https://console.redhat.com/api/rhsm/v2',
TOKEN: currentSession!.accessToken,
});
const response = await client.organization.checkOrgScaCapability();
return !!response.body && response.body.simpleContentAccess === 'enabled';
const data = await client.organization.checkOrgScaCapability();
return data?.body?.simpleContentAccess === 'enabled';
}

async function isSubscriptionManagerInstalled(connection: extensionApi.ProviderContainerConnection): Promise<boolean> {
Expand Down
Loading
Loading