Skip to content

Commit

Permalink
[8.x] Agentless api fix delete path (#195762) (#195836)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [Agentless api fix delete path
(#195762)](#195762)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Amir Ben
Nun","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-10T22:44:25Z","message":"Agentless
api fix delete path (#195762)\n\n## Summary\r\nAgentless API delete path
should have the `ess` or `serverless` mark\r\nbased on the
environment.\r\nThis PR build the request URL based on
that.","sha":"872d9da30e74f64dd33e25c6fed2d55e6aa4af47","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Fleet","v9.0.0","Team:Cloud
Security","backport:prev-major","v8.16.0"],"title":"Agentless api fix
delete
path","number":195762,"url":"https://github.com/elastic/kibana/pull/195762","mergeCommit":{"message":"Agentless
api fix delete path (#195762)\n\n## Summary\r\nAgentless API delete path
should have the `ess` or `serverless` mark\r\nbased on the
environment.\r\nThis PR build the request URL based on
that.","sha":"872d9da30e74f64dd33e25c6fed2d55e6aa4af47"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/195762","number":195762,"mergeCommit":{"message":"Agentless
api fix delete path (#195762)\n\n## Summary\r\nAgentless API delete path
should have the `ess` or `serverless` mark\r\nbased on the
environment.\r\nThis PR build the request URL based on
that.","sha":"872d9da30e74f64dd33e25c6fed2d55e6aa4af47"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Amir Ben Nun <[email protected]>
  • Loading branch information
kibanamachine and amirbenun authored Oct 11, 2024
1 parent e3f1f72 commit 3521b8c
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 3521b8c

Please sign in to comment.