Skip to content

Commit

Permalink
[Security Solution][Endpoint] Ensure that DS indices for response act…
Browse files Browse the repository at this point in the history
…ions are created prior to sending action to Endpoint (#196953)

## Summary

PR adds changes to Security Solution so that DOT indices (restricted in
Serverless) are created in Kibana prior to Elastic Defend (Endpoint)
attempting to stream documents to these indices. The indices that are
now created in kibana are:

- `.logs-endpoint.diagnostic.collection-<namespace_from_policy>`
- `.logs-endpoint.action.responses-<namespace_from_policy>`
- `.logs-endpoint.heartbeat-<namespace_from_policy>` _(⚠️ created only
in serverless only)_


### Fleet changes:

- Added support for the following two server-side extension points:
- `packagePolicyPostUpdate` : callbacks invoked after an integration
policy has been updated successfully
- `agentPolicyPostUpdate` : callbacks invoked after an agent policy has
been updated successfully


### Security Solution:

- Logic was added to the following Fleet server-side extension points
that checks if the necessary indices exist and if not, it creates them:
    - After creating an Elastic Defend integration policy
    - After updating an Elastic Defend integration policy
- After updating a Fleet Agent Policy that includes Elastic Defend
integration policy
  • Loading branch information
paul-tavares authored Oct 25, 2024
1 parent a1d755a commit ae9c0d3
Show file tree
Hide file tree
Showing 19 changed files with 1,028 additions and 136 deletions.
16 changes: 8 additions & 8 deletions x-pack/plugins/fleet/server/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export const createPackagePolicyServiceMock = (): jest.Mocked<PackagePolicyClien
inspect: jest.fn(),
delete: jest.fn(),
get: jest.fn(),
getByIDs: jest.fn(),
getByIDs: jest.fn().mockResolvedValue(Promise.resolve([])),
list: jest.fn(),
listIds: jest.fn(),
update: jest.fn(),
Expand Down Expand Up @@ -224,13 +224,13 @@ export const createPackagePolicyServiceMock = (): jest.Mocked<PackagePolicyClien
*/
export const createMockAgentPolicyService = (): jest.Mocked<AgentPolicyServiceInterface> => {
return {
get: jest.fn(),
list: jest.fn(),
getFullAgentPolicy: jest.fn(),
getByIds: jest.fn(),
turnOffAgentTamperProtections: jest.fn(),
fetchAllAgentPolicies: jest.fn(),
fetchAllAgentPolicyIds: jest.fn(),
get: jest.fn().mockReturnValue(Promise.resolve()),
list: jest.fn().mockReturnValue(Promise.resolve()),
getFullAgentPolicy: jest.fn().mockReturnValue(Promise.resolve()),
getByIds: jest.fn().mockReturnValue(Promise.resolve()),
turnOffAgentTamperProtections: jest.fn().mockReturnValue(Promise.resolve()),
fetchAllAgentPolicies: jest.fn().mockReturnValue(Promise.resolve()),
fetchAllAgentPolicyIds: jest.fn().mockReturnValue(Promise.resolve()),
};
};

Expand Down
32 changes: 32 additions & 0 deletions x-pack/plugins/fleet/server/services/agent_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,38 @@ describe('Agent policy', () => {
})
).resolves.not.toThrow();
});

it('should run external "agentPolicyPostUpdate" callbacks when update is successful', async () => {
const soClient = getAgentPolicyCreateMock();
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;

const postUpdateCallback = jest.fn(async (policy) => policy);
mockedAppContextService.getExternalCallbacks.mockImplementation((type) => {
if (type === 'agentPolicyPostUpdate') {
return new Set([postUpdateCallback]);
}
});

soClient.get.mockResolvedValue({
attributes: {},
id: 'test-id',
type: 'mocked',
references: [],
});

await expect(
agentPolicyService.update(soClient, esClient, 'test-id', {
name: 'test',
namespace: 'default',
})
).resolves.not.toThrow();

expect(mockedAppContextService.getExternalCallbacks).toHaveBeenCalledWith(
'agentPolicyPostUpdate'
);

expect(postUpdateCallback).toHaveBeenCalled();
});
});

