Skip to content

Commit 83f674d

Browse files
test
Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
1 parent 9c6afbd commit 83f674d

4 files changed

Lines changed: 6 additions & 142 deletions

File tree

code/extensions/che-api/src/api/github-service.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ export const GithubService = Symbol('GithubService');
2222
export interface GithubService {
2323
getToken(): Promise<string>;
2424

25-
/**
26-
* Persists the given token to the corresponding secret, the in-memory value will be updated as well.
27-
* A new secret will be created if there is no secret yet.
28-
* Note: The existing token will be owerriten by the given one.
29-
*/
30-
persistDeviceAuthToken(token: string): Promise<void>;
31-
32-
/* Removes Device Authentication secret, extracts a token from another source (.git-credentials/credentials file or git-credentials secret) */
33-
removeDeviceAuthToken(): Promise<void>;
34-
3525
getUser(): Promise<GithubUser>;
3626
getTokenScopes(token: string): Promise<string[]>;
3727
}

code/extensions/che-api/src/impl/github-service-impl.ts

Lines changed: 3 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,23 @@
1010

1111
/* eslint-disable header/header */
1212

13-
import * as k8s from '@kubernetes/client-node';
1413
import { AxiosInstance } from 'axios';
1514
import * as fs from 'fs-extra';
1615
import { inject, injectable } from 'inversify';
1716
import * as path from 'path';
1817

1918
import { GithubService, GithubUser } from '../api/github-service';
2019
import { Logger } from '../logger';
21-
import { K8SServiceImpl } from './k8s-service-impl';
22-
import { base64Decode, base64Encode, createLabelsSelector, randomString } from './utils';
2320

2421
const GIT_CREDENTIALS_PATH = path.resolve('/.git-credentials', 'credentials');
25-
const GIT_CREDENTIAL_LABEL = {
26-
'controller.devfile.io/git-credential': 'true'
27-
};
28-
const DEVICE_AUTHENTICATION_LABEL = {
29-
'che.eclipse.org/device-authentication': 'true'
30-
}
31-
const DEVICE_AUTHENTICATION_LABEL_SELECTOR: string = createLabelsSelector(DEVICE_AUTHENTICATION_LABEL);
32-
const SCM_URL_ATTRIBUTE = 'che.eclipse.org/scm-url';
33-
const GITHUB_URL = 'https://github.com';
34-
const GIT_CREDENTIALS_LABEL_SELECTOR: string = createLabelsSelector(GIT_CREDENTIAL_LABEL);
3522

3623
@injectable()
3724
export class GithubServiceImpl implements GithubService {
3825
private token: string | undefined;
3926

4027
constructor(
4128
@inject(Logger) private logger: Logger,
42-
@inject(K8SServiceImpl) private readonly k8sService: K8SServiceImpl,
29+
// @inject(K8SServiceImpl) private readonly k8sService: K8SServiceImpl,
4330
@inject(Symbol.for('AxiosInstance')) private readonly axiosInstance: AxiosInstance
4431
) {
4532
this.initializeToken();
@@ -72,54 +59,6 @@ export class GithubServiceImpl implements GithubService {
7259
return result.headers['x-oauth-scopes'].split(', ');
7360
}
7461

75-
async persistDeviceAuthToken(token: string): Promise<void> {
76-
this.token = token;
77-
this.logger.info(`Github Service: adding token to the device-authentication secret...`);
78-
79-
const deviceAuthSecrets = await this.k8sService.getSecret(DEVICE_AUTHENTICATION_LABEL_SELECTOR);
80-
if (deviceAuthSecrets.length < 1) {
81-
this.logger.info(`Github Service: device-authentication secret not found, creating a new secret...`);
82-
83-
const namespace = this.k8sService.getDevWorkspaceNamespace();
84-
const newSecret = toDeviceAuthSecret(token, namespace);
85-
await this.k8sService.createNamespacedSecret(newSecret);
86-
87-
this.logger.info(`Github Service: device-authentication secret was created successfully!`);
88-
return;
89-
}
90-
91-
const deviceAuthSecret = deviceAuthSecrets[0];
92-
this.logger.info(`Github Service: updating exsting device-authentication secret...`);
93-
94-
const data = {
95-
token: base64Encode(`${token}`)
96-
};
97-
98-
const updatedSecret = { ...deviceAuthSecret, data };
99-
const name = deviceAuthSecret.metadata?.name || `device-authentication-secret-${randomString(5).toLowerCase()}`;
100-
this.k8sService.replaceNamespacedSecret(name, updatedSecret);
101-
102-
this.logger.info(`Github Service: device-authentication secret was updated successfully!`);
103-
}
104-
105-
async removeDeviceAuthToken(): Promise<void> {
106-
this.logger.info(`Github Service: got request for removing a device-authentication secret`);
107-
const deviceAuthSecrets = await this.k8sService.getSecret(DEVICE_AUTHENTICATION_LABEL_SELECTOR);
108-
if (deviceAuthSecrets.length < 1) {
109-
this.logger.warn('Github Service: device-authentication secret not found');
110-
throw new Error('device-authentication secret not found');
111-
}
112-
113-
for (const secret of deviceAuthSecrets) {
114-
this.logger.info(`Github Service: removing device-authentication secret with ${secret.metadata?.name} name...`);
115-
await this.k8sService.deleteNamespacedSecret(secret);
116-
this.logger.info(`Github Service: device-authentication secret with ${secret.metadata?.name} name was deleted successfully!`);
117-
}
118-
119-
// another token should be used by the Github Service after removing the Device Authentication token
120-
this.initializeToken();
121-
}
122-
12362
private async initializeToken(): Promise<void> {
12463
this.logger.info('Github Service: extracting token...');
12564

@@ -136,19 +75,11 @@ export class GithubServiceImpl implements GithubService {
13675
this.logger.info('Github Service: git-credential token is used');
13776
return;
13877
}
139-
this.token = await this.getTokenFromSecret();
14078
}
14179

14280
/* Extracts a token from the device-authentication secret */
143-
private async getDeviceAuthToken(): Promise<string | undefined> {
144-
const deviceAuthSecrets = await this.k8sService.getSecret(DEVICE_AUTHENTICATION_LABEL_SELECTOR);
145-
this.logger.info(`Github Service: found ${deviceAuthSecrets.length} device-authentication secrets`);
146-
if (deviceAuthSecrets.length > 0) {
147-
const decodedToken = base64Decode(deviceAuthSecrets[0].data!.token);
148-
return decodedToken;
149-
} else {
150-
return undefined;
151-
}
81+
private async getDeviceAuthToken(): Promise<undefined> {
82+
return undefined;
15283
}
15384

15485
/* Extracts tokens from the .git-credentials/credentials file */
@@ -169,43 +100,4 @@ export class GithubServiceImpl implements GithubService {
169100
this.logger.info(`Github Service: found ${tokens.length} tokens in the ${GIT_CREDENTIALS_PATH} file`);
170101
return tokens;
171102
}
172-
173-
/* Extracts token from the git-credential secret */
174-
private async getTokenFromSecret(): Promise<string | undefined> {
175-
this.logger.info(`Github Service: looking for the corresponding git-credentials secret to get token...`);
176-
177-
const gitCredentialSecrets = await this.k8sService.getSecret(GIT_CREDENTIALS_LABEL_SELECTOR);
178-
if (gitCredentialSecrets.length === 0) {
179-
this.logger.warn('Github Service: token is not found');
180-
return undefined;
181-
}
182-
183-
const githubSecrets = gitCredentialSecrets.filter(secret => secret.metadata?.annotations?.[SCM_URL_ATTRIBUTE] === GITHUB_URL);
184-
this.logger.info(`Github Service: found ${githubSecrets.length} github secrets`);
185-
186-
const credentials = githubSecrets.length > 0 ? githubSecrets[0].data!.credentials : gitCredentialSecrets[0].data!.credentials;
187-
const decodedCredentials = base64Decode(credentials);
188-
const decodedToken = decodedCredentials.substring(decodedCredentials.lastIndexOf(':') + 1, decodedCredentials.indexOf('@'));
189-
this.logger.info('Github Service: a token from the git-credential secret is used');
190-
191-
return decodedToken;
192-
}
193-
}
194-
195-
function toDeviceAuthSecret(token: string, namespace: string): k8s.V1Secret {
196-
return {
197-
apiVersion: 'v1',
198-
kind: 'Secret',
199-
metadata: {
200-
name: `device-authentication-secret-${randomString(5).toLowerCase()}`,
201-
namespace,
202-
labels: {
203-
'che.eclipse.org/device-authentication': 'true'
204-
}
205-
},
206-
type: 'Opaque',
207-
data: {
208-
token: base64Encode(`${token}`)
209-
},
210-
};
211103
}

code/extensions/che-github-authentication/src/device-authentication.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,22 @@
1313
import { inject, injectable } from 'inversify';
1414
import * as vscode from 'vscode';
1515
import { ExtensionContext } from './extension-context';
16-
import { GitHubAuthProvider, GithubService } from './github';
16+
import { GitHubAuthProvider } from './github';
1717
import { CHANNEL_NAME, Logger } from './logger';
1818

1919
@injectable()
2020
export class DeviceAuthentication {
2121
constructor(
2222
@inject(Logger) private logger: Logger,
2323
@inject(ExtensionContext) private extensionContext: ExtensionContext,
24-
@inject(GitHubAuthProvider) private gitHubAuthProvider: GitHubAuthProvider,
25-
@inject(Symbol.for('GithubServiceInstance')) private githubService: GithubService
24+
@inject(GitHubAuthProvider) private gitHubAuthProvider: GitHubAuthProvider
2625
) {
2726
this.extensionContext.getContext().subscriptions.push(
2827
vscode.commands.registerCommand('github-authentication.device-code-flow.authentication', async () => this.trigger()),
2928
);
3029
this.logger.info('Device Authentication command has been registered');
3130

32-
this.extensionContext.getContext().subscriptions.push(
33-
vscode.commands.registerCommand('github-authentication.device-code-flow.remove-token', async () => this.removeDeviceAuthToken()),
34-
);
31+
3532
this.logger.info('Remove Device Authentication Token command has been registered');
3633
}
3734

@@ -58,7 +55,6 @@ export class DeviceAuthentication {
5855
this.logger.info(`Device Authentication: token for scopes: ${scopes} has been generated successfully`);
5956

6057
try {
61-
await this.githubService.persistDeviceAuthToken(token);
6258
await this.gitHubAuthProvider.createSession([scopes]);
6359
this.onTokenGenerated(scopes);
6460

@@ -73,18 +69,6 @@ export class DeviceAuthentication {
7369
}
7470
}
7571

76-
async removeDeviceAuthToken(): Promise<void> {
77-
try {
78-
await this.githubService.removeDeviceAuthToken();
79-
const message = 'The token was deleted successfully. Some operations may require Github Sign Out => Sign In to use another token.'
80-
vscode.window.showInformationMessage(message);
81-
} catch (error) {
82-
const message = `Can not remove Device Authentication token: ${error.message}`;
83-
vscode.window.showErrorMessage(message);
84-
}
85-
86-
}
87-
8872
private async onTokenGenerated(scopes: string): Promise<void> {
8973
const message = `A new session has been created for ${scopes} scopes. Please reload window to apply it.`
9074
const reloadNow = vscode.l10n.t('Reload Now');

code/extensions/che-github-authentication/src/github.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ export interface GithubUser {
2727

2828
export interface GithubService {
2929
getToken(): Promise<string>;
30-
persistDeviceAuthToken(token: string): Promise<void>;
31-
removeDeviceAuthToken(): Promise<void>;
3230
getUser(): Promise<GithubUser>;
3331
getTokenScopes(token: string): Promise<string[]>;
3432
}

0 commit comments

Comments
 (0)