Skip to content

Commit 53c9823

Browse files
Merge pull request #669 from zengyan-amazon/wreck-cert
Fix bug of missing some OIDC config
2 parents e760893 + 996cc33 commit 53c9823

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

server/auth/types/openid/helper.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ export function getBaseRedirectUrl(config: SecurityPluginConfigType, core: CoreS
4545
return `${protocol}://${host}:${port}`;
4646
}
4747

48-
export async function callTokenEndpoint(tokenEndpoint: string, query: any): Promise<TokenResponse> {
49-
const tokenResponse = await wreck.post(tokenEndpoint, {
48+
export async function callTokenEndpoint(
49+
tokenEndpoint: string,
50+
query: any,
51+
wreckClient: typeof wreck
52+
): Promise<TokenResponse> {
53+
const tokenResponse = await wreckClient.post(tokenEndpoint, {
5054
payload: stringify(query),
5155
headers: {
5256
'Content-Type': 'application/x-www-form-urlencoded',

server/auth/types/openid/openid_auth.ts

+42-12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import {
2626
AuthToolkit,
2727
IKibanaResponse,
2828
} from 'kibana/server';
29+
import HTTP from 'http';
30+
import HTTPS from 'https';
31+
import { PeerCertificate } from 'tls';
2932
import { SecurityPluginConfigType } from '../../..';
3033
import { SecuritySessionCookie } from '../../../session/security_cookie';
3134
import { OpenIdAuthRoutes } from './routes';
@@ -34,8 +37,6 @@ import { callTokenEndpoint } from './helper';
3437
import { composeNextUrlQeuryParam } from '../../../utils/next_url';
3538

3639
export interface OpenIdAuthConfig {
37-
ca?: Buffer | undefined;
38-
// checkServerIdentity: (host: string, cert: any) => void;
3940
authorizationEndpoint?: string;
4041
tokenEndpoint?: string;
4142
endSessionEndpoint?: string;
@@ -44,12 +45,18 @@ export interface OpenIdAuthConfig {
4445
authHeaderName?: string;
4546
}
4647

48+
export interface WreckHttpsOptions {
49+
ca?: string | Buffer | Array<string | Buffer>;
50+
checkServerIdentity?: (host: string, cert: PeerCertificate) => Error | undefined;
51+
}
52+
4753
export class OpenIdAuthentication extends AuthenticationType {
4854
public readonly type: string = 'openid';
4955

5056
private openIdAuthConfig: OpenIdAuthConfig;
5157
private authHeaderName: string;
5258
private openIdConnectUrl: string;
59+
private wreckClient: typeof wreck;
5360

5461
constructor(
5562
config: SecurityPluginConfigType,
@@ -60,15 +67,10 @@ export class OpenIdAuthentication extends AuthenticationType {
6067
logger: Logger
6168
) {
6269
super(config, sessionStorageFactory, router, esClient, core, logger);
63-
this.openIdAuthConfig = {};
6470

65-
if (this.config.openid?.root_ca) {
66-
this.openIdAuthConfig.ca = fs.readFileSync(this.config.openid.root_ca);
67-
}
68-
if (this.config.openid?.verify_hostnames) {
69-
logger.debug(`openId auth 'verify_hostnames' option is on.`);
70-
}
71+
this.wreckClient = this.createWreckClient();
7172

73+
this.openIdAuthConfig = {};
7274
this.authHeaderName = this.config.openid?.header || '';
7375
this.openIdAuthConfig.authHeaderName = this.authHeaderName;
7476

@@ -84,7 +86,7 @@ export class OpenIdAuthentication extends AuthenticationType {
8486

8587
private async init() {
8688
try {
87-
const response = await wreck.get(this.openIdConnectUrl, {});
89+
const response = await this.wreckClient.get(this.openIdConnectUrl);
8890
const payload = JSON.parse(response.payload as string);
8991

9092
this.openIdAuthConfig.authorizationEndpoint = payload.authorization_endpoint;
@@ -97,7 +99,8 @@ export class OpenIdAuthentication extends AuthenticationType {
9799
this.sessionStorageFactory,
98100
this.openIdAuthConfig,
99101
this.securityClient,
100-
this.coreSetup
102+
this.coreSetup,
103+
this.wreckClient
101104
);
102105
routes.setupRoutes();
103106
} catch (error) {
@@ -106,6 +109,32 @@ export class OpenIdAuthentication extends AuthenticationType {
106109
}
107110
}
108111

112+
private createWreckClient(): typeof wreck {
113+
const wreckHttpsOption: WreckHttpsOptions = {};
114+
if (this.config.openid?.root_ca) {
115+
wreckHttpsOption.ca = [fs.readFileSync(this.config.openid.root_ca)];
116+
}
117+
if (this.config.openid?.verify_hostnames === false) {
118+
this.logger.debug(`openId auth 'verify_hostnames' option is off.`);
119+
wreckHttpsOption.checkServerIdentity = (host: string, cert: PeerCertificate) => {
120+
return undefined;
121+
};
122+
}
123+
if (Object.keys(wreckHttpsOption).length > 0) {
124+
return wreck.defaults({
125+
agents: {
126+
http: new HTTP.Agent(),
127+
https: new HTTPS.Agent(wreckHttpsOption),
128+
httpsAllowUnauthorized: new HTTPS.Agent({
129+
rejectUnauthorized: false,
130+
}),
131+
},
132+
});
133+
} else {
134+
return wreck;
135+
}
136+
}
137+
109138
requestIncludesAuthInfo(request: KibanaRequest): boolean {
110139
return request.headers.authorization ? true : false;
111140
}
@@ -151,7 +180,8 @@ export class OpenIdAuthentication extends AuthenticationType {
151180
};
152181
const refreshTokenResponse = await callTokenEndpoint(
153182
this.openIdAuthConfig.tokenEndpoint!,
154-
query
183+
query,
184+
this.wreckClient
155185
);
156186

157187
// if no id_token from refresh token call, maybe the Idp doesn't allow refresh id_token

server/auth/types/openid/routes.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import { schema } from '@kbn/config-schema';
1616
import { randomString } from '@hapi/cryptiles';
1717
import { stringify } from 'querystring';
18+
import wreck from '@hapi/wreck';
1819
import {
1920
IRouter,
2021
SessionStorageFactory,
@@ -38,7 +39,8 @@ export class OpenIdAuthRoutes {
3839
private readonly sessionStorageFactory: SessionStorageFactory<SecuritySessionCookie>,
3940
private readonly openIdAuthConfig: OpenIdAuthConfig,
4041
private readonly securityClient: SecurityClient,
41-
private readonly core: CoreSetup
42+
private readonly core: CoreSetup,
43+
private readonly wreckClient: typeof wreck
4244
) {}
4345

4446
private redirectToLogin(request: KibanaRequest, response: KibanaResponseFactory) {
@@ -136,7 +138,8 @@ export class OpenIdAuthRoutes {
136138
try {
137139
const tokenResponse = await callTokenEndpoint(
138140
this.openIdAuthConfig.tokenEndpoint!,
139-
query
141+
query,
142+
this.wreckClient
140143
);
141144

142145
const user = await this.securityClient.authenticateWithHeader(

0 commit comments

Comments
 (0)