describe('deployPolicy', () => {
Expand Down
16 changes: 14 additions & 2 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import type {
PostAgentPolicyUpdateCallback,
PreconfiguredAgentPolicy,
OutputsForAgentPolicy,
PostAgentPolicyPostUpdateCallback,
} from '../types';
import {
AGENT_POLICY_INDEX,
Expand Down Expand Up @@ -309,8 +310,8 @@ class AgentPolicyService {

public async runExternalCallbacks(
externalCallbackType: ExternalCallback[0],
agentPolicy: NewAgentPolicy | Partial<AgentPolicy>
): Promise<NewAgentPolicy | Partial<AgentPolicy>> {
agentPolicy: NewAgentPolicy | Partial<AgentPolicy> | AgentPolicy
): Promise<NewAgentPolicy | Partial<AgentPolicy> | AgentPolicy> {
const logger = appContextService.getLogger();
logger.debug(`Running external callbacks for ${externalCallbackType}`);
try {
Expand All @@ -333,6 +334,12 @@ class AgentPolicyService {
);
updatedNewAgentPolicy = result;
}
if (externalCallbackType === 'agentPolicyPostUpdate') {
result = await (callback as PostAgentPolicyPostUpdateCallback)(
newAgentPolicy as AgentPolicy
);
updatedNewAgentPolicy = result;
}
}
newAgentPolicy = updatedNewAgentPolicy;
}
Expand Down Expand Up @@ -741,6 +748,11 @@ class AgentPolicyService {
bumpRevision: true,
removeProtection: false,
skipValidation: options?.skipValidation ?? false,
}).then((updatedAgentPolicy) => {
return this.runExternalCallbacks(
'agentPolicyPostUpdate',
updatedAgentPolicy
) as unknown as AgentPolicy;
});
}

Expand Down
35 changes: 35 additions & 0 deletions x-pack/plugins/fleet/server/services/package_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,41 @@ describe('Package policy service', () => {
});
});

it('should run "packagePolicyPostUpdate" external callbacks', async () => {
const soClient = savedObjectsClientMock.create();
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
const mockPackagePolicy = createPackagePolicyMock();
const attributes = {
...mockPackagePolicy,
inputs: [],
};

jest.spyOn(appContextService, 'getExternalCallbacks');

soClient.get.mockResolvedValue({
id: 'test-package-policy',
type: LEGACY_PACKAGE_POLICY_SAVED_OBJECT_TYPE,
references: [],
attributes,
});

soClient.update.mockResolvedValue({
id: 'test-package-policy',
type: LEGACY_PACKAGE_POLICY_SAVED_OBJECT_TYPE,
references: [],
attributes,
});

await packagePolicyService.update(soClient, esClient, 'test-package-policy', {
...mockPackagePolicy,
inputs: [],
});

expect(appContextService.getExternalCallbacks).toHaveBeenCalledWith(
'packagePolicyPostUpdate'
);
});

