forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds API tests for APM agent keys (elastic#125302)
* Add API tests for apm agent keys Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
0f57c23
commit 4027f91
Showing
3 changed files
with
236 additions
and
9 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
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
175 changes: 175 additions & 0 deletions
175
x-pack/test/apm_api_integration/tests/settings/agent_keys/agent_keys.spec.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,175 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import expect from '@kbn/expect'; | ||
import { first } from 'lodash'; | ||
import { FtrProviderContext } from '../../../common/ftr_provider_context'; | ||
import { PrivilegeType } from '../../../../../plugins/apm/common/privilege_type'; | ||
import { ApmApiError, ApmApiSupertest } from '../../../common/apm_api_supertest'; | ||
import { ApmUser } from '../../../common/authentication'; | ||
|
||
export default function ApiTest({ getService }: FtrProviderContext) { | ||
const registry = getService('registry'); | ||
const apmApiClient = getService('apmApiClient'); | ||
const esClient = getService('es'); | ||
|
||
const agentKeyName = 'test'; | ||
const allApplicationPrivileges = [ | ||
PrivilegeType.AGENT_CONFIG, | ||
PrivilegeType.EVENT, | ||
PrivilegeType.SOURCEMAP, | ||
]; | ||
|
||
async function createAgentKey(apiClient: ApmApiSupertest, privileges = allApplicationPrivileges) { | ||
return await apiClient({ | ||
endpoint: 'POST /api/apm/agent_keys', | ||
params: { | ||
body: { | ||
name: agentKeyName, | ||
privileges, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
async function invalidateAgentKey(apiClient: ApmApiSupertest, id: string) { | ||
return await apiClient({ | ||
endpoint: 'POST /internal/apm/api_key/invalidate', | ||
params: { | ||
body: { id }, | ||
}, | ||
}); | ||
} | ||
|
||
async function getAgentKeys(apiClient: ApmApiSupertest) { | ||
return await apiClient({ endpoint: 'GET /internal/apm/agent_keys' }); | ||
} | ||
|
||
registry.when( | ||
'When the user does not have the required privileges', | ||
{ config: 'basic', archives: [] }, | ||
() => { | ||
describe('When the user does not have the required cluster privileges', () => { | ||
it('should return an error when creating an agent key', async () => { | ||
const error = await expectToReject(() => createAgentKey(apmApiClient.writeUser)); | ||
expect(error.res.status).to.be(500); | ||
expect(error.res.body.message).contain('is missing the following requested privilege'); | ||
}); | ||
|
||
it('should return an error when invalidating an agent key', async () => { | ||
const error = await expectToReject(() => | ||
invalidateAgentKey(apmApiClient.writeUser, agentKeyName) | ||
); | ||
expect(error.res.status).to.be(500); | ||
}); | ||
|
||
it('should return an error when getting a list of agent keys', async () => { | ||
const error = await expectToReject(() => getAgentKeys(apmApiClient.writeUser)); | ||
expect(error.res.status).to.be(500); | ||
}); | ||
}); | ||
|
||
describe('When the user does not have the required application privileges', () => { | ||
allApplicationPrivileges.map((privilege) => { | ||
it(`should return an error when creating an agent key with ${privilege} privilege`, async () => { | ||
const error = await expectToReject(() => | ||
createAgentKey(apmApiClient.manageOwnAgentKeysUser, [privilege]) | ||
); | ||
expect(error.res.status).to.be(500); | ||
expect(error.res.body.message).contain('is missing the following requested privilege'); | ||
}); | ||
}); | ||
}); | ||
} | ||
); | ||
|
||
registry.when( | ||
'When the user has the required privileges', | ||
{ config: 'basic', archives: [] }, | ||
() => { | ||
afterEach(async () => { | ||
await esClient.security.invalidateApiKey({ | ||
username: ApmUser.apmManageOwnAndCreateAgentKeys, | ||
}); | ||
}); | ||
|
||
it('should be able to create an agent key', async () => { | ||
const { status, body } = await createAgentKey(apmApiClient.createAndAllAgentKeysUser); | ||
expect(status).to.be(200); | ||
expect(body).to.have.property('agentKey'); | ||
expect(body.agentKey).to.have.property('id'); | ||
expect(body.agentKey).to.have.property('api_key'); | ||
expect(body.agentKey).to.have.property('encoded'); | ||
expect(body.agentKey.name).to.be(agentKeyName); | ||
|
||
const { api_keys: apiKeys } = await esClient.security.getApiKey({}); | ||
expect( | ||
apiKeys.filter((key) => !key.invalidated && key.metadata?.application === 'apm') | ||
).to.have.length(1); | ||
}); | ||
|
||
it('should be able to invalidate an agent key', async () => { | ||
// Create | ||
const { body: createAgentKeyBody } = await createAgentKey( | ||
apmApiClient.createAndAllAgentKeysUser | ||
); | ||
const { | ||
agentKey: { id }, | ||
} = createAgentKeyBody; | ||
|
||
// Invalidate | ||
const { status, body } = await invalidateAgentKey( | ||
apmApiClient.createAndAllAgentKeysUser, | ||
id | ||
); | ||
expect(status).to.be(200); | ||
expect(body).to.have.property('invalidatedAgentKeys'); | ||
expect(body.invalidatedAgentKeys).to.eql([id]); | ||
|
||
// Get | ||
const { api_keys: apiKeys } = await esClient.security.getApiKey({}); | ||
expect( | ||
apiKeys.filter((key) => !key.invalidated && key.metadata?.application === 'apm') | ||
).to.be.empty(); | ||
}); | ||
|
||
it('should be able to get a list of agent keys', async () => { | ||
// Create | ||
const { body: createAgentKeyBody } = await createAgentKey( | ||
apmApiClient.createAndAllAgentKeysUser | ||
); | ||
const { | ||
agentKey: { id }, | ||
} = createAgentKeyBody; | ||
|
||
// Get | ||
const { | ||
status, | ||
body: { agentKeys }, | ||
} = await getAgentKeys(apmApiClient.createAndAllAgentKeysUser); | ||
|
||
expect(status).to.be(200); | ||
const agentKey = first(agentKeys); | ||
expect(agentKey?.id).to.be(id); | ||
expect(agentKey?.name).to.be(agentKeyName); | ||
expect(agentKey).to.have.property('creation'); | ||
expect(agentKey?.invalidated).to.be(false); | ||
expect(agentKey).to.have.property('username'); | ||
expect(agentKey).to.have.property('realm'); | ||
expect(agentKey?.metadata.application).to.be('apm'); | ||
}); | ||
} | ||
); | ||
|
||
async function expectToReject(fn: () => Promise<any>): Promise<ApmApiError> { | ||
try { | ||
await fn(); | ||
} catch (e) { | ||
return e; | ||
} | ||
throw new Error(`Expected fn to throw`); | ||
} | ||
} |