-
Notifications
You must be signed in to change notification settings - Fork 166
/
adminUtils.ts
150 lines (139 loc) · 4.82 KB
/
adminUtils.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
146
147
148
149
150
import {
CustomObjectsApi,
V1ClusterRoleBinding,
V1ClusterRoleBindingList,
} from '@kubernetes/client-node';
import { KubeFastifyInstance } from '../types';
import { getAdminGroups, getAllGroupsByUser, getAllowedGroups, getGroup } from './groupsUtils';
import { flatten, uniq } from 'lodash';
const SYSTEM_AUTHENTICATED = 'system:authenticated';
/** Usernames with invalid characters can start with `b64:` to keep their unwanted characters */
export const KUBE_SAFE_PREFIX = 'b64:';
const getGroupUserList = async (
fastify: KubeFastifyInstance,
groupListNames: string[],
): Promise<string[]> => {
const customObjectApi = fastify.kube.customObjectsApi;
return Promise.all(groupListNames.map((group) => getGroup(customObjectApi, group))).then(
(usersPerGroup: string[][]) => uniq(flatten(usersPerGroup)),
);
};
export const getAdminUserList = async (fastify: KubeFastifyInstance): Promise<string[]> => {
const adminGroups = getAdminGroups();
const adminGroupsList = adminGroups
.split(',')
.filter((groupName) => groupName && !groupName.startsWith('system:')); // Handle edge-cases and ignore k8s defaults
adminGroupsList.push('cluster-admins');
return getGroupUserList(fastify, adminGroupsList);
};
export const getAllowedUserList = async (fastify: KubeFastifyInstance): Promise<string[]> => {
const allowedGroups = getAllowedGroups();
const allowedGroupList = allowedGroups
.split(',')
.filter((groupName) => groupName && !groupName.startsWith('system:')); // Handle edge-cases and ignore k8s defaults
return getGroupUserList(fastify, allowedGroupList);
};
export const getGroupsConfig = async (
fastify: KubeFastifyInstance,
customObjectApi: CustomObjectsApi,
username: string,
): Promise<boolean> => {
try {
const adminGroups = getAdminGroups();
const adminGroupsList = adminGroups.split(',');
if (adminGroupsList.includes(SYSTEM_AUTHENTICATED)) {
throw new Error('It is not allowed to set system:authenticated as admin group.');
} else {
return await checkUserInGroups(fastify, customObjectApi, adminGroupsList, username);
}
} catch (e) {
fastify.log.error(e, 'Error getting groups config');
return false;
}
};
export const isUserAdmin = async (
fastify: KubeFastifyInstance,
username: string,
namespace: string,
): Promise<boolean> => {
const isAdmin: boolean = await isUserClusterRole(fastify, username, namespace);
return isAdmin || (await getGroupsConfig(fastify, fastify.kube.customObjectsApi, username));
};
export const isUserAllowed = async (
fastify: KubeFastifyInstance,
username: string,
): Promise<boolean> => {
try {
const allowedGroups = getAllowedGroups();
const allowedGroupsList = allowedGroups.split(',');
if (allowedGroupsList.includes(SYSTEM_AUTHENTICATED)) {
return true;
} else {
return await checkUserInGroups(
fastify,
fastify.kube.customObjectsApi,
allowedGroupsList,
username,
);
}
} catch (e) {
fastify.log.error(e, 'Error determining isUserAllowed.');
return false;
}
};
const checkRoleBindings = (
roleBindings: V1ClusterRoleBindingList,
username: string,
groups: string[],
): boolean => {
return (
roleBindings.items.filter(
(role: V1ClusterRoleBinding): boolean =>
role.subjects?.some(
(subject) => subject.name === username || groups.includes(subject.name),
) &&
role.roleRef.kind === 'ClusterRole' &&
role.roleRef.name === 'cluster-admin',
).length !== 0
);
};
export const isUserClusterRole = async (
fastify: KubeFastifyInstance,
username: string,
namespace: string,
): Promise<boolean> => {
try {
const clusterrolebinding = await fastify.kube.rbac.listClusterRoleBinding();
const rolebinding = await fastify.kube.rbac.listNamespacedRoleBinding(namespace);
const groups = await getAllGroupsByUser(fastify.kube.customObjectsApi, username);
const isAdminClusterRoleBinding = checkRoleBindings(clusterrolebinding.body, username, groups);
const isAdminRoleBinding = checkRoleBindings(rolebinding.body, username, groups);
return isAdminClusterRoleBinding || isAdminRoleBinding;
} catch (e) {
fastify.log.error(
`Failed to list rolebindings for user, ${e.response?.body?.message || e.message}`,
);
return false;
}
};
const checkUserInGroups = async (
fastify: KubeFastifyInstance,
customObjectApi: CustomObjectsApi,
groupList: string[],
userName: string,
): Promise<boolean> => {
for (const group of groupList) {
try {
const groupUsers = await getGroup(customObjectApi, group);
if (
groupUsers?.includes(userName) ||
groupUsers?.includes(`${KUBE_SAFE_PREFIX}${userName}`)
) {
return true;
}
} catch (e) {
fastify.log.error(e, 'Error checking if user is in group.');
}
}
return false;
};