Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,42 @@ describe('Query API Keys route', () => {
);
});
});

describe('validate API Key names', () => {
it('should substitute an empty string for keys with `null` names', async () => {
esClientMock.asCurrentUser.security.queryApiKeys.mockRestore();
esClientMock.asCurrentUser.security.queryApiKeys.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: 'undefined_name',
invalidated: false,
},
{
id: 'null_name',
name: 'null_name',
invalidated: false,
},
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import type { SecurityApiKey } from '@elastic/elasticsearch/lib/api/types';

import { schema } from '@kbn/config-schema';
import type { QueryApiKeyResult } from '@kbn/security-plugin-types-common';

Expand All @@ -16,6 +18,15 @@ interface QueryClause {
[key: string]: any;
}

const transformAPIKeyNames = (keys: SecurityApiKey[]) => {
return keys.map((key) => {
if (!key.name) {
key.name = key.id;
}
return key;
});
};

export function defineQueryApiKeysAndAggregationsRoute({
router,
getAuthenticationService,
Expand Down Expand Up @@ -165,7 +176,7 @@ export function defineQueryApiKeysAndAggregationsRoute({

queryResult = {
// @ts-expect-error Elasticsearch client types do not know about Cross-Cluster API keys yet.
apiKeys: queryResponse.api_keys,
apiKeys: transformAPIKeyNames(queryResponse.api_keys),
total: queryResponse.total,
count: queryResponse.api_keys.length,
};
Expand Down