-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fleet] Create enrollment API keys as current user #96464
Changes from all commits
e336deb
11eac0c
b4e3882
de707ea
9833563
34fc7cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ import { ENROLLMENT_API_KEYS_INDEX } from '../../constants'; | |
import { agentPolicyService } from '../agent_policy'; | ||
import { escapeSearchQueryPhrase } from '../saved_object'; | ||
|
||
import { createAPIKey, invalidateAPIKeys } from './security'; | ||
import { invalidateAPIKeys } from './security'; | ||
|
||
const uuidRegex = /^\([0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}\)$/; | ||
|
||
|
@@ -77,14 +77,9 @@ export async function getEnrollmentAPIKey( | |
|
||
/** | ||
* Invalidate an api key and mark it as inactive | ||
* @param soClient | ||
* @param id | ||
*/ | ||
export async function deleteEnrollmentApiKey( | ||
soClient: SavedObjectsClientContract, | ||
esClient: ElasticsearchClient, | ||
id: string | ||
) { | ||
export async function deleteEnrollmentApiKey(esClient: ElasticsearchClient, id: string) { | ||
const enrollmentApiKey = await getEnrollmentAPIKey(esClient, id); | ||
|
||
await invalidateAPIKeys([enrollmentApiKey.api_key_id]); | ||
|
@@ -102,7 +97,6 @@ export async function deleteEnrollmentApiKey( | |
} | ||
|
||
export async function deleteEnrollmentApiKeyForAgentPolicyId( | ||
soClient: SavedObjectsClientContract, | ||
esClient: ElasticsearchClient, | ||
agentPolicyId: string | ||
) { | ||
|
@@ -120,7 +114,7 @@ export async function deleteEnrollmentApiKeyForAgentPolicyId( | |
} | ||
|
||
for (const apiKey of items) { | ||
await deleteEnrollmentApiKey(soClient, esClient, apiKey.id); | ||
await deleteEnrollmentApiKey(esClient, apiKey.id); | ||
} | ||
} | ||
} | ||
|
@@ -182,19 +176,37 @@ export async function generateEnrollmentAPIKey( | |
} | ||
|
||
const name = providedKeyName ? `${providedKeyName} (${id})` : id; | ||
const key = await createAPIKey(soClient, name, { | ||
// Useless role to avoid to have the privilege of the user that created the key | ||
'fleet-apikey-enroll': { | ||
cluster: [], | ||
applications: [ | ||
{ | ||
application: '.fleet', | ||
privileges: ['no-privileges'], | ||
resources: ['*'], | ||
|
||
const { body: key } = await esClient.security | ||
.createApiKey({ | ||
body: { | ||
name, | ||
// @ts-expect-error Metadata in api keys | ||
metadata: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nchaulet Do you know if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the doc it's the key that need to be used https://www.elastic.co/guide/en/elasticsearch/reference/master/security-api-create-api-key.html#security-api-create-api-key-request-body |
||
managed_by: 'fleet', | ||
managed: true, | ||
type: 'enroll', | ||
policy_id: data.agentPolicyId, | ||
}, | ||
], | ||
}, | ||
}); | ||
role_descriptors: { | ||
// Useless role to avoid to have the privilege of the user that created the key | ||
'fleet-apikey-enroll': { | ||
cluster: [], | ||
index: [], | ||
applications: [ | ||
{ | ||
application: '.fleet', | ||
privileges: ['no-privileges'], | ||
resources: ['*'], | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}) | ||
.catch((err) => { | ||
throw new Error(`Impossible to create an api key: ${err.message}`); | ||
}); | ||
|
||
if (!key) { | ||
throw new Error( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add some metadata to these enrollment keys?
Maybe it would be nice to have this defined as a constant outside which makes it easier to document and reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes we could add some metadata, what do you have in mind here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In elastic/fleet-server#195 we have:
We could do something similar here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great add this here 9833563