-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction.ts
More file actions
155 lines (140 loc) · 4.84 KB
/
function.ts
File metadata and controls
155 lines (140 loc) · 4.84 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
143
144
145
146
147
148
149
150
151
152
153
154
155
import type { Environment, PluginResult, Project, StepCall, UserObject } from '@cpn-console/hooks'
import type { KeycloakProjectApi } from '@cpn-console/keycloak-plugin/types/class.js'
import type { Gitlab as GitlabInterface } from '@gitbeaker/core'
import { parseError, specificallyDisabled } from '@cpn-console/hooks'
import { compressUUID, removeTrailingSlash, requiredEnv } from '@cpn-console/shared'
import { Gitlab } from '@gitbeaker/rest'
import { deleteKeycloakGroup, ensureKeycloakGroups } from './keycloak.js'
import { isNewNsName, type TenantKeycloakMapper } from './utils.js'
import { deleteGitlabYamlConfig, upsertGitlabConfig } from './yaml.js'
const okSkipped: PluginResult = {
status: {
result: 'OK',
message: 'Plugin disabled',
},
}
export type ListPerms = Record<'prod' | 'hors-prod', Record<'view' | 'edit', UserObject['id'][]>>
function getListPrems(environments: Environment[]): ListPerms {
const allProdPerms = environments
.filter(env => env.stage === 'prod')
.map(env => env.permissions)
.flat()
const allHProdPerms = environments
.filter(env => env.stage !== 'prod')
.map(env => env.permissions)
.flat()
const listPerms: ListPerms = {
'hors-prod': {
edit: [],
view: [],
},
prod: {
edit: [],
view: [],
},
}
for (const permission of allProdPerms) {
if (permission.permissions.rw && !listPerms.prod.edit.includes(permission.userId)) {
listPerms.prod.edit.push(permission.userId)
}
if (permission.permissions.ro && !listPerms.prod.view.includes(permission.userId)) {
listPerms.prod.view.push(permission.userId)
}
}
for (const permission of allHProdPerms) {
if (permission.permissions.rw && !listPerms['hors-prod'].edit.includes(permission.userId)) {
listPerms['hors-prod'].edit.push(permission.userId)
}
if (permission.permissions.ro && !listPerms['hors-prod'].view.includes(permission.userId)) {
listPerms['hors-prod'].view.push(permission.userId)
}
}
return listPerms
}
function getGitlabApi(): GitlabInterface {
const gitlabUrl = removeTrailingSlash(requiredEnv('GITLAB_URL'))
const gitlabToken = requiredEnv('GITLAB_TOKEN')
return new Gitlab({ token: gitlabToken, host: gitlabUrl })
}
export const upsertProject: StepCall<Project> = async (payload) => {
try {
if (specificallyDisabled(payload.config.observability?.enabled)) {
return okSkipped
}
// init args
const project = payload.args
const keycloakApi = payload.apis.keycloak as KeycloakProjectApi
// init gitlab api
const gitlabApi = getGitlabApi()
const keycloakRootGroupPath = await keycloakApi.getProjectGroupPath()
const tenantRbacProd = [`${keycloakRootGroupPath}/grafana/prod-RW`, `${keycloakRootGroupPath}/grafana/prod-RO`]
const tenantRbacHProd = [`${keycloakRootGroupPath}/grafana/hprod-RW`, `${keycloakRootGroupPath}/grafana/hprod-RO`]
const compressedUUID = compressUUID(project.id)
const tenantsToCreate: TenantKeycloakMapper = {}
for (const environment of payload.args.environments) {
if (!environment.apis.kubernetes) {
throw new Error(`no kubernetes apis on environment ${environment.name}`)
}
const name = isNewNsName(await environment.apis.kubernetes.getNsName()) ? compressedUUID : project.slug
if (environment.stage === 'prod') {
tenantsToCreate[`prod-${name}`] = {
groups: tenantRbacProd,
name,
type: 'prod',
}
} else {
tenantsToCreate[`hprod-${name}`] = {
groups: tenantRbacHProd,
name,
type: 'hprod',
}
}
}
const listPerms = getListPrems(project.environments)
// Upsert or delete Gitlab config based on prod/non-prod environment
const yamlResult = await upsertGitlabConfig(project, gitlabApi, tenantsToCreate)
await ensureKeycloakGroups(listPerms, keycloakApi)
return {
status: {
result: 'OK',
message: yamlResult,
},
}
} catch (error) {
return {
status: {
result: 'KO',
message: 'An error happened while creating Kibana resources',
},
error: parseError(error),
}
}
}
export const deleteProject: StepCall<Project> = async (payload) => {
try {
if (specificallyDisabled(payload.config.observability?.enabled)) {
return okSkipped
}
const project = payload.args
const gitlabApi = getGitlabApi()
const keycloakApi = payload.apis.keycloak as KeycloakProjectApi
await Promise.all([
deleteKeycloakGroup(keycloakApi),
deleteGitlabYamlConfig(project, gitlabApi),
])
return {
status: {
result: 'OK',
message: 'Deleted',
},
}
} catch (error) {
return {
status: {
result: 'OK',
message: 'An error happened while deleting resources',
},
error: JSON.stringify(error),
}
}
}