-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathpackage_policies_to_agent_permissions.ts
168 lines (138 loc) · 5.29 KB
/
package_policies_to_agent_permissions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { SavedObjectsClientContract } from '@kbn/core/server';
import type { FullAgentPolicyOutputPermissions, RegistryDataStreamPrivileges } from '../../common';
import { PACKAGE_POLICY_DEFAULT_INDEX_PRIVILEGES } from '../constants';
import type { PackagePolicy } from '../types';
import { getPackageInfo } from './epm/packages';
export const DEFAULT_CLUSTER_PERMISSIONS = ['monitor'];
export async function storedPackagePoliciesToAgentPermissions(
soClient: SavedObjectsClientContract,
packagePolicies: string[] | PackagePolicy[]
): Promise<FullAgentPolicyOutputPermissions | undefined> {
if (packagePolicies.length === 0) {
return;
}
// I'm not sure what permissions to return for this case, so let's return the defaults
if (typeof packagePolicies[0] === 'string') {
throw new Error(
'storedPackagePoliciesToAgentPermissions should be called with a PackagePolicy'
);
}
const permissionEntries = (packagePolicies as PackagePolicy[]).map<Promise<[string, any]>>(
async (packagePolicy) => {
if (!packagePolicy.package) {
throw new Error(`No package for package policy ${packagePolicy.name}`);
}
const pkg = await getPackageInfo({
savedObjectsClient: soClient,
pkgName: packagePolicy.package.name,
pkgVersion: packagePolicy.package.version,
});
if (!pkg.data_streams || pkg.data_streams.length === 0) {
return [packagePolicy.name, undefined];
}
let dataStreamsForPermissions: DataStreamMeta[];
switch (pkg.name) {
case 'endpoint':
// - Endpoint doesn't store the `data_stream` metadata in
// `packagePolicy.inputs`, so we will use _all_ data_streams from the
// package.
dataStreamsForPermissions = pkg.data_streams;
break;
case 'apm':
// - APM doesn't store the `data_stream` metadata in
// `packagePolicy.inputs`, so we will use _all_ data_streams from
// the package.
dataStreamsForPermissions = pkg.data_streams;
break;
case 'osquery_manager':
// - Osquery manager doesn't store the `data_stream` metadata in
// `packagePolicy.inputs`, so we will use _all_ data_streams from
// the package.
dataStreamsForPermissions = pkg.data_streams;
break;
default:
// - Normal packages store some of the `data_stream` metadata in
// `packagePolicy.inputs[].streams[].data_stream`
// - The rest of the metadata needs to be fetched from the
// `data_stream` object in the package. The link is
// `packagePolicy.inputs[].type == pkg.data_streams.streams[].input`
// - Some packages (custom logs) have a compiled dataset, stored in
// `input.streams.compiled_stream.data_stream.dataset`
dataStreamsForPermissions = packagePolicy.inputs
.filter((i) => i.enabled)
.flatMap((input) => {
if (!input.streams) {
return [];
}
const dataStreams_: DataStreamMeta[] = [];
input.streams
.filter((s) => s.enabled)
.forEach((stream) => {
if (!('data_stream' in stream)) {
return;
}
const ds: DataStreamMeta = {
type: stream.data_stream.type,
dataset:
stream.compiled_stream?.data_stream?.dataset ?? stream.data_stream.dataset,
};
if (stream.data_stream.elasticsearch) {
ds.elasticsearch = stream.data_stream.elasticsearch;
}
dataStreams_.push(ds);
});
return dataStreams_;
});
}
let clusterRoleDescriptor = {};
const cluster = packagePolicy?.elasticsearch?.privileges?.cluster ?? [];
if (cluster.length > 0) {
clusterRoleDescriptor = {
cluster,
};
}
return [
packagePolicy.name,
{
indices: dataStreamsForPermissions.map((ds) =>
getDataStreamPrivileges(ds, packagePolicy.namespace)
),
...clusterRoleDescriptor,
},
];
}
);
return Object.fromEntries(await Promise.all(permissionEntries));
}
interface DataStreamMeta {
type: string;
dataset: string;
dataset_is_prefix?: boolean;
hidden?: boolean;
elasticsearch?: {
privileges?: RegistryDataStreamPrivileges;
};
}
export function getDataStreamPrivileges(dataStream: DataStreamMeta, namespace: string = '*') {
let index = `${dataStream.type}-${dataStream.dataset}`;
if (dataStream.dataset_is_prefix) {
index = `${index}.*`;
}
if (dataStream.hidden) {
index = `.${index}`;
}
index += `-${namespace}`;
const privileges = dataStream?.elasticsearch?.privileges?.indices?.length
? dataStream.elasticsearch.privileges.indices
: PACKAGE_POLICY_DEFAULT_INDEX_PRIVILEGES;
return {
names: [index],
privileges,
};
}