-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use kibana_system user for Fleet setup and package operations #112808
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fe70e60
Use kibana_system user for Fleet setup and package operations
joshdover 319c955
Fix type issues
joshdover 7330ebe
Fix more types
joshdover 83da7be
Merge remote-tracking branch 'upstream/master' into fix-111755
joshdover 52f6ee4
Merge remote-tracking branch 'upstream/master' into fix-111755
joshdover 68ac40e
Merge branch 'master' into fix-111755
kibanamachine ac0eb73
Move tests for transforms to endpoint-specific suite
joshdover ca174a0
Merge remote-tracking branch 'upstream/master' into fix-111755
joshdover 9355de6
Fix transform tests -- forgot to save
joshdover ce83148
Merge remote-tracking branch 'upstream/master' into fix-111755
joshdover 4e9a2b1
Add additional endpoint transform to test
joshdover a3f65ae
Merge branch 'master' into fix-111755
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import path from 'path'; | |
|
||
import type { TypeOf } from '@kbn/config-schema'; | ||
import mime from 'mime-types'; | ||
import type { RequestHandler, ResponseHeaders, KnownHeaders } from 'src/core/server'; | ||
import type { ResponseHeaders, KnownHeaders } from 'src/core/server'; | ||
|
||
import type { | ||
GetInfoResponse, | ||
|
@@ -34,6 +34,7 @@ import type { | |
DeletePackageRequestSchema, | ||
BulkUpgradePackagesFromRegistryRequestSchema, | ||
GetStatsRequestSchema, | ||
FleetRequestHandler, | ||
UpdatePackageRequestSchema, | ||
} from '../../types'; | ||
import { | ||
|
@@ -57,7 +58,7 @@ import { getAsset } from '../../services/epm/archive/storage'; | |
import { getPackageUsageStats } from '../../services/epm/packages/get'; | ||
import { updatePackage } from '../../services/epm/packages/update'; | ||
|
||
export const getCategoriesHandler: RequestHandler< | ||
export const getCategoriesHandler: FleetRequestHandler< | ||
undefined, | ||
TypeOf<typeof GetCategoriesRequestSchema.query> | ||
> = async (context, request, response) => { | ||
|
@@ -72,12 +73,12 @@ export const getCategoriesHandler: RequestHandler< | |
} | ||
}; | ||
|
||
export const getListHandler: RequestHandler< | ||
export const getListHandler: FleetRequestHandler< | ||
undefined, | ||
TypeOf<typeof GetPackagesRequestSchema.query> | ||
> = async (context, request, response) => { | ||
try { | ||
const savedObjectsClient = context.core.savedObjects.client; | ||
const savedObjectsClient = context.fleet.epm.internalSoClient; | ||
const res = await getPackages({ | ||
savedObjectsClient, | ||
...request.query, | ||
|
@@ -93,9 +94,9 @@ export const getListHandler: RequestHandler< | |
} | ||
}; | ||
|
||
export const getLimitedListHandler: RequestHandler = async (context, request, response) => { | ||
export const getLimitedListHandler: FleetRequestHandler = async (context, request, response) => { | ||
try { | ||
const savedObjectsClient = context.core.savedObjects.client; | ||
const savedObjectsClient = context.fleet.epm.internalSoClient; | ||
const res = await getLimitedPackages({ savedObjectsClient }); | ||
const body: GetLimitedPackagesResponse = { | ||
response: res, | ||
|
@@ -108,110 +109,105 @@ export const getLimitedListHandler: RequestHandler = async (context, request, re | |
} | ||
}; | ||
|
||
export const getFileHandler: RequestHandler<TypeOf<typeof GetFileRequestSchema.params>> = async ( | ||
context, | ||
request, | ||
response | ||
) => { | ||
try { | ||
const { pkgName, pkgVersion, filePath } = request.params; | ||
const savedObjectsClient = context.core.savedObjects.client; | ||
const installation = await getInstallation({ savedObjectsClient, pkgName }); | ||
const useLocalFile = pkgVersion === installation?.version; | ||
export const getFileHandler: FleetRequestHandler<TypeOf<typeof GetFileRequestSchema.params>> = | ||
async (context, request, response) => { | ||
try { | ||
const { pkgName, pkgVersion, filePath } = request.params; | ||
const savedObjectsClient = context.fleet.epm.internalSoClient; | ||
const installation = await getInstallation({ savedObjectsClient, pkgName }); | ||
const useLocalFile = pkgVersion === installation?.version; | ||
|
||
if (useLocalFile) { | ||
const assetPath = `${pkgName}-${pkgVersion}/${filePath}`; | ||
const fileBuffer = getArchiveEntry(assetPath); | ||
// only pull local installation if we don't have it cached | ||
const storedAsset = | ||
!fileBuffer && (await getAsset({ savedObjectsClient, path: assetPath })); | ||
|
||
if (useLocalFile) { | ||
const assetPath = `${pkgName}-${pkgVersion}/${filePath}`; | ||
const fileBuffer = getArchiveEntry(assetPath); | ||
// only pull local installation if we don't have it cached | ||
const storedAsset = !fileBuffer && (await getAsset({ savedObjectsClient, path: assetPath })); | ||
// error, if neither is available | ||
if (!fileBuffer && !storedAsset) { | ||
return response.custom({ | ||
body: `installed package file not found: ${filePath}`, | ||
statusCode: 404, | ||
}); | ||
} | ||
|
||
// if storedAsset is not available, fileBuffer *must* be | ||
// b/c we error if we don't have at least one, and storedAsset is the least likely | ||
const { buffer, contentType } = storedAsset | ||
? { | ||
contentType: storedAsset.media_type, | ||
buffer: storedAsset.data_utf8 | ||
? Buffer.from(storedAsset.data_utf8, 'utf8') | ||
: Buffer.from(storedAsset.data_base64, 'base64'), | ||
} | ||
: { | ||
contentType: mime.contentType(path.extname(assetPath)), | ||
buffer: fileBuffer, | ||
}; | ||
|
||
if (!contentType) { | ||
return response.custom({ | ||
body: `unknown content type for file: ${filePath}`, | ||
statusCode: 400, | ||
}); | ||
} | ||
|
||
// error, if neither is available | ||
if (!fileBuffer && !storedAsset) { | ||
return response.custom({ | ||
body: `installed package file not found: ${filePath}`, | ||
statusCode: 404, | ||
body: buffer, | ||
statusCode: 200, | ||
headers: { | ||
'cache-control': 'max-age=10, public', | ||
'content-type': contentType, | ||
}, | ||
}); | ||
} | ||
|
||
// if storedAsset is not available, fileBuffer *must* be | ||
// b/c we error if we don't have at least one, and storedAsset is the least likely | ||
const { buffer, contentType } = storedAsset | ||
? { | ||
contentType: storedAsset.media_type, | ||
buffer: storedAsset.data_utf8 | ||
? Buffer.from(storedAsset.data_utf8, 'utf8') | ||
: Buffer.from(storedAsset.data_base64, 'base64'), | ||
} else { | ||
const registryResponse = await getFile(pkgName, pkgVersion, filePath); | ||
const headersToProxy: KnownHeaders[] = ['content-type', 'cache-control']; | ||
const proxiedHeaders = headersToProxy.reduce((headers, knownHeader) => { | ||
const value = registryResponse.headers.get(knownHeader); | ||
if (value !== null) { | ||
headers[knownHeader] = value; | ||
} | ||
: { | ||
contentType: mime.contentType(path.extname(assetPath)), | ||
buffer: fileBuffer, | ||
}; | ||
return headers; | ||
}, {} as ResponseHeaders); | ||
|
||
if (!contentType) { | ||
return response.custom({ | ||
body: `unknown content type for file: ${filePath}`, | ||
statusCode: 400, | ||
body: registryResponse.body, | ||
statusCode: registryResponse.status, | ||
headers: proxiedHeaders, | ||
}); | ||
} | ||
|
||
return response.custom({ | ||
body: buffer, | ||
statusCode: 200, | ||
headers: { | ||
'cache-control': 'max-age=10, public', | ||
'content-type': contentType, | ||
}, | ||
}); | ||
} else { | ||
const registryResponse = await getFile(pkgName, pkgVersion, filePath); | ||
const headersToProxy: KnownHeaders[] = ['content-type', 'cache-control']; | ||
const proxiedHeaders = headersToProxy.reduce((headers, knownHeader) => { | ||
const value = registryResponse.headers.get(knownHeader); | ||
if (value !== null) { | ||
headers[knownHeader] = value; | ||
} | ||
return headers; | ||
}, {} as ResponseHeaders); | ||
|
||
return response.custom({ | ||
body: registryResponse.body, | ||
statusCode: registryResponse.status, | ||
headers: proxiedHeaders, | ||
}); | ||
} catch (error) { | ||
return defaultIngestErrorHandler({ error, response }); | ||
} | ||
} catch (error) { | ||
return defaultIngestErrorHandler({ error, response }); | ||
} | ||
}; | ||
}; | ||
|
||
export const getInfoHandler: RequestHandler<TypeOf<typeof GetInfoRequestSchema.params>> = async ( | ||
context, | ||
request, | ||
response | ||
) => { | ||
try { | ||
const { pkgkey } = request.params; | ||
const savedObjectsClient = context.core.savedObjects.client; | ||
// TODO: change epm API to /packageName/version so we don't need to do this | ||
const { pkgName, pkgVersion } = splitPkgKey(pkgkey); | ||
const res = await getPackageInfo({ savedObjectsClient, pkgName, pkgVersion }); | ||
const body: GetInfoResponse = { | ||
response: res, | ||
}; | ||
return response.ok({ body }); | ||
} catch (error) { | ||
return defaultIngestErrorHandler({ error, response }); | ||
} | ||
}; | ||
export const getInfoHandler: FleetRequestHandler<TypeOf<typeof GetInfoRequestSchema.params>> = | ||
async (context, request, response) => { | ||
try { | ||
const { pkgkey } = request.params; | ||
const savedObjectsClient = context.fleet.epm.internalSoClient; | ||
Comment on lines
+186
to
+190
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here as well |
||
// TODO: change epm API to /packageName/version so we don't need to do this | ||
const { pkgName, pkgVersion } = splitPkgKey(pkgkey); | ||
const res = await getPackageInfo({ savedObjectsClient, pkgName, pkgVersion }); | ||
const body: GetInfoResponse = { | ||
response: res, | ||
}; | ||
return response.ok({ body }); | ||
} catch (error) { | ||
return defaultIngestErrorHandler({ error, response }); | ||
} | ||
}; | ||
|
||
export const updatePackageHandler: RequestHandler< | ||
export const updatePackageHandler: FleetRequestHandler< | ||
TypeOf<typeof UpdatePackageRequestSchema.params>, | ||
unknown, | ||
TypeOf<typeof UpdatePackageRequestSchema.body> | ||
> = async (context, request, response) => { | ||
try { | ||
const { pkgkey } = request.params; | ||
const savedObjectsClient = context.core.savedObjects.client; | ||
const savedObjectsClient = context.fleet.epm.internalSoClient; | ||
|
||
const { pkgName } = splitPkgKey(pkgkey); | ||
|
||
|
@@ -226,30 +222,27 @@ export const updatePackageHandler: RequestHandler< | |
} | ||
}; | ||
|
||
export const getStatsHandler: RequestHandler<TypeOf<typeof GetStatsRequestSchema.params>> = async ( | ||
context, | ||
request, | ||
response | ||
) => { | ||
try { | ||
const { pkgName } = request.params; | ||
const savedObjectsClient = context.core.savedObjects.client; | ||
const body: GetStatsResponse = { | ||
response: await getPackageUsageStats({ savedObjectsClient, pkgName }), | ||
}; | ||
return response.ok({ body }); | ||
} catch (error) { | ||
return defaultIngestErrorHandler({ error, response }); | ||
} | ||
}; | ||
export const getStatsHandler: FleetRequestHandler<TypeOf<typeof GetStatsRequestSchema.params>> = | ||
async (context, request, response) => { | ||
try { | ||
const { pkgName } = request.params; | ||
const savedObjectsClient = context.fleet.epm.internalSoClient; | ||
const body: GetStatsResponse = { | ||
response: await getPackageUsageStats({ savedObjectsClient, pkgName }), | ||
}; | ||
return response.ok({ body }); | ||
} catch (error) { | ||
return defaultIngestErrorHandler({ error, response }); | ||
} | ||
}; | ||
|
||
export const installPackageFromRegistryHandler: RequestHandler< | ||
export const installPackageFromRegistryHandler: FleetRequestHandler< | ||
TypeOf<typeof InstallPackageFromRegistryRequestSchema.params>, | ||
undefined, | ||
TypeOf<typeof InstallPackageFromRegistryRequestSchema.body> | ||
> = async (context, request, response) => { | ||
const savedObjectsClient = context.core.savedObjects.client; | ||
const esClient = context.core.elasticsearch.client.asCurrentUser; | ||
const savedObjectsClient = context.fleet.epm.internalSoClient; | ||
const esClient = context.core.elasticsearch.client.asInternalUser; | ||
const { pkgkey } = request.params; | ||
|
||
const res = await installPackage({ | ||
|
@@ -284,13 +277,13 @@ const bulkInstallServiceResponseToHttpEntry = ( | |
} | ||
}; | ||
|
||
export const bulkInstallPackagesFromRegistryHandler: RequestHandler< | ||
export const bulkInstallPackagesFromRegistryHandler: FleetRequestHandler< | ||
undefined, | ||
undefined, | ||
TypeOf<typeof BulkUpgradePackagesFromRegistryRequestSchema.body> | ||
> = async (context, request, response) => { | ||
const savedObjectsClient = context.core.savedObjects.client; | ||
const esClient = context.core.elasticsearch.client.asCurrentUser; | ||
const savedObjectsClient = context.fleet.epm.internalSoClient; | ||
const esClient = context.core.elasticsearch.client.asInternalUser; | ||
const bulkInstalledResponses = await bulkInstallPackages({ | ||
savedObjectsClient, | ||
esClient, | ||
|
@@ -303,7 +296,7 @@ export const bulkInstallPackagesFromRegistryHandler: RequestHandler< | |
return response.ok({ body }); | ||
}; | ||
|
||
export const installPackageByUploadHandler: RequestHandler< | ||
export const installPackageByUploadHandler: FleetRequestHandler< | ||
undefined, | ||
undefined, | ||
TypeOf<typeof InstallPackageByUploadRequestSchema.body> | ||
|
@@ -314,8 +307,8 @@ export const installPackageByUploadHandler: RequestHandler< | |
body: { message: 'Requires Enterprise license' }, | ||
}); | ||
} | ||
const savedObjectsClient = context.core.savedObjects.client; | ||
const esClient = context.core.elasticsearch.client.asCurrentUser; | ||
const savedObjectsClient = context.fleet.epm.internalSoClient; | ||
const esClient = context.core.elasticsearch.client.asInternalUser; | ||
const contentType = request.headers['content-type'] as string; // from types it could also be string[] or undefined but this is checked later | ||
const archiveBuffer = Buffer.from(request.body); | ||
|
||
|
@@ -336,15 +329,15 @@ export const installPackageByUploadHandler: RequestHandler< | |
} | ||
}; | ||
|
||
export const deletePackageHandler: RequestHandler< | ||
export const deletePackageHandler: FleetRequestHandler< | ||
TypeOf<typeof DeletePackageRequestSchema.params>, | ||
undefined, | ||
TypeOf<typeof DeletePackageRequestSchema.body> | ||
> = async (context, request, response) => { | ||
try { | ||
const { pkgkey } = request.params; | ||
const savedObjectsClient = context.core.savedObjects.client; | ||
const esClient = context.core.elasticsearch.client.asCurrentUser; | ||
const savedObjectsClient = context.fleet.epm.internalSoClient; | ||
const esClient = context.core.elasticsearch.client.asInternalUser; | ||
const res = await removeInstallation({ | ||
savedObjectsClient, | ||
pkgkey, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only changes on this function are the change to
FleetRequestHandler
type and usinginternalSoClient
instead of the current user one. Everything else is the same, but got re-formatted by the linter.