Skip to content

Commit

Permalink
Merge pull request #60 from sighupio/fix/delete-user
Browse files Browse the repository at this point in the history
 fix delete user resource bug
  • Loading branch information
Rampage1xx authored Feb 23, 2021
2 parents b0fb7cd + 6d6a153 commit 8f5dfac
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 2 additions & 0 deletions internal/resources/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ func (r *resourcesService) CreateUser(ctx context.Context, username string) User
// the PermissionManagerUser CRD object associated to the user with the given username.
func (r *resourcesService) DeleteUser(ctx context.Context, username string) {
metadataName := "permissionmanager.user." + username

_, err := r.kubeclient.AppsV1().RESTClient().Delete().AbsPath(resourceURL + "/" + metadataName).DoRaw(ctx)

if err != nil {
log.Printf("Failed to delete user:%s\n %v\n", username, err)
}
Expand Down
1 change: 1 addition & 0 deletions web-client/src/components/edit-user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export default function EditUser({user}: EditUserParameters) {
* delete all the user-resources currently in the k8s cluster
*/
async function deleteUserResources() {

await httpRequests.rolebindingRequests.delete.rolebinding(rbs);
await httpRequests.rolebindingRequests.delete.clusterRolebinding(crbs);
}
Expand Down
7 changes: 5 additions & 2 deletions web-client/src/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export const VERBS = [
'delete',
]

export const templateNamespacedResourceRolePrefix = 'template-namespaced-resources___'
export const resourceSeparator = '___'

export const templateNamespacedResourceRolePrefix = 'template-namespaced-resources' + resourceSeparator

export const templateClusterResourceRolePrefix = 'template-cluster-resources' + resourceSeparator

export const templateClusterResourceRolePrefix = 'template-cluster-resources___'
16 changes: 13 additions & 3 deletions web-client/src/services/role.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {templateClusterResourceRolePrefix} from "../constants";
import {resourceSeparator, templateClusterResourceRolePrefix} from "../constants";
import uuid from "uuid";
import {ClusterRoleBinding, RoleBinding} from "../hooks/useRbac";

Expand Down Expand Up @@ -49,11 +49,21 @@ export interface ExtractedUserRoles {
*/
export function extractUsersRoles(roleBindings: RoleBinding[], clusterRoleBindings: ClusterRoleBinding[], username: string): ExtractedUserRoles {
const rbs = (roleBindings || []).filter(rb => {
return rb.metadata.name.startsWith(username)
const separatedResourceName = rb.metadata.name.split(resourceSeparator);

if (separatedResourceName.length === 0) return false

// the first split always contains the name of the user
return separatedResourceName[0] === username
})

const crbs = (clusterRoleBindings || []).filter(crb => {
return crb.metadata.name.startsWith(username)
const separatedResourceName = crb.metadata.name.split(resourceSeparator);

if (separatedResourceName.length === 0) return false

// the first split always contains the name of the user
return separatedResourceName[0] === username
})

const normalizedRoleBindings: NormalizedRoleBinding[] = [...rbs, ...crbs]
Expand Down
12 changes: 6 additions & 6 deletions web-client/src/services/rolebindingCreateRequests.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {templateClusterResourceRolePrefix} from "../constants";
import {resourceSeparator, templateClusterResourceRolePrefix} from "../constants";
import {ClusterAccess} from "../components/types";
import {AxiosInstance, AxiosResponse} from "axios";
import {AggregatedRoleBinding} from "./role";
Expand Down Expand Up @@ -144,7 +144,7 @@ export class RolebindingCreateRequests {
// we grab all the 'ALL_NAMESPACE' rolebindings and create them on the backend
for (const allNamespaceRolebinding of aggregatedRoleBindings.filter(e => e.namespaces === 'ALL_NAMESPACES')) {
// we construct the resource name
const clusterRolebindingName = username + '___' + allNamespaceRolebinding.roleName + 'all_namespaces'
const clusterRolebindingName = username + resourceSeparator + allNamespaceRolebinding.roleName + 'all_namespaces'

// means that we already created the resource
if (consumed.includes(clusterRolebindingName)) continue;
Expand All @@ -165,8 +165,8 @@ export class RolebindingCreateRequests {
for (const namespace of namespacedRoleBinding.namespaces) {

// we construct the resource name
const rolebindingName = username + '___' + namespacedRoleBinding.roleName + '___' + namespace

const rolebindingName = username + resourceSeparator + namespacedRoleBinding.roleName + resourceSeparator + namespace
// means that we already created the resource
if (consumed.includes(rolebindingName)) continue;

Expand All @@ -191,9 +191,9 @@ export class RolebindingCreateRequests {
return;
}

const roleName = username + '___' + this.getRoleName(clusterAccess);
const roleName = username + resourceSeparator + this.getRoleName(clusterAccess);

const clusterRolebindingName = username + '___' + roleName
const clusterRolebindingName = username + resourceSeparator + roleName

//todo this must be changed in the future to support dynamic cluster roles. Right now it's just a single api call based on a radio select
await this.clusterRolebinding({
Expand Down

0 comments on commit 8f5dfac

Please sign in to comment.