Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add group check for cluster-admin #465

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions backend/src/utils/adminUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
V1ClusterRoleBindingList,
} from '@kubernetes/client-node';
import { KubeFastifyInstance } from '../types';
import { getAdminGroups, getAllowedGroups, getGroup } from './groupsUtils';
import { getAdminGroups, getAllGroupsByUser, getAllowedGroups, getGroup } from './groupsUtils';
import { flatten, uniq } from 'lodash';

const SYSTEM_AUTHENTICATED = 'system:authenticated';
Expand Down Expand Up @@ -91,11 +91,17 @@ export const isUserAllowed = async (
}
};

const checkRoleBindings = (roleBindings: V1ClusterRoleBindingList, username: string): boolean => {
const checkRoleBindings = (
roleBindings: V1ClusterRoleBindingList,
username: string,
groups: string[],
): boolean => {
return (
roleBindings.items.filter(
(role: V1ClusterRoleBinding): boolean =>
role.subjects?.some((subject) => subject.name === username) &&
role.subjects?.some(
(subject) => subject.name === username || groups.includes(subject.name),
) &&
role.roleRef.kind === 'ClusterRole' &&
role.roleRef.name === 'cluster-admin',
).length !== 0
Expand All @@ -110,8 +116,9 @@ export const isUserClusterRole = async (
try {
const clusterrolebinding = await fastify.kube.rbac.listClusterRoleBinding();
const rolebinding = await fastify.kube.rbac.listNamespacedRoleBinding(namespace);
const isAdminClusterRoleBinding = checkRoleBindings(clusterrolebinding.body, username);
const isAdminRoleBinding = checkRoleBindings(rolebinding.body, username);
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}`);
Expand Down
17 changes: 17 additions & 0 deletions backend/src/utils/groupsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,20 @@ export const getAllGroups = async (customObjectsApi: CustomObjectsApi): Promise<
throw new Error(`Failed to list groups.`);
}
};

export const getAllGroupsByUser = async (
customObjectsApi: CustomObjectsApi,
username: string,
): Promise<string[]> => {
try {
const adminGroupResponse = await customObjectsApi.listClusterCustomObject(
'user.openshift.io',
'v1',
'groups',
);
const groups = adminGroupResponse.body as GroupCustomObject;
return groups.items.filter((x) => x.users.includes(username)).map((x) => x.metadata.name);
} catch (e) {
throw new Error(`Failed to list groups filtered by username.`);
}
};