-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from 19 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 eabbf32
Add a multi-value input component
bl-nero 64a67e3
Add application access section to the role editor
bl-nero 351faa7
Review
bl-nero 6d2eda5
Merge branch 'master' into bl-nero/role-editor-4
bl-nero 75c47ed
Merge branch 'bl-nero/role-editor-4' into bl-nero/multi-input
bl-nero 635a7b9
Merge branch 'bl-nero/multi-input' into bl-nero/role-editor-5
bl-nero b4e8317
Merge branch 'master' into bl-nero/role-editor-4
bl-nero 16fbc92
Merge branch 'bl-nero/role-editor-4' into bl-nero/multi-input
bl-nero 36f989c
Update the k8s operator docs
bl-nero ba129e7
Update operator CRDs and Terraform resources
bl-nero 960a336
Merge branch 'bl-nero/role-editor-4' into bl-nero/multi-input
bl-nero 41ef64b
Merge branch 'bl-nero/role-editor-4' into bl-nero/role-editor-5
bl-nero f0ae6c6
Merge branch 'master' into bl-nero/role-editor-4
bl-nero af9332e
Merge branch 'bl-nero/role-editor-4' into bl-nero/multi-input
bl-nero 78ec54b
Merge branch 'bl-nero/multi-input' into bl-nero/role-editor-5
bl-nero aa88cbc
Lint, licenses
bl-nero 3aeb3d1
Merge branch 'bl-nero/multi-input' into bl-nero/role-editor-5
bl-nero ee35e03
lint
bl-nero 2bf6044
Merge branch 'master' into bl-nero/role-editor-5
bl-nero 09a534c
Review
bl-nero 4d7a3bc
Lint
bl-nero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
web/packages/shared/components/FieldMultiInput/FieldMultiInput.story.tsx
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,37 @@ | ||
/** | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import React, { useState } from 'react'; | ||
|
||
import Box from 'design/Box'; | ||
|
||
import { FieldMultiInput } from './FieldMultiInput'; | ||
|
||
export default { | ||
title: 'Shared', | ||
}; | ||
|
||
export function Story() { | ||
const [items, setItems] = useState([]); | ||
return ( | ||
<Box width="500px"> | ||
<FieldMultiInput label="Some items" value={items} onChange={setItems} /> | ||
</Box> | ||
); | ||
} | ||
Story.storyName = 'FieldMultiInput'; |
71 changes: 71 additions & 0 deletions
71
web/packages/shared/components/FieldMultiInput/FieldMultiInput.test.tsx
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,71 @@ | ||
/** | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import userEvent from '@testing-library/user-event'; | ||
import React, { useState } from 'react'; | ||
|
||
import { render, screen } from 'design/utils/testing'; | ||
|
||
import { FieldMultiInput, FieldMultiInputProps } from './FieldMultiInput'; | ||
|
||
const TestFieldMultiInput = ({ | ||
onChange, | ||
...rest | ||
}: Partial<FieldMultiInputProps>) => { | ||
const [items, setItems] = useState<string[]>([]); | ||
const handleChange = (it: string[]) => { | ||
setItems(it); | ||
onChange?.(it); | ||
}; | ||
return <FieldMultiInput value={items} onChange={handleChange} {...rest} />; | ||
}; | ||
|
||
test('adding, editing, and removing items', async () => { | ||
const user = userEvent.setup(); | ||
const onChange = jest.fn(); | ||
render(<TestFieldMultiInput onChange={onChange} />); | ||
|
||
await user.type(screen.getByRole('textbox'), 'apples'); | ||
expect(onChange).toHaveBeenLastCalledWith(['apples']); | ||
|
||
await user.click(screen.getByRole('button', { name: 'Add More' })); | ||
expect(onChange).toHaveBeenLastCalledWith(['apples', '']); | ||
|
||
await user.type(screen.getAllByRole('textbox')[1], 'oranges'); | ||
expect(onChange).toHaveBeenLastCalledWith(['apples', 'oranges']); | ||
|
||
await user.click(screen.getAllByRole('button', { name: 'Remove Item' })[0]); | ||
expect(onChange).toHaveBeenLastCalledWith(['oranges']); | ||
|
||
await user.click(screen.getAllByRole('button', { name: 'Remove Item' })[0]); | ||
expect(onChange).toHaveBeenLastCalledWith([]); | ||
}); | ||
|
||
test('keyboard handling', async () => { | ||
const user = userEvent.setup(); | ||
const onChange = jest.fn(); | ||
render(<TestFieldMultiInput onChange={onChange} />); | ||
|
||
await user.click(screen.getByRole('textbox')); | ||
await user.keyboard('apples{Enter}oranges'); | ||
expect(onChange).toHaveBeenLastCalledWith(['apples', 'oranges']); | ||
|
||
await user.click(screen.getAllByRole('textbox')[0]); | ||
await user.keyboard('{Enter}bananas'); | ||
expect(onChange).toHaveBeenLastCalledWith(['apples', 'bananas', 'oranges']); | ||
}); |
139 changes: 139 additions & 0 deletions
139
web/packages/shared/components/FieldMultiInput/FieldMultiInput.tsx
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,139 @@ | ||
/** | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Box from 'design/Box'; | ||
import { ButtonSecondary } from 'design/Button'; | ||
import ButtonIcon from 'design/ButtonIcon'; | ||
import Flex from 'design/Flex'; | ||
import * as Icon from 'design/Icon'; | ||
import Input from 'design/Input'; | ||
import { useRef } from 'react'; | ||
import styled, { useTheme } from 'styled-components'; | ||
|
||
export type FieldMultiInputProps = { | ||
label?: string; | ||
value: string[]; | ||
disabled?: boolean; | ||
onChange?(val: string[]): void; | ||
}; | ||
|
||
/** | ||
* Allows editing a list of strings, one value per row. Use instead of | ||
* `FieldSelectCreatable` when: | ||
* | ||
* - There are no predefined values to be picked from. | ||
* - Values are expected to be relatively long and would be unreadable after | ||
* being truncated. | ||
*/ | ||
export function FieldMultiInput({ | ||
label, | ||
value, | ||
disabled, | ||
onChange, | ||
}: FieldMultiInputProps) { | ||
if (value.length === 0) { | ||
value = ['']; | ||
} | ||
|
||
const theme = useTheme(); | ||
// Index of the input to be focused after the next rendering. | ||
const toFocus = useRef<number | undefined>(); | ||
|
||
const setFocus = element => { | ||
element?.focus(); | ||
toFocus.current = undefined; | ||
}; | ||
|
||
function insertItem(index: number) { | ||
onChange?.(value.toSpliced(index, 0, '')); | ||
} | ||
|
||
function removeItem(index: number) { | ||
onChange?.(value.toSpliced(index, 1)); | ||
} | ||
|
||
function handleKeyDown(index: number, e: React.KeyboardEvent) { | ||
if (e.key === 'Enter') { | ||
insertItem(index + 1); | ||
toFocus.current = index + 1; | ||
} | ||
} | ||
|
||
return ( | ||
<Box> | ||
<Fieldset> | ||
{label && <Legend>{label}</Legend>} | ||
{value.map((val, i) => ( | ||
// Note on keys: using index as a key is an anti-pattern in general, | ||
// but here, we can safely assume that even though the list is | ||
// editable, we don't rely on any unmanaged HTML element state other | ||
// than focus, which we deal with separately anyway. The alternatives | ||
// would be either to require an array with keys generated | ||
// synthetically and injected from outside (which would make the API | ||
// difficult to use) or to keep the array with generated IDs as local | ||
// state (which would require us to write a prop/state reconciliation | ||
// procedure whose complexity would probably outweigh the benefits). | ||
<Flex key={i} alignItems="center" gap={2}> | ||
<Box flex="1"> | ||
<Input | ||
value={val} | ||
ref={toFocus.current === i ? setFocus : null} | ||
onChange={e => | ||
onChange?.( | ||
value.map((v, j) => (j === i ? e.target.value : v)) | ||
) | ||
} | ||
onKeyDown={e => handleKeyDown(i, e)} | ||
/> | ||
</Box> | ||
<ButtonIcon | ||
size="0" | ||
title="Remove Item" | ||
onClick={() => removeItem(i)} | ||
disabled={disabled} | ||
> | ||
<Icon.Cross size="small" color={theme.colors.text.muted} /> | ||
</ButtonIcon> | ||
</Flex> | ||
))} | ||
<ButtonSecondary | ||
alignSelf="start" | ||
onClick={() => insertItem(value.length)} | ||
> | ||
<Icon.Plus size="small" mr={2} /> | ||
Add More | ||
</ButtonSecondary> | ||
</Fieldset> | ||
</Box> | ||
); | ||
} | ||
|
||
const Fieldset = styled.fieldset` | ||
border: none; | ||
margin: 0; | ||
padding: 0; | ||
display: flex; | ||
flex-direction: column; | ||
gap: ${props => props.theme.space[2]}px; | ||
`; | ||
|
||
const Legend = styled.legend` | ||
margin: 0 0 ${props => props.theme.space[1]}px 0; | ||
padding: 0; | ||
${props => props.theme.typography.body3} | ||
`; |
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 |
---|---|---|
|
@@ -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, | ||
|
@@ -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([ | ||
|
@@ -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} | ||
/> | ||
); | ||
|
@@ -194,12 +197,7 @@ describe('KubernetesAccessSpecSection', () => { | |
render( | ||
<StatefulSection<KubernetesAccessSpec> | ||
component={KubernetesAccessSpecSection} | ||
defaultValue={{ | ||
kind: 'kube_cluster', | ||
groups: [], | ||
labels: [], | ||
resources: [], | ||
}} | ||
defaultValue={newAccessSpec('kube_cluster')} | ||
onChange={onChange} | ||
/> | ||
); | ||
|
@@ -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'); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
element
needs a type