[Fleet] Allow readonly user to access /agent_policies_spaces API (#203434)

This commit is contained in:
Nicolas Chaulet 2024-12-10 10:00:50 -05:00 committed by GitHub
parent 9089dbebca
commit ebcbb0b26e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 1 deletions

View file

@ -237,7 +237,7 @@ export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType
path: APP_API_ROUTES.AGENT_POLICIES_SPACES,
access: 'internal',
fleetAuthz: {
fleet: { allAgentPolicies: true },
fleet: { readAgentPolicies: true },
},
})
.addVersion(

View file

@ -27,6 +27,10 @@ export default function (providerContext: FtrProviderContext) {
username: testUsers.fleet_all_int_all.username,
password: testUsers.fleet_all_int_all.password,
});
const apiClientReadOnly = new SpaceTestApiClient(supertestWithoutAuth, {
username: testUsers.fleet_read_only.username,
password: testUsers.fleet_read_only.password,
});
let defaultSpacePolicy1: CreateAgentPolicyResponse;
let spaceTest1Policy1: CreateAgentPolicyResponse;
@ -113,5 +117,19 @@ export default function (providerContext: FtrProviderContext) {
expect(res.item.id).to.eql(`${TEST_SPACE_1}-fleet-server-policy`);
});
});
describe('GET /agent_policies_spaces', () => {
it('should return all spaces user can write agent policies to', async () => {
const res = await apiClient.getAgentPoliciesSpaces();
expect(res.items.map(({ id }: { id: string }) => id)).to.eql(['default', 'test1']);
});
it('should return no spaces for user with readonly access', async () => {
const res = await apiClientReadOnly.getAgentPoliciesSpaces();
expect(res.items.map(({ id }: { id: string }) => id)).to.eql([]);
});
});
});
}

View file

@ -179,6 +179,18 @@ export class SpaceTestApiClient {
return res;
}
async getAgentPoliciesSpaces(spaceId?: string) {
const { body: res } = await this.supertest
.get(`${this.getBaseUrl(spaceId)}/internal/fleet/agent_policies_spaces`)
.auth(this.auth.username, this.auth.password)
.set('kbn-xsrf', 'xxxx')
.set('elastic-api-version', '1')
.expect(200);
return res;
}
// Enrollment API Keys
async getEnrollmentApiKey(
keyId: string,