-
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
[Fleet] POC - Add support for OTEL policies and integrations #193889
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da34b1a
[Fleet] POC - Add support for OTEL policies and integrations
criamico 4b06cad
Make route work
criamico d2248e9
Create endpoint to upload an OTEL template in yml format
criamico 173b31b
Merge otel policies to full agent policy
criamico 3d4a6c0
Nest otel configuration under inputs and retrieve template
criamico 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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export interface OtelPolicy { | ||
id: string; | ||
name: string; | ||
description?: string; | ||
namespace?: string; | ||
policy_ids: string[]; | ||
output_id?: string | null; | ||
integration?: { | ||
name: string; | ||
version?: string; | ||
}; | ||
vars?: Record<string, string | string[]>; | ||
pipelines?: string[]; | ||
revision: number; | ||
created_at: string; | ||
created_by: string; | ||
updated_at: string; | ||
updated_by: string; | ||
} | ||
|
||
export interface OtelInputReceivers { | ||
receivers?: { | ||
[extendedName: string]: { | ||
name?: string; | ||
pipelines?: string[]; | ||
parameters?: Record<string, string | string[]>; | ||
}; | ||
}; | ||
} | ||
|
||
export interface OtelInputExtensions { | ||
extensions?: { | ||
config_integrations?: { integrations?: string }; | ||
file_integrations?: { path?: string }; | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* 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 { TypeOf } from '@kbn/config-schema'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { load } from 'js-yaml'; | ||
|
||
import type { | ||
FleetRequestHandler, | ||
OtelPolicySOAttributes, | ||
OtelIntegrationSOAttributes, | ||
} from '../../types'; | ||
import type { | ||
CreateOtelPolicyRequestSchema, | ||
InstallOtelIntegrationRequestSchema, | ||
} from '../../types/rest_spec/otel'; | ||
import { defaultFleetErrorHandler } from '../../errors'; | ||
import { | ||
OTEL_POLICY_SAVED_OBJECT_TYPE, | ||
OTEL_INTEGRATION_SAVED_OBJECT_TYPE, | ||
} from '../../../common/constants'; | ||
import { agentPolicyService, appContextService } from '../../services'; | ||
|
||
export const createOtelPolicyHandler: FleetRequestHandler< | ||
undefined, | ||
undefined, | ||
TypeOf<typeof CreateOtelPolicyRequestSchema.body> | ||
> = async (context, request, response) => { | ||
const fleetContext = await context.fleet; | ||
const coreContext = await context.core; | ||
const logger = appContextService.getLogger(); | ||
const user = appContextService.getSecurityCore().authc.getCurrentUser(request) || undefined; | ||
const soClient = fleetContext.internalSoClient; | ||
const esClient = coreContext.elasticsearch.client.asInternalUser; | ||
|
||
const { id, ...newPolicy } = request.body; | ||
const policyId = id || uuidv4(); | ||
|
||
try { | ||
const isoDate = new Date().toISOString(); | ||
const newSo = await soClient.create<OtelPolicySOAttributes>( | ||
OTEL_POLICY_SAVED_OBJECT_TYPE, | ||
{ | ||
...newPolicy, | ||
revision: 1, | ||
created_at: isoDate, | ||
created_by: user?.username ?? 'system', | ||
updated_at: isoDate, | ||
updated_by: user?.username ?? 'system', | ||
}, | ||
{ id: policyId } | ||
); | ||
|
||
// bump agent policy revision | ||
for (const newPolicyId of newPolicy.policy_ids) { | ||
await agentPolicyService.bumpRevision(soClient, esClient, newPolicyId, { | ||
user, | ||
}); | ||
} | ||
|
||
const createdPolicy = { id: newSo.id, version: newSo.version, ...newSo.attributes }; | ||
logger.debug(`Created new otel policy with id ${newSo.id} and version ${newSo.version}`); | ||
|
||
return response.ok({ | ||
body: { | ||
item: createdPolicy, | ||
}, | ||
}); | ||
} catch (error) { | ||
return defaultFleetErrorHandler({ error, response }); | ||
} | ||
}; | ||
|
||
export const createOtelIntegrationPolicyHandler: FleetRequestHandler< | ||
TypeOf<typeof InstallOtelIntegrationRequestSchema.params>, | ||
undefined, | ||
TypeOf<typeof InstallOtelIntegrationRequestSchema.body> | ||
> = async (context, request, response) => { | ||
const fleetContext = await context.fleet; | ||
const soClient = fleetContext.internalSoClient; | ||
const user = appContextService.getSecurityCore().authc.getCurrentUser(request) || undefined; | ||
const logger = appContextService.getLogger(); | ||
|
||
const { name } = request.params; | ||
const jsonConfig = request?.body ? load(request.body) : {}; | ||
|
||
try { | ||
const isoDate = new Date().toISOString(); | ||
const template = { | ||
name, | ||
config: jsonConfig, | ||
}; | ||
const newSo = await soClient.create<OtelIntegrationSOAttributes>( | ||
OTEL_INTEGRATION_SAVED_OBJECT_TYPE, | ||
{ | ||
...template, | ||
created_at: isoDate, | ||
created_by: user?.username ?? 'system', | ||
updated_at: isoDate, | ||
updated_by: user?.username ?? 'system', | ||
} | ||
); | ||
logger.debug( | ||
`Created new otel ${name} integration with id ${newSo.id} and version ${newSo.version}` | ||
); | ||
return response.ok({ body: { item: template } }); | ||
} catch (error) { | ||
return defaultFleetErrorHandler({ error, response }); | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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 { FleetAuthzRouter } from '../../services/security'; | ||
import { | ||
API_VERSIONS, | ||
OTEL_POLICIES_ROUTES, | ||
OTEL_INTEGRATIONS_ROUTES, | ||
} from '../../../common/constants'; | ||
import { | ||
CreateOtelPolicyRequestSchema, | ||
InstallOtelIntegrationRequestSchema, | ||
} from '../../types/rest_spec/otel'; | ||
|
||
const MAX_FILE_SIZE_BYTES = 104857600; // 100MB | ||
|
||
import { createOtelPolicyHandler, createOtelIntegrationPolicyHandler } from './handlers'; | ||
|
||
export const registerRoutes = (router: FleetAuthzRouter) => { | ||
// Create policy | ||
router.versioned | ||
.post({ | ||
path: OTEL_POLICIES_ROUTES.CREATE_PATTERN, | ||
fleetAuthz: { | ||
integrations: { writeIntegrationPolicies: true }, | ||
}, | ||
description: 'Create new otel policy', | ||
options: { | ||
tags: ['oas-tag:Fleet Otel policies'], | ||
}, | ||
}) | ||
.addVersion( | ||
{ | ||
version: API_VERSIONS.public.v1, | ||
validate: { | ||
request: CreateOtelPolicyRequestSchema, | ||
}, | ||
}, | ||
createOtelPolicyHandler | ||
); | ||
|
||
// create integration | ||
router.versioned | ||
.post({ | ||
path: OTEL_INTEGRATIONS_ROUTES.CREATE_PATTERN, | ||
fleetAuthz: { | ||
integrations: { writeIntegrationPolicies: true }, | ||
}, | ||
description: 'Create new otel integration', | ||
options: { | ||
tags: ['oas-tag:Fleet Otel integrations'], | ||
body: { | ||
accepts: ['application/x-yaml', 'text/x-yaml'], | ||
parse: false, | ||
maxBytes: MAX_FILE_SIZE_BYTES, | ||
}, | ||
}, | ||
}) | ||
.addVersion( | ||
{ | ||
version: API_VERSIONS.public.v1, | ||
validate: { | ||
request: InstallOtelIntegrationRequestSchema, | ||
}, | ||
}, | ||
createOtelIntegrationPolicyHandler | ||
); | ||
}; |
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
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.
Interesting, does
flattened
work for complex data types? Or we do some additional processing for these when reading the saved object?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.
Yes it works well for complex data types and it doesn't need additional processing. We're already using it for existing mappings.