[Fleet] Remove unused authenticateAgentWithAccessToken method from agent service (#116183)

This commit is contained in:
Nicolas Chaulet 2021-10-25 15:33:33 -04:00 committed by GitHub
parent 81264f73e9
commit 0119bd8e45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 1 additions and 203 deletions

View file

@ -114,7 +114,6 @@ export const createMockAgentService = (): jest.Mocked<AgentService> => {
return {
getAgentStatusById: jest.fn(),
getAgentStatusForAgentPolicy: jest.fn(),
authenticateAgentWithAccessToken: jest.fn(),
getAgent: jest.fn(),
listAgents: jest.fn(),
};

View file

@ -74,7 +74,6 @@ import {
import {
getAgentStatusById,
getAgentStatusForAgentPolicy,
authenticateAgentWithAccessToken,
getAgentsByKuery,
getAgentById,
} from './services/agents';
@ -342,7 +341,6 @@ export class FleetPlugin
listAgents: getAgentsByKuery,
getAgentStatusById,
getAgentStatusForAgentPolicy,
authenticateAgentWithAccessToken,
},
agentPolicyService: {
get: agentPolicyService.get,

View file

@ -1,156 +0,0 @@
/*
* 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 type { KibanaRequest } from 'kibana/server';
import { elasticsearchServiceMock } from 'src/core/server/mocks';
import { authenticateAgentWithAccessToken } from './authenticate';
describe('test agent autenticate services', () => {
it('should succeed with a valid API key and an active agent', async () => {
const mockEsClient = elasticsearchServiceMock.createInternalClient();
mockEsClient.search.mockResolvedValue({
body: {
hits: {
hits: [
{
// @ts-expect-error
_id: 'agent1',
_source: {
// @ts-expect-error
active: true,
// @ts-expect-error
access_api_key_id: 'pedTuHIBTEDt93wW0Fhr',
},
},
],
},
},
});
await authenticateAgentWithAccessToken(mockEsClient, {
auth: { isAuthenticated: true },
headers: {
authorization: 'ApiKey cGVkVHVISUJURUR0OTN3VzBGaHI6TnU1U0JtbHJSeC12Rm9qQWpoSHlUZw==',
},
} as KibanaRequest);
});
it('should throw if the request is not authenticated', async () => {
const mockEsClient = elasticsearchServiceMock.createInternalClient();
mockEsClient.search.mockResolvedValue({
body: {
hits: {
hits: [
{
// @ts-expect-error
_id: 'agent1',
_source: {
// @ts-expect-error
active: true,
// @ts-expect-error
access_api_key_id: 'pedTuHIBTEDt93wW0Fhr',
},
},
],
},
},
});
expect(
authenticateAgentWithAccessToken(mockEsClient, {
auth: { isAuthenticated: false },
headers: {
authorization: 'ApiKey cGVkVHVISUJURUR0OTN3VzBGaHI6TnU1U0JtbHJSeC12Rm9qQWpoSHlUZw==',
},
} as KibanaRequest)
).rejects.toThrow(/Request not authenticated/);
});
it('should throw if the ApiKey headers is malformed', async () => {
const mockEsClient = elasticsearchServiceMock.createInternalClient();
const hits = [
{
_id: 'agent1',
_source: {
active: true,
access_api_key_id: 'pedTuHIBTEDt93wW0Fhr',
},
},
];
mockEsClient.search.mockResolvedValue({
body: {
hits: {
// @ts-expect-error
hits,
},
},
});
expect(
authenticateAgentWithAccessToken(mockEsClient, {
auth: { isAuthenticated: true },
headers: {
authorization: 'aaaa',
},
} as KibanaRequest)
).rejects.toThrow(/Authorization header is malformed/);
});
it('should throw if the agent is not active', async () => {
const mockEsClient = elasticsearchServiceMock.createInternalClient();
const hits = [
{
_id: 'agent1',
_source: {
active: false,
access_api_key_id: 'pedTuHIBTEDt93wW0Fhr',
},
},
];
mockEsClient.search.mockResolvedValue({
body: {
hits: {
// @ts-expect-error
hits,
},
},
});
expect(
authenticateAgentWithAccessToken(mockEsClient, {
auth: { isAuthenticated: true },
headers: {
authorization: 'ApiKey cGVkVHVISUJURUR0OTN3VzBGaHI6TnU1U0JtbHJSeC12Rm9qQWpoSHlUZw==',
},
} as KibanaRequest)
).rejects.toThrow(/Agent inactive/);
});
it('should throw if there is no agent matching the API key', async () => {
const mockEsClient = elasticsearchServiceMock.createInternalClient();
mockEsClient.search.mockResolvedValue({
body: {
hits: {
// @ts-expect-error
hits: [],
},
},
});
expect(
authenticateAgentWithAccessToken(mockEsClient, {
auth: { isAuthenticated: true },
headers: {
authorization: 'ApiKey cGVkVHVISUJURUR0OTN3VzBGaHI6TnU1U0JtbHJSeC12Rm9qQWpoSHlUZw==',
},
} as KibanaRequest)
).rejects.toThrow(/Agent not found/);
});
});

View file

@ -1,34 +0,0 @@
/*
* 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 Boom from '@hapi/boom';
import type { KibanaRequest } from 'src/core/server';
import type { ElasticsearchClient } from 'src/core/server';
import type { Agent } from '../../types';
import * as APIKeyService from '../api_keys';
import { getAgentByAccessAPIKeyId } from './crud';
export async function authenticateAgentWithAccessToken(
esClient: ElasticsearchClient,
request: KibanaRequest
): Promise<Agent> {
if (!request.auth.isAuthenticated) {
throw Boom.unauthorized('Request not authenticated');
}
let res: { apiKey: string; apiKeyId: string };
try {
res = APIKeyService.parseApiKeyFromHeaders(request.headers);
} catch (err) {
throw Boom.unauthorized(err.message);
}
const agent = await getAgentByAccessAPIKeyId(esClient, res.apiKeyId);
return agent;
}

View file

@ -12,5 +12,4 @@ export * from './crud';
export * from './update';
export * from './actions';
export * from './reassign';
export * from './authenticate';
export * from './setup';

View file

@ -5,10 +5,9 @@
* 2.0.
*/
import type { KibanaRequest } from 'kibana/server';
import type { ElasticsearchClient, SavedObjectsClientContract } from 'kibana/server';
import type { AgentStatus, Agent } from '../types';
import type { AgentStatus } from '../types';
import type { GetAgentStatusResponse } from '../../common';
@ -48,13 +47,6 @@ export interface AgentService {
* Get an Agent by id
*/
getAgent: typeof getAgentById;
/**
* Authenticate an agent with access toekn
*/
authenticateAgentWithAccessToken(
esClient: ElasticsearchClient,
request: KibanaRequest
): Promise<Agent>;
/**
* Return the status by the Agent's id
*/