-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathworkspacePreferencesApi.ts
More file actions
142 lines (121 loc) · 4.52 KB
/
workspacePreferencesApi.ts
File metadata and controls
142 lines (121 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright (c) 2018-2025 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 * as k8s from '@kubernetes/client-node';
import { createError } from '@/devworkspaceClient/services/helpers/createError';
import {
CoreV1API,
prepareCoreV1API,
} from '@/devworkspaceClient/services/helpers/prepareCoreV1API';
import { IWorkspacePreferencesApi } from '@/devworkspaceClient/types';
const ERROR_LABEL = 'CORE_V1_API_ERROR';
export const DEV_WORKSPACE_PREFERENCES_CONFIGMAP = 'workspace-preferences-configmap';
export const SKIP_AUTHORIZATION_KEY = 'skip-authorisation';
export const TRUSTED_SOURCES_KEY = 'trusted-sources';
export class WorkspacePreferencesApiService implements IWorkspacePreferencesApi {
private readonly coreV1API: CoreV1API;
constructor(kc: k8s.KubeConfig) {
this.coreV1API = prepareCoreV1API(kc);
}
async getWorkspacePreferences(namespace: string): Promise<api.IWorkspacePreferences> {
try {
const response = await this.coreV1API.readNamespacedConfigMap({
name: DEV_WORKSPACE_PREFERENCES_CONFIGMAP,
namespace,
});
const data = response.data || {};
if (!data[SKIP_AUTHORIZATION_KEY]) {
data[SKIP_AUTHORIZATION_KEY] = '[]';
}
return Object.keys(data).reduce((acc, key) => {
if (key === SKIP_AUTHORIZATION_KEY) {
if (data[key] === '[]') {
acc[key] = [];
} else {
const providers = data[key].replace('[', '').replace(']', '').split(/,\s+/);
acc[key] = providers as api.GitProvider[];
}
} else {
acc[key] = JSON.parse(data[key]);
}
return acc;
}, {} as api.IWorkspacePreferences);
} catch (e) {
throw createError(e, ERROR_LABEL, 'Unable to get workspace preferences data');
}
}
private async updateWorkspacePreferences(
namespace: string,
updatedData: api.IWorkspacePreferences,
): Promise<void> {
const data = Object.keys(updatedData).reduce(
(acc, key) => {
if (key === SKIP_AUTHORIZATION_KEY) {
acc[key] = '[' + updatedData[key].join(', ') + ']';
} else {
acc[key] = JSON.stringify(updatedData[key]);
}
return acc;
},
{} as Record<string, string>,
);
try {
await this.coreV1API.patchNamespacedConfigMap({
name: DEV_WORKSPACE_PREFERENCES_CONFIGMAP,
namespace,
body: { data },
});
} catch (error) {
const message = `Unable to update workspace preferences in the namespace "${namespace}"`;
throw createError(error, ERROR_LABEL, message);
}
}
public async removeProviderFromSkipAuthorizationList(
namespace: string,
provider: api.GitProvider,
): Promise<void> {
const devWorkspacePreferences = await this.getWorkspacePreferences(namespace);
const skipAuthorization = devWorkspacePreferences[SKIP_AUTHORIZATION_KEY].filter(
(val: string) => val !== provider,
);
const data = Object.assign({}, devWorkspacePreferences, {
[SKIP_AUTHORIZATION_KEY]: skipAuthorization,
});
await this.updateWorkspacePreferences(namespace, data);
}
public async addTrustedSource(
namespace: string,
trustedSource: api.TrustedSourceAll | api.TrustedSourceUrl,
): Promise<void> {
const devWorkspacePreferences = await this.getWorkspacePreferences(namespace);
const _trustedSources = devWorkspacePreferences[TRUSTED_SOURCES_KEY] as api.TrustedSources;
let data: api.IWorkspacePreferences;
if (trustedSource === '*') {
data = Object.assign({}, devWorkspacePreferences, {
[TRUSTED_SOURCES_KEY]: trustedSource,
});
} else {
const trustedSources = Array.isArray(_trustedSources) ? _trustedSources : [];
trustedSources.push(trustedSource);
data = Object.assign({}, devWorkspacePreferences, {
[TRUSTED_SOURCES_KEY]: trustedSources,
});
}
await this.updateWorkspacePreferences(namespace, data);
}
public async removeTrustedSources(namespace: string): Promise<void> {
const devWorkspacePreferences = await this.getWorkspacePreferences(namespace);
const data = Object.assign({}, devWorkspacePreferences);
delete data[TRUSTED_SOURCES_KEY];
await this.updateWorkspacePreferences(namespace, data);
}
}