-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add createApiKey support to security plugin (#42146)
* Add createApiKey support to security plugin * Expiration is optional * Start moving code to new platform * Add unit tests * Fix jest test * Apply PR feedback * Apply PR feedback * Apply PR feedback pt2
- Loading branch information
Showing
7 changed files
with
189 additions
and
0 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
60 changes: 60 additions & 0 deletions
60
x-pack/plugins/security/server/authentication/api_keys.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,60 @@ | ||
/* | ||
* 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 { createAPIKey } from './api_keys'; | ||
import { loggingServiceMock } from '../../../../../src/core/server/mocks'; | ||
|
||
const mockCallAsCurrentUser = jest.fn(); | ||
|
||
beforeAll(() => jest.resetAllMocks()); | ||
|
||
describe('createAPIKey()', () => { | ||
it('returns null when security feature is disabled', async () => { | ||
const result = await createAPIKey({ | ||
body: { | ||
name: '', | ||
role_descriptors: {}, | ||
}, | ||
loggers: loggingServiceMock.create(), | ||
callAsCurrentUser: mockCallAsCurrentUser, | ||
isSecurityFeatureDisabled: () => true, | ||
}); | ||
expect(result).toBeNull(); | ||
expect(mockCallAsCurrentUser).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls callCluster with proper body arguments', async () => { | ||
mockCallAsCurrentUser.mockResolvedValueOnce({ | ||
id: '123', | ||
name: 'key-name', | ||
expiration: '1d', | ||
api_key: 'abc123', | ||
}); | ||
const result = await createAPIKey({ | ||
body: { | ||
name: 'key-name', | ||
role_descriptors: { foo: true }, | ||
expiration: '1d', | ||
}, | ||
loggers: loggingServiceMock.create(), | ||
callAsCurrentUser: mockCallAsCurrentUser, | ||
isSecurityFeatureDisabled: () => false, | ||
}); | ||
expect(result).toEqual({ | ||
api_key: 'abc123', | ||
expiration: '1d', | ||
id: '123', | ||
name: 'key-name', | ||
}); | ||
expect(mockCallAsCurrentUser).toHaveBeenCalledWith('shield.createAPIKey', { | ||
body: { | ||
name: 'key-name', | ||
role_descriptors: { foo: true }, | ||
expiration: '1d', | ||
}, | ||
}); | ||
}); | ||
}); |
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,65 @@ | ||
/* | ||
* 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 { LoggerFactory, ScopedClusterClient } from '../../../../../src/core/server'; | ||
|
||
export interface CreateAPIKeyOptions { | ||
loggers: LoggerFactory; | ||
callAsCurrentUser: ScopedClusterClient['callAsCurrentUser']; | ||
isSecurityFeatureDisabled: () => boolean; | ||
body: { | ||
name: string; | ||
role_descriptors: Record<string, any>; | ||
expiration?: string; | ||
}; | ||
} | ||
|
||
/** | ||
* The return value when creating an API key in Elasticsearch. The API key returned by this API | ||
* can then be used by sending a request with a Authorization header with a value having the | ||
* prefix ApiKey `{token}` where token is id and api_key joined by a colon `{id}:{api_key}` and | ||
* then encoded to base64. | ||
*/ | ||
export interface CreateAPIKeyResult { | ||
/** | ||
* Unique id for this API key | ||
*/ | ||
id: string; | ||
/** | ||
* Name for this API key | ||
*/ | ||
name: string; | ||
/** | ||
* Optional expiration in milliseconds for this API key | ||
*/ | ||
expiration?: number; | ||
/** | ||
* Generated API key | ||
*/ | ||
api_key: string; | ||
} | ||
|
||
export async function createAPIKey({ | ||
body, | ||
loggers, | ||
callAsCurrentUser, | ||
isSecurityFeatureDisabled, | ||
}: CreateAPIKeyOptions): Promise<CreateAPIKeyResult | null> { | ||
const logger = loggers.get('api-keys'); | ||
|
||
if (isSecurityFeatureDisabled()) { | ||
return null; | ||
} | ||
|
||
logger.debug('Trying to create an API key'); | ||
|
||
// User needs `manage_api_key` privilege to use this API | ||
const key = (await callAsCurrentUser('shield.createAPIKey', { body })) as CreateAPIKeyResult; | ||
|
||
logger.debug('API key was created successfully'); | ||
|
||
return key; | ||
} |
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