-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add reject the authorisation API
Signed-off-by: Oleksii Orel <[email protected]>
- Loading branch information
Showing
9 changed files
with
204 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/dashboard-backend/src/devworkspaceClient/services/devWorkspacePreferencesApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { api } from '@eclipse-che/common'; | ||
import { GitProvider } from '@eclipse-che/common/lib/dto/api'; | ||
import * as k8s from '@kubernetes/client-node'; | ||
|
||
import { createError } from '@/devworkspaceClient/services/helpers/createError'; | ||
import { | ||
CoreV1API, | ||
prepareCoreV1API, | ||
} from '@/devworkspaceClient/services/helpers/prepareCoreV1API'; | ||
import { IDevWorkspacePreferencesApi } from '@/devworkspaceClient/types'; | ||
|
||
const ERROR_LABEL = 'CORE_V1_API_ERROR'; | ||
const DEV_WORKSPACE_PREFERENCES_CONFIGMAP = 'workspace-preferences-configmap'; | ||
|
||
const SKIP_AUTORIZATION_KEY = 'skip-authorisation'; | ||
|
||
export class DevWorkspacePreferencesApiService implements IDevWorkspacePreferencesApi { | ||
private readonly coreV1API: CoreV1API; | ||
|
||
constructor(kc: k8s.KubeConfig) { | ||
this.coreV1API = prepareCoreV1API(kc); | ||
} | ||
|
||
async getWorkspacePreferences(namespace: string): Promise<api.IDevWorkspacePreferences> { | ||
try { | ||
const response = await this.coreV1API.readNamespacedConfigMap( | ||
DEV_WORKSPACE_PREFERENCES_CONFIGMAP, | ||
namespace, | ||
); | ||
const data = response.body.data; | ||
if (data === undefined) { | ||
throw new Error('Data is empty'); | ||
} | ||
|
||
const skipAuthorisation = | ||
data[SKIP_AUTORIZATION_KEY] && data[SKIP_AUTORIZATION_KEY] !== '[]' | ||
? data[SKIP_AUTORIZATION_KEY].replace(/^\[/, '').replace(/\]$/, '').split(', ') | ||
: []; | ||
|
||
return Object.assign({}, data, { | ||
[SKIP_AUTORIZATION_KEY]: skipAuthorisation, | ||
}) as api.IDevWorkspacePreferences; | ||
} catch (e) { | ||
throw createError(e, ERROR_LABEL, 'Unable to get workspace preferences data'); | ||
} | ||
} | ||
|
||
public async removeProviderFromSkipAuthorization( | ||
namespace: string, | ||
provider: GitProvider, | ||
): Promise<void> { | ||
const devWorkspacePreferences = await this.getWorkspacePreferences(namespace); | ||
|
||
const skipAuthorisation = devWorkspacePreferences[SKIP_AUTORIZATION_KEY].filter( | ||
(val: string) => val !== provider, | ||
); | ||
const skipAuthorisationStr = | ||
skipAuthorisation.length > 0 ? `[${skipAuthorisation.join(', ')}]` : '[]'; | ||
const data = Object.assign({}, devWorkspacePreferences, { | ||
[SKIP_AUTORIZATION_KEY]: skipAuthorisationStr, | ||
}); | ||
|
||
try { | ||
await this.coreV1API.patchNamespacedConfigMap( | ||
DEV_WORKSPACE_PREFERENCES_CONFIGMAP, | ||
namespace, | ||
{ data }, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
undefined, | ||
{ | ||
headers: { | ||
'content-type': k8s.PatchUtils.PATCH_FORMAT_STRATEGIC_MERGE_PATCH, | ||
}, | ||
}, | ||
); | ||
} catch (error) { | ||
const message = `Unable to update workspace preferences in the namespace "${namespace}"`; | ||
throw createError(undefined, ERROR_LABEL, message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/dashboard-backend/src/routes/api/workspacePreferences.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2018-2023 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'; | ||
|
||
import { baseApiPath } from '@/constants/config'; | ||
import { namespacedSchema, namespacedWorkspacePreferencesSchema } from '@/constants/schemas'; | ||
import { restParams } from '@/models'; | ||
import { getDevWorkspaceClient } from '@/routes/api/helpers/getDevWorkspaceClient'; | ||
import { getToken } from '@/routes/api/helpers/getToken'; | ||
import { getSchema } from '@/services/helpers'; | ||
|
||
const tags = ['WorkspacePreferences']; | ||
|
||
export function registerWorkspacePreferencesRoute(instance: FastifyInstance) { | ||
instance.register(async server => { | ||
server.get( | ||
`${baseApiPath}/workspacepreferences/:namespace`, | ||
getSchema({ tags, params: namespacedSchema }), | ||
async function (request: FastifyRequest) { | ||
const { namespace } = request.params as restParams.INamespacedParams; | ||
const token = getToken(request); | ||
const { devWorkspacePreferencesApi } = getDevWorkspaceClient(token); | ||
return devWorkspacePreferencesApi.getWorkspacePreferences(namespace); | ||
}, | ||
); | ||
|
||
server.delete( | ||
`${baseApiPath}/workspacepreferences/:namespace/skip-authorisation/:provider`, | ||
getSchema({ | ||
tags, | ||
params: namespacedWorkspacePreferencesSchema, | ||
response: { | ||
204: { | ||
description: 'The Provider is successfully removed from skip-authorisation list', | ||
type: 'null', | ||
}, | ||
}, | ||
}), | ||
async function (request: FastifyRequest, reply: FastifyReply) { | ||
const { namespace, provider } = request.params as restParams.IWorkspacePreferencesParams; | ||
const token = getToken(request); | ||
const { devWorkspacePreferencesApi } = getDevWorkspaceClient(token); | ||
await devWorkspacePreferencesApi.removeProviderFromSkipAuthorization(namespace, provider); | ||
reply.code(204); | ||
return reply.send(); | ||
}, | ||
); | ||
}); | ||
} |