Skip to content

Commit 46879ee

Browse files
committed
[APM] Use callWithInternalUser for agent configuration endpoin… (#50211)
* [APM] Use callWithInternalUser for agent configuration endpoints Closes #50050. * Review feedback * Use internalClient for agent conf queries only
1 parent 7dc6d62 commit 46879ee

File tree

21 files changed

+133
-38
lines changed

21 files changed

+133
-38
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const apm: LegacyPluginInitializer = kibana => {
9191
catalogue: ['apm'],
9292
privileges: {
9393
all: {
94-
api: ['apm'],
94+
api: ['apm', 'apm_write'],
9595
catalogue: ['apm'],
9696
savedObject: {
9797
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;
@@ -113,12 +114,21 @@ export async function inspectSearchParams(
113114
}
114115
});
115116

117+
const internalClientSpy = jest.fn().mockReturnValueOnce({
118+
hits: {
119+
total: 0
120+
}
121+
});
122+
116123
const mockSetup = {
117124
start: 1528113600000,
118125
end: 1528977600000,
119126
client: {
120127
search: clientSpy
121128
} as any,
129+
internalClient: {
130+
search: internalClientSpy
131+
} as any,
122132
config: {
123133
get: () => 'myIndex' as any,
124134
has: () => true
@@ -135,8 +145,15 @@ export async function inspectSearchParams(
135145
// we're only extracting the search params
136146
}
137147

148+
let params;
149+
if (clientSpy.mock.calls.length) {
150+
params = clientSpy.mock.calls[0][0];
151+
} else {
152+
params = internalClientSpy.mock.calls[0][0];
153+
}
154+
138155
return {
139-
params: clientSpy.mock.calls[0][0],
156+
params,
140157
teardown: () => clientSpy.mockClear()
141158
};
142159
}

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: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,23 @@ interface APMOptions {
8484
includeLegacyData: boolean;
8585
}
8686

87-
export function getESClient(req: Legacy.Request) {
87+
interface ClientCreateOptions {
88+
clientAsInternalUser?: boolean;
89+
}
90+
91+
export type ESClient = ReturnType<typeof getESClient>;
92+
93+
export function getESClient(
94+
req: Legacy.Request,
95+
{ clientAsInternalUser = false }: ClientCreateOptions = {}
96+
) {
8897
const cluster = req.server.plugins.elasticsearch.getCluster('data');
8998
const query = req.query as StringMap;
9099

100+
const callMethod = clientAsInternalUser
101+
? cluster.callWithInternalUser.bind(cluster)
102+
: cluster.callWithRequest.bind(cluster, req);
103+
91104
return {
92105
search: async <Hits = unknown, U extends SearchParams = {}>(
93106
params: U,
@@ -110,22 +123,18 @@ export function getESClient(req: Legacy.Request) {
110123
console.log(JSON.stringify(nextParams.body, null, 4));
111124
}
112125

113-
return (cluster.callWithRequest(
114-
req,
115-
'search',
116-
nextParams
117-
) as unknown) as Promise<
126+
return (callMethod('search', nextParams) as unknown) as Promise<
118127
AggregationSearchResponseWithTotalHitsAsObject<Hits, U>
119128
>;
120129
},
121130
index: <Body>(params: IndexDocumentParams<Body>) => {
122-
return cluster.callWithRequest(req, 'index', params);
131+
return callMethod('index', params);
123132
},
124133
delete: (params: IndicesDeleteParams) => {
125-
return cluster.callWithRequest(req, 'delete', params);
134+
return callMethod('delete', params);
126135
},
127136
indicesCreate: (params: IndicesCreateParams) => {
128-
return cluster.callWithRequest(req, 'indices.create', params);
137+
return callMethod('indices.create', params);
129138
}
130139
};
131140
}

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
@@ -10,21 +10,25 @@ import { uiSettingsServiceMock } from 'src/core/server/mocks';
1010

1111
function getMockRequest() {
1212
const callWithRequestSpy = jest.fn();
13+
const callWithInternalUserSpy = jest.fn();
1314
const mockRequest = ({
1415
params: {},
1516
query: {},
1617
server: {
1718
config: () => ({ get: () => 'apm-*' }),
1819
plugins: {
1920
elasticsearch: {
20-
getCluster: () => ({ callWithRequest: callWithRequestSpy })
21+
getCluster: () => ({
22+
callWithRequest: callWithRequestSpy,
23+
callWithInternalUser: callWithInternalUserSpy
24+
})
2125
}
2226
}
2327
},
2428
getUiSettingsService: () => ({ get: async () => false })
2529
} as any) as Legacy.Request;
2630

27-
return { callWithRequestSpy, mockRequest };
31+
return { callWithRequestSpy, callWithInternalUserSpy, mockRequest };
2832
}
2933

3034
describe('setupRequest', () => {
@@ -46,6 +50,27 @@ describe('setupRequest', () => {
4650
});
4751
});
4852

53+
it('should call callWithInternalUser with default args', async () => {
54+
const { mockRequest, callWithInternalUserSpy } = getMockRequest();
55+
const { internalClient } = await setupRequest(mockRequest);
56+
await internalClient.search({
57+
index: 'apm-*',
58+
body: { foo: 'bar' }
59+
} as any);
60+
expect(callWithInternalUserSpy).toHaveBeenCalledWith('search', {
61+
index: 'apm-*',
62+
body: {
63+
foo: 'bar',
64+
query: {
65+
bool: {
66+
filter: [{ range: { 'observer.version_major': { gte: 7 } } }]
67+
}
68+
}
69+
},
70+
ignore_throttled: true
71+
});
72+
});
73+
4974
describe('observer.version_major filter', () => {
5075
describe('if index is apm-*', () => {
5176
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
@@ -36,7 +36,8 @@ export async function setupRequest(req: Legacy.Request) {
3636
start: moment.utc(query.start).valueOf(),
3737
end: moment.utc(query.end).valueOf(),
3838
uiFiltersES: await decodeUiFilters(server, query.uiFilters),
39-
client: getESClient(req),
39+
client: getESClient(req, { clientAsInternalUser: false }),
40+
internalClient: getESClient(req, { clientAsInternalUser: true }),
4041
config
4142
};
4243
}

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, config } = setup;
24+
const { internalClient, config } = setup;
2525

2626
const params: IndexDocumentParams<AgentConfiguration> = {
2727
type: '_doc',
@@ -45,5 +45,5 @@ export async function createOrUpdateConfiguration({
4545
params.id = configurationId;
4646
}
4747

48-
return client.index(params);
48+
return internalClient.index(params);
4949
}

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, config } = setup;
16+
const { internalClient, config } = setup;
1717

1818
const params = {
1919
refresh: 'wait_for',
2020
index: config.get<string>('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, config } = setup;
22+
const { internalClient, config } = 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);
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, config } = setup;
15+
const { internalClient, config } = setup;
1616

1717
const params = {
1818
index: config.get<string>('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)