14
14
* limitations under the License.
15
15
*/
16
16
17
- import { errorHandler , loggerToWinstonLogger } from '@backstage/backend-common' ;
17
+ import {
18
+ createLegacyAuthAdapters ,
19
+ errorHandler ,
20
+ loggerToWinstonLogger ,
21
+ PluginEndpointDiscovery ,
22
+ } from '@backstage/backend-common' ;
18
23
import {
19
24
coreServices ,
20
25
createBackendPlugin ,
26
+ HttpAuthService ,
27
+ PermissionsService ,
21
28
} from '@backstage/backend-plugin-api' ;
22
29
import { Config } from '@backstage/config' ;
30
+ import { NotAllowedError } from '@backstage/errors' ;
31
+ import { AuthorizeResult } from '@backstage/plugin-permission-common' ;
32
+ import { createPermissionIntegrationRouter } from '@backstage/plugin-permission-node' ;
23
33
24
34
import express from 'express' ;
25
35
import Router from 'express-promise-router' ;
36
+ import { Request } from 'express-serve-static-core' ;
26
37
import { Logger } from 'winston' ;
27
38
28
39
import {
29
40
Cluster ,
30
41
ClusterOverview ,
42
+ ocmClusterReadPermission ,
43
+ ocmEntityPermissions ,
31
44
} from '@janus-idp/backstage-plugin-ocm-common' ;
32
45
33
46
import { readOcmConfigs } from '../helpers/config' ;
@@ -52,11 +65,25 @@ import { ManagedClusterInfo } from '../types';
52
65
export interface RouterOptions {
53
66
logger : Logger ;
54
67
config : Config ;
68
+ discovery : PluginEndpointDiscovery ;
69
+ permissions : PermissionsService ;
70
+ httpAuth ?: HttpAuthService ;
55
71
}
56
72
57
- const buildRouter = ( config : Config , logger : Logger ) => {
73
+ const buildRouter = (
74
+ config : Config ,
75
+ logger : Logger ,
76
+ httpAuth : HttpAuthService ,
77
+ permissions : PermissionsService ,
78
+ ) => {
58
79
const router = Router ( ) ;
80
+
81
+ const permissionsIntegrationRouter = createPermissionIntegrationRouter ( {
82
+ permissions : ocmEntityPermissions ,
83
+ } ) ;
84
+
59
85
router . use ( express . json ( ) ) ;
86
+ router . use ( permissionsIntegrationRouter ) ;
60
87
61
88
const clients = Object . fromEntries (
62
89
readOcmConfigs ( config ) . map ( provider => [
@@ -68,43 +95,63 @@ const buildRouter = (config: Config, logger: Logger) => {
68
95
] ) ,
69
96
) ;
70
97
71
- router . get (
72
- '/status/:providerId/:clusterName' ,
73
- async ( { params : { clusterName, providerId } } , response ) => {
74
- logger . debug (
75
- `Incoming status request for ${ clusterName } cluster on ${ providerId } hub` ,
76
- ) ;
77
-
78
- if ( ! clients . hasOwnProperty ( providerId ) ) {
79
- throw Object . assign ( new Error ( 'Hub not found' ) , {
80
- statusCode : 404 ,
81
- name : 'HubNotFound' ,
82
- } ) ;
83
- }
84
-
85
- const normalizedClusterName = translateResourceToOCM (
86
- clusterName ,
87
- clients [ providerId ] . hubResourceName ,
88
- ) ;
89
-
90
- const mc = await getManagedCluster (
91
- clients [ providerId ] . client ,
92
- normalizedClusterName ,
93
- ) ;
94
- const mci = await getManagedClusterInfo (
95
- clients [ providerId ] . client ,
96
- normalizedClusterName ,
97
- ) ;
98
-
99
- response . send ( {
100
- name : clusterName ,
101
- ...parseManagedCluster ( mc ) ,
102
- ...parseUpdateInfo ( mci ) ,
103
- } as Cluster ) ;
104
- } ,
105
- ) ;
98
+ const authorize = async ( request : Request ) => {
99
+ const decision = (
100
+ await permissions . authorize ( [ { permission : ocmClusterReadPermission } ] , {
101
+ credentials : await httpAuth . credentials ( request ) ,
102
+ } )
103
+ ) [ 0 ] ;
104
+
105
+ return decision ;
106
+ } ;
107
+
108
+ router . get ( '/status/:providerId/:clusterName' , async ( request , response ) => {
109
+ const decision = await authorize ( request ) ;
110
+
111
+ if ( decision . result === AuthorizeResult . DENY ) {
112
+ throw new NotAllowedError ( 'Unauthorized' ) ;
113
+ }
114
+
115
+ const { clusterName, providerId } = request . params ;
116
+ logger . debug (
117
+ `Incoming status request for ${ clusterName } cluster on ${ providerId } hub` ,
118
+ ) ;
119
+
120
+ if ( ! clients . hasOwnProperty ( providerId ) ) {
121
+ throw Object . assign ( new Error ( 'Hub not found' ) , {
122
+ statusCode : 404 ,
123
+ name : 'HubNotFound' ,
124
+ } ) ;
125
+ }
126
+
127
+ const normalizedClusterName = translateResourceToOCM (
128
+ clusterName ,
129
+ clients [ providerId ] . hubResourceName ,
130
+ ) ;
131
+
132
+ const mc = await getManagedCluster (
133
+ clients [ providerId ] . client ,
134
+ normalizedClusterName ,
135
+ ) ;
136
+ const mci = await getManagedClusterInfo (
137
+ clients [ providerId ] . client ,
138
+ normalizedClusterName ,
139
+ ) ;
140
+
141
+ response . send ( {
142
+ name : clusterName ,
143
+ ...parseManagedCluster ( mc ) ,
144
+ ...parseUpdateInfo ( mci ) ,
145
+ } as Cluster ) ;
146
+ } ) ;
147
+
148
+ router . get ( '/status' , async ( request , response ) => {
149
+ const decision = await authorize ( request ) ;
150
+
151
+ if ( decision . result === AuthorizeResult . DENY ) {
152
+ throw new NotAllowedError ( 'Unauthorized' ) ;
153
+ }
106
154
107
- router . get ( '/status' , async ( _ , response ) => {
108
155
logger . debug ( `Incoming status request for all clusters` ) ;
109
156
110
157
const allClusters = await Promise . all (
@@ -144,8 +191,11 @@ export async function createRouter(
144
191
) : Promise < express . Router > {
145
192
const { logger } = options ;
146
193
const { config } = options ;
194
+ const { permissions } = options ;
195
+
196
+ const { httpAuth } = createLegacyAuthAdapters ( options ) ;
147
197
148
- return buildRouter ( config , logger ) ;
198
+ return buildRouter ( config , logger , httpAuth , permissions ) ;
149
199
}
150
200
151
201
export const ocmPlugin = createBackendPlugin ( {
@@ -156,9 +206,18 @@ export const ocmPlugin = createBackendPlugin({
156
206
logger : coreServices . logger ,
157
207
config : coreServices . rootConfig ,
158
208
http : coreServices . httpRouter ,
209
+ httpAuth : coreServices . httpAuth ,
210
+ permissions : coreServices . permissions ,
159
211
} ,
160
- async init ( { config, logger, http } ) {
161
- http . use ( buildRouter ( config , loggerToWinstonLogger ( logger ) ) ) ;
212
+ async init ( { config, logger, http, httpAuth, permissions } ) {
213
+ http . use (
214
+ buildRouter (
215
+ config ,
216
+ loggerToWinstonLogger ( logger ) ,
217
+ httpAuth ,
218
+ permissions ,
219
+ ) ,
220
+ ) ;
162
221
} ,
163
222
} ) ;
164
223
} ,
0 commit comments