describe('remove protections', () => {
beforeEach(() => {
mockAgentPolicyService.bumpRevision.mockReset();
Expand Down
90 changes: 45 additions & 45 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import type {
DryRunPackagePolicy,
PostPackagePolicyCreateCallback,
PostPackagePolicyPostCreateCallback,
PutPackagePolicyPostUpdateCallback,
} from '../types';
import type { ExternalCallback } from '..';

Expand Down Expand Up @@ -127,6 +128,8 @@ import type {
PackagePolicyClient,
PackagePolicyClientFetchAllItemsOptions,
PackagePolicyService,
RunExternalCallbacksPackagePolicyArgument,
RunExternalCallbacksPackagePolicyResponse,
} from './package_policy_service';
import { installAssetsForInputPackagePolicy } from './epm/packages/install';
import { auditLoggingService } from './audit_logging';
Expand Down Expand Up @@ -1091,9 +1094,16 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
await Promise.all([...bumpPromises, assetRemovePromise, deleteSecretsPromise]);

sendUpdatePackagePolicyTelemetryEvent(soClient, [packagePolicyUpdate], [oldPackagePolicy]);

logger.debug(`Package policy ${id} update completed`);

return newPolicy;
// Run external post-update callbacks and return
return packagePolicyService.runExternalCallbacks(
'packagePolicyPostUpdate',
newPolicy,
soClient,
esClient
);
}

public async bulkUpdate(
Expand Down Expand Up @@ -1930,56 +1940,29 @@ class PackagePolicyClientImpl implements PackagePolicyClient {

public async runExternalCallbacks<A extends ExternalCallback[0]>(
externalCallbackType: A,
packagePolicy: A extends 'packagePolicyDelete'
? DeletePackagePoliciesResponse
: A extends 'packagePolicyPostDelete'
? PostDeletePackagePoliciesResponse
: A extends 'packagePolicyPostCreate'
? PackagePolicy
: A extends 'packagePolicyCreate'
? NewPackagePolicy
: never,
packagePolicy: RunExternalCallbacksPackagePolicyArgument<A>,
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
context?: RequestHandlerContext,
request?: KibanaRequest
): Promise<
A extends 'packagePolicyDelete'
? void
: A extends 'packagePolicyPostDelete'
? void
: A extends 'packagePolicyPostCreate'
? PackagePolicy
: A extends 'packagePolicyCreate'
? NewPackagePolicy
: never
>;
public async runExternalCallbacks(
externalCallbackType: ExternalCallback[0],
packagePolicy:
| PackagePolicy
| NewPackagePolicy
| PostDeletePackagePoliciesResponse
| DeletePackagePoliciesResponse,
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
context?: RequestHandlerContext,
request?: KibanaRequest
): Promise<PackagePolicy | NewPackagePolicy | void> {
): Promise<RunExternalCallbacksPackagePolicyResponse<A>> {
const logger = appContextService.getLogger();
const numberOfCallbacks = appContextService.getExternalCallbacks(externalCallbackType)?.size;
let runResult: any;

logger.debug(`Running ${numberOfCallbacks} external callbacks for ${externalCallbackType}`);

try {
if (externalCallbackType === 'packagePolicyPostDelete') {
return await this.runPostDeleteExternalCallbacks(
runResult = await this.runPostDeleteExternalCallbacks(
packagePolicy as PostDeletePackagePoliciesResponse,
soClient,
esClient,
context,
request
);
} else if (externalCallbackType === 'packagePolicyDelete') {
return await this.runDeleteExternalCallbacks(
runResult = await this.runDeleteExternalCallbacks(
packagePolicy as DeletePackagePoliciesResponse,
soClient,
esClient
Expand All @@ -1988,21 +1971,33 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
if (!Array.isArray(packagePolicy)) {
let newData = packagePolicy;
const externalCallbacks = appContextService.getExternalCallbacks(externalCallbackType);

if (externalCallbacks && externalCallbacks.size > 0) {
let updatedNewData = newData;
let updatedNewData: any = newData;

for (const callback of externalCallbacks) {
let result;
let thisCallbackResponse;

if (externalCallbackType === 'packagePolicyPostCreate') {
result = await (callback as PostPackagePolicyPostCreateCallback)(
thisCallbackResponse = await (callback as PostPackagePolicyPostCreateCallback)(
updatedNewData as PackagePolicy,
soClient,
esClient,
context,
request
);
updatedNewData = PackagePolicySchema.validate(result) as NewPackagePolicy;
updatedNewData = PackagePolicySchema.validate(thisCallbackResponse);
} else if (externalCallbackType === 'packagePolicyPostUpdate') {
thisCallbackResponse = await (callback as PutPackagePolicyPostUpdateCallback)(
updatedNewData as PackagePolicy,
soClient,
esClient,
context,
request
);
updatedNewData = PackagePolicySchema.validate(thisCallbackResponse);
} else {
result = await (callback as PostPackagePolicyCreateCallback)(
thisCallbackResponse = await (callback as PostPackagePolicyCreateCallback)(
updatedNewData as NewPackagePolicy,
soClient,
esClient,
Expand All @@ -2012,10 +2007,10 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
}

if (externalCallbackType === 'packagePolicyCreate') {
updatedNewData = NewPackagePolicySchema.validate(result) as NewPackagePolicy;
updatedNewData = NewPackagePolicySchema.validate(thisCallbackResponse);
} else if (externalCallbackType === 'packagePolicyUpdate') {
const omitted = {
...omit(result, [
...omit(thisCallbackResponse, [
'id',
'spaceIds',
'version',
Expand All @@ -2026,23 +2021,28 @@ class PackagePolicyClientImpl implements PackagePolicyClient {
'created_by',
'elasticsearch',
]),
inputs: result.inputs.map((input) => omit(input, ['compiled_input'])),
inputs: thisCallbackResponse.inputs.map((input) =>
omit(input, ['compiled_input'])
),
};

updatedNewData = UpdatePackagePolicySchema.validate(omitted) as PackagePolicy;
updatedNewData = UpdatePackagePolicySchema.validate(omitted);
}
}

newData = updatedNewData;
}
return newData;

runResult = newData;
}
}
} catch (error) {
logger.error(`Error running external callbacks for ${externalCallbackType}:`);
logger.error(error);
throw error;
}

return runResult as unknown as RunExternalCallbacksPackagePolicyResponse<A>;
}

public async runPostDeleteExternalCallbacks(
Expand Down
Loading

0 comments on commit ae9c0d3

Please sign in to comment.