Skip to content

Commit bc9182e

Browse files
authored
fix: allow All Access Tokens to use all resources (#3906)
1 parent 6ac6063 commit bc9182e

File tree

5 files changed

+60
-53
lines changed

5 files changed

+60
-53
lines changed

src/authorizations/components/AllAccessTokenOverlay.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Libraries
2-
import React, {ChangeEvent, FC, useState, useContext} from 'react'
2+
import React, {ChangeEvent, FC, useState, useContext, useMemo} from 'react'
33
import {useDispatch, useSelector} from 'react-redux'
44

55
// Components
@@ -32,6 +32,7 @@ import {event} from 'src/cloud/utils/reporting'
3232
// Selectors
3333
import {getOrg} from 'src/organizations/selectors'
3434
import {getMe} from 'src/me/selectors'
35+
import {getAllTokensResources} from 'src/resources/selectors'
3536

3637
// Types
3738
import {Authorization} from 'src/types'
@@ -46,12 +47,21 @@ const AllAccessTokenOverlay: FC<OwnProps> = props => {
4647
const [description, setDescription] = useState<string>('')
4748
const {id: orgID} = useSelector(getOrg)
4849
const {id: meID} = useSelector(getMe)
50+
const allPermissionTypes = useSelector(getAllTokensResources)
51+
52+
const sortedPermissionTypes = useMemo(
53+
() =>
54+
allPermissionTypes.sort((a, b) =>
55+
a.toLowerCase().localeCompare(b.toLowerCase())
56+
),
57+
[allPermissionTypes]
58+
)
4959

5060
const handleSave = () => {
5161
const token: Authorization = {
5262
orgID,
5363
description,
54-
permissions: allAccessPermissions(orgID, meID),
64+
permissions: allAccessPermissions(sortedPermissionTypes, orgID, meID),
5565
}
5666
dispatch(createAuthorization(token))
5767
handleDismiss()

src/authorizations/components/GenerateTokenDropdown.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ const GenerateTokenDropdown: FC<ReduxProps & GenerateTokenProps> = ({
3030

3131
const customApiOption = 'Custom API Token'
3232

33-
const handleAllAccess = () => {
34-
showOverlay('add-master-token', null, dismissOverlay)
35-
event('generate_token_dropdown.all_access_overlay.opened')
33+
const handleAllAccess = async () => {
34+
try {
35+
await getAllResources()
36+
showOverlay('add-master-token', null, dismissOverlay)
37+
event('generate_token_dropdown.all_access_overlay.opened')
38+
} catch (e) {
39+
dispatch(notify(getResourcesTokensFailure()))
40+
event('generate_token_dropdown.all_access_overlay.failed')
41+
}
3642
}
3743

3844
const handleCustomApi = async () => {

src/authorizations/utils/permissions.test.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {allAccessPermissions} from './permissions'
1+
import {allAccessPermissions, PermissionTypes} from './permissions'
22
import {CLOUD} from 'src/shared/constants'
33
import {
44
generateDescription,
@@ -710,10 +710,40 @@ const apiPermission5 = [
710710
]
711711

712712
test('all-access tokens/authorizations production test', () => {
713+
const allPermissions = new Set<PermissionTypes>()
714+
713715
if (CLOUD) {
714-
expect(allAccessPermissions('bulldogs', 'mario')).toMatchObject(cloudHvhs)
716+
cloudHvhs.forEach(permission => {
717+
allPermissions.add(permission.resource.type as PermissionTypes)
718+
})
719+
const sortedPermissions = Array.from(allPermissions).sort((a, b) =>
720+
a.toLowerCase().localeCompare(b.toLowerCase())
721+
)
722+
expect(
723+
allAccessPermissions(sortedPermissions, 'bulldogs', 'mario')
724+
).toMatchObject(
725+
cloudHvhs.sort((a, b) =>
726+
a.resource.type
727+
.toLowerCase()
728+
.localeCompare(b.resource.type.toLowerCase())
729+
)
730+
)
715731
} else {
716-
expect(allAccessPermissions('bulldogs', 'mario')).toMatchObject(ossHvhs)
732+
ossHvhs.forEach(permission => {
733+
allPermissions.add(permission.resource.type as PermissionTypes)
734+
})
735+
const sortedPermissions = Array.from(allPermissions).sort((a, b) =>
736+
a.toLowerCase().localeCompare(b.toLowerCase())
737+
)
738+
expect(
739+
allAccessPermissions(sortedPermissions, 'bulldogs', 'mario')
740+
).toMatchObject(
741+
ossHvhs.sort((a, b) =>
742+
a.resource.type
743+
.toLowerCase()
744+
.localeCompare(b.resource.type.toLowerCase())
745+
)
746+
)
717747
}
718748
})
719749

src/authorizations/utils/permissions.ts

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,7 @@
11
import {Permission, ResourceType} from 'src/types'
2-
import {CLOUD} from 'src/shared/constants'
32
import {capitalize} from 'lodash'
43

5-
type PermissionTypes = Permission['resource']['type']
6-
7-
const sharedPermissionTypes: PermissionTypes[] = [
8-
'annotations',
9-
'authorizations',
10-
'buckets',
11-
'checks',
12-
'dashboards',
13-
'dbrp',
14-
'documents',
15-
'labels',
16-
'notificationRules',
17-
'notificationEndpoints',
18-
'orgs',
19-
'secrets',
20-
'tasks',
21-
'telegrafs',
22-
'users',
23-
'variables',
24-
'views',
25-
]
26-
27-
const cloudPermissionTypes = ['flows', 'functions']
28-
29-
const ossPermissionTypes = [
30-
'notebooks',
31-
'scrapers',
32-
'sources',
33-
'remotes',
34-
'replications',
35-
]
36-
37-
// TODO: replace this with some server side mechanism
38-
const allPermissionTypes: PermissionTypes[] = sharedPermissionTypes.concat(
39-
(CLOUD ? cloudPermissionTypes : ossPermissionTypes) as PermissionTypes[]
40-
)
41-
42-
allPermissionTypes.sort((a, b) =>
43-
a.toLowerCase().localeCompare(b.toLowerCase())
44-
)
4+
export type PermissionTypes = Permission['resource']['type']
455

466
const ensureT = (orgID: string, userID: string) => (
477
t: PermissionTypes
@@ -70,10 +30,6 @@ const ensureT = (orgID: string, userID: string) => (
7030
]
7131
}
7232

73-
if (!allPermissionTypes.includes(t)) {
74-
throw new Error('Unexpected object: ' + t)
75-
}
76-
7733
return [
7834
{
7935
action: 'read' as 'read',
@@ -87,6 +43,7 @@ const ensureT = (orgID: string, userID: string) => (
8743
}
8844

8945
export const allAccessPermissions = (
46+
allPermissionTypes: PermissionTypes[],
9047
orgID: string,
9148
userID: string
9249
): Permission[] => {

src/resources/selectors/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Task,
1313
Telegraf,
1414
} from 'src/types'
15+
import {PermissionTypes} from 'src/authorizations/utils/permissions'
1516

1617
export const getStatus = (
1718
{resources}: AppState,
@@ -68,3 +69,6 @@ export const getLabels = (state: AppState, labelIDs: string[]): Label[] => {
6869
.map(labelID => getByID<Label>(state, ResourceType.Labels, labelID))
6970
.filter(label => !!label)
7071
}
72+
73+
export const getAllTokensResources = (state: AppState): PermissionTypes[] =>
74+
get(state, 'resources.tokens.allResources', []) || []

0 commit comments

Comments
 (0)