From f2ca740f79240a9883f65f7443a2af0e3f6cd323 Mon Sep 17 00:00:00 2001 From: Francois-Clement Brossard Date: Tue, 12 Dec 2023 21:53:51 +0900 Subject: [PATCH] [SR] Prevent change to snapshot name / repository for managed SLM policies (#172291) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Managed SLM policies `Snapshot name` and `Repository` field values can be changed from the **Snapshot and Restore** UI. This is particularly an issue with the `cloud-snapshot-policy` as changes to the snapshot name or target repository will lead to plan failures on ESS. Besides having a warning message displayed for managed SLM policies: > This is a managed policy. Changing this policy might affect other systems that use it. Proceed with caution. field values being editable is prone to errors. This PR disable the `Snapshot name` and `Repository` field for managed SLM policies to prevent edits. **Example - Regular SLM policy:** ![Untitled2](https://github.com/elastic/kibana/assets/7076736/64d501d2-3ab5-462a-af64-e04c2bc7721e) **Example - Managed SLM policy:** ![Untitled](https://github.com/elastic/kibana/assets/7076736/abf9f2a0-c672-4317-b6e6-942c80769cff) I'm open to suggestions if there is a better way to do this. This PR fixes #124916 ### Checklist - [x] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers) ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Yulia Čech <6585477+yuliacech@users.noreply.github.com> Co-authored-by: Yulia Cech --- .../client_integration/policy_edit.test.ts | 51 +++++++++++-------- .../policy_form/steps/step_logistics.tsx | 2 + 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/x-pack/plugins/snapshot_restore/__jest__/client_integration/policy_edit.test.ts b/x-pack/plugins/snapshot_restore/__jest__/client_integration/policy_edit.test.ts index 59e52d8cf6539..643b52202162b 100644 --- a/x-pack/plugins/snapshot_restore/__jest__/client_integration/policy_edit.test.ts +++ b/x-pack/plugins/snapshot_restore/__jest__/client_integration/policy_edit.test.ts @@ -7,7 +7,7 @@ import { act } from 'react-dom/test-utils'; -import { setupEnvironment, pageHelpers, nextTick } from './helpers'; +import { setupEnvironment, pageHelpers } from './helpers'; import { API_BASE_PATH } from '../../common'; import { PolicyForm } from '../../public/application/components/policy_form'; import { PolicyFormTestBed } from './helpers/policy_form.helpers'; @@ -39,12 +39,10 @@ describe('', () => { features: [{ name: 'kibana' }, { name: 'tasks' }], }); - testBed = await setup(httpSetup); - await act(async () => { - await nextTick(); - testBed.component.update(); + testBed = await setup(httpSetup); }); + testBed.component.update(); }); test('should set the correct page title', () => { @@ -64,12 +62,10 @@ describe('', () => { repositories: [{ name: 'this-is-a-new-repository' }], }); - testBed = await setup(httpSetup); - await act(async () => { - await nextTick(); - testBed.component.update(); + testBed = await setup(httpSetup); }); + testBed.component.update(); }); test('should show repository-not-found warning', () => { @@ -99,10 +95,7 @@ describe('', () => { test('should use the same Form component as the "" section', async () => { testBedPolicyAdd = await setupPolicyAdd(httpSetup); - await act(async () => { - await nextTick(); - testBedPolicyAdd.component.update(); - }); + testBedPolicyAdd.component.update(); const formEdit = testBed.component.find(PolicyForm); const formAdd = testBedPolicyAdd.component.find(PolicyForm); @@ -118,6 +111,28 @@ describe('', () => { expect(nameInput.props().disabled).toEqual(true); }); + test('should disable the repo and snapshot fields for managed policies', async () => { + httpRequestsMockHelpers.setGetPolicyResponse(POLICY_EDIT.name, { + policy: { + ...POLICY_EDIT, + isManagedPolicy: true, + }, + }); + + await act(async () => { + testBed = await setup(httpSetup); + }); + testBed.component.update(); + + const { find } = testBed; + + const snapshotInput = find('snapshotNameInput'); + expect(snapshotInput.props().disabled).toEqual(true); + + const repoSelect = find('repositorySelect'); + expect(repoSelect.props().disabled).toEqual(true); + }); + describe('form payload', () => { it('should send the correct payload with changed values', async () => { const { form, actions } = testBed; @@ -138,10 +153,7 @@ describe('', () => { form.setInputValue('expireAfterUnitSelect', EXPIRE_AFTER_UNIT); actions.clickNextButton(); - await act(async () => { - actions.clickSubmitButton(); - await nextTick(); - }); + actions.clickSubmitButton(); const { name, isManagedPolicy, schedule, repository, retention } = POLICY_EDIT; @@ -182,10 +194,7 @@ describe('', () => { form.setInputValue('expireAfterValueInput', EXPIRE_AFTER_VALUE); actions.clickNextButton(); - await act(async () => { - actions.clickSubmitButton(); - await nextTick(); - }); + actions.clickSubmitButton(); const { name, isManagedPolicy, schedule, repository, retention, snapshotName } = POLICY_EDIT; diff --git a/x-pack/plugins/snapshot_restore/public/application/components/policy_form/steps/step_logistics.tsx b/x-pack/plugins/snapshot_restore/public/application/components/policy_form/steps/step_logistics.tsx index e98e0eaf52a8c..137296595dc43 100644 --- a/x-pack/plugins/snapshot_restore/public/application/components/policy_form/steps/step_logistics.tsx +++ b/x-pack/plugins/snapshot_restore/public/application/components/policy_form/steps/step_logistics.tsx @@ -273,6 +273,7 @@ export const PolicyStepLogistics: React.FunctionComponent = ({ }} fullWidth data-test-subj="repositorySelect" + disabled={policy?.isManagedPolicy && isEditing} /> ); }; @@ -342,6 +343,7 @@ export const PolicyStepLogistics: React.FunctionComponent = ({ } )} data-test-subj="snapshotNameInput" + disabled={policy?.isManagedPolicy && isEditing} />