Skip to content

Commit f86ba90

Browse files
authored
feat: remove public /permissions/me usage (#300)
1 parent 15a2a16 commit f86ba90

File tree

10 files changed

+101
-359
lines changed

10 files changed

+101
-359
lines changed

cypress/e2e/specs/spec_renderer.spec.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ProductActionsResponse } from '@kong/sdk-portal-js'
12
import { product, versions } from '../fixtures/consts'
23
import petstoreJson from '../fixtures/oas_specs/petstoreJson.json'
34
import petstoreJson3 from '../fixtures/oas_specs/petstoreJson3.0.json'
@@ -330,18 +331,23 @@ describe('Spec Renderer Page', () => {
330331
rbac_enabled: true
331332
}).as('getPortalContext')
332333

333-
cy.intercept('GET', 'api/v2/portals/*/developers/me/permissions', {
334+
const response: ProductActionsResponse = {
335+
actions: {
336+
register: false,
337+
view: false,
338+
view_documentation: false
339+
}
340+
}
341+
342+
cy.intercept('GET', '/api/v2/products/*/actions', {
334343
statusCode: 200,
335-
body: [{
336-
resource: 'krn:konnect:reg/*:org/*:portals/*/services/*',
337-
actions: []
338-
}],
344+
body: response,
339345
delay: 300
340-
}).as('getPermissions')
346+
}).as('getProductActions')
341347

342348
cy.visit(`/spec/${product.id}`)
343349

344-
cy.wait('@getPermissions')
350+
cy.wait('@getProductActions')
345351

346352
cy.get('[data-testid="forbidden"]').should('exist')
347353
})
@@ -351,39 +357,41 @@ describe('Spec Renderer Page', () => {
351357
rbac_enabled: true
352358
}).as('getPortalContext')
353359

354-
cy.intercept('GET', 'api/v2/portals/*/developers/me/permissions', {
360+
const response: ProductActionsResponse = {
361+
actions: {
362+
register: true,
363+
view: true,
364+
view_documentation: true
365+
}
366+
}
367+
368+
cy.intercept('GET', '/api/v2/products/*/actions', {
355369
statusCode: 200,
356-
body: [{
357-
resource: 'krn:konnect:reg/*:org/*:portals/*/services/*',
358-
actions: [
359-
'#view',
360-
'#consume'
361-
]
362-
}],
370+
body: response,
363371
delay: 300
364-
}).as('getPermissions')
372+
}).as('getProductActions')
365373

366374
cy.visit(`/spec/${product.id}`)
367375

368-
cy.wait('@getPermissions')
376+
cy.wait('@getProductActions')
369377

370378
cy.get('[data-testid="kong-public-ui-spec-details-swagger"]', { timeout: 12000 })
371379
.get('.info h2').should('contain', 'Swagger Petstore')
372380

373381
cy.get('[data-testid="register-button"]').should('exist')
374382
})
375383

