@@ -26,6 +26,9 @@ import {
26
26
AuthToolkit ,
27
27
IKibanaResponse ,
28
28
} from 'kibana/server' ;
29
+ import HTTP from 'http' ;
30
+ import HTTPS from 'https' ;
31
+ import { PeerCertificate } from 'tls' ;
29
32
import { SecurityPluginConfigType } from '../../..' ;
30
33
import { SecuritySessionCookie } from '../../../session/security_cookie' ;
31
34
import { OpenIdAuthRoutes } from './routes' ;
@@ -34,8 +37,6 @@ import { callTokenEndpoint } from './helper';
34
37
import { composeNextUrlQeuryParam } from '../../../utils/next_url' ;
35
38
36
39
export interface OpenIdAuthConfig {
37
- ca ?: Buffer | undefined ;
38
- // checkServerIdentity: (host: string, cert: any) => void;
39
40
authorizationEndpoint ?: string ;
40
41
tokenEndpoint ?: string ;
41
42
endSessionEndpoint ?: string ;
@@ -44,12 +45,18 @@ export interface OpenIdAuthConfig {
44
45
authHeaderName ?: string ;
45
46
}
46
47
48
+ export interface WreckHttpsOptions {
49
+ ca ?: string | Buffer | Array < string | Buffer > ;
50
+ checkServerIdentity ?: ( host : string , cert : PeerCertificate ) => Error | undefined ;
51
+ }
52
+
47
53
export class OpenIdAuthentication extends AuthenticationType {
48
54
public readonly type : string = 'openid' ;
49
55
50
56
private openIdAuthConfig : OpenIdAuthConfig ;
51
57
private authHeaderName : string ;
52
58
private openIdConnectUrl : string ;
59
+ private wreckClient : typeof wreck ;
53
60
54
61
constructor (
55
62
config : SecurityPluginConfigType ,
@@ -60,15 +67,10 @@ export class OpenIdAuthentication extends AuthenticationType {
60
67
logger : Logger
61
68
) {
62
69
super ( config , sessionStorageFactory , router , esClient , core , logger ) ;
63
- this . openIdAuthConfig = { } ;
64
70
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 ( ) ;
71
72
73
+ this . openIdAuthConfig = { } ;
72
74
this . authHeaderName = this . config . openid ?. header || '' ;
73
75
this . openIdAuthConfig . authHeaderName = this . authHeaderName ;
74
76
@@ -84,7 +86,7 @@ export class OpenIdAuthentication extends AuthenticationType {
84
86
85
87
private async init ( ) {
86
88
try {
87
- const response = await wreck . get ( this . openIdConnectUrl , { } ) ;
89
+ const response = await this . wreckClient . get ( this . openIdConnectUrl ) ;
88
90
const payload = JSON . parse ( response . payload as string ) ;
89
91
90
92
this . openIdAuthConfig . authorizationEndpoint = payload . authorization_endpoint ;
@@ -97,7 +99,8 @@ export class OpenIdAuthentication extends AuthenticationType {
97
99
this . sessionStorageFactory ,
98
100
this . openIdAuthConfig ,
99
101
this . securityClient ,
100
- this . coreSetup
102
+ this . coreSetup ,
103
+ this . wreckClient
101
104
) ;
102
105
routes . setupRoutes ( ) ;
103
106
} catch ( error ) {
@@ -106,6 +109,32 @@ export class OpenIdAuthentication extends AuthenticationType {
106
109
}
107
110
}
108
111
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
+
109
138
requestIncludesAuthInfo ( request : KibanaRequest ) : boolean {
110
139
return request . headers . authorization ? true : false ;
111
140
}
@@ -151,7 +180,8 @@ export class OpenIdAuthentication extends AuthenticationType {
151
180
} ;
152
181
const refreshTokenResponse = await callTokenEndpoint (
153
182
this . openIdAuthConfig . tokenEndpoint ! ,
154
- query
183
+ query ,
184
+ this . wreckClient
155
185
) ;
156
186
157
187
// if no id_token from refresh token call, maybe the Idp doesn't allow refresh id_token
0 commit comments