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

[Fleet] Invalidate api keys in agents default_api_key_history on force unenroll #135910

Merged
merged 2 commits into from Jul 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions x-pack/plugins/fleet/common/types/models/agent.ts
Expand Up @@ -85,6 +85,7 @@ interface AgentBase {
export interface Agent extends AgentBase {
id: string;
access_api_key?: string;
default_api_key_history?: FleetServerAgent['default_api_key_history'];
status?: AgentStatus;
packages: string[];
sort?: Array<number | string | null>;
Expand Down Expand Up @@ -206,6 +207,13 @@ export interface FleetServerAgent {
* A list of tags used for organizing/filtering agents
*/
tags?: string[];
/**
* Default API Key History
*/
default_api_key_history?: Array<{
id: string;
retired_at: string;
}>;
}
/**
* An Elastic Agent metadata
Expand Down
38 changes: 37 additions & 1 deletion x-pack/plugins/fleet/server/services/agents/unenroll.test.ts
Expand Up @@ -10,8 +10,13 @@ import type { SavedObject } from '@kbn/core/server';

import type { AgentPolicy } from '../../types';
import { HostedAgentPolicyRestrictionRelatedError } from '../../errors';
import { invalidateAPIKeys } from '../api_keys';

import { unenrollAgent, unenrollAgents } from './unenroll';
import { invalidateAPIKeysForAgents, unenrollAgent, unenrollAgents } from './unenroll';

jest.mock('../api_keys');

const mockedInvalidateAPIKeys = invalidateAPIKeys as jest.MockedFunction<typeof invalidateAPIKeys>;

const agentInHostedDoc = {
_id: 'agent-in-hosted-policy',
Expand Down Expand Up @@ -229,6 +234,37 @@ describe('unenrollAgents (plural)', () => {
});
});

describe('invalidateAPIKeysForAgents', () => {
beforeEach(() => {
mockedInvalidateAPIKeys.mockReset();
});
it('revoke all the agents API keys', async () => {
await invalidateAPIKeysForAgents([
{
id: 'agent1',
default_api_key_id: 'defaultApiKey1',
access_api_key_id: 'accessApiKey1',
default_api_key_history: [
{
id: 'defaultApiKeyHistory1',
},
{
id: 'defaultApiKeyHistory2',
},
],
} as any,
]);

expect(mockedInvalidateAPIKeys).toBeCalledTimes(1);
expect(mockedInvalidateAPIKeys).toBeCalledWith([
'accessApiKey1',
'defaultApiKey1',
'defaultApiKeyHistory1',
'defaultApiKeyHistory2',
]);
});
});

function createClientMock() {
const soClientMock = savedObjectsClientMock.create();

Expand Down
8 changes: 5 additions & 3 deletions x-pack/plugins/fleet/server/services/agents/unenroll.ts
Expand Up @@ -8,7 +8,7 @@
import type { ElasticsearchClient, SavedObjectsClientContract } from '@kbn/core/server';

import type { Agent, BulkActionResult } from '../../types';
import * as APIKeyService from '../api_keys';
import { invalidateAPIKeys } from '../api_keys';
import { HostedAgentPolicyRestrictionRelatedError } from '../../errors';

import { createAgentAction } from './actions';
Expand Down Expand Up @@ -163,12 +163,14 @@ export async function invalidateAPIKeysForAgents(agents: Agent[]) {
if (agent.default_api_key_id) {
keys.push(agent.default_api_key_id);
}

if (agent.default_api_key_history) {
agent.default_api_key_history.forEach((apiKey) => keys.push(apiKey.id));
}
return keys;
}, []);

if (apiKeys.length) {
await APIKeyService.invalidateAPIKeys(apiKeys);
await invalidateAPIKeys(apiKeys);
}
}

Expand Down