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

[8.x] Agentless api fix delete path (#195762) #195836

Merged
merged 1 commit into from
Oct 11, 2024
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
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}`;
};
Loading