[Cloud Security] [Agentless] [Bug] Fix error log [object Object] - createAgentlessAgent function in Fleet is not showing the error in the logs. (#190635)

This commit is contained in:
seanrathier 2024-09-03 13:29:50 -04:00 committed by GitHub
parent 738002fc89
commit bcb030e558
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 142 additions and 19 deletions

View file

@ -95,44 +95,62 @@ class AgentlessAgentService {
requestConfig.data.stack_version = appContextService.getKibanaVersion();
}
logger.debug(
`Creating agentless agent with request config ${JSON.stringify({
...requestConfig,
httpsAgent: {
...requestConfig.httpsAgent,
options: {
...requestConfig.httpsAgent.options,
cert: requestConfig.httpsAgent.options.cert ? 'REDACTED' : undefined,
key: requestConfig.httpsAgent.options.key ? 'REDACTED' : undefined,
ca: requestConfig.httpsAgent.options.ca ? 'REDACTED' : undefined,
},
const requestConfigDebug = JSON.stringify({
...requestConfig,
httpsAgent: {
...requestConfig.httpsAgent,
options: {
...requestConfig.httpsAgent.options,
cert: requestConfig.httpsAgent.options.cert ? 'REDACTED' : undefined,
key: requestConfig.httpsAgent.options.key ? 'REDACTED' : undefined,
ca: requestConfig.httpsAgent.options.ca ? 'REDACTED' : undefined,
},
})}`
);
},
});
logger.debug(`Creating agentless agent with request config ${requestConfigDebug}`);
const response = await axios<AgentlessApiResponse>(requestConfig).catch(
(error: Error | AxiosError) => {
if (!axios.isAxiosError(error)) {
logger.error(`Creating agentless failed with an error ${error}`);
logger.error(
`Creating agentless failed with an error ${error} ${JSON.stringify(
requestConfigDebug
)}`
);
throw new AgentlessAgentCreateError(error.message);
}
const errorLogCodeCause = `${error.code} ${this.convertCauseErrorsToString(error)}`;
if (error.response) {
// The request was made and the server responded with a status code and error data
logger.error(
`Creating agentless failed with a response status code that falls out of the range of 2xx: ${error.response.status} ${error.response.statusText} ${requestConfig.data}`
`Creating agentless failed because the Agentless API responding with a status code that falls out of the range of 2xx: ${JSON.stringify(
error.response.status
)}} ${JSON.stringify(error.response.data)}} ${JSON.stringify(requestConfigDebug)}`
);
throw new AgentlessAgentCreateError(
`the Agentless API could not create the agentless agent`
);
} else if (error.request) {
// The request was made but no response was received
logger.error(
`Creating agentless failed to receive a response from the Agentless API ${JSON.stringify(
error.cause
`Creating agentless agent failed while sending the request to the Agentless API: ${errorLogCodeCause} ${JSON.stringify(
requestConfigDebug
)}`
);
throw new AgentlessAgentCreateError(`no response received from the Agentless API`);
} else {
logger.error(`Creating agentless failed to create the request ${error.cause}`);
throw new AgentlessAgentCreateError('the request could not be created');
// Something happened in setting up the request that triggered an Error
logger.error(
`Creating agentless agent failed to be created ${errorLogCodeCause} ${JSON.stringify(
requestConfigDebug
)}`
);
throw new AgentlessAgentCreateError(
'the Agentless API could not create the agentless agent'
);
}
}
);
@ -141,6 +159,13 @@ class AgentlessAgentService {
return response;
}
private convertCauseErrorsToString = (error: AxiosError) => {
if (error.cause instanceof AggregateError) {
return error.cause.errors.map((e: Error) => e.message);
}
return error.cause;
};
private async getFleetUrlAndTokenForAgentlessAgent(
esClient: ElasticsearchClient,
policyId: string,

View file

@ -0,0 +1,98 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { CLOUD_CREDENTIALS_PACKAGE_VERSION } from '@kbn/cloud-security-posture-plugin/common/constants';
import expect from '@kbn/expect';
import type { FtrProviderContext } from '../ftr_provider_context';
// eslint-disable-next-line import/no-default-export
export default function ({ getPageObjects, getService }: FtrProviderContext) {
const pageObjects = getPageObjects([
'common',
'cspSecurity',
'security',
'header',
'cisAddIntegration',
]);
const CIS_AWS_OPTION_TEST_ID = 'cisAwsTestId';
const AWS_SINGLE_ACCOUNT_TEST_ID = 'awsSingleTestId';
describe('Agentless cloud', function () {
let cisIntegration: typeof pageObjects.cisAddIntegration;
let cisIntegrationAws: typeof pageObjects.cisAddIntegration.cisAws;
before(async () => {
cisIntegration = pageObjects.cisAddIntegration;
cisIntegrationAws = pageObjects.cisAddIntegration.cisAws; // Start the usage api mock server on port 8081
});
after(async () => {
await pageObjects.cspSecurity.logout();
});
it(`should create agentless-agent`, async () => {
const integrationPolicyName = `cloud_security_posture-${new Date().toISOString()}`;
await cisIntegration.navigateToAddIntegrationCspmWithVersionPage(
CLOUD_CREDENTIALS_PACKAGE_VERSION
);
await cisIntegration.clickOptionButton(CIS_AWS_OPTION_TEST_ID);
await cisIntegration.clickOptionButton(AWS_SINGLE_ACCOUNT_TEST_ID);
await cisIntegration.inputIntegrationName(integrationPolicyName);
await cisIntegration.selectSetupTechnology('agentless');
await cisIntegration.selectAwsCredentials('direct');
await pageObjects.header.waitUntilLoadingHasFinished();
await cisIntegration.clickSaveButton();
await pageObjects.header.waitUntilLoadingHasFinished();
expect(await cisIntegrationAws.showPostInstallCloudFormationModal()).to.be(false);
await cisIntegration.navigateToIntegrationCspList();
await pageObjects.header.waitUntilLoadingHasFinished();
expect(await cisIntegration.getFirstCspmIntegrationPageIntegration()).to.be(
integrationPolicyName
);
expect(await cisIntegration.getFirstCspmIntegrationPageAgent()).to.be(
`Agentless policy for ${integrationPolicyName}`
);
});
it(`should create default agent-based agent`, async () => {
const integrationPolicyName = `cloud_security_posture-${new Date().toISOString()}`;
await cisIntegration.navigateToAddIntegrationCspmWithVersionPage(
CLOUD_CREDENTIALS_PACKAGE_VERSION
);
await cisIntegration.clickOptionButton(CIS_AWS_OPTION_TEST_ID);
await cisIntegration.clickOptionButton(AWS_SINGLE_ACCOUNT_TEST_ID);
await cisIntegration.inputIntegrationName(integrationPolicyName);
await cisIntegration.clickSaveButton();
await pageObjects.header.waitUntilLoadingHasFinished();
expect(await cisIntegrationAws.showPostInstallCloudFormationModal()).to.be(true);
const agentPolicyName = await cisIntegration.getAgentBasedPolicyValue();
await cisIntegration.navigateToIntegrationCspList();
await pageObjects.header.waitUntilLoadingHasFinished();
expect(await cisIntegration.getFirstCspmIntegrationPageIntegration()).to.be(
integrationPolicyName
);
expect(await cisIntegration.getFirstCspmIntegrationPageAgent()).to.be(agentPolicyName);
});
});
}