Skip to content

Commit

Permalink
[8.12] Ensure all API Keys have a defined name (#175721) (#175730)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.12`:
- [Ensure all API Keys have a defined name
(#175721)](#175721)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Larry
Gregory","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-01-26T20:16:06Z","message":"Ensure
all API Keys have a defined name (#175721)\n\n## Summary\r\n\r\nResolves
https://github.com/elastic/kibana/issues/173890.\r\n\r\nAPI Keys created
in the 7.x era did not require a
`name`\r\n(https://www.elastic.co/guide/en/elasticsearch/reference/7.5/security-api-create-api-key.html#security-api-create-api-key-request-body).\r\nThe
`name` is no longer an optional field, but our UIs have come
to\r\nassume that a `name` will always be available. This updates our
`GET\r\n/internal/security/api_key` API to ensure that a `name` will
always\r\nexist.","sha":"707b4233898c18e63896581a61ddf8bfb7f20979","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Security","Feature:Users/Roles/API
Keys","backport:all-open","v8.13.0"],"title":"Ensure all API Keys have a
defined
name","number":175721,"url":"https://github.com/elastic/kibana/pull/175721","mergeCommit":{"message":"Ensure
all API Keys have a defined name (#175721)\n\n## Summary\r\n\r\nResolves
https://github.com/elastic/kibana/issues/173890.\r\n\r\nAPI Keys created
in the 7.x era did not require a
`name`\r\n(https://www.elastic.co/guide/en/elasticsearch/reference/7.5/security-api-create-api-key.html#security-api-create-api-key-request-body).\r\nThe
`name` is no longer an optional field, but our UIs have come
to\r\nassume that a `name` will always be available. This updates our
`GET\r\n/internal/security/api_key` API to ensure that a `name` will
always\r\nexist.","sha":"707b4233898c18e63896581a61ddf8bfb7f20979"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.13.0","branchLabelMappingKey":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/175721","number":175721,"mergeCommit":{"message":"Ensure
all API Keys have a defined name (#175721)\n\n## Summary\r\n\r\nResolves
https://github.com/elastic/kibana/issues/173890.\r\n\r\nAPI Keys created
in the 7.x era did not require a
`name`\r\n(https://www.elastic.co/guide/en/elasticsearch/reference/7.5/security-api-create-api-key.html#security-api-create-api-key-request-body).\r\nThe
`name` is no longer an optional field, but our UIs have come
to\r\nassume that a `name` will always be available. This updates our
`GET\r\n/internal/security/api_key` API to ensure that a `name` will
always\r\nexist.","sha":"707b4233898c18e63896581a61ddf8bfb7f20979"}}]}]
BACKPORT-->

Co-authored-by: Larry Gregory <[email protected]>
  • Loading branch information
kibanamachine and legrego authored Jan 27, 2024
1 parent 6e9144f commit 42f0a85
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
40 changes: 38 additions & 2 deletions x-pack/plugins/security/server/routes/api_keys/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,44 @@ describe('Get API Keys route', () => {
);

expect(response.status).toBe(200);
expect(response.payload.apiKeys).toContainEqual({ id: '123', invalidated: false });
expect(response.payload.apiKeys).not.toContainEqual({ id: '456', invalidated: true });
expect(response.payload.apiKeys).toContainEqual({ id: '123', name: '', invalidated: false });
expect(response.payload.apiKeys).not.toContainEqual({ id: '456', name: '', invalidated: true });
});

it('should substitute an empty string for keys with `null` names', async () => {
esClientMock.asCurrentUser.security.getApiKey.mockRestore();
esClientMock.asCurrentUser.security.getApiKey.mockResponse({
api_keys: [
{ id: 'with_name', name: 'foo', invalidated: false },
{ id: 'undefined_name', invalidated: false },
{ id: 'null_name', name: null, invalidated: false },
],
} as any);

const response = await routeHandler(
mockContext,
httpServerMock.createKibanaRequest(),
kibanaResponseFactory
);

expect(response.status).toBe(200);
expect(response.payload.apiKeys).toEqual([
{
id: 'with_name',
name: 'foo',
invalidated: false,
},
{
id: 'undefined_name',
name: '',
invalidated: false,
},
{
id: 'null_name',
name: '',
invalidated: false,
},
]);
});

it('should return `404` if API keys are disabled', async () => {
Expand Down
9 changes: 8 additions & 1 deletion x-pack/plugins/security/server/routes/api_keys/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ export function defineGetApiKeysRoutes({
owner: !clusterPrivileges.manage_api_key && !clusterPrivileges.read_security,
});

const validKeys = apiResponse.api_keys.filter(({ invalidated }) => !invalidated);
const validKeys = apiResponse.api_keys
.filter(({ invalidated }) => !invalidated)
.map((key) => {
if (!key.name) {
key.name = '';
}
return key;
});

return response.ok<GetAPIKeysResult>({
body: {
Expand Down

0 comments on commit 42f0a85

Please sign in to comment.