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

Wizard: Add services to on-prem mapper (HMS-5405) #2807

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export const mapOnPremToHosted = (
}
: undefined,
hostname: blueprint.customizations?.hostname || undefined,
services: blueprint.customizations?.services || undefined,
},
metadata: {
parent_id: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React from 'react';

import { FormGroup } from '@patternfly/react-core';

import { useAppSelector } from '../../../../../store/hooks';
import { useGetOscapCustomizationsQuery } from '../../../../../store/imageBuilderApi';
import {
addDisabledService,
addEnabledService,
removeDisabledService,
removeEnabledService,
selectComplianceProfileID,
selectDistribution,
selectServices,
} from '../../../../../store/wizardSlice';
import ChippingInput from '../../../ChippingInput';
import { isServiceValid } from '../../../validators';

const ServicesInput = () => {
const disabledServices = useAppSelector(selectServices).disabled;
const maskedServices = useAppSelector(selectServices).masked;
const enabledServices = useAppSelector(selectServices).enabled;

const release = useAppSelector(selectDistribution);
const complianceProfileID = useAppSelector(selectComplianceProfileID);

const { data: oscapProfileInfo } = useGetOscapCustomizationsQuery(
{
distribution: release,
// @ts-ignore if complianceProfileID is undefined the query is going to get skipped, so it's safe here to ignore the linter here
profile: complianceProfileID,
},
{
skip: !complianceProfileID,
}
);

const disabledAndMaskedRequiredByOpenSCAP = disabledServices
.concat(maskedServices)
.filter(
(service) =>
oscapProfileInfo?.services?.disabled?.includes(service) ||
oscapProfileInfo?.services?.masked?.includes(service)
);

const enabledRequiredByOpenSCAP = enabledServices.filter((service) =>
oscapProfileInfo?.services?.enabled?.includes(service)
);

return (
<>
<FormGroup isRequired={false} label="Disabled services">
<ChippingInput
ariaLabel="Add disabled service"
placeholder="Add disabled service"
validator={isServiceValid}
list={disabledServices
.concat(maskedServices)
.filter(
(service) =>
!disabledAndMaskedRequiredByOpenSCAP.includes(service)
)}
requiredList={disabledAndMaskedRequiredByOpenSCAP}
item="Disabled service"
addAction={addDisabledService}
removeAction={removeDisabledService}
/>
</FormGroup>
<FormGroup isRequired={false} label="Enabled services">
<ChippingInput
ariaLabel="Add enabled service"
placeholder="Add enabled service"
validator={isServiceValid}
list={enabledServices.filter(
(service) => !enabledRequiredByOpenSCAP.includes(service)
)}
requiredList={enabledRequiredByOpenSCAP}
item="Enabled service"
addAction={addEnabledService}
removeAction={removeEnabledService}
/>
</FormGroup>
</>
);
};

export default ServicesInput;
3 changes: 3 additions & 0 deletions src/Components/CreateImageWizard/steps/Services/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import React from 'react';

import { Text, Form, Title } from '@patternfly/react-core';

import ServicesInput from './components/ServicesInputs';

const ServicesStep = () => {
return (
<Form>
<Title headingLevel="h1" size="xl">
Systemd services
</Title>
<Text>Enable and disable systemd services.</Text>
<ServicesInput />
</Form>
);
};
Expand Down
10 changes: 10 additions & 0 deletions src/Components/CreateImageWizard/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,13 @@ export const isKernelArgumentValid = (arg: string) => {
export const isPortValid = (port: string) => {
return /^(\d{1,5}|[a-z]{1,6})(-\d{1,5})?:[a-z]{1,6}$/.test(port);
};

export const isServiceValid = (service: string) => {
// Restraints taken from service name syntax reference
// https://www.rfc-editor.org/rfc/rfc6335#section-5.1
return (
service.length <= 15 &&
/^[a-zA-Z][a-zA-Z0-9-]*[a-zA-Z]$/.test(service) &&
!/--/.test(service)
);
};
51 changes: 51 additions & 0 deletions src/store/wizardSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,12 +808,57 @@ export const wizardSlice = createSlice({
changeEnabledServices: (state, action: PayloadAction<string[]>) => {
state.services.enabled = action.payload;
},
addEnabledService: (state, action: PayloadAction<string>) => {
if (
!state.services.enabled.some((service) => service === action.payload)
) {
state.services.enabled.push(action.payload);
}
},
removeEnabledService: (state, action: PayloadAction<string>) => {
state.services.enabled.splice(
state.services.enabled.findIndex(
(service) => service === action.payload
),
1
);
},
changeMaskedServices: (state, action: PayloadAction<string[]>) => {
state.services.masked = action.payload;
},
addMaskedService: (state, action: PayloadAction<string>) => {
if (
!state.services.masked.some((service) => service === action.payload)
) {
state.services.masked.push(action.payload);
}
},
removeMaskedService: (state, action: PayloadAction<string>) => {
state.services.masked.splice(
state.services.masked.findIndex(
(service) => service === action.payload
),
1
);
},
changeDisabledServices: (state, action: PayloadAction<string[]>) => {
state.services.disabled = action.payload;
},
addDisabledService: (state, action: PayloadAction<string>) => {
if (
!state.services.disabled.some((service) => service === action.payload)
) {
state.services.disabled.push(action.payload);
}
},
removeDisabledService: (state, action: PayloadAction<string>) => {
state.services.disabled.splice(
state.services.disabled.findIndex(
(service) => service === action.payload
),
1
);
},
changeKernelName: (state, action: PayloadAction<string>) => {
state.kernel.name = action.payload;
},
Expand Down Expand Up @@ -973,8 +1018,14 @@ export const {
loadWizardState,
setFirstBootScript,
changeEnabledServices,
addEnabledService,
removeEnabledService,
changeMaskedServices,
addMaskedService,
removeMaskedService,
changeDisabledServices,
addDisabledService,
removeDisabledService,
changeKernelName,
addKernelArg,
removeKernelArg,
Expand Down
15 changes: 15 additions & 0 deletions src/test/Components/Blueprints/ImportBlueprintModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,20 @@ describe('Import modal', () => {
await clickNext();
const hostnameInput = await screen.findByPlaceholderText(/Add a hostname/i);
expect(hostnameInput).toHaveValue('base-image');

// Kernel
await clickNext();

// Firewall
await clickNext();

// Services
await clickNext();
await screen.findByText('sshd');
await screen.findByText('cockpit.socket');
await screen.findByText('httpd');
await screen.findByText('postfix');
await screen.findByText('telnetd');
await screen.findByText('rpcbind');
}, 20000);
});
Loading
Loading