-
Notifications
You must be signed in to change notification settings - Fork 8k
/
package_service.ts
211 lines (179 loc) · 5.82 KB
/
package_service.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* 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.
*/
/* eslint-disable max-classes-per-file */
import type {
KibanaRequest,
ElasticsearchClient,
SavedObjectsClientContract,
Logger,
} from '@kbn/core/server';
import type { PackageList } from '../../../common';
import type {
CategoryId,
EsAssetReference,
InstallablePackage,
Installation,
RegistryPackage,
ArchivePackage,
BundledPackage,
} from '../../types';
import { checkSuperuser } from '../security';
import { FleetUnauthorizedError } from '../../errors';
import { installTransforms, isTransform } from './elasticsearch/transform/install';
import type { FetchFindLatestPackageOptions } from './registry';
import { fetchFindLatestPackageOrThrow, getPackage } from './registry';
import { ensureInstalledPackage, getInstallation, getPackages } from './packages';
import { generatePackageInfoFromArchiveBuffer } from './archive';
export type InstalledAssetType = EsAssetReference;
export interface PackageService {
asScoped(request: KibanaRequest): PackageClient;
asInternalUser: PackageClient;
}
export interface PackageClient {
getInstallation(pkgName: string): Promise<Installation | undefined>;
ensureInstalledPackage(options: {
pkgName: string;
pkgVersion?: string;
spaceId?: string;
}): Promise<Installation | undefined>;
fetchFindLatestPackage(
packageName: string,
options?: FetchFindLatestPackageOptions
): Promise<RegistryPackage | BundledPackage>;
readBundledPackage(
bundledPackage: BundledPackage
): Promise<{ packageInfo: ArchivePackage; paths: string[] }>;
getPackage(
packageName: string,
packageVersion: string
): Promise<{ packageInfo: ArchivePackage; paths: string[] }>;
getPackages(params?: {
excludeInstallStatus?: false;
category?: CategoryId;
prerelease?: false;
}): Promise<PackageList>;
reinstallEsAssets(
packageInfo: InstallablePackage,
assetPaths: string[]
): Promise<InstalledAssetType[]>;
}
export class PackageServiceImpl implements PackageService {
constructor(
private readonly internalEsClient: ElasticsearchClient,
private readonly internalSoClient: SavedObjectsClientContract,
private readonly logger: Logger
) {}
public asScoped(request: KibanaRequest) {
const preflightCheck = () => {
if (!checkSuperuser(request)) {
throw new FleetUnauthorizedError(
`User does not have adequate permissions to access Fleet packages.`
);
}
};
return new PackageClientImpl(
this.internalEsClient,
this.internalSoClient,
this.logger,
preflightCheck
);
}
public get asInternalUser() {
return new PackageClientImpl(this.internalEsClient, this.internalSoClient, this.logger);
}
}
class PackageClientImpl implements PackageClient {
constructor(
private readonly internalEsClient: ElasticsearchClient,
private readonly internalSoClient: SavedObjectsClientContract,
private readonly logger: Logger,
private readonly preflightCheck?: () => void | Promise<void>
) {}
public async getInstallation(pkgName: string) {
await this.#runPreflight();
return getInstallation({
pkgName,
savedObjectsClient: this.internalSoClient,
});
}
public async ensureInstalledPackage(options: {
pkgName: string;
pkgVersion?: string;
spaceId?: string;
}): Promise<Installation | undefined> {
await this.#runPreflight();
return ensureInstalledPackage({
...options,
esClient: this.internalEsClient,
savedObjectsClient: this.internalSoClient,
});
}
public async fetchFindLatestPackage(
packageName: string,
options?: FetchFindLatestPackageOptions
): Promise<RegistryPackage | BundledPackage> {
await this.#runPreflight();
return fetchFindLatestPackageOrThrow(packageName, options);
}
public async readBundledPackage(bundledPackage: BundledPackage) {
await this.#runPreflight();
return generatePackageInfoFromArchiveBuffer(bundledPackage.buffer, 'application/zip');
}
public async getPackage(
packageName: string,
packageVersion: string,
options?: Parameters<typeof getPackage>['2']
) {
await this.#runPreflight();
return getPackage(packageName, packageVersion, options);
}
public async getPackages(params?: {
excludeInstallStatus?: false;
category?: CategoryId;
prerelease?: false;
}) {
const { excludeInstallStatus, category, prerelease } = params || {};
await this.#runPreflight();
return getPackages({
savedObjectsClient: this.internalSoClient,
excludeInstallStatus,
category,
prerelease,
});
}
public async reinstallEsAssets(
packageInfo: InstallablePackage,
assetPaths: string[]
): Promise<InstalledAssetType[]> {
await this.#runPreflight();
let installedAssets: InstalledAssetType[] = [];
const transformPaths = assetPaths.filter(isTransform);
if (transformPaths.length !== assetPaths.length) {
throw new Error('reinstallEsAssets is currently only implemented for transform assets');
}
if (transformPaths.length) {
const installedTransformAssets = await this.#reinstallTransforms(packageInfo, transformPaths);
installedAssets = [...installedAssets, ...installedTransformAssets];
}
return installedAssets;
}
async #reinstallTransforms(packageInfo: InstallablePackage, paths: string[]) {
const { installedTransforms } = await installTransforms(
packageInfo,
paths,
this.internalEsClient,
this.internalSoClient,
this.logger
);
return installedTransforms;
}
#runPreflight() {
if (this.preflightCheck) {
return this.preflightCheck();
}
}
}