|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import expect from '@kbn/expect'; |
| 9 | +import { DataStreamsResponseBodySchemaBody } from '@kbn/data-usage-plugin/common/rest_types'; |
| 10 | +import { DATA_USAGE_DATA_STREAMS_API_ROUTE } from '@kbn/data-usage-plugin/common'; |
| 11 | +import type { RoleCredentials } from '@kbn/ftr-common-functional-services'; |
| 12 | +import { |
| 13 | + NoIndicesMeteringError, |
| 14 | + NoPrivilegeMeteringError, |
| 15 | +} from '@kbn/data-usage-plugin/server/common/errors'; |
| 16 | +import { FtrProviderContext } from '../../../../ftr_provider_context'; |
| 17 | + |
| 18 | +export default function ({ getService }: FtrProviderContext) { |
| 19 | + const svlDatastreamsHelpers = getService('svlDatastreamsHelpers'); |
| 20 | + const svlCommonApi = getService('svlCommonApi'); |
| 21 | + const samlAuth = getService('samlAuth'); |
| 22 | + const supertestWithoutAuth = getService('supertestWithoutAuth'); |
| 23 | + const testDataStreamName = 'test-data-stream'; |
| 24 | + const otherTestDataStreamName = 'other-test-data-stream'; |
| 25 | + let roleAuthc: RoleCredentials; |
| 26 | + |
| 27 | + describe('privileges with custom roles', function () { |
| 28 | + // custom role testing is not supported in MKI |
| 29 | + // the metering api which this route calls requires one of: monitor,view_index_metadata,manage,all |
| 30 | + this.tags(['skipMKI']); |
| 31 | + before(async () => { |
| 32 | + await svlDatastreamsHelpers.createDataStream(testDataStreamName); |
| 33 | + await svlDatastreamsHelpers.createDataStream(otherTestDataStreamName); |
| 34 | + }); |
| 35 | + after(async () => { |
| 36 | + await svlDatastreamsHelpers.deleteDataStream(testDataStreamName); |
| 37 | + await svlDatastreamsHelpers.deleteDataStream(otherTestDataStreamName); |
| 38 | + }); |
| 39 | + afterEach(async () => { |
| 40 | + await samlAuth.invalidateM2mApiKeyWithRoleScope(roleAuthc); |
| 41 | + await samlAuth.deleteCustomRole(); |
| 42 | + }); |
| 43 | + it('returns all data streams for indices with necessary privileges', async () => { |
| 44 | + await samlAuth.setCustomRole({ |
| 45 | + elasticsearch: { |
| 46 | + indices: [{ names: ['*'], privileges: ['all'] }], |
| 47 | + }, |
| 48 | + kibana: [ |
| 49 | + { |
| 50 | + base: ['all'], |
| 51 | + feature: {}, |
| 52 | + spaces: ['*'], |
| 53 | + }, |
| 54 | + ], |
| 55 | + }); |
| 56 | + roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('customRole'); |
| 57 | + const res = await supertestWithoutAuth |
| 58 | + .get(DATA_USAGE_DATA_STREAMS_API_ROUTE) |
| 59 | + .query({ includeZeroStorage: true }) |
| 60 | + .set(svlCommonApi.getInternalRequestHeader()) |
| 61 | + .set(roleAuthc.apiKeyHeader) |
| 62 | + .set('elastic-api-version', '1'); |
| 63 | + |
| 64 | + const dataStreams: DataStreamsResponseBodySchemaBody = res.body; |
| 65 | + const foundTestDataStream = dataStreams.find((stream) => stream.name === testDataStreamName); |
| 66 | + const foundTestDataStream2 = dataStreams.find( |
| 67 | + (stream) => stream.name === otherTestDataStreamName |
| 68 | + ); |
| 69 | + expect(res.statusCode).to.be(200); |
| 70 | + expect(foundTestDataStream?.name).to.be(testDataStreamName); |
| 71 | + expect(foundTestDataStream2?.name).to.be(otherTestDataStreamName); |
| 72 | + }); |
| 73 | + it('returns data streams for only a subset of indices with necessary privileges', async () => { |
| 74 | + await samlAuth.setCustomRole({ |
| 75 | + elasticsearch: { |
| 76 | + indices: [{ names: ['test-data-stream*'], privileges: ['all'] }], |
| 77 | + }, |
| 78 | + kibana: [ |
| 79 | + { |
| 80 | + base: ['all'], |
| 81 | + feature: {}, |
| 82 | + spaces: ['*'], |
| 83 | + }, |
| 84 | + ], |
| 85 | + }); |
| 86 | + roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('customRole'); |
| 87 | + const res = await supertestWithoutAuth |
| 88 | + .get(DATA_USAGE_DATA_STREAMS_API_ROUTE) |
| 89 | + .query({ includeZeroStorage: true }) |
| 90 | + .set(svlCommonApi.getInternalRequestHeader()) |
| 91 | + .set(roleAuthc.apiKeyHeader) |
| 92 | + .set('elastic-api-version', '1'); |
| 93 | + |
| 94 | + const dataStreams: DataStreamsResponseBodySchemaBody = res.body; |
| 95 | + const foundTestDataStream = dataStreams.find((stream) => stream.name === testDataStreamName); |
| 96 | + const dataStreamNoPermission = dataStreams.find( |
| 97 | + (stream) => stream.name === otherTestDataStreamName |
| 98 | + ); |
| 99 | + |
| 100 | + expect(res.statusCode).to.be(200); |
| 101 | + expect(foundTestDataStream?.name).to.be(testDataStreamName); |
| 102 | + expect(dataStreamNoPermission?.name).to.be(undefined); |
| 103 | + }); |
| 104 | + it('returns no data streams without necessary privileges', async () => { |
| 105 | + await samlAuth.setCustomRole({ |
| 106 | + elasticsearch: { |
| 107 | + indices: [{ names: ['*'], privileges: ['write'] }], |
| 108 | + }, |
| 109 | + kibana: [ |
| 110 | + { |
| 111 | + base: ['all'], |
| 112 | + feature: {}, |
| 113 | + spaces: ['*'], |
| 114 | + }, |
| 115 | + ], |
| 116 | + }); |
| 117 | + roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('customRole'); |
| 118 | + const res = await supertestWithoutAuth |
| 119 | + .get(DATA_USAGE_DATA_STREAMS_API_ROUTE) |
| 120 | + .query({ includeZeroStorage: true }) |
| 121 | + .set(svlCommonApi.getInternalRequestHeader()) |
| 122 | + .set(roleAuthc.apiKeyHeader) |
| 123 | + .set('elastic-api-version', '1'); |
| 124 | + |
| 125 | + expect(res.statusCode).to.be(403); |
| 126 | + expect(res.body.message).to.contain(NoPrivilegeMeteringError); |
| 127 | + }); |
| 128 | + it('returns no data streams when there are none it has access to', async () => { |
| 129 | + await samlAuth.setCustomRole({ |
| 130 | + elasticsearch: { |
| 131 | + indices: [{ names: ['none*'], privileges: ['all'] }], |
| 132 | + }, |
| 133 | + kibana: [ |
| 134 | + { |
| 135 | + base: ['all'], |
| 136 | + feature: {}, |
| 137 | + spaces: ['*'], |
| 138 | + }, |
| 139 | + ], |
| 140 | + }); |
| 141 | + roleAuthc = await samlAuth.createM2mApiKeyWithRoleScope('customRole'); |
| 142 | + const res = await supertestWithoutAuth |
| 143 | + .get(DATA_USAGE_DATA_STREAMS_API_ROUTE) |
| 144 | + .query({ includeZeroStorage: true }) |
| 145 | + .set(svlCommonApi.getInternalRequestHeader()) |
| 146 | + .set(roleAuthc.apiKeyHeader) |
| 147 | + .set('elastic-api-version', '1'); |
| 148 | + |
| 149 | + expect(res.statusCode).to.be(404); |
| 150 | + expect(res.body.message).to.contain(NoIndicesMeteringError); |
| 151 | + }); |
| 152 | + }); |
| 153 | +} |
0 commit comments