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] expose method to get agent and list agents to other plugins #70087

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions x-pack/plugins/ingest_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ import {
AgentService,
datasourceService,
} from './services';
import { getAgentStatusById, authenticateAgentWithAccessToken } from './services/agents';
import {
getAgentStatusById,
authenticateAgentWithAccessToken,
listAgents,
getAgent,
} from './services/agents';
import { CloudSetup } from '../../cloud/server';
import { agentCheckinState } from './services/agents/checkin/state';

Expand Down Expand Up @@ -236,7 +241,7 @@ export class IngestManagerPlugin
plugins: {
encryptedSavedObjects: EncryptedSavedObjectsPluginStart;
}
) {
): Promise<IngestManagerStartContract> {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

await appContextService.start({
encryptedSavedObjectsStart: plugins.encryptedSavedObjects,
encryptedSavedObjectsSetup: this.encryptedSavedObjectsSetup,
Expand All @@ -255,6 +260,8 @@ export class IngestManagerPlugin
return {
esIndexPatternService: new ESIndexPatternSavedObjectService(),
agentService: {
getAgent,
listAgents,
getAgentStatusById,
authenticateAgentWithAccessToken,
},
Expand Down
34 changes: 30 additions & 4 deletions x-pack/plugins/ingest_manager/server/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsClientContract } from 'kibana/server';
import { AgentStatus } from '../types';
import { SavedObjectsClientContract, KibanaRequest } from 'kibana/server';
import { AgentStatus, Agent } from '../types';
import * as settingsService from './settings';
export { ESIndexPatternSavedObjectService } from './es_index_pattern';

Expand All @@ -24,12 +24,38 @@ export interface ESIndexPatternService {
* A service that provides exported functions that return information about an Agent
*/
export interface AgentService {
/**
* Get an Agent by id
*/
getAgent(soClient: SavedObjectsClientContract, agentId: string): Promise<Agent>;
Copy link
Contributor

Choose a reason for hiding this comment

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

When we use this method, we will have to look at the active property of the agent to know if it is unenrolled?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it's correct

/**
* Authenticate an agent with access toekn
*/
authenticateAgentWithAccessToken(
soClient: SavedObjectsClientContract,
request: KibanaRequest
): Promise<Agent>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this method and why do we pass the whole request? Can we pass what is needed incase the request is not compatible from Security Solution to Ingest Manager.

Copy link
Member Author

Choose a reason for hiding this comment

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

This method is used for the endpoint artifact API, to authenticate user, the method is already used it was just not typed correctly

/**
* Return the status by the Agent's id
* @param soClient
* @param agentId
*/
getAgentStatusById(soClient: SavedObjectsClientContract, agentId: string): Promise<AgentStatus>;
/**
* List agents
*/
listAgents(
soClient: SavedObjectsClientContract,
options: {
page: number;
perPage: number;
kuery?: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

@nchaulet thanks for making this change. So we must page through the data? What is the relationship between showInactive and kuery? And do you have an example of the kuery to get a the unenrolled agent that have or had an Endpoint Integration?

Copy link
Member Author

Choose a reason for hiding this comment

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

the showInactive allow to show inactive agent (unenrolled), kuery allow to filter on other fields

For the kuery to get unenrolled agent that have an endpoint integration it will be something like this (if this get merged #69731)

listAgents(soClient, {
  showInactive: true,
  kuery: `fleet-agents.package:endpoint`
});

if this is not merged you will have to provide the config id

listAgents(soClient, {
  showInactive: true,
  kuery: `fleet-agents.config_id:config_with_endpoint_1 or fleet-agents.config_id:config_with_endpoint_1`
});

Also I am working on adding an unrolled_at field so you can filter by date after.

Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer the first one. I can start coding against the API once you merge.

showInactive: boolean;
}
): Promise<{
agents: Agent[];
total: number;
page: number;
perPage: number;
}>;
}

// Saved object services
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/security_solution/server/endpoint/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export const createMockEndpointAppContextServiceStartContract = (): jest.Mocked<
export const createMockAgentService = (): jest.Mocked<AgentService> => {
return {
getAgentStatusById: jest.fn(),
authenticateAgentWithAccessToken: jest.fn(),
getAgent: jest.fn(),
listAgents: jest.fn(),
};
};

Expand Down