Skip to content
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

[8.0] ClusterClient: do no filter auth headers (#122917) #123117

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/core/server/elasticsearch/client/cluster_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ describe('ClusterClient', () => {
});
});

it('creates a scoped facade with filtered auth headers', () => {
it('does not filter auth headers', () => {
const config = createConfig({
requestHeadersWhitelist: ['authorization'],
});
getAuthHeaders.mockReturnValue({
authorization: 'auth',
other: 'nope',
other: 'yep',
});

const clusterClient = new ClusterClient(config, logger, 'custom-type', getAuthHeaders);
Expand All @@ -160,7 +160,12 @@ describe('ClusterClient', () => {

expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: { ...DEFAULT_HEADERS, authorization: 'auth', 'x-opaque-id': expect.any(String) },
headers: {
...DEFAULT_HEADERS,
authorization: 'auth',
other: 'yep',
'x-opaque-id': expect.any(String),
},
});
});

Expand All @@ -170,7 +175,7 @@ describe('ClusterClient', () => {
});
getAuthHeaders.mockReturnValue({
authorization: 'auth',
other: 'nope',
other: 'yep',
});

const clusterClient = new ClusterClient(config, logger, 'custom-type', getAuthHeaders);
Expand All @@ -184,7 +189,12 @@ describe('ClusterClient', () => {

expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: { ...DEFAULT_HEADERS, authorization: 'auth', 'x-opaque-id': expect.any(String) },
headers: {
...DEFAULT_HEADERS,
authorization: 'auth',
other: 'yep',
'x-opaque-id': expect.any(String),
},
});
});

Expand Down
17 changes: 7 additions & 10 deletions src/core/server/elasticsearch/client/cluster_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ export interface ICustomClusterClient extends IClusterClient {
export class ClusterClient implements ICustomClusterClient {
public readonly asInternalUser: KibanaClient;
private readonly rootScopedClient: KibanaClient;
private readonly allowListHeaders: string[];

private isClosed = false;

constructor(
Expand All @@ -72,8 +70,6 @@ export class ClusterClient implements ICustomClusterClient {
getExecutionContext,
scoped: true,
});

this.allowListHeaders = ['x-opaque-id', ...this.config.requestHeadersWhitelist];
}

asScoped(request: ScopeableRequest) {
Expand All @@ -95,14 +91,15 @@ export class ClusterClient implements ICustomClusterClient {
private getScopedHeaders(request: ScopeableRequest): Headers {
let scopedHeaders: Headers;
if (isRealRequest(request)) {
const requestHeaders = ensureRawRequest(request).headers;
const requestHeaders = ensureRawRequest(request).headers ?? {};
const requestIdHeaders = isKibanaRequest(request) ? { 'x-opaque-id': request.id } : {};
const authHeaders = this.getAuthHeaders(request);
const authHeaders = this.getAuthHeaders(request) ?? {};

scopedHeaders = filterHeaders(
{ ...requestHeaders, ...requestIdHeaders, ...authHeaders },
this.allowListHeaders
);
scopedHeaders = {
...filterHeaders(requestHeaders, this.config.requestHeadersWhitelist),
...requestIdHeaders,
...authHeaders,
};
} else {
scopedHeaders = filterHeaders(request?.headers ?? {}, this.config.requestHeadersWhitelist);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/http/router/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function filterHeaders(
headers: Headers,
fieldsToKeep: string[],
fieldsToExclude: string[] = []
) {
): Headers {
const fieldsToExcludeNormalized = fieldsToExclude.map(normalizeHeaderField);
// Normalize list of headers we want to allow in upstream request
const fieldsToKeepNormalized = fieldsToKeep
Expand Down