Skip to content

Commit 57ab6e7

Browse files
authored
[7.x] [APM] Use callWithInternalUser for agent configuration e… (#50488)
* [APM] Use callWithInternalUser for agent configuration endpoints Closes #50050. * Review feedback * Use internalClient for agent conf queries only
1 parent e9bad96 commit 57ab6e7

File tree

21 files changed

+135
-36
lines changed

21 files changed

+135
-36
lines changed

x-pack/legacy/plugins/apm/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const apm: LegacyPluginInitializer = kibana => {
8787
catalogue: ['apm'],
8888
privileges: {
8989
all: {
90-
api: ['apm'],
90+
api: ['apm', 'apm_write'],
9191
catalogue: ['apm'],
9292
savedObject: {
9393
all: [],

x-pack/legacy/plugins/apm/public/utils/testHelpers.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ interface MockSetup {
9797
start: number;
9898
end: number;
9999
client: any;
100+
internalClient: any;
100101
config: {
101102
get: any;
102103
has: any;
@@ -122,12 +123,21 @@ export async function inspectSearchParams(
122123
}
123124
});
124125

126+
const internalClientSpy = jest.fn().mockReturnValueOnce({
127+
hits: {
128+
total: 0
129+
}
130+
});
131+
125132
const mockSetup = {
126133
start: 1528113600000,
127134
end: 1528977600000,
128135
client: {
129136
search: clientSpy
130137
} as any,
138+
internalClient: {
139+
search: internalClientSpy
140+
} as any,
131141
config: {
132142
get: () => 'myIndex' as any,
133143
has: () => true
@@ -153,8 +163,15 @@ export async function inspectSearchParams(
153163
// we're only extracting the search params
154164
}
155165

166+
let params;
167+
if (clientSpy.mock.calls.length) {
168+
params = clientSpy.mock.calls[0][0];
169+
} else {
170+
params = internalClientSpy.mock.calls[0][0];
171+
}
172+
156173
return {
157-
params: clientSpy.mock.calls[0][0],
174+
params,
158175
teardown: () => clientSpy.mockClear()
159176
};
160177
}

x-pack/legacy/plugins/apm/server/lib/errors/distribution/__tests__/get_buckets.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ describe('timeseriesFetcher', () => {
3131
client: {
3232
search: clientSpy
3333
} as any,
34+
internalClient: {
35+
search: clientSpy
36+
} as any,
3437
config: {
3538
get: () => 'myIndex' as any,
3639
has: () => true

x-pack/legacy/plugins/apm/server/lib/helpers/es_client.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,23 @@ interface APMOptions {
9292
includeLegacyData: boolean;
9393
}
9494

95-
export function getESClient(req: Legacy.Request) {
95+
interface ClientCreateOptions {
96+
clientAsInternalUser?: boolean;
97+
}
98+
99+
export type ESClient = ReturnType<typeof getESClient>;
100+
101+
export function getESClient(
102+
req: Legacy.Request,
103+
{ clientAsInternalUser = false }: ClientCreateOptions = {}
104+
) {
96105
const cluster = req.server.plugins.elasticsearch.getCluster('data');
97106
const query = req.query as Record<string, unknown>;
98107

108+
const callMethod = clientAsInternalUser
109+
? cluster.callWithInternalUser.bind(cluster)
110+
: cluster.callWithRequest.bind(cluster, req);
111+
99112
return {
100113
search: async <
101114
TDocument = unknown,
@@ -121,20 +134,18 @@ export function getESClient(req: Legacy.Request) {
121134
console.log(JSON.stringify(nextParams.body, null, 4));
122135
}
123136

124-
return (cluster.callWithRequest(
125-
req,
126-
'search',
127-
nextParams
128-
) as unknown) as Promise<ESSearchResponse<TDocument, TSearchRequest>>;
137+
return (callMethod('search', nextParams) as unknown) as Promise<
138+
ESSearchResponse<TDocument, TSearchRequest>
139+
>;
129140
},
130141
index: <Body>(params: APMIndexDocumentParams<Body>) => {
131-
return cluster.callWithRequest(req, 'index', params);
142+
return callMethod('index', params);
132143
},
133144
delete: (params: IndicesDeleteParams) => {
134-
return cluster.callWithRequest(req, 'delete', params);
145+
return callMethod('delete', params);
135146
},
136147
indicesCreate: (params: IndicesCreateParams) => {
137-
return cluster.callWithRequest(req, 'indices.create', params);
148+
return callMethod('indices.create', params);
138149
}
139150
};
140151
}

x-pack/legacy/plugins/apm/server/lib/helpers/setup_request.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@ jest.mock('../settings/apm_indices/get_apm_indices', () => ({
2121

2222
function getMockRequest() {
2323
const callWithRequestSpy = jest.fn();
24+
const callWithInternalUserSpy = jest.fn();
2425
const mockRequest = ({
2526
params: {},
2627
query: {},
2728
server: {
2829
config: () => ({ get: () => 'apm-*' }),
2930
plugins: {
3031
elasticsearch: {
31-
getCluster: () => ({ callWithRequest: callWithRequestSpy })
32+
getCluster: () => ({
33+
callWithRequest: callWithRequestSpy,
34+
callWithInternalUser: callWithInternalUserSpy
35+
})
3236
}
3337
}
3438
},
3539
getUiSettingsService: () => ({ get: async () => false })
3640
} as any) as Legacy.Request;
3741

38-
return { callWithRequestSpy, mockRequest };
42+
return { callWithRequestSpy, callWithInternalUserSpy, mockRequest };
3943
}
4044

4145
describe('setupRequest', () => {
@@ -57,6 +61,27 @@ describe('setupRequest', () => {
5761
});
5862
});
5963

64+
it('should call callWithInternalUser with default args', async () => {
65+
const { mockRequest, callWithInternalUserSpy } = getMockRequest();
66+
const { internalClient } = await setupRequest(mockRequest);
67+
await internalClient.search({
68+
index: 'apm-*',
69+
body: { foo: 'bar' }
70+
} as any);
71+
expect(callWithInternalUserSpy).toHaveBeenCalledWith('search', {
72+
index: 'apm-*',
73+
body: {
74+
foo: 'bar',
75+
query: {
76+
bool: {
77+
filter: [{ range: { 'observer.version_major': { gte: 7 } } }]
78+
}
79+
}
80+
},
81+
ignore_throttled: true
82+
});
83+
});
84+
6085
describe('observer.version_major filter', () => {
6186
describe('if index is apm-*', () => {
6287
it('should merge `observer.version_major` filter with existing boolean filters', async () => {

x-pack/legacy/plugins/apm/server/lib/helpers/setup_request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export async function setupRequest(req: Legacy.Request) {
4141
start: moment.utc(query.start).valueOf(),
4242
end: moment.utc(query.end).valueOf(),
4343
uiFiltersES,
44-
client: getESClient(req),
44+
client: getESClient(req, { clientAsInternalUser: false }),
45+
internalClient: getESClient(req, { clientAsInternalUser: true }),
4546
config,
4647
indices
4748
};

x-pack/legacy/plugins/apm/server/lib/settings/agent_configuration/create_or_update_configuration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function createOrUpdateConfiguration({
2121
>;
2222
setup: Setup;
2323
}) {
24-
const { client, indices } = setup;
24+
const { internalClient, indices } = setup;
2525

2626
const params: APMIndexDocumentParams<AgentConfiguration> = {
2727
refresh: true,
@@ -44,5 +44,5 @@ export async function createOrUpdateConfiguration({
4444
params.id = configurationId;
4545
}
4646

47-
return client.index(params);
47+
return internalClient.index(params);
4848
}

x-pack/legacy/plugins/apm/server/lib/settings/agent_configuration/delete_configuration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ export async function deleteConfiguration({
1313
configurationId: string;
1414
setup: Setup;
1515
}) {
16-
const { client, indices } = setup;
16+
const { internalClient, indices } = setup;
1717

1818
const params = {
1919
refresh: 'wait_for',
2020
index: indices['apm_oss.apmAgentConfigurationIndex'],
2121
id: configurationId
2222
};
2323

24-
return client.delete(params);
24+
return internalClient.delete(params);
2525
}

x-pack/legacy/plugins/apm/server/lib/settings/agent_configuration/get_environments/get_existing_environments_for_service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function getExistingEnvironmentsForService({
1919
serviceName: string | undefined;
2020
setup: Setup;
2121
}) {
22-
const { client, indices } = setup;
22+
const { internalClient, indices } = setup;
2323

2424
const bool = serviceName
2525
? { filter: [{ term: { [SERVICE_NAME]: serviceName } }] }
@@ -42,7 +42,7 @@ export async function getExistingEnvironmentsForService({
4242
}
4343
};
4444

45-
const resp = await client.search(params);
45+
const resp = await internalClient.search(params);
4646
const buckets = idx(resp.aggregations, _ => _.environments.buckets) || [];
4747
return buckets.map(bucket => bucket.key as string);
4848
}

x-pack/legacy/plugins/apm/server/lib/settings/agent_configuration/list_configurations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export type AgentConfigurationListAPIResponse = PromiseReturnType<
1212
typeof listConfigurations
1313
>;
1414
export async function listConfigurations({ setup }: { setup: Setup }) {
15-
const { client, indices } = setup;
15+
const { internalClient, indices } = setup;
1616

1717
const params = {
1818
index: indices['apm_oss.apmAgentConfigurationIndex']
1919
};
2020

21-
const resp = await client.search<AgentConfiguration>(params);
21+
const resp = await internalClient.search<AgentConfiguration>(params);
2222
return resp.hits.hits.map(item => ({
2323
id: item._id,
2424
...item._source

0 commit comments

Comments
 (0)