-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathauth-config.ts
145 lines (135 loc) · 3.79 KB
/
auth-config.ts
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
/**
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
export interface FineGrainedAccessSpecs {
users: string[],
roleName: string,
pattern: string,
templateName: string
}
export class AuthConfig {
private static readonly adminRolePermissions: string[] = [
'Overall/Administer',
'Overall/Read',
'Job/Move',
'Job/Build',
'Job/Read',
'Job/Delete',
'Job/Create',
'Job/Discover',
'Job/Cancel',
'Job/Configure',
'Job Config History/DeleteEntry',
'Job/Workspace',
'Credentials/Delete',
'Credentials/ManageDomains',
'Credentials/Update',
'Credentials/View',
'Credentials/Create',
'Manage ownership/Nodes',
'Manage ownership/Jobs',
'Agent/Configure',
'Agent/Create',
'Agent/Build',
'Agent/Provision',
'Agent/Connect',
'Agent/Delete',
'Agent/Disconnect',
'Run/Replay',
'Run/Delete',
'Run/Update',
'View/Delete',
'View/Read',
'View/Create',
'View/Configure',
'SCM/Tag',
];
private static readonly readOnlyRolePermissions: string[] = [
'Overall/Read',
'Job/Read',
'View/Read',
];
private static readonly builderTemplatePermissions: string[] = [
'Job/Build',
'Job/Cancel',
'Job/Discover',
'Job/Read',
'Lockable Resources/View',
'Run/Replay',
'Metrics/View',
'View/Read',
];
public static addOidcConfigToJenkinsYaml(yamlObject: any, authType: string, admins?: string[], fineGrainedAccessItems?: FineGrainedAccessSpecs[]): any {
const jenkinsYaml: any = yamlObject;
let adminUsers: string[] = ['admin'];
const readOnlyUsers: string[] = ['anonymous', 'authenticated'];
if (admins) {
adminUsers = adminUsers.concat(admins);
}
const oidcConfig: { [x: string]: any; } = {
oic: {
clientId: 'clientId',
clientSecret: 'clientSecret',
disableSslVerification: false,
userNameField: 'sub',
escapeHatchEnabled: false,
logoutFromOpenidProvider: true,
postLogoutRedirectUrl: '',
escapeHatchSecret: 'random',
serverConfiguration: {},
},
};
const githubAuthConfig: { [x: string]: any; } = {
github: {
githubWebUri: 'https://github.com',
githubApiUri: 'https://api.github.com',
clientID: 'clientID',
clientSecret: 'clientSecret',
oauthScopes: 'read:org,user:email',
},
};
const rolesAndPermissions: { [x: string]: any; } = {
roleBased: {
permissionTemplates: [{
name: 'builder-template',
permissions: AuthConfig.builderTemplatePermissions,
}],
roles: {
global: [{
entries: adminUsers.map((user) => ({ user })),
name: 'admin',
pattern: '.*',
permissions: AuthConfig.adminRolePermissions
,
},
{
entries: readOnlyUsers.map((user) => ({ user })),
name: 'read',
pattern: '.*',
permissions: AuthConfig.readOnlyRolePermissions,
},
],
},
},
};
jenkinsYaml.jenkins.authorizationStrategy = rolesAndPermissions;
if (typeof fineGrainedAccessItems !== 'undefined') {
jenkinsYaml.jenkins.authorizationStrategy.roleBased.roles.items = fineGrainedAccessItems.map((item) => ({
entries: item.users.map((user) => ({ user })),
name: item.roleName,
pattern: item.pattern,
templateName: item.templateName,
}));
}
if (authType === 'github') {
jenkinsYaml.jenkins.securityRealm = githubAuthConfig;
} else {
jenkinsYaml.jenkins.securityRealm = oidcConfig;
}
return jenkinsYaml;
}
}