Skip to content

Commit 6ac6063

Browse files
authored
fix: scope user resources correctly for custom api token (#3905)
1 parent c027acb commit 6ac6063

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

src/authorizations/components/CustomApiTokenOverlay.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {AppState, ResourceType, Authorization} from 'src/types'
3535
import {Bucket, Telegraf} from 'src/client'
3636

3737
// Seletors
38+
import {getMe} from 'src/me/selectors'
3839
import {getOrg} from 'src/organizations/selectors'
3940
import {getAll} from 'src/resources/selectors'
4041
import {getResourcesStatus} from 'src/resources/selectors/getResourcesStatus'
@@ -59,6 +60,7 @@ interface StateProps {
5960
remoteDataState: RemoteDataState
6061
orgID: string
6162
orgName: string
63+
meID: string
6264
}
6365
interface DispatchProps {
6466
getBuckets: () => void
@@ -231,8 +233,13 @@ const CustomApiTokenOverlay: FC<Props> = props => {
231233
}
232234

233235
const generateToken = async () => {
234-
const {orgID, showOverlay, orgName, createAuthorization} = props
235-
const apiPermissions = formatApiPermissions(permissions, orgID, orgName)
236+
const {meID, orgID, showOverlay, orgName, createAuthorization} = props
237+
const apiPermissions = formatApiPermissions(
238+
permissions,
239+
meID,
240+
orgID,
241+
orgName
242+
)
236243

237244
const token: Authorization = {
238245
orgID: orgID,
@@ -384,6 +391,7 @@ const mstp = (state: AppState) => {
384391
remoteDataState,
385392
orgID: getOrg(state).id,
386393
orgName: getOrg(state).name,
394+
meID: getMe(state).id,
387395
}
388396
}
389397

src/authorizations/utils/permissions.test.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ const apiPermission5 = [
708708
},
709709
},
710710
]
711+
711712
test('all-access tokens/authorizations production test', () => {
712713
if (CLOUD) {
713714
expect(allAccessPermissions('bulldogs', 'mario')).toMatchObject(cloudHvhs)
@@ -744,6 +745,7 @@ describe('generateDescription method', () => {
744745
})
745746
})
746747

748+
const meID = '08e1982cb86af000'
747749
const orgID = 'ba9198e037d35d4d'
748750
const orgName = 'dev'
749751
const monitoringID = '25a6692ba25d7147'
@@ -868,26 +870,62 @@ const orgsApiPerm = [
868870
describe('Testing formatApiPermissions function', () => {
869871
test('does it convert all access permission object into api permission', () => {
870872
expect(
871-
formatApiPermissions(allAccessAccordionPerms, orgID, orgName)
873+
formatApiPermissions(allAccessAccordionPerms, meID, orgID, orgName)
872874
).toMatchObject(allAccessApiPerm)
873875
})
876+
874877
test('does it convert non-all access permission object into api permission', () => {
875878
expect(
876-
formatApiPermissions(nonAllAcessAccordionPerms, orgID, orgName)
879+
formatApiPermissions(nonAllAcessAccordionPerms, meID, orgID, orgName)
877880
).toMatchObject(nonAllAcessApiPerms)
878881
})
882+
879883
test('does it convert orgs permission object into api permission', () => {
880884
expect(
881-
formatApiPermissions(orgsAccordionPerm, orgID, orgName)
885+
formatApiPermissions(orgsAccordionPerm, meID, orgID, orgName)
882886
).toMatchObject(orgsApiPerm)
883887
})
888+
889+
test('if custom api token for users has correct scope', () => {
890+
const permissions = {
891+
annotations: {read: false, write: false},
892+
authorizations: {read: false, write: false},
893+
buckets: {
894+
read: false,
895+
write: false,
896+
sublevelPermissions: {
897+
'3a64ac1f8ade33ef': {
898+
id: '3a64ac1f8ade33ef',
899+
name: 'devbucket',
900+
orgID,
901+
permissions: {read: false, write: false},
902+
},
903+
},
904+
},
905+
orgs: {read: false, write: false},
906+
users: {read: true, write: true},
907+
}
908+
909+
expect(formatApiPermissions(permissions, meID, orgID, orgName)).toEqual([
910+
{
911+
action: 'read',
912+
resource: {id: meID, type: 'users'},
913+
},
914+
{
915+
action: 'write',
916+
resource: {id: meID, type: 'users'},
917+
},
918+
])
919+
})
884920
})
921+
885922
describe('Testing formatPermissionsObj function', () => {
886923
test('for api permissions with IDs, it creates perms with sublevel permissions', () => {
887924
expect(formatPermissionsObj(nonAllAcessApiPerms)).toMatchObject(
888925
nonAllAcessAccordionPerms2
889926
)
890927
})
928+
891929
test('for all access permissions, it creates an all access accordion api permission', () => {
892930
expect(formatPermissionsObj(orgsApiPerm)).toMatchObject(orgsAccordionPerm)
893931
})

src/authorizations/utils/permissions.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export const formatPermissionsObj = permissions => {
169169
return newPerms
170170
}
171171

172-
export const formatApiPermissions = (permissions, orgID, orgName) => {
172+
export const formatApiPermissions = (permissions, meID, orgID, orgName) => {
173173
const apiPerms = []
174174
Object.keys(permissions).forEach(key => {
175175
if (key === 'otherResources') {
@@ -185,6 +185,14 @@ export const formatApiPermissions = (permissions, orgID, orgName) => {
185185
type: key,
186186
},
187187
})
188+
} else if (key === 'users') {
189+
apiPerms.push({
190+
action: 'read',
191+
resource: {
192+
id: meID,
193+
type: key,
194+
},
195+
})
188196
} else {
189197
apiPerms.push({
190198
action: 'read',
@@ -205,6 +213,14 @@ export const formatApiPermissions = (permissions, orgID, orgName) => {
205213
type: key,
206214
},
207215
})
216+
} else if (key === 'users') {
217+
apiPerms.push({
218+
action: 'write',
219+
resource: {
220+
id: meID,
221+
type: key,
222+
},
223+
})
208224
} else {
209225
apiPerms.push({
210226
action: 'write',

0 commit comments

Comments
 (0)