Skip to content

Commit

Permalink
[WIP] Securing Spaces (#21995)
Browse files Browse the repository at this point in the history
* Splitting to a client and a wrapper, integation tests are passing
without spaces

* Adding the spaces wrapper back in the mix using a priority collection

* Restructuring the secure wrapper as we don't need to switch between
repositories

* Checking authorization at the current space

* Beginning to make the rbac api integration tests run against spaces

* Adding identical data to the rbac esArchives for two more spaces

* Adding some space tests for find

* Beginning to work on the spaces client

* Fixing find and filtering out unrequested privileges from response

* Adding get code and test

* Introducing an RBAC Auth Scope

* Exposing the spacesClient a bit better

* Moving the server.expose to the security plugin init

* Moving checkPrivilegesAtAllResources to it's own thing

* No longer using the auth scope for RBAC, dashboard mode didn't work with
it

* Securing the create space method

* Adding secure update method

* Adding secured delete endpoints

* Restructuring some code in the spaces client

* Adding tests for the select endpoint

* Spaces can't be managed via the SavedObjectsClient now

* Creating separate space_all and space_read privileges

* Splitting out the spaces and global privileges

* Fixing edit role screen after API changes

* Revising comment, there is a Set in JavaScript now, but lodash can't
equal them

* Using authorization mode to log deprecation warning on login

* Changing the signature of checkPrivileges

We're now using the legacy fallback when there are no application
privileges. This improves performance at the sake of legacy 403s being
displayed to users who have no application privileges.

* Refactoring the way we specify resources when checking privileges

* Exposing the space service more intuitively

* Fixing comments

* Security defines all actions

* Renaming `response` returned by the checkPrivileges function

* Hard-coding the kibana app privileges teporarily

* Adding Authenticator authorization mode tests

* Adding actions.manageSpaces tests

* Adding check privileges tests

* Fixing checkPrivileges test snapshots

* Making sure tests fail until I correct this deficiency

* Adding stubbed out authorization mode tests

* Fixing tests for RegisterPrivilegesWithCluster

* Fixing service AuthorizationService tests

* Addinng serializer tests

* Adding validateEsResponse tests

* We don't need the SecureSavedObjectsClient anymore!

* Adding SecureSavedObjectsClientWrapper tests...

* Fixing a few stray tests

* Fixing issue when user isn't authenticated and check useRbacForRequest

* Validating spaces we're adding to roles

* Reusing hasAnyPrivileges from hasAnyResourcePrivileges

* Better variable name

* toArray -> toPrioritizedArray

* Using Space throughout the SpacesClient

* GetActiveSpace uses the SpacesClient now

* Squashed commit of the following:

commit 23515b1
Author: kobelb <brandon.kobel@elastic.co>
Date:   Mon Sep 10 06:57:58 2018 -0400

    Adding more users to the spaces tests

commit 4bbde73
Author: kobelb <brandon.kobel@elastic.co>
Date:   Mon Sep 10 06:09:35 2018 -0400

    Adding not space aware get tests

commit 5d11bef
Author: kobelb <brandon.kobel@elastic.co>
Date:   Sat Sep 8 14:06:20 2018 -0400

    Adding not space aware test to find

commit f9383fd
Author: kobelb <brandon.kobel@elastic.co>
Date:   Sat Sep 8 13:49:04 2018 -0400

    Adding bulk create tests and testing non space aware type with bulkGet

commit 5388b5a
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 16:18:04 2018 -0400

    Adding bulk create test

commit 0674263
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 15:58:21 2018 -0400

    Ignoring some modules

commit 6b011d3
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 15:55:58 2018 -0400

    Making the users match for saved objects security and spaces

commit de2f994
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 14:18:53 2018 -0400

    Making the space suites define their own test expectations

commit 5407866
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 13:15:46 2018 -0400

    Removing redundant spaces folder

commit 9913923
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 13:14:45 2018 -0400

    Removing unneeded objects from the esarchive

commit bc602b1
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 13:08:12 2018 -0400

    Moving some tests around

commit 7fec308
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 12:38:23 2018 -0400

    Deleting rbac_api_integration tests, they've been migrated elsewhere

commit 29c018e
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 10:01:16 2018 -0400

    Importing SuperTest where needed

commit 38d2e74
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 08:44:53 2018 -0400

    Removing the "saved_objects" folder

commit 70eada4
Merge: 1b2708f 9023431
Author: Brandon Kobel <brandon.kobel@gmail.com>
Date:   Fri Sep 7 10:04:03 2018 -0400

    Merge pull request #4 from legrego/spaces-api-tests

    Initial round of spaces api testing

commit 9023431
Merge: 6410f72 1b2708f
Author: Larry Gregory <larry.gregory@elastic.co>
Date:   Fri Sep 7 09:37:57 2018 -0400

    Merge remote-tracking branch 'kobelb/spaces/securing-api-tests' into spaces-api-tests

commit 1b2708f
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 08:17:25 2018 -0400

    Even more typescript

commit 369a429
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 08:08:28 2018 -0400

    Typescriptifying Get

commit f53f2ab
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 08:01:48 2018 -0400

    Typescriptifying Find

commit f707e03
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 07:55:10 2018 -0400

    Typescriptifying Create

commit 485d983
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 07:31:54 2018 -0400

    Changing the namespace agnostic type name

commit 71c2122
Author: kobelb <brandon.kobel@elastic.co>
Date:   Fri Sep 7 07:25:15 2018 -0400

    Adding update tests

commit f60e953
Author: kobelb <brandon.kobel@elastic.co>
Date:   Thu Sep 6 15:53:34 2018 -0400

    Delete tests

commit 94682e5
Author: kobelb <brandon.kobel@elastic.co>
Date:   Thu Sep 6 12:07:39 2018 -0400

    Adding get security and spaces tests

commit 481943f
Author: kobelb <brandon.kobel@elastic.co>
Date:   Thu Sep 6 11:58:42 2018 -0400

    Generalizing bulk get

commit 14d9058
Merge: 6627127 fc5f7fa
Author: Brandon Kobel <brandon.kobel@gmail.com>
Date:   Thu Sep 6 10:46:07 2018 -0400

    Merge pull request #3 from legrego/remove-privs-api

    Remove privs api and hardcoded privs list

commit 6410f72
Author: Larry Gregory <larry.gregory@elastic.co>
Date:   Thu Sep 6 09:35:30 2018 -0400

    add missing superagent type

commit 4afacc0
Author: Larry Gregory <larry.gregory@elastic.co>
Date:   Wed Sep 5 20:19:15 2018 -0400

    initial round of spaces api testing

commit 6627127
Author: kobelb <brandon.kobel@elastic.co>
Date:   Wed Sep 5 17:29:37 2018 -0400

    Adding GET test suite

commit 68a5537
Author: kobelb <brandon.kobel@elastic.co>
Date:   Wed Sep 5 13:32:49 2018 -0400

    Copying find to security and spaces

commit fc5f7fa
Author: Larry Gregory <larry.gregory@elastic.co>
Date:   Wed Sep 5 12:36:30 2018 -0400

    move es privilege tests to api_integration

commit 189fbe6
Author: kobelb <brandon.kobel@elastic.co>
Date:   Wed Sep 5 12:24:41 2018 -0400

    Switching approach to dynamically enabling security

commit c72200f
Author: Larry Gregory <larry.gregory@elastic.co>
Date:   Wed Sep 5 11:57:26 2018 -0400

    remove get privileges api

commit 1607f80
Author: kobelb <brandon.kobel@elastic.co>
Date:   Wed Sep 5 11:47:19 2018 -0400

    Dynamically supplying users so we reduce some duplication

commit 9deec1b
Author: kobelb <brandon.kobel@elastic.co>
Date:   Wed Sep 5 09:32:36 2018 -0400

    Security and Spaces create tests

commit a8232dd
Author: kobelb <brandon.kobel@elastic.co>
Date:   Wed Sep 5 07:22:10 2018 -0400

    Using a create "test suite"

commit f07f668
Author: kobelb <brandon.kobel@elastic.co>
Date:   Wed Sep 5 05:54:46 2018 -0400

    Using the spaces esArchive always now

commit b2021ad
Merge: d3babea 7b4575b
Author: kobelb <brandon.kobel@elastic.co>
Date:   Wed Sep 5 05:43:48 2018 -0400

    Merge branch 'spaces/securing' into spaces/securing-api-tests

commit d3babea
Author: kobelb <brandon.kobel@elastic.co>
Date:   Tue Sep 4 17:43:38 2018 -0400

    Moving over the spaces only saved objects tests

commit 94054a2
Author: kobelb <brandon.kobel@elastic.co>
Date:   Tue Sep 4 17:26:43 2018 -0400

    Copying over the security only saved object api tests

* update public api to use SpacesClient

* fix

* test and api fixes

* fix tests

* Disallowing use of Spaces with the SpacesSavedObjectsClientWrapper

* Adding spaces audit logging

* Updating snapshots

* Adding get and getAll tests for the spaces client

* Adding update tests

* Adding create tests

* Adding SpacesClient delete tests

* Fixing authenticate tests

* Making tests more consistent

* Fixing kibana privileges tests

* Fixing a few type issues

* Making typescript ignore the .only test suites

* Switching to beforeEach and afterEach and removing the mocha types

* Switching to our own expect.js definitions

The definitions in @types/expect have expect being globally defined
which conflicts with the jest definitions, and expect isn't globally
defined in the mocha tests and we must use the module value

* Fixing more linting rules

* Removing test stubs and replacing with TODOs. Updating snapshots

* No longer shadowing application for the put role API

* Relying on the errors thrown by the SpacedClient when determining active
space

* Back to after/before... mocha doesn't have beforeAll/afterAll

* We got them types, thanks Spencer!!!

* Ignoring space type from the secure saved objects client find with no
type
  • Loading branch information
kobelb authored Sep 12, 2018
1 parent e9542a6 commit b90b30f
Show file tree
Hide file tree
Showing 190 changed files with 16,618 additions and 5,225 deletions.
11 changes: 7 additions & 4 deletions packages/kbn-test/src/functional_tests/lib/run_elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ import { setupUsers, DEFAULT_SUPERUSER_PASS } from './auth';

export async function runElasticsearch({ config, options }) {
const { log, esFrom } = options;
const isOss = config.get('esTestCluster.license') === 'oss';
const license = config.get('esTestCluster.license');
const isTrialLicense = config.get('esTestCluster.license') === 'trial';

const cluster = createEsTestCluster({
port: config.get('servers.elasticsearch.port'),
password: !isOss ? DEFAULT_SUPERUSER_PASS : config.get('servers.elasticsearch.password'),
license: config.get('esTestCluster.license'),
password: isTrialLicense
? DEFAULT_SUPERUSER_PASS
: config.get('servers.elasticsearch.password'),
license,
log,
basePath: resolve(KIBANA_ROOT, '.es'),
esFrom: esFrom || config.get('esTestCluster.from'),
Expand All @@ -40,7 +43,7 @@ export async function runElasticsearch({ config, options }) {

await cluster.start(esArgs);

if (!isOss) {
if (isTrialLicense) {
await setupUsers(log, config);
}

Expand Down
1 change: 1 addition & 0 deletions src/dev/typescript/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Project } from './project';
export const PROJECTS = [
new Project(resolve(REPO_ROOT, 'tsconfig.json')),
new Project(resolve(REPO_ROOT, 'x-pack/tsconfig.json')),
new Project(resolve(REPO_ROOT, 'x-pack/test/tsconfig.json'), 'x-pack/test'),

// NOTE: using glob.sync rather than glob-all or globby
// because it takes less than 10 ms, while the other modules
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`1, 1 throws Error 1`] = `"Already have entry with this priority"`;
57 changes: 57 additions & 0 deletions src/server/saved_objects/service/lib/priority_collection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { PriorityCollection } from './priority_collection';

test(`1, 2, 3`, () => {
const priorityCollection = new PriorityCollection();
priorityCollection.add(1, 1);
priorityCollection.add(2, 2);
priorityCollection.add(3, 3);
expect(priorityCollection.toPrioritizedArray()).toEqual([1, 2, 3]);
});

test(`3, 2, 1`, () => {
const priorityCollection = new PriorityCollection();
priorityCollection.add(3, 3);
priorityCollection.add(2, 2);
priorityCollection.add(1, 1);
expect(priorityCollection.toPrioritizedArray()).toEqual([1, 2, 3]);
});

test(`2, 3, 1`, () => {
const priorityCollection = new PriorityCollection();
priorityCollection.add(2, 2);
priorityCollection.add(3, 3);
priorityCollection.add(1, 1);
expect(priorityCollection.toPrioritizedArray()).toEqual([1, 2, 3]);
});

test(`Number.MAX_VALUE, NUMBER.MIN_VALUE, 1`, () => {
const priorityCollection = new PriorityCollection();
priorityCollection.add(Number.MAX_VALUE, 3);
priorityCollection.add(Number.MIN_VALUE, 1);
priorityCollection.add(1, 2);
expect(priorityCollection.toPrioritizedArray()).toEqual([1, 2, 3]);
});

test(`1, 1 throws Error`, () => {
const priorityCollection = new PriorityCollection();
priorityCollection.add(1, 1);
expect(() => priorityCollection.add(1, 1)).toThrowErrorMatchingSnapshot();
});
47 changes: 47 additions & 0 deletions src/server/saved_objects/service/lib/priority_collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

interface PriorityCollectionEntry<T> {
priority: number;
value: T;
}

export class PriorityCollection<T> {
private readonly array: Array<PriorityCollectionEntry<T>> = [];

public add(priority: number, value: T) {
let i = 0;
while (i < this.array.length) {
const current = this.array[i];
if (priority === current.priority) {
throw new Error('Already have entry with this priority');
}

if (priority < current.priority) {
break;
}
++i;
}
this.array.splice(i, 0, { priority, value });
}

public toPrioritizedArray(): T[] {
return this.array.map(entry => entry.value);
}
}
29 changes: 12 additions & 17 deletions src/server/saved_objects/service/lib/scoped_client_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,23 @@
* specific language governing permissions and limitations
* under the License.
*/
import { PriorityCollection } from './priority_collection';

/**
* Provider for the Scoped Saved Object Client.
*/
export class ScopedSavedObjectsClientProvider {

_wrapperFactories = [];
_wrapperFactories = new PriorityCollection();

constructor({
defaultClientFactory
}) {
this._originalClientFactory = this._clientFactory = defaultClientFactory;
}

// the client wrapper factories are put at the front of the array, so that
// when we use `reduce` below they're invoked in LIFO order. This is so that
// if multiple plugins register their client wrapper factories, then we can use
// the plugin dependencies/optionalDependencies to implicitly control the order
// in which these are used. For example, if we have a plugin a that declares a
// dependency on plugin b, that means that plugin b's client wrapper would want
// to be able to run first when the SavedObjectClient methods are invoked to
// provide additional context to plugin a's client wrapper.
addClientWrapperFactory(wrapperFactory) {
this._wrapperFactories.unshift(wrapperFactory);
addClientWrapperFactory(priority, wrapperFactory) {
this._wrapperFactories.add(priority, wrapperFactory);
}

setClientFactory(customClientFactory) {
Expand All @@ -55,11 +48,13 @@ export class ScopedSavedObjectsClientProvider {
request,
});

return this._wrapperFactories.reduce((clientToWrap, wrapperFactory) => {
return wrapperFactory({
request,
client: clientToWrap,
});
}, client);
return this._wrapperFactories
.toPrioritizedArray()
.reduceRight((clientToWrap, wrapperFactory) => {
return wrapperFactory({
request,
client: clientToWrap,
});
}, client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,20 @@ test(`throws error when more than one scoped saved objects client factory is set
}).toThrowErrorMatchingSnapshot();
});

test(`invokes and uses instance from single added wrapper factory`, () => {
test(`invokes and uses wrappers in specified order`, () => {
const defaultClient = Symbol();
const defaultClientFactoryMock = jest.fn().mockReturnValue(defaultClient);
const clientProvider = new ScopedSavedObjectsClientProvider({
defaultClientFactory: defaultClientFactoryMock
});
const wrappedClient = Symbol();
const clientWrapperFactoryMock = jest.fn().mockReturnValue(wrappedClient);
const request = Symbol();

clientProvider.addClientWrapperFactory(clientWrapperFactoryMock);
const actualClient = clientProvider.getClient(request);

expect(actualClient).toBe(wrappedClient);
expect(clientWrapperFactoryMock).toHaveBeenCalledWith({
request,
client: defaultClient
});
});

test(`invokes and uses wrappers in LIFO order`, () => {
const defaultClient = Symbol();
const defaultClientFactoryMock = jest.fn().mockReturnValue(defaultClient);
const clientProvider = new ScopedSavedObjectsClientProvider({
defaultClientFactory: defaultClientFactoryMock
});
const firstWrappedClient = Symbol();
const firstWrappedClient = Symbol('first client');
const firstClientWrapperFactoryMock = jest.fn().mockReturnValue(firstWrappedClient);
const secondWrapperClient = Symbol();
const secondWrapperClient = Symbol('second client');
const secondClientWrapperFactoryMock = jest.fn().mockReturnValue(secondWrapperClient);
const request = Symbol();

clientProvider.addClientWrapperFactory(firstClientWrapperFactoryMock);
clientProvider.addClientWrapperFactory(secondClientWrapperFactoryMock);
clientProvider.addClientWrapperFactory(1, secondClientWrapperFactoryMock);
clientProvider.addClientWrapperFactory(0, firstClientWrapperFactoryMock);
const actualClient = clientProvider.getClient(request);

expect(actualClient).toBe(firstWrappedClient);
Expand Down
121 changes: 121 additions & 0 deletions test/api_integration/apis/saved_objects/bulk_create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import expect from 'expect.js';

export default function ({ getService }) {
const supertest = getService('supertest');
const es = getService('es');
const esArchiver = getService('esArchiver');

const BULK_REQUESTS = [
{
type: 'visualization',
id: 'dd7caf20-9efd-11e7-acb3-3dab96693fab',
attributes: {
title: 'An existing visualization'
}
},
{
type: 'dashboard',
id: 'a01b2f57-fcfd-4864-b735-09e28f0d815e',
attributes: {
title: 'A great new dashboard'
}
},
];

describe('_bulk_create', () => {
describe('with kibana index', () => {
before(() => esArchiver.load('saved_objects/basic'));
after(() => esArchiver.unload('saved_objects/basic'));

it('should return 200 with individual responses', async () => (
await supertest
.post(`/api/saved_objects/_bulk_create`)
.send(BULK_REQUESTS)
.expect(200)
.then(resp => {
expect(resp.body).to.eql({
saved_objects: [
{
type: 'visualization',
id: 'dd7caf20-9efd-11e7-acb3-3dab96693fab',
error: {
'message': 'version conflict, document already exists',
'statusCode': 409
}
},
{
type: 'dashboard',
id: 'a01b2f57-fcfd-4864-b735-09e28f0d815e',
updated_at: resp.body.saved_objects[1].updated_at,
version: 1,
attributes: {
title: 'A great new dashboard'
}
},
]
});
})
));
});

describe('without kibana index', () => {
before(async () => (
// just in case the kibana server has recreated it
await es.indices.delete({
index: '.kibana',
ignore: [404],
})
));

it('should return 200 with individual responses', async () => (
await supertest
.post('/api/saved_objects/_bulk_create')
.send(BULK_REQUESTS)
.expect(200)
.then(resp => {
expect(resp.body).to.eql({
saved_objects: [
{
type: 'visualization',
id: 'dd7caf20-9efd-11e7-acb3-3dab96693fab',
updated_at: resp.body.saved_objects[0].updated_at,
version: 1,
attributes: {
title: 'An existing visualization'
}
},
{
type: 'dashboard',
id: 'a01b2f57-fcfd-4864-b735-09e28f0d815e',
updated_at: resp.body.saved_objects[1].updated_at,
version: 1,
attributes: {
title: 'A great new dashboard'
}
},
]
});
})
));
});
});
}
1 change: 1 addition & 0 deletions test/api_integration/apis/saved_objects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

export default function ({ loadTestFile }) {
describe('saved_objects', () => {
loadTestFile(require.resolve('./bulk_create'));
loadTestFile(require.resolve('./bulk_get'));
loadTestFile(require.resolve('./create'));
loadTestFile(require.resolve('./delete'));
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

// Forbid unused local variables as the rule was deprecated by ts-lint
"noUnusedLocals": true,

},
"include": [
"src/**/*"
Expand Down
Loading

0 comments on commit b90b30f

Please sign in to comment.