-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IngestManager] Expose agent authentication using access key (#69650)
* [IngestManager] Expose agent authentication using access key * Add unit tests to authenticateAgentWithAccessToken service
- Loading branch information
Showing
9 changed files
with
193 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
x-pack/plugins/ingest_manager/server/services/agents/authenticate.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { KibanaRequest } from 'kibana/server'; | ||
import { savedObjectsClientMock } 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 mockSavedObjectsClient = savedObjectsClientMock.create(); | ||
mockSavedObjectsClient.find.mockReturnValue( | ||
Promise.resolve({ | ||
page: 1, | ||
per_page: 100, | ||
total: 1, | ||
saved_objects: [ | ||
{ | ||
id: 'agent1', | ||
type: 'agent', | ||
references: [], | ||
score: 0, | ||
attributes: { | ||
active: true, | ||
access_api_key_id: 'pedTuHIBTEDt93wW0Fhr', | ||
}, | ||
}, | ||
], | ||
}) | ||
); | ||
await authenticateAgentWithAccessToken(mockSavedObjectsClient, { | ||
auth: { isAuthenticated: true }, | ||
headers: { | ||
authorization: 'ApiKey cGVkVHVISUJURUR0OTN3VzBGaHI6TnU1U0JtbHJSeC12Rm9qQWpoSHlUZw==', | ||
}, | ||
} as KibanaRequest); | ||
}); | ||
|
||
it('should throw if the request is not authenticated', async () => { | ||
const mockSavedObjectsClient = savedObjectsClientMock.create(); | ||
mockSavedObjectsClient.find.mockReturnValue( | ||
Promise.resolve({ | ||
page: 1, | ||
per_page: 100, | ||
total: 1, | ||
saved_objects: [ | ||
{ | ||
id: 'agent1', | ||
type: 'agent', | ||
references: [], | ||
score: 0, | ||
attributes: { | ||
active: true, | ||
access_api_key_id: 'pedTuHIBTEDt93wW0Fhr', | ||
}, | ||
}, | ||
], | ||
}) | ||
); | ||
expect( | ||
authenticateAgentWithAccessToken(mockSavedObjectsClient, { | ||
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 mockSavedObjectsClient = savedObjectsClientMock.create(); | ||
mockSavedObjectsClient.find.mockReturnValue( | ||
Promise.resolve({ | ||
page: 1, | ||
per_page: 100, | ||
total: 1, | ||
saved_objects: [ | ||
{ | ||
id: 'agent1', | ||
type: 'agent', | ||
references: [], | ||
score: 0, | ||
attributes: { | ||
active: false, | ||
access_api_key_id: 'pedTuHIBTEDt93wW0Fhr', | ||
}, | ||
}, | ||
], | ||
}) | ||
); | ||
expect( | ||
authenticateAgentWithAccessToken(mockSavedObjectsClient, { | ||
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 mockSavedObjectsClient = savedObjectsClientMock.create(); | ||
mockSavedObjectsClient.find.mockReturnValue( | ||
Promise.resolve({ | ||
page: 1, | ||
per_page: 100, | ||
total: 1, | ||
saved_objects: [ | ||
{ | ||
id: 'agent1', | ||
type: 'agent', | ||
references: [], | ||
score: 0, | ||
attributes: { | ||
active: false, | ||
access_api_key_id: 'pedTuHIBTEDt93wW0Fhr', | ||
}, | ||
}, | ||
], | ||
}) | ||
); | ||
expect( | ||
authenticateAgentWithAccessToken(mockSavedObjectsClient, { | ||
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 mockSavedObjectsClient = savedObjectsClientMock.create(); | ||
mockSavedObjectsClient.find.mockReturnValue( | ||
Promise.resolve({ | ||
page: 1, | ||
per_page: 100, | ||
total: 1, | ||
saved_objects: [], | ||
}) | ||
); | ||
expect( | ||
authenticateAgentWithAccessToken(mockSavedObjectsClient, { | ||
auth: { isAuthenticated: true }, | ||
headers: { | ||
authorization: 'ApiKey cGVkVHVISUJURUR0OTN3VzBGaHI6TnU1U0JtbHJSeC12Rm9qQWpoSHlUZw==', | ||
}, | ||
} as KibanaRequest) | ||
).rejects.toThrow(/Agent not found/); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
x-pack/plugins/ingest_manager/server/services/agents/authenticate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import Boom from 'boom'; | ||
import { KibanaRequest, SavedObjectsClientContract } from 'src/core/server'; | ||
import { Agent } from '../../types'; | ||
import * as APIKeyService from '../api_keys'; | ||
import { getAgentByAccessAPIKeyId } from './crud'; | ||
|
||
export async function authenticateAgentWithAccessToken( | ||
soClient: SavedObjectsClientContract, | ||
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(soClient, res.apiKeyId); | ||
|
||
return agent; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters