Skip to content

Commit

Permalink
Agentless api fix delete path (elastic#195762)
Browse files Browse the repository at this point in the history
## Summary
Agentless API delete path should have the `ess` or `serverless` mark
based on the environment.
This PR build the request URL based on that.
  • Loading branch information
amirbenun authored Oct 10, 2024
1 parent db54cb1 commit 872d9da
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,82 @@ describe('Agentless Agent service', () => {
);
});

it('should delete agentless agent for ESS', async () => {
const returnValue = {
id: 'mocked',
};

(axios as jest.MockedFunction<typeof axios>).mockResolvedValueOnce(returnValue);
jest.spyOn(appContextService, 'getConfig').mockReturnValue({
agentless: {
enabled: true,
api: {
url: 'http://api.agentless.com',
tls: {
certificate: '/path/to/cert',
key: '/path/to/key',
ca: '/path/to/ca',
},
},
},
} as any);
jest.spyOn(appContextService, 'getCloud').mockReturnValue({ isCloudEnabled: true } as any);

const deleteAgentlessAgentReturnValue = await agentlessAgentService.deleteAgentlessAgent(
'mocked-agentless-agent-policy-id'
);

expect(axios).toHaveBeenCalledTimes(1);
expect(deleteAgentlessAgentReturnValue).toEqual(returnValue);
expect(axios).toHaveBeenCalledWith(
expect.objectContaining({
headers: expect.anything(),
httpsAgent: expect.anything(),
method: 'DELETE',
url: 'http://api.agentless.com/api/v1/ess/deployments/mocked-agentless-agent-policy-id',
})
);
});

it('should delete agentless agent for serverless', async () => {
const returnValue = {
id: 'mocked',
};

(axios as jest.MockedFunction<typeof axios>).mockResolvedValueOnce(returnValue);
jest.spyOn(appContextService, 'getConfig').mockReturnValue({
agentless: {
enabled: true,
api: {
url: 'http://api.agentless.com',
tls: {
certificate: '/path/to/cert',
key: '/path/to/key',
ca: '/path/to/ca',
},
},
},
} as any);
jest
.spyOn(appContextService, 'getCloud')
.mockReturnValue({ isCloudEnabled: true, isServerlessEnabled: true } as any);

const deleteAgentlessAgentReturnValue = await agentlessAgentService.deleteAgentlessAgent(
'mocked-agentless-agent-policy-id'
);

expect(axios).toHaveBeenCalledTimes(1);
expect(deleteAgentlessAgentReturnValue).toEqual(returnValue);
expect(axios).toHaveBeenCalledWith(
expect.objectContaining({
headers: expect.anything(),
httpsAgent: expect.anything(),
method: 'DELETE',
url: 'http://api.agentless.com/api/v1/serverless/deployments/mocked-agentless-agent-policy-id',
})
);
});

it('should redact sensitive information from debug logs', async () => {
const returnValue = {
id: 'mocked',
Expand Down
11 changes: 5 additions & 6 deletions x-pack/plugins/fleet/server/services/agents/agentless_agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ import { appContextService } from '../app_context';
import { listEnrollmentApiKeys } from '../api_keys';
import { listFleetServerHosts } from '../fleet_server_host';
import type { AgentlessConfig } from '../utils/agentless';
import {
prependAgentlessApiBasePathToEndpoint,
isAgentlessApiEnabled,
getDeletionEndpointPath,
} from '../utils/agentless';
import { prependAgentlessApiBasePathToEndpoint, isAgentlessApiEnabled } from '../utils/agentless';

class AgentlessAgentService {
public async createAgentlessAgent(
Expand Down Expand Up @@ -188,7 +184,10 @@ class AgentlessAgentService {
const agentlessConfig = appContextService.getConfig()?.agentless;
const tlsConfig = this.createTlsConfig(agentlessConfig);
const requestConfig = {
url: getDeletionEndpointPath(agentlessConfig, `/deployments/${agentlessPolicyId}`),
url: prependAgentlessApiBasePathToEndpoint(
agentlessConfig,
`/deployments/${agentlessPolicyId}`
),
method: 'DELETE',
headers: {
'Content-type': 'application/json',
Expand Down
7 changes: 0 additions & 7 deletions x-pack/plugins/fleet/server/services/utils/agentless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,3 @@ export const prependAgentlessApiBasePathToEndpoint = (
: AGENTLESS_ESS_API_BASE_PATH;
return `${agentlessConfig.api.url}${endpointPrefix}${endpoint}`;
};

export const getDeletionEndpointPath = (
agentlessConfig: FleetConfigType['agentless'],
endpoint: AgentlessApiEndpoints
) => {
return `${agentlessConfig.api.url}${AGENTLESS_ESS_API_BASE_PATH}${endpoint}`;
};

0 comments on commit 872d9da

Please sign in to comment.