Skip to content
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

[Ingest Manager] Shared Fleet agent policy action #76013

Merged
merged 16 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions x-pack/plugins/ingest_manager/common/types/models/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,57 @@ export type AgentStatus =
| 'unenrolling'
| 'degraded';

export type AgentActionType = 'CONFIG_CHANGE' | 'DATA_DUMP' | 'RESUME' | 'PAUSE' | 'UNENROLL';
export type AgentActionType = 'CONFIG_CHANGE' | 'UNENROLL';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some cleanup as currently we only support this two actions


export interface NewAgentAction {
type: AgentActionType;
data?: any;
sent_at?: string;
}

export interface AgentAction extends NewAgentAction {
export interface AgentAction {
type: AgentActionType;
data?: any;
sent_at?: string;
nchaulet marked this conversation as resolved.
Show resolved Hide resolved
id: string;
agent_id: string;
created_at: string;
ack_data?: any;
}

export interface AgentActionSOAttributes {
export interface AgentPolicyAction {
id: string;
type: AgentActionType;
data?: any;
policy_id: string;
policy_revision: number;
created_at: string;
ack_data?: any;
}

interface CommonAgentActionSOAttributes {
type: AgentActionType;
sent_at?: string;
timestamp?: string;
created_at: string;
agent_id: string;
data?: string;
ack_data?: string;
}

export type BaseAgentActionSOAttributes = CommonAgentActionSOAttributes & {
agent_id?: string;
policy_id?: string;
policy_revision?: number;
};

export type AgentActionSOAttributes = CommonAgentActionSOAttributes & {
agent_id: string;
};
export type AgentPolicyActionSOAttributes = CommonAgentActionSOAttributes & {
policy_id: string;
policy_revision: number;
};
nchaulet marked this conversation as resolved.
Show resolved Hide resolved

export interface NewAgentEvent {
type: 'STATE' | 'ERROR' | 'ACTION_RESULT' | 'ACTION';
subtype: // State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import {
Agent,
AgentAction,
NewAgentAction,
NewAgentEvent,
AgentEvent,
AgentStatus,
AgentType,
NewAgentAction,
} from '../models';

export interface GetAgentsRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { RequestHandler } from 'kibana/server';
import { TypeOf } from '@kbn/config-schema';
import { PostNewAgentActionRequestSchema } from '../../types/rest_spec';
import { ActionsService } from '../../services/agents';
import { NewAgentAction } from '../../../common/types/models';
import { PostNewAgentActionResponse } from '../../../common/types/rest_spec';

export const postNewAgentActionHandlerBuilder = function (
Expand All @@ -26,7 +25,7 @@ export const postNewAgentActionHandlerBuilder = function (

const agent = await actionsService.getAgent(soClient, request.params.agentId);

const newAgentAction = request.body.action as NewAgentAction;
nchaulet marked this conversation as resolved.
Show resolved Hide resolved
const newAgentAction = request.body.action;

const savedAgentAction = await actionsService.createAgentAction(soClient, {
created_at: new Date().toISOString(),
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/ingest_manager/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ const savedObjectTypes: { [key: string]: SavedObjectsType } = {
mappings: {
properties: {
agent_id: { type: 'keyword' },
policy_id: { type: 'keyword' },
policy_revision: { type: 'integer' },
type: { type: 'keyword' },
data: { type: 'binary' },
ack_data: { type: 'text' },
sent_at: { type: 'date' },
created_at: { type: 'date' },
},
Expand Down
32 changes: 31 additions & 1 deletion x-pack/plugins/ingest_manager/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
ListWithKuery,
} from '../types';
import { DeleteAgentPolicyResponse, storedPackagePoliciesToAgentInputs } from '../../common';
import { listAgents } from './agents';
import { createAgentPolicyAction, listAgents } from './agents';
import { packagePolicyService } from './package_policy';
import { outputService } from './output';
import { agentPolicyUpdateEventHandler } from './agent_policy_update';
Expand Down Expand Up @@ -67,6 +67,10 @@ class AgentPolicyService {
updated_by: user ? user.username : 'system',
});

if (options.bumpRevision) {
await this.triggerAgentPolicyUpdatedEvent(soClient, 'updated', id);
}

return (await this.get(soClient, id)) as AgentPolicy;
}

Expand Down Expand Up @@ -383,6 +387,32 @@ class AgentPolicyService {
};
}

public async createFleetPolicyChangeAction(
soClient: SavedObjectsClientContract,
agentPolicyId: string
) {
const policy = await agentPolicyService.getFullAgentPolicy(soClient, agentPolicyId);
if (!policy || !policy.revision) {
return;
}
const packages = policy.inputs.reduce<string[]>((acc, input) => {
const packageName = input.meta?.package?.name;
if (packageName && acc.indexOf(packageName) < 0) {
return [packageName, ...acc];
nchaulet marked this conversation as resolved.
Show resolved Hide resolved
}
return acc;
}, []);

await createAgentPolicyAction(soClient, {
type: 'CONFIG_CHANGE',
data: { config: policy } as any,
ack_data: { packages },
created_at: new Date().toISOString(),
policy_id: policy.id,
policy_revision: policy.revision,
});
}

public async getFullAgentPolicy(
soClient: SavedObjectsClientContract,
id: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@ import { SavedObjectsClientContract } from 'src/core/server';
import { generateEnrollmentAPIKey, deleteEnrollmentApiKeyForAgentPolicyId } from './api_keys';
import { unenrollForAgentPolicyId } from './agents';
import { outputService } from './output';
import { agentPolicyService } from './agent_policy';

export async function agentPolicyUpdateEventHandler(
soClient: SavedObjectsClientContract,
action: string,
agentPolicyId: string
) {
const adminUser = await outputService.getAdminUser(soClient);
// If no admin user fleet is not enabled just skip this hook
if (!adminUser) {
const outputId = await outputService.getDefaultOutputId(soClient);
// If no admin user and no default output fleet is not enabled just skip this hook
if (!adminUser || !outputId) {
return;
}

if (action === 'created') {
await generateEnrollmentAPIKey(soClient, {
agentPolicyId,
});
await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicyId);
}

if (action === 'updated') {
await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicyId);
}

if (action === 'deleted') {
Expand Down
Loading