elastic / kibana

Your window into the Elastic Stack
https://www.elastic.co/products/kibana
Other
19.77k stars 8.17k forks source link

[KPT] Kibana Persistence Toolkit design #102177

Open pgayvallet opened 3 years ago

pgayvallet commented 3 years ago

This issue is a retranscription of the Kibana Persistence ToolKit (aka KPT) proposal that was initially presented during a company event.

The main problems KPT is trying to solve are:

Some background

Why does Saved Objects need to restrict access to Elasticsearch features

The consequences of having restricted Elasticsearch features

{
 aggs: {
   evil: {
     terms: {
       field: 'alert.id',
       min_doc_count: 0,
     }
   }
 }
}

Features that could be used on other Kibana app-specific indices

Impacts of the limitations

The Kibana Persistence Toolkit

The goal of KPT is to provide a low level persistence API, that would support RBAC, Spaces and possibly Audit Logging, that would be leveraged by the SO implementation, and could also be used directly with app-specific indices.

Some characteristics of the KPT layer:

Architecture changes

Saved Objects

Screenshot 2021-06-15 at 11 49 22

App-specific indices

Screenshot 2021-06-15 at 11 50 34

What KPT is not

Usage examples (non-final API)

Creating a scoped KPT client

// for any arbitrary app-specific index
const scopedClient = core.persistence
 .getClientFactory({
   namespaceField: 'namespace',
   typeField: 'type',
 })
 .forType('alert')
 .asScoped(request);

// shorter version for savedObjects
const scopedClient2 = core.savedObjects.getPersistenceClient('alert').asScoped(request);

const { body } = scopedClient.search({
 index: '.my-alert-index',
 body: {
   // security filter will be added under the hood
   query: { term: { 'alert.group.id': 42 } },
 },
});

const serializer = core.savedObjects.createSerializer();
const myAlerts = body.hits.hits.map(serializer.rawToSavedObject);

Creating a custom security filter

During the transition period, an API will be added to allow consumers to create ES filters to apply security and spaces restriction. The API could look like

Note: The example is simplified here, the real API needs to handle a little more than that, such as multi-space support

type SecurityFilterFactory = (options: {
 namespaceField: string;
 typeField: string;
}) => SecurityFilterBuilder;

interface SecurityFilterBuilder {
 create(options: { namespace: string; type: string }): estypes.QueryContainer;
}

const filterFactory = core.persistence.getFilterFactory({
 namespaceField: 'namespace',
 typeField: 'type',
});
const securityFilter = filterFactory.create({
 namespace: 'default',
 type: 'alert',
});

esClient.search({
 body: {
   query: { bool: { filter: securityFilter } },
 },
});
elasticmachine commented 3 years ago

Pinging @elastic/kibana-core (Team:Core)

pgayvallet commented 3 years ago

cc @elastic/kibana-security @kobelb

jportner commented 2 years ago

We had some discussion on Slack today about potential "soft deletion" of saved objects. This would allow us to implement a sort of Trash feature where an object gets permanently removed after a period of time (30 days?)

A quick search turned up one such existing ER: #89564 Perhaps with the KPT redesign it would be easier to implement such a feature. Thoughts?

pgayvallet commented 2 years ago

Perhaps with the KPT redesign it would be easier to implement such a feature. Thoughts?

Given that KPT is supposed to be a lower level wrapper than the SO APIs, I'd say the opposite to be honest 😅 . If I can see such soft deletion being a feature of a high-level API such as SOs, I personally don't think a low level wrapper such as KPT could and/or should hold such responsibilities.

@rudolf wdyt?

lukeelmers commented 2 years ago

I tend to agree with @pgayvallet here: While I'm on board with the idea of soft deletes, and it seems like it would be a useful feature to implement, it does feel like a higher-level concern than the KPT and would probably make more sense as (yet another) addition to the existing SO APIs. But let's see what everyone else thinks 🙂

rudolf commented 2 years ago

I think the important thing is that nothing in KPT might prevent us from implementing soft-deletes.

Architecturally I agree it would be better in a layer higher than KPT. If KPT exposed scripted updates and scripted update by query then we would probably have the required building blocks to create fairly performant soft-deletes.