Skip to content

Commit 0e6603c

Browse files
SiddharthMantrielasticmachine
authored andcommitted
fix(api keys): Query API Keys correctly returns valid API key name if not present (elastic#234083)
Closes elastic#234194 ## Summary Reintroduces fallback for API Keys with null names which was causing the API Keys page to break ### Release notes Fixes a potential bug on the API Keys Management page when trying to load API keys with null names. --------- Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 47541c5 commit 0e6603c

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎x-pack/platform/plugins/shared/security/server/routes/api_keys/query.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,42 @@ describe('Query API Keys route', () => {
227227
);
228228
});
229229
});
230+
231+
describe('validate API Key names', () => {
232+
it('should substitute an empty string for keys with `null` names', async () => {
233+
esClientMock.asCurrentUser.security.queryApiKeys.mockRestore();
234+
esClientMock.asCurrentUser.security.queryApiKeys.mockResponse({
235+
api_keys: [
236+
{ id: 'with_name', name: 'foo', invalidated: false },
237+
{ id: 'undefined_name', invalidated: false },
238+
{ id: 'null_name', name: null, invalidated: false },
239+
],
240+
} as any);
241+
242+
const response = await routeHandler(
243+
mockContext,
244+
httpServerMock.createKibanaRequest(),
245+
kibanaResponseFactory
246+
);
247+
248+
expect(response.status).toBe(200);
249+
expect(response.payload.apiKeys).toEqual([
250+
{
251+
id: 'with_name',
252+
name: 'foo',
253+
invalidated: false,
254+
},
255+
{
256+
id: 'undefined_name',
257+
name: 'undefined_name',
258+
invalidated: false,
259+
},
260+
{
261+
id: 'null_name',
262+
name: 'null_name',
263+
invalidated: false,
264+
},
265+
]);
266+
});
267+
});
230268
});

‎x-pack/platform/plugins/shared/security/server/routes/api_keys/query.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* 2.0.
66
*/
77

8+
import type { SecurityApiKey } from '@elastic/elasticsearch/lib/api/types';
9+
810
import { schema } from '@kbn/config-schema';
911
import type { QueryApiKeyResult } from '@kbn/security-plugin-types-common';
1012

@@ -16,6 +18,15 @@ interface QueryClause {
1618
[key: string]: any;
1719
}
1820

21+
const transformAPIKeyNames = (keys: SecurityApiKey[]) => {
22+
return keys.map((key) => {
23+
if (!key.name) {
24+
key.name = key.id;
25+
}
26+
return key;
27+
});
28+
};
29+
1930
export function defineQueryApiKeysAndAggregationsRoute({
2031
router,
2132
getAuthenticationService,
@@ -165,7 +176,7 @@ export function defineQueryApiKeysAndAggregationsRoute({
165176

166177
queryResult = {
167178
// @ts-expect-error Elasticsearch client types do not know about Cross-Cluster API keys yet.
168-
apiKeys: queryResponse.api_keys,
179+
apiKeys: transformAPIKeyNames(queryResponse.api_keys),
169180
total: queryResponse.total,
170181
count: queryResponse.api_keys.length,
171182
};

0 commit comments

Comments
 (0)