Skip to content

Commit

Permalink
Fallback to using api key id when name is not defined (#175733)
Browse files Browse the repository at this point in the history
Followup to #175721.

Prefer using the key's `id` when `name` is not defined.
  • Loading branch information
legrego authored Jan 26, 2024
1 parent bf40a0d commit 2cf9668
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 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,11 +65,15 @@ describe('Get API Keys route', () => {
);

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

it('should substitute an empty string for keys with `null` names', async () => {
it('should substitute the API key id for keys with `null` names', async () => {
esClientMock.asCurrentUser.security.getApiKey.mockRestore();
esClientMock.asCurrentUser.security.getApiKey.mockResponse({
api_keys: [
Expand All @@ -94,12 +98,12 @@ describe('Get API Keys route', () => {
},
{
id: 'undefined_name',
name: '',
name: 'undefined_name',
invalidated: false,
},
{
id: 'null_name',
name: '',
name: 'null_name',
invalidated: false,
},
]);
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/security/server/routes/api_keys/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function defineGetApiKeysRoutes({
.filter(({ invalidated }) => !invalidated)
.map((key) => {
if (!key.name) {
key.name = '';
key.name = key.id;
}
return key;
});
Expand Down

0 comments on commit 2cf9668

Please sign in to comment.