1- /* eslint-disable no-param-reassign */
2- import { getSpec } from 'src/app'
3- import { debug } from 'console'
4- import { find } from 'lodash'
5- import get from 'lodash/get'
61import Authz from 'src/authz'
72import { HttpError } from 'src/error'
83import { OpenApiRequestExt } from 'src/otomi-models'
9- import { RepoService } from '../services/RepoService'
104
115const HttpMethodMapping : Record < string , string > = {
126 DELETE : 'delete' ,
@@ -16,34 +10,12 @@ const HttpMethodMapping: Record<string, string> = {
1610 PUT : 'update' ,
1711}
1812
19- function renameKeys ( obj : Record < string , any > ) {
20- const newKeys = {
21- serviceId : 'id' ,
22- secretId : 'id' ,
23- }
24- if ( Object . keys ( obj ) . length === 1 && 'teamId' in obj ) return { id : obj . teamId }
25- const keyValues = Object . keys ( obj ) . map ( ( key ) => {
26- const newKey = newKeys [ key ] || key
27- return { [ newKey ] : obj [ key ] }
28- } )
29- return Object . assign ( { } , ...keyValues )
30- }
31-
32- // const badCode = (code) => code >= 300 || code < 200
33- // const wrapResponse = (filter, orig) => {
34- // return function (obj) {
35- // if (badCode(this.statusCode)) return orig(obj)
36- // const ret = filter(obj)
37- // return orig(ret)
38- // }
39- // }
40-
4113/**
4214 * Authorize a request based on RBAC and ABAC rules.
4315 * Called by the security handler.
4416 * Throws HttpError if authorization fails.
4517 */
46- export function authorize ( req : OpenApiRequestExt , authz : Authz , repoService : RepoService ) : void {
18+ export function authorize ( req : OpenApiRequestExt , authz : Authz ) : void {
4719 const { body, user } = req
4820 // express-openapi-validator stores path params in req.openapi.pathParams
4921 const teamId = req . openapi ?. pathParams ?. teamId ?? req . params ?. teamId ?? req . query ?. teamId ?? body ?. teamId
@@ -55,8 +27,6 @@ export function authorize(req: OpenApiRequestExt, authz: Authz, repoService: Rep
5527 // If there is no RBAC then we allow the request
5628 if ( ! schemaName ) return
5729
58- const apiSpec = getSpec ( ) . spec
59-
6030 // Initialize rules for the user
6131 authz . init ( user )
6232
@@ -80,53 +50,4 @@ export function authorize(req: OpenApiRequestExt, authz: Authz, repoService: Rep
8050 if ( ! valid ) {
8151 throw new HttpError ( 403 , `User not allowed to perform "${ action } " on "${ schemaName } " resource` )
8252 }
83-
84- // Check ABAC permissions for create/update operations
85- const schemaToRepoMap : Record < string , string > = {
86- Service : 'services' ,
87- Team : 'teamConfig' ,
88- App : 'apps' ,
89- Build : 'builds' ,
90- Workload : 'workloads' ,
91- Settings : 'otomi' ,
92- Netpol : 'netpols' ,
93- Policy : 'policies' ,
94- SealedSecret : 'sealedSecrets' ,
95- }
96- const teamSpecificCollections = [ 'builds' , 'services' , 'workloads' , 'netpols' , 'policies' , 'sealedSecrets' ]
97-
98- //TODO lookup if we can remove this
99- const collectionId = schemaToRepoMap [ schemaName ]
100- if ( collectionId && [ 'create' , 'update' ] . includes ( action ) ) {
101- // Look up x-allow-values from the API spec for ABAC validation
102- let dataOrig = get (
103- apiSpec ,
104- `components.schemas.TeamSelfService.properties.${ schemaName . toLowerCase ( ) } .x-allow-values` ,
105- { } ,
106- )
107-
108- if ( action === 'update' ) {
109- try {
110- const pathParams = req . openapi ?. pathParams ?? req . params
111- const selector = renameKeys ( pathParams )
112- let collection
113- if ( teamSpecificCollections . includes ( collectionId ) ) {
114- collection = repoService . getTeamConfigService ( teamId ) . getCollection ( collectionId )
115- } else {
116- collection = repoService . getCollection ( collectionId )
117- }
118- dataOrig = find ( collection , selector ) || { }
119- } catch ( error ) {
120- debug ( 'Error in authorize' , error )
121- }
122- }
123-
124- const violatedAttributes = authz . validateWithAbac ( action , schemaName , teamId , req . body , dataOrig )
125- if ( violatedAttributes . length > 0 ) {
126- throw new HttpError (
127- 403 ,
128- `User not allowed to modify the following attributes: ${ violatedAttributes . join ( ', ' ) } of ${ schemaName } resource` ,
129- )
130- }
131- }
13253}
0 commit comments