[8.x] [Fleet] Fix migrating enrollment api keys for space awareness (#207847) (#207875)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Fleet] Fix migrating enrollment api keys for space awareness
(#207847)](https://github.com/elastic/kibana/pull/207847)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nicolas
Chaulet","email":"nicolas.chaulet@elastic.co"},"sourceCommit":{"committedDate":"2025-01-22T16:24:51Z","message":"[Fleet]
Fix migrating enrollment api keys for space awareness
(#207847)","sha":"0d32932f6bbbd70554f662e41e0b3a468ed709ff","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v9.0.0","backport:prev-minor"],"title":"[Fleet]
Fix migrating enrollment api keys for space
awareness","number":207847,"url":"https://github.com/elastic/kibana/pull/207847","mergeCommit":{"message":"[Fleet]
Fix migrating enrollment api keys for space awareness
(#207847)","sha":"0d32932f6bbbd70554f662e41e0b3a468ed709ff"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/207847","number":207847,"mergeCommit":{"message":"[Fleet]
Fix migrating enrollment api keys for space awareness
(#207847)","sha":"0d32932f6bbbd70554f662e41e0b3a468ed709ff"}}]}]
BACKPORT-->

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
This commit is contained in:
Kibana Machine 2025-01-23 05:11:57 +11:00 committed by GitHub
parent 06af0cf9a3
commit c41692ac68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 2 deletions

View file

@ -119,7 +119,10 @@ export async function getEnrollmentAPIKey(
if (spaceId) {
if (spaceId === DEFAULT_SPACE_ID) {
if (body._source?.namespaces && !body._source?.namespaces.includes(DEFAULT_SPACE_ID)) {
if (
(body._source?.namespaces?.length ?? 0) > 0 &&
!body._source?.namespaces?.includes(DEFAULT_SPACE_ID)
) {
throw new EnrollmentKeyNotFoundError(`Enrollment api key ${id} not found in namespace`);
}
} else if (!body._source?.namespaces?.includes(spaceId)) {

View file

@ -506,7 +506,8 @@ export class SpaceTestApiClient {
.post(`${this.getBaseUrl(spaceId)}/internal/fleet/enable_space_awareness`)
.auth(this.auth.username, this.auth.password)
.set('kbn-xsrf', 'xxxx')
.set('elastic-api-version', '1');
.set('elastic-api-version', '1')
.expect(200);
return res;
}

View file

@ -123,6 +123,16 @@ export default function (providerContext: FtrProviderContext) {
expect(policiesTestSpaceIds.length).to.eql(0);
});
it('enrollment api keys should be available', async () => {
const defaultSpaceEnrollmentApiKeys = await apiClient.getEnrollmentApiKeys();
expect(defaultSpaceEnrollmentApiKeys.items.length).to.eql(3);
await apiClient.getEnrollmentApiKeys(defaultSpaceEnrollmentApiKeys.items[0].id);
const testSpaceEnrollmentApiKeys = await apiClient.getEnrollmentApiKeys(TEST_SPACE_1);
expect(testSpaceEnrollmentApiKeys.items.length).to.eql(0);
});
it('package policies should be migrated to the default space', async () => {
const policiesDefaultSpaceIds = (await apiClient.getPackagePolicies()).items
.map(({ id }) => id)