1010
1111/* eslint-disable header/header */
1212
13- import * as k8s from '@kubernetes/client-node' ;
1413import { AxiosInstance } from 'axios' ;
1514import * as fs from 'fs-extra' ;
1615import { inject , injectable } from 'inversify' ;
1716import * as path from 'path' ;
1817
1918import { GithubService , GithubUser } from '../api/github-service' ;
2019import { Logger } from '../logger' ;
21- import { K8SServiceImpl } from './k8s-service-impl' ;
22- import { base64Decode , base64Encode , createLabelsSelector , randomString } from './utils' ;
2320
2421const 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 ( )
3724export 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}
0 commit comments