Skip to content

Commit

Permalink
[Fleet] Expose policy change method from fleet plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Oct 17, 2019
1 parent 42616bf commit 349d35d
Show file tree
Hide file tree
Showing 15 changed files with 1,359 additions and 1,318 deletions.
16 changes: 16 additions & 0 deletions x-pack/legacy/plugins/fleet/common/types/domain_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ export interface ConfigurationBlock
export type Agent = t.TypeOf<typeof RuntimeAgent>;
export type AgentAction = t.TypeOf<typeof RuntimeAgentAction>;
export type AgentEvent = t.TypeOf<typeof RuntimeAgentEvent>;

export type PolicyUpdatedEvent =
| {
type: 'created';
policyId: string;
paylod: any;
}
| {
type: 'updated';
policyId: string;
paylod: any;
}
| {
type: 'deleted';
policyId: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface FrameworkAdapter {
getServerInfo(): {
protocol: string;
};
expose(name: string, thing: any): void;
}

export interface FrameworkRequest<KibanaServerRequestGenaric extends Partial<Request> = any> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ export class FrameworkAdapter implements FrameworkAdapterType {
public getServerInfo() {
return this.server.info;
}

public expose(name: string, thing: any) {
this.server.expose(name, thing);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { FrameworkAdapter } from './adapter_types';
export class MemorizeFrameworkAdapter implements FrameworkAdapter {
constructor(private readonly adapter?: FrameworkAdapter) {}

getSetting(settingPath: string): string {
public getSetting(settingPath: string): string {
return Slapshot.memorize('getSetting', () => {
if (!this.adapter) {
throw new Error('an adapter must be provided to run online');
Expand All @@ -19,9 +19,11 @@ export class MemorizeFrameworkAdapter implements FrameworkAdapter {
}) as string;
}

getServerInfo() {
public getServerInfo() {
return {
protocol: 'http://',
};
}

public expose(name: string, thing: any) {}
}
17 changes: 15 additions & 2 deletions x-pack/legacy/plugins/fleet/server/kibana.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@

import { compose } from './libs/compose/kibana';
import { initRestApi } from './routes/init_api';
import { FrameworkUser } from './adapters/framework/adapter_types';
import { PolicyUpdatedEvent } from '../common/types/domain_data';

export const initServerWithKibana = (hapiServer: any) => {
const libsRequestFactory = compose(hapiServer);
initRestApi(hapiServer, libsRequestFactory);
const libs = compose(hapiServer);
initRestApi(hapiServer, libs);
// expose methods
libs.framework.expose('policyUpdated', async function handlePolicyUpdate(
event: PolicyUpdatedEvent,
user: FrameworkUser = {
kind: 'internal',
}
) {
if (event.type === 'deleted') {
await libs.agents.unenrollForPolicy(user, event.policyId);
}
});
};
36 changes: 36 additions & 0 deletions x-pack/legacy/plugins/fleet/server/libs/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,42 @@ describe('Agent lib', () => {
});
});

describe('unenrollForPolicy', () => {
it('should set all the of agents for this policy as inactive', async () => {
const token = new TokenLib({} as TokensRepository, {} as FrameworkLib);
const agentsRepository = new InMemoryAgentsRepository();
const agentEventsRepository = new InMemoryAgentEventsRepository();
agentsRepository.agents['agent:1'] = ({
id: 'agent:1',
local_metadata: { key: 'local1' },
user_provided_metadata: { key: 'user1' },
actions: [],
events: [],
active: true,
policy_id: 'policy:1',
} as unknown) as Agent;
agentsRepository.agents['agent:2'] = ({
id: 'agent:2',
local_metadata: { key: 'local1' },
user_provided_metadata: { key: 'user1' },
actions: [],
events: [],
active: true,
policy_id: 'policy:1',
} as unknown) as Agent;
const policy = new PolicyLib({} as PoliciesRepository);
const agentLib = new AgentLib(agentsRepository, agentEventsRepository, token, policy);

await agentLib.unenrollForPolicy(getUser(), 'policy:1');

const refreshAgent1 = (await agentsRepository.getById(getUser(), 'agent:1')) as Agent;
const refreshAgent2 = (await agentsRepository.getById(getUser(), 'agent:2')) as Agent;

expect(refreshAgent1.active).toBeFalsy();
expect(refreshAgent2.active).toBeFalsy();
});
});

describe('addAction', () => {
it('should throw if the agent do not exists', async () => {
const token = new TokenLib({} as TokensRepository, {} as FrameworkLib);
Expand Down
16 changes: 16 additions & 0 deletions x-pack/legacy/plugins/fleet/server/libs/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ export class AgentLib {
return { ...agent, access_token: accessToken };
}

public async unenrollForPolicy(user: FrameworkUser, policyId: string) {
let hasMore = true;
let page = 1;
while (hasMore) {
const { agents } = await this.agentsRepository.listForPolicy(user, policyId, {
page: page++,
perPage: 100,
});

if (agents.length === 0) {
hasMore = false;
}
await this.unenroll(user, agents.map(a => a.id));
}
}

public async unenroll(
user: FrameworkUser,
ids: string[]
Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/fleet/server/libs/compose/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ export function compose(server: any): FleetServerLib {
policies,
artifacts,
install,
framework,
};
}
4 changes: 4 additions & 0 deletions x-pack/legacy/plugins/fleet/server/libs/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export class FrameworkLib {
return this.adapter.getSetting(`xpack.fleet.${setting}`);
}

public expose(key: string, method: any) {
this.adapter.expose(key, method);
}

public getServerConfig() {
return {
host: this.adapter.getSetting('server.host'),
Expand Down
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/fleet/server/libs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import { TokenLib } from './token';
import { PolicyLib } from './policy';
import { ArtifactLib } from './artifact';
import { InstallLib } from './install';
import { FrameworkLib } from './framework';

export interface FleetServerLib {
agents: AgentLib;
tokens: TokenLib;
policies: PolicyLib;
artifacts: ArtifactLib;
install: InstallLib;
framework: FrameworkLib;
}
Loading

0 comments on commit 349d35d

Please sign in to comment.