|
| 1 | +import express from 'express'; |
| 2 | +import request from 'supertest'; |
| 3 | +import { ApiGateway, ApiGatewayOptions, Query, Request } from '../src'; |
| 4 | +import { |
| 5 | + compilerApi, |
| 6 | + DataSourceStorageMock, |
| 7 | + AdapterApiMock |
| 8 | +} from './mocks'; |
| 9 | + |
| 10 | +const API_SECRET = 'secret'; |
| 11 | +const AUTH_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M'; |
| 12 | +const logger = () => undefined; |
| 13 | +function createApiGateway( |
| 14 | + options: Partial<ApiGatewayOptions> = {} |
| 15 | +) { |
| 16 | + process.env.NODE_ENV = 'production'; |
| 17 | + |
| 18 | + const app = express(); |
| 19 | + const adapterApi: any = new AdapterApiMock(); |
| 20 | + const dataSourceStorage: any = new DataSourceStorageMock(); |
| 21 | + const apiGateway = new ApiGateway(API_SECRET, compilerApi, () => adapterApi, logger, { |
| 22 | + standalone: true, |
| 23 | + dataSourceStorage, |
| 24 | + basePath: '/cubejs-api', |
| 25 | + refreshScheduler: {}, |
| 26 | + ...options, |
| 27 | + }); |
| 28 | + apiGateway.initApp(app); |
| 29 | + return { |
| 30 | + app, |
| 31 | + apiGateway, |
| 32 | + dataSourceStorage, |
| 33 | + adapterApi |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +describe('Gateway permissions', () => { |
| 38 | + test('Liveliness declined', async () => { |
| 39 | + const { app, apiGateway } = createApiGateway({ |
| 40 | + contextToPermissions: async () => new Promise((resolve) => { |
| 41 | + resolve(['graphql', 'meta', 'data', 'jobs']); |
| 42 | + }), |
| 43 | + }); |
| 44 | + |
| 45 | + await request(app) |
| 46 | + .get('/readyz') |
| 47 | + .set('Authorization', AUTH_TOKEN) |
| 48 | + .expect(200); |
| 49 | + |
| 50 | + await request(app) |
| 51 | + .get('/livez') |
| 52 | + .set('Authorization', AUTH_TOKEN) |
| 53 | + .expect(200); |
| 54 | + |
| 55 | + apiGateway.release(); |
| 56 | + }); |
| 57 | + |
| 58 | + test('GraphQL declined', async () => { |
| 59 | + const { app, apiGateway } = createApiGateway({ |
| 60 | + contextToPermissions: async () => new Promise((resolve) => { |
| 61 | + resolve(['liveliness', 'meta', 'data', 'jobs']); |
| 62 | + }), |
| 63 | + }); |
| 64 | + |
| 65 | + const res = await request(app) |
| 66 | + .get('/cubejs-api/graphql') |
| 67 | + .set('Authorization', AUTH_TOKEN) |
| 68 | + .expect(403); |
| 69 | + |
| 70 | + expect(res.body && res.body.error) |
| 71 | + .toStrictEqual('Permission is not allowed: graphql'); |
| 72 | + |
| 73 | + apiGateway.release(); |
| 74 | + }); |
| 75 | + |
| 76 | + test('Meta declined', async () => { |
| 77 | + const { app, apiGateway } = createApiGateway({ |
| 78 | + contextToPermissions: async () => new Promise((resolve) => { |
| 79 | + resolve(['liveliness', 'graphql', 'data', 'jobs']); |
| 80 | + }), |
| 81 | + }); |
| 82 | + |
| 83 | + const res1 = await request(app) |
| 84 | + .get('/cubejs-api/v1/meta') |
| 85 | + .set('Authorization', AUTH_TOKEN) |
| 86 | + .expect(403); |
| 87 | + |
| 88 | + expect(res1.body && res1.body.error) |
| 89 | + .toStrictEqual('Permission is not allowed: meta'); |
| 90 | + |
| 91 | + const res2 = await request(app) |
| 92 | + .post('/cubejs-api/v1/pre-aggregations/can-use') |
| 93 | + .set('Authorization', AUTH_TOKEN) |
| 94 | + .expect(403); |
| 95 | + |
| 96 | + expect(res2.body && res2.body.error) |
| 97 | + .toStrictEqual('Permission is not allowed: meta'); |
| 98 | + |
| 99 | + apiGateway.release(); |
| 100 | + }); |
| 101 | + |
| 102 | + test('Data declined', async () => { |
| 103 | + const { app, apiGateway } = createApiGateway({ |
| 104 | + contextToPermissions: async () => new Promise((resolve) => { |
| 105 | + resolve(['liveliness', 'graphql', 'meta', 'jobs']); |
| 106 | + }), |
| 107 | + }); |
| 108 | + |
| 109 | + const res1 = await request(app) |
| 110 | + .get('/cubejs-api/v1/load') |
| 111 | + .set('Authorization', AUTH_TOKEN) |
| 112 | + .expect(403); |
| 113 | + |
| 114 | + expect(res1.body && res1.body.error) |
| 115 | + .toStrictEqual('Permission is not allowed: data'); |
| 116 | + |
| 117 | + const res2 = await request(app) |
| 118 | + .post('/cubejs-api/v1/load') |
| 119 | + .set('Authorization', AUTH_TOKEN) |
| 120 | + .expect(403); |
| 121 | + |
| 122 | + expect(res2.body && res2.body.error) |
| 123 | + .toStrictEqual('Permission is not allowed: data'); |
| 124 | + |
| 125 | + const res3 = await request(app) |
| 126 | + .get('/cubejs-api/v1/subscribe') |
| 127 | + .set('Authorization', AUTH_TOKEN) |
| 128 | + .expect(403); |
| 129 | + |
| 130 | + expect(res3.body && res3.body.error) |
| 131 | + .toStrictEqual('Permission is not allowed: data'); |
| 132 | + |
| 133 | + const res4 = await request(app) |
| 134 | + .get('/cubejs-api/v1/sql') |
| 135 | + .set('Authorization', AUTH_TOKEN) |
| 136 | + .expect(403); |
| 137 | + |
| 138 | + expect(res4.body && res4.body.error) |
| 139 | + .toStrictEqual('Permission is not allowed: data'); |
| 140 | + |
| 141 | + const res5 = await request(app) |
| 142 | + .post('/cubejs-api/v1/sql') |
| 143 | + .set('Content-type', 'application/json') |
| 144 | + .set('Authorization', AUTH_TOKEN) |
| 145 | + .expect(403); |
| 146 | + |
| 147 | + expect(res5.body && res5.body.error) |
| 148 | + .toStrictEqual('Permission is not allowed: data'); |
| 149 | + |
| 150 | + const res6 = await request(app) |
| 151 | + .get('/cubejs-api/v1/dry-run') |
| 152 | + .set('Authorization', AUTH_TOKEN) |
| 153 | + .expect(403); |
| 154 | + |
| 155 | + expect(res6.body && res6.body.error) |
| 156 | + .toStrictEqual('Permission is not allowed: data'); |
| 157 | + |
| 158 | + const res7 = await request(app) |
| 159 | + .post('/cubejs-api/v1/dry-run') |
| 160 | + .set('Content-type', 'application/json') |
| 161 | + .set('Authorization', AUTH_TOKEN) |
| 162 | + .expect(403); |
| 163 | + |
| 164 | + expect(res7.body && res7.body.error) |
| 165 | + .toStrictEqual('Permission is not allowed: data'); |
| 166 | + |
| 167 | + apiGateway.release(); |
| 168 | + }); |
| 169 | + |
| 170 | + test('Jobs declined', async () => { |
| 171 | + const { app, apiGateway } = createApiGateway({ |
| 172 | + contextToPermissions: async () => new Promise((resolve) => { |
| 173 | + resolve(['liveliness', 'graphql', 'data', 'meta']); |
| 174 | + }), |
| 175 | + }); |
| 176 | + |
| 177 | + const res1 = await request(app) |
| 178 | + .post('/cubejs-api/v1/pre-aggregations/jobs') |
| 179 | + .set('Authorization', AUTH_TOKEN) |
| 180 | + .expect(403); |
| 181 | + |
| 182 | + expect(res1.body && res1.body.error) |
| 183 | + .toStrictEqual('Permission is not allowed: jobs'); |
| 184 | + |
| 185 | + const res2 = await request(app) |
| 186 | + .get('/cubejs-api/v1/run-scheduled-refresh') |
| 187 | + .set('Authorization', AUTH_TOKEN) |
| 188 | + .expect(403); |
| 189 | + |
| 190 | + expect(res2.body && res2.body.error) |
| 191 | + .toStrictEqual('Permission is not allowed: jobs'); |
| 192 | + |
| 193 | + apiGateway.release(); |
| 194 | + }); |
| 195 | +}); |
0 commit comments