376-
it('does not call developers/me/permissions if rbac not enabled', () => {
384+
it('does not retrieve product actions if rbac not enabled', () => {
377385
cy.intercept('GET', '**/api/v2/portal', {
378386
rbac_enabled: false
379387
}).as('getPortalContext')
380388

381-
cy.intercept('get', 'api/v2/portals/*/developers/me/permissions', cy.spy().as('apiNotCalled'))
389+
cy.intercept('get', '/api/v2/products/*/actions', cy.spy().as('apiNotCalled'))
382390

383391
cy.visit(`/spec/${product.id}`)
384392

385393
cy.get('[data-testid="kong-public-ui-spec-details-swagger"]', { timeout: 12000 })
386-
.get('.info h2').should('contain', 'Swagger Petstore')
394+
.get('.info h2').should('contain', 'Swagger Petstore')
387395

388396
cy.get('[data-testid="register-button"]').should('exist')
389397

@@ -405,12 +413,12 @@ describe('Spec Renderer Page', () => {
405413
cy.mockAppearance()
406414
})
407415

408-
it('allows seeing spec when portal is public and rbac enabled, does not call developers/me/permissions', () => {
416+
it('allows seeing spec when portal is public and rbac enabled, does not retrieve product actions', () => {
409417
cy.intercept('GET', '**/portal_api/portal/portal_context', {
410418
rbac_enabled: true
411419
}).as('getPortalContext')
412420

413-
cy.intercept('get', 'api/v2/portals/*/developers/me/permissions', cy.spy().as('apiNotCalled'))
421+
cy.intercept('get', '/api/v2/products/*/actions', cy.spy().as('apiNotCalled'))
414422

415423
cy.visit(`/spec/${product.id}`)
416424

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"@kong-ui-public/document-viewer": "0.10.5",
3333
"@kong-ui-public/spec-renderer": "0.13.1",
3434
"@kong/kong-auth-elements": "2.8.0",
35-
"@kong/kongponents": "8.123.3",
35+
"@kong/kongponents": "8.126.1",
3636
"@kong/sdk-portal-js": "2.3.6",
3737
"@xstate/vue": "2.0.0",
3838
"axios": "1.6.0",

src/components/AuthValidate.vue

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/helpers/permissions.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/router/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,9 @@ export const portalRouter = () => {
8787
name: 'spec',
8888
meta: {
8989
title: helpText.specTitle,
90-
isAuthorized: (route, { portalId }) => canUserAccess({
91-
service: 'konnect',
92-
action: '#view',
93-
resourcePath: `portals/${portalId}/services/${route.params.product}`
90+
isAuthorized: (route) => canUserAccess({
91+
action: 'view',
92+
productId: route.params.product
9493
})
9594
},
9695
component: () => import('../views/Spec.vue')
@@ -100,10 +99,9 @@ export const portalRouter = () => {
10099
name: 'api-documentation-page',
101100
meta: {
102101
title: helpText.docsTitle,
103-
isAuthorized: (route, { portalId }) => canUserAccess({
104-
service: 'konnect',
105-
action: '#view',
106-
resourcePath: `portals/${portalId}/services/${route.params.product}`
102+
isAuthorized: (route) => canUserAccess({
103+
action: 'view',
104+
productId: route.params.product
107105
})
108106
},
109107
component: () => import('../views/ApiDocumentationPage.vue')

src/router/route-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import useLDFeatureFlag from '@/hooks/useLDFeatureFlag'
2-
import { usePermissionsStore } from '@/stores'
2+
import { ProductAction, usePermissionsStore } from '@/stores'
33

44
export const AUTH_ROUTES = {
55
login: true,
@@ -18,7 +18,7 @@ export const PRIVATE_ROUTES = {
1818
...AUTH_ROUTES
1919
}
2020

21-
export function canUserAccess (krnArgs) {
21+
export function canUserAccess (krnArgs: { action: ProductAction; productId: string }) {
2222
const { canUserAccess } = usePermissionsStore()
2323

2424
return canUserAccess(krnArgs)

src/services/SessionCookie.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { authApi } from '@/services'
2-
import { usePermissionsStore, useAppStore } from '@/stores'
3-
import { storeToRefs } from 'pinia'
42

53
/**
64
* @typedef {Object} SessionUser
@@ -76,10 +74,6 @@ export default class SessionCookie {
7674
}
7775

7876
async saveData (data: Record<string, any>, force = true) {
79-
const appStore = useAppStore()
80-
const permissionsStore = usePermissionsStore()
81-
const { portalId, isRbacEnabled, isPublic } = storeToRefs(appStore)
82-
8377
this.data = data
8478

8579
const sessionExists = this.exists()
@@ -88,25 +82,6 @@ export default class SessionCookie {
8882
if (force || (!force && !sessionExists)) {
8983
localStorage.setItem(this.sessionName, this.encode(this.data))
9084
}
91-
92-
if (sessionExists && !isPublic.value && isRbacEnabled.value) {
93-
try {
94-
const { data: developerPermissions } = await authApi.client.get(`/api/v2/portals/${portalId.value}/developers/me/permissions`)
95-
96-
// response can be a JSON (object) or string
97-
// when permissions feature flag is not enabled, string with HTTP 200 is returned
98-
if (typeof developerPermissions === 'object') {
99-
// Add permission krns to the store
100-
await permissionsStore.addKrns({
101-
krns: developerPermissions,
102-
replaceAll: true
103-
})
104-
}
105-
} catch (e) {
106-
// eslint-disable-next-line no-console
107-
console.error('Failed to fetch permissions', e)
108-
}
109-
}
11085
}
11186

11287
getUser () {

0 commit comments

Comments
 (0)