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 application access section to the role editor #47803

Merged
merged 22 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ebf686c
Add Kubernetes access section to the role editor
bl-nero Oct 6, 2024
eabbf32
Add a multi-value input component
bl-nero Oct 18, 2024
64a67e3
Add application access section to the role editor
bl-nero Oct 22, 2024
351faa7
Review
bl-nero Oct 23, 2024
6d2eda5
Merge branch 'master' into bl-nero/role-editor-4
bl-nero Oct 23, 2024
75c47ed
Merge branch 'bl-nero/role-editor-4' into bl-nero/multi-input
bl-nero Oct 23, 2024
635a7b9
Merge branch 'bl-nero/multi-input' into bl-nero/role-editor-5
bl-nero Oct 23, 2024
b4e8317
Merge branch 'master' into bl-nero/role-editor-4
bl-nero Oct 24, 2024
16fbc92
Merge branch 'bl-nero/role-editor-4' into bl-nero/multi-input
bl-nero Oct 24, 2024
36f989c
Update the k8s operator docs
bl-nero Oct 24, 2024
ba129e7
Update operator CRDs and Terraform resources
bl-nero Oct 24, 2024
960a336
Merge branch 'bl-nero/role-editor-4' into bl-nero/multi-input
bl-nero Oct 24, 2024
41ef64b
Merge branch 'bl-nero/role-editor-4' into bl-nero/role-editor-5
bl-nero Oct 24, 2024
f0ae6c6
Merge branch 'master' into bl-nero/role-editor-4
bl-nero Oct 24, 2024
af9332e
Merge branch 'bl-nero/role-editor-4' into bl-nero/multi-input
bl-nero Oct 24, 2024
78ec54b
Merge branch 'bl-nero/multi-input' into bl-nero/role-editor-5
bl-nero Oct 24, 2024
aa88cbc
Lint, licenses
bl-nero Oct 24, 2024
3aeb3d1
Merge branch 'bl-nero/multi-input' into bl-nero/role-editor-5
bl-nero Oct 24, 2024
ee35e03
lint
bl-nero Oct 24, 2024
2bf6044
Merge branch 'master' into bl-nero/role-editor-5
bl-nero Oct 25, 2024
09a534c
Review
bl-nero Oct 25, 2024
4d7a3bc
Lint
bl-nero Oct 25, 2024
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 @@ -54,7 +54,7 @@ export function FieldMultiInput({
// Index of the input to be focused after the next rendering.
const toFocus = useRef<number | undefined>();

const setFocus = element => {
const setFocus = (element: HTMLInputElement) => {
element?.focus();
toFocus.current = undefined;
};
Expand Down
69 changes: 55 additions & 14 deletions web/packages/teleport/src/Roles/RoleEditor/StandardEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ import { createTeleportContext } from 'teleport/mocks/contexts';

import {
AccessSpec,
AppAccessSpec,
KubernetesAccessSpec,
newAccessSpec,
newRole,
roleToRoleEditorModel,
ServerAccessSpec,
StandardEditorModel,
} from './standardmodel';
import {
AppAccessSpecSection,
KubernetesAccessSpecSection,
SectionProps,
ServerAccessSpecSection,
Expand Down Expand Up @@ -69,15 +72,19 @@ test('adding and removing sections', async () => {
await user.click(
screen.getByRole('button', { name: 'Add New Specifications' })
);
expect(getAllMenuItemNames()).toEqual(['Kubernetes', 'Servers']);
expect(getAllMenuItemNames()).toEqual([
'Kubernetes',
'Servers',
'Applications',
]);

await user.click(screen.getByRole('menuitem', { name: 'Servers' }));
expect(getAllSectionNames()).toEqual(['Role Metadata', 'Servers']);

await user.click(
screen.getByRole('button', { name: 'Add New Specifications' })
);
expect(getAllMenuItemNames()).toEqual(['Kubernetes']);
expect(getAllMenuItemNames()).toEqual(['Kubernetes', 'Applications']);

await user.click(screen.getByRole('menuitem', { name: 'Kubernetes' }));
expect(getAllSectionNames()).toEqual([
Expand Down Expand Up @@ -154,17 +161,13 @@ const StatefulSection = <S extends AccessSpec>({
);
};

test('editing server access specs', async () => {
test('ServerAccessSpecSection', async () => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<StatefulSection<ServerAccessSpec>
component={ServerAccessSpecSection}
defaultValue={{
kind: 'node',
labels: [],
logins: [],
}}
defaultValue={newAccessSpec('node')}
onChange={onChange}
/>
);
Expand Down Expand Up @@ -194,12 +197,7 @@ describe('KubernetesAccessSpecSection', () => {
render(
<StatefulSection<KubernetesAccessSpec>
component={KubernetesAccessSpecSection}
defaultValue={{
kind: 'kube_cluster',
groups: [],
labels: [],
resources: [],
}}
defaultValue={newAccessSpec('kube_cluster')}
onChange={onChange}
/>
);
Expand Down Expand Up @@ -312,6 +310,49 @@ describe('KubernetesAccessSpecSection', () => {
});
});

test('AppAccessSpecSection', async () => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<StatefulSection<AppAccessSpec>
component={AppAccessSpecSection}
defaultValue={newAccessSpec('app')}
onChange={onChange}
/>
);

await user.click(screen.getByRole('button', { name: 'Add a Label' }));
await user.type(screen.getByPlaceholderText('label key'), 'env');
await user.type(screen.getByPlaceholderText('label value'), 'prod');
await user.type(
within(screen.getByRole('group', { name: 'AWS Role ARNs' })).getByRole(
'textbox'
),
'arn:aws:iam::123456789012:role/admin'
);
await user.type(
within(screen.getByRole('group', { name: 'Azure Identities' })).getByRole(
'textbox'
),
'/subscriptions/1020304050607-cafe-8090-a0b0c0d0e0f0/resourceGroups/example-resource-group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/admin'
);
await user.type(
within(
screen.getByRole('group', { name: 'GCP Service Accounts' })
).getByRole('textbox'),
'[email protected]'
);
expect(onChange).toHaveBeenLastCalledWith({
kind: 'app',
labels: [{ name: 'env', value: 'prod' }],
awsRoleARNs: ['arn:aws:iam::123456789012:role/admin'],
azureIdentities: [
'/subscriptions/1020304050607-cafe-8090-a0b0c0d0e0f0/resourceGroups/example-resource-group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/admin',
],
gcpServiceAccounts: ['[email protected]'],
} as AppAccessSpec);
});

const reactSelectValueContainer = (input: HTMLInputElement) =>
// eslint-disable-next-line testing-library/no-node-access
input.closest('.react-select__value-container');
49 changes: 48 additions & 1 deletion web/packages/teleport/src/Roles/RoleEditor/StandardEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import { Role, RoleWithYaml } from 'teleport/services/resources';

import { LabelsInput } from 'teleport/components/LabelsInput';

import { FieldMultiInput } from '../../../../shared/components/FieldMultiInput/FieldMultiInput';

import {
roleEditorModelToRole,
hasModifiedFields,
Expand All @@ -60,6 +62,7 @@ import {
kubernetesResourceKindOptions,
kubernetesVerbOptions,
KubernetesResourceModel,
AppAccessSpec,
} from './standardmodel';
import { EditorSaveCancelButton } from './Shared';
import { RequiresResetToStandard } from './RequiresResetToStandard';
Expand Down Expand Up @@ -356,7 +359,7 @@ const Section = ({
/**
* All access spec kinds, in order of appearance in the resource kind dropdown.
*/
const allAccessSpecKinds: AccessSpecKind[] = ['kube_cluster', 'node'];
const allAccessSpecKinds: AccessSpecKind[] = ['kube_cluster', 'node', 'app'];

/** Maps access specification kind to UI component configuration. */
const specSections: Record<
Expand All @@ -377,6 +380,11 @@ const specSections: Record<
tooltip: 'Configures access to SSH servers',
component: ServerAccessSpecSection,
},
app: {
title: 'Applications',
tooltip: 'Configures access to applications',
component: AppAccessSpecSection,
},
};

/**
Expand Down Expand Up @@ -589,6 +597,45 @@ function KubernetesResourceView({
);
}

export function AppAccessSpecSection({
value,
isProcessing,
onChange,
}: SectionProps<AppAccessSpec>) {
return (
<Flex flexDirection="column" gap={3}>
<Box>
<Text typography="body3" mb={1}>
Labels
</Text>
<LabelsInput
disableBtns={isProcessing}
labels={value.labels}
setLabels={labels => onChange?.({ ...value, labels })}
/>
</Box>
<FieldMultiInput
label="AWS Role ARNs"
disabled={isProcessing}
value={value.awsRoleARNs}
onChange={arns => onChange?.({ ...value, awsRoleARNs: arns })}
/>
<FieldMultiInput
label="Azure Identities"
disabled={isProcessing}
value={value.azureIdentities}
onChange={ids => onChange?.({ ...value, azureIdentities: ids })}
/>
<FieldMultiInput
label="GCP Service Accounts"
disabled={isProcessing}
value={value.gcpServiceAccounts}
onChange={accts => onChange?.({ ...value, gcpServiceAccounts: accts })}
/>
</Flex>
);
}

export const EditorWrapper = styled(Box)<{ mute?: boolean }>`
opacity: ${p => (p.mute ? 0.4 : 1)};
pointer-events: ${p => (p.mute ? 'none' : '')};
Expand Down
117 changes: 117 additions & 0 deletions web/packages/teleport/src/Roles/RoleEditor/standardmodel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,76 @@ describe('roleToRoleEditorModel', () => {
],
} as RoleEditorModel);
});

it('creates an app access spec', () => {
const minRole = minimalRole();
expect(
roleToRoleEditorModel({
...minRole,
spec: {
...minRole.spec,
allow: {
app_labels: { foo: 'bar' },
},
},
})
).toEqual({
...minimalRoleModel(),
accessSpecs: [
{
kind: 'app',
labels: [{ name: 'foo', value: 'bar' }],
awsRoleARNs: [],
azureIdentities: [],
gcpServiceAccounts: [],
},
],
} as RoleEditorModel);

expect(
roleToRoleEditorModel({
...minRole,
spec: {
...minRole.spec,
allow: {
app_labels: { foo: 'bar' },
aws_role_arns: [
'arn:aws:iam::123456789012:role/role1',
'arn:aws:iam::123456789012:role/role2',
],
azure_identities: [
'/subscriptions/1020304050607-cafe-8090-a0b0c0d0e0f0/resourceGroups/example-resource-group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1',
'/subscriptions/1020304050607-cafe-8090-a0b0c0d0e0f0/resourceGroups/example-resource-group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id2',
],
gcp_service_accounts: [
'[email protected]',
'[email protected]',
],
},
},
})
).toEqual({
...minimalRoleModel(),
accessSpecs: [
{
kind: 'app',
labels: [{ name: 'foo', value: 'bar' }],
awsRoleARNs: [
'arn:aws:iam::123456789012:role/role1',
'arn:aws:iam::123456789012:role/role2',
],
azureIdentities: [
'/subscriptions/1020304050607-cafe-8090-a0b0c0d0e0f0/resourceGroups/example-resource-group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1',
'/subscriptions/1020304050607-cafe-8090-a0b0c0d0e0f0/resourceGroups/example-resource-group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id2',
],
gcpServiceAccounts: [
'[email protected]',
'[email protected]',
],
},
],
} as RoleEditorModel);
});
});

test('labelsToModel', () => {
Expand Down Expand Up @@ -438,6 +508,53 @@ describe('roleEditorModelToRole', () => {
},
} as Role);
});

it('converts an app access spec', () => {
const minRole = minimalRole();
expect(
roleEditorModelToRole({
...minimalRoleModel(),
accessSpecs: [
{
kind: 'app',
labels: [{ name: 'foo', value: 'bar' }],
awsRoleARNs: [
'arn:aws:iam::123456789012:role/role1',
'arn:aws:iam::123456789012:role/role2',
],
azureIdentities: [
'/subscriptions/1020304050607-cafe-8090-a0b0c0d0e0f0/resourceGroups/example-resource-group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1',
'/subscriptions/1020304050607-cafe-8090-a0b0c0d0e0f0/resourceGroups/example-resource-group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id2',
],
gcpServiceAccounts: [
'[email protected]',
'[email protected]',
],
},
],
})
).toEqual({
...minRole,
spec: {
...minRole.spec,
allow: {
app_labels: { foo: 'bar' },
aws_role_arns: [
'arn:aws:iam::123456789012:role/role1',
'arn:aws:iam::123456789012:role/role2',
],
azure_identities: [
'/subscriptions/1020304050607-cafe-8090-a0b0c0d0e0f0/resourceGroups/example-resource-group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1',
'/subscriptions/1020304050607-cafe-8090-a0b0c0d0e0f0/resourceGroups/example-resource-group/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id2',
],
gcp_service_accounts: [
'[email protected]',
'[email protected]',
],
},
},
} as Role);
});
});

test('labelsModelToLabels', () => {
Expand Down
Loading
Loading