-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dispatch permissions to frontend in experimental contexts mode …
…(#10383) * feat: dispatch resources permissions (backend) Signed-off-by: Philippe Martin <[email protected]> * feat: provide getContextsPermissions to main world Signed-off-by: Philippe Martin <[email protected]> * feat: store for contexts permissions Signed-off-by: Philippe Martin <[email protected]> * refactor: use KubernetesResourceName instead of string Signed-off-by: Philippe Martin <[email protected]> * refactor: revert "refactor: use KubernetesResourceName instead of string" This reverts commit 885552c6ef0b2a2e3f6ee6e1efc437a0f25ef7f8. Signed-off-by: Philippe Martin <[email protected]> * feat: use string type for resource name Signed-off-by: Philippe Martin <[email protected]> * fix: permissions store is inactive when non-experimental Signed-off-by: Philippe Martin <[email protected]> * fix: use map pattern + fix undefined Signed-off-by: Philippe Martin <[email protected]> * feat: add doc on permission.reason field Signed-off-by: Philippe Martin <[email protected]> * fix: remove use of any Signed-off-by: Philippe Martin <[email protected]> * fix: move Object.defineProperty into beforeAll Signed-off-by: Philippe Martin <[email protected]> * fix: do not make Window properties writable in tests Signed-off-by: Philippe Martin <[email protected]> --------- Signed-off-by: Philippe Martin <[email protected]>
- Loading branch information
Showing
9 changed files
with
398 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
export interface ContextPermission { | ||
contextName: string; | ||
// the resource name is a generic string type and not a string literal type, as we want to handle CRDs names | ||
resourceName: string; | ||
// permitted if allowed and not denied | ||
// > When multiple authorization modules are configured, each is checked in sequence. | ||
// > If any authorizer approves or denies a request, that decision is immediately returned | ||
// > and no other authorizer is consulted. If all modules have no opinion on the request, | ||
// > then the request is denied. An overall deny verdict means that the API server rejects | ||
// > the request and responds with an HTTP 403 (Forbidden) status. | ||
// (source: https://kubernetes.io/docs/reference/access-authn-authz/authorization/) | ||
permitted: boolean; | ||
// A free-form and optional text reason for the resource being allowed or denied. | ||
// We cannot rely on having a reason for every request. | ||
// For exemple on Kind cluster, a reason is given only when the access is allowed, no reason is done for denial. | ||
reason?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/renderer/src/stores/kubernetes-context-permission-experimental.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
import { get } from 'svelte/store'; | ||
import { beforeAll, expect, test, vi } from 'vitest'; | ||
|
||
import type { ContextPermission } from '/@api/kubernetes-contexts-permissions'; | ||
|
||
import { kubernetesContextsPermissions, kubernetesContextsPermissionsStore } from './kubernetes-context-permission'; | ||
|
||
const callbacks = new Map<string, () => Promise<void>>(); | ||
const eventEmitter = { | ||
receive: (message: string, callback: () => Promise<void>) => { | ||
callbacks.set(message, callback); | ||
}, | ||
}; | ||
|
||
beforeAll(() => { | ||
Object.defineProperty(global, 'window', { | ||
value: { | ||
kubernetesGetContextsPermissions: vi.fn(), | ||
getConfigurationValue: vi.fn(), | ||
addEventListener: eventEmitter.receive, | ||
events: { | ||
receive: eventEmitter.receive, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
test('kubernetesContextsPermissions in experimental states mode', async () => { | ||
vi.mocked(window.getConfigurationValue).mockResolvedValue(true); | ||
|
||
const initialValues: ContextPermission[] = []; | ||
const nextValues: ContextPermission[] = [ | ||
{ | ||
contextName: 'context1', | ||
resourceName: 'pods', | ||
permitted: true, | ||
}, | ||
{ | ||
contextName: 'context2', | ||
resourceName: 'deployments', | ||
permitted: false, | ||
}, | ||
]; | ||
vi.mocked(window.kubernetesGetContextsPermissions).mockResolvedValue(initialValues); | ||
|
||
kubernetesContextsPermissionsStore.setup(); | ||
|
||
// send 'extensions-already-started' event | ||
const callbackExtensionsStarted = callbacks.get('extensions-already-started'); | ||
expect(callbackExtensionsStarted).toBeDefined(); | ||
await callbackExtensionsStarted!(); | ||
|
||
await vi.waitFor(() => { | ||
const currentValue = get(kubernetesContextsPermissions); | ||
expect(currentValue).toEqual(initialValues); | ||
}, 500); | ||
|
||
// data has been updated in the backend | ||
vi.mocked(window.kubernetesGetContextsPermissions).mockResolvedValue(nextValues); | ||
|
||
// send an event indicating the data is updated | ||
const event = 'kubernetes-contexts-permissions'; | ||
const callback = callbacks.get(event); | ||
expect(callback).toBeDefined(); | ||
await callback!(); | ||
|
||
await vi.waitFor(() => { | ||
// check received data is updated | ||
const currentValue = get(kubernetesContextsPermissions); | ||
expect(currentValue).toEqual(nextValues); | ||
}, 500); | ||
}); |
Oops, something went wrong.