oidc-guide.asciidoc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. [role="xpack"]
  2. [[oidc-guide]]
  3. == Configuring single sign-on to the {stack} using OpenID Connect
  4. The Elastic Stack supports single sign-on (SSO) using OpenID Connect via {kib} using
  5. {es} as the backend service that holds most of the functionality. {kib} and {es}
  6. together represent an OpenID Connect Relying Party (RP) that supports the authorization code flow and implicit flow as these are defined in the OpenID Connect specification.
  7. This guide assumes that you have an OpenID Connect Provider where the
  8. Elastic Stack Relying Party will be registered.
  9. NOTE: The OpenID Connect realm support in {kib} is designed with the expectation that it
  10. will be the primary authentication method for the users of that {kib} instance. The
  11. <<oidc-configure-kibana>> section describes what this entails and how you can set it up to support
  12. other realms if necessary.
  13. [[oidc-guide-op]]
  14. === The OpenID Connect Provider
  15. The OpenID Connect Provider (OP) is the entity in OpenID Connect that is responsible for
  16. authenticating the user and for granting the necessary tokens with the authentication and
  17. user information to be consumed by the Relying Parties.
  18. In order for the Elastic Stack to be able use your OpenID Connect Provider for authentication,
  19. a trust relationship needs to be established between the OP and the RP. In the OpenID Connect
  20. Provider, this means registering the RP as a client. OpenID Connect defines a dynamic client
  21. registration protocol but this is usually geared towards real-time client registration and
  22. not the trust establishment process for cross security domain single sign on. All OPs will
  23. also allow for the manual registration of an RP as a client, via a user interface or (less often)
  24. via the consumption of a metadata document.
  25. The process for registering the Elastic Stack RP will be different from OP to OP and following
  26. the provider's relevant documentation is prudent. The information for the
  27. RP that you commonly need to provide for registration are the following:
  28. - `Relying Party Name`: An arbitrary identifier for the relying party. Neither the specification
  29. nor the Elastic Stack implementation impose any constraints on this value.
  30. - `Redirect URI`: This is the URI where the OP will redirect the user's browser after authentication. The
  31. appropriate value for this will depend on your setup and whether or not {kib} sits behind a proxy or
  32. load balancer. It will typically be +$\{kibana-url}/api/security/oidc/callback+ (for the authorization code flow) or +$\{kibana-url}/api/security/oidc/implicit+ (for the implicit flow) where _$\{kibana-url}_ is the base URL for your {kib} instance. You might also see this
  33. called `Callback URI`.
  34. At the end of the registration process, the OP will assign a Client Identifier and a Client Secret for the RP ({stack}) to use.
  35. Note these two values as they will be used in the {es} configuration.
  36. [[oidc-elasticsearch-authentication]]
  37. === Configure {es} for OpenID Connect authentication
  38. The following is a summary of the configuration steps required in order to enable authentication
  39. using OpenID Connect in {es}:
  40. . <<oidc-enable-http,Enable SSL/TLS for HTTP>>
  41. . <<oidc-enable-token,Enable the Token Service>>
  42. . <<oidc-create-realm,Create one or more OpenID Connect realms>>
  43. . <<oidc-role-mappings,Configure role mappings>>
  44. [[oidc-enable-http]]
  45. ==== Enable TLS for HTTP
  46. If your {es} cluster is operating in production mode, then you must
  47. configure the HTTP interface to use SSL/TLS before you can enable OpenID Connect
  48. authentication.
  49. For more information, see
  50. <<encrypt-http-communication>>.
  51. [[oidc-enable-token]]
  52. ==== Enable the token service
  53. The {es} OpenID Connect implementation makes use of the {es} Token Service. This service
  54. is automatically enabled if you configure TLS on the HTTP interface, and can be
  55. explicitly configured by including the following in your `elasticsearch.yml` file:
  56. [source, yaml]
  57. ------------------------------------------------------------
  58. xpack.security.authc.token.enabled: true
  59. ------------------------------------------------------------
  60. [[oidc-create-realm]]
  61. ==== Create an OpenID Connect realm
  62. OpenID Connect based authentication is enabled by configuring the appropriate realm within
  63. the authentication chain for {es}.
  64. This realm has a few mandatory settings, and a number of optional settings.
  65. The available settings are described in detail in
  66. <<ref-oidc-settings>>. This
  67. guide will explore the most common settings.
  68. Create an OpenID Connect (the realm type is `oidc`) realm in your `elasticsearch.yml` file
  69. similar to what is shown below:
  70. NOTE: The values used below are meant to be an example and are not intended to apply to
  71. every use case. The details below the configuration snippet provide insights and suggestions
  72. to help you pick the proper values, depending on your OP configuration.
  73. [source, yaml]
  74. -------------------------------------------------------------------------------------
  75. xpack.security.authc.realms.oidc.oidc1:
  76. order: 2
  77. rp.client_id: "the_client_id"
  78. rp.response_type: code
  79. rp.redirect_uri: "https://kibana.example.org:5601/api/security/oidc/callback"
  80. op.issuer: "https://op.example.org"
  81. op.authorization_endpoint: "https://op.example.org/oauth2/v1/authorize"
  82. op.token_endpoint: "https://op.example.org/oauth2/v1/token"
  83. op.jwkset_path: oidc/jwkset.json
  84. op.userinfo_endpoint: "https://op.example.org/oauth2/v1/userinfo"
  85. op.endsession_endpoint: "https://op.example.org/oauth2/v1/logout"
  86. rp.post_logout_redirect_uri: "https://kibana.example.org:5601/security/logged_out"
  87. claims.principal: sub
  88. claims.groups: "http://example.info/claims/groups"
  89. -------------------------------------------------------------------------------------
  90. The configuration values used in the example above are:
  91. xpack.security.authc.realms.oidc.oidc1::
  92. This defines a new `oidc` authentication realm named "oidc1".
  93. See <<realms>> for more explanation of realms.
  94. order::
  95. You should define a unique order on each realm in your authentication chain.
  96. It is recommended that the OpenID Connect realm be at the bottom of your authentication
  97. chain (that is, that it has the _highest_ order).
  98. rp.client_id::
  99. This, usually opaque, arbitrary string, is the Client Identifier that was assigned to the Elastic Stack RP by the OP upon
  100. registration.
  101. rp.response_type::
  102. This is an identifier that controls which OpenID Connect authentication flow this RP supports and also
  103. which flow this RP requests the OP should follow. Supported values are
  104. - `code`, which means that the RP wants to use the Authorization Code flow. If your OP supports the
  105. Authorization Code flow, you should select this instead of the Implicit Flow.
  106. - `id_token token` which means that the RP wants to use the Implicit flow and we also request an oAuth2
  107. access token from the OP, that we can potentially use for follow up requests ( UserInfo ). This
  108. should be selected if the OP offers a UserInfo endpoint in its configuration, or if you know that
  109. the claims you will need to use for role mapping are not available in the ID Token.
  110. - `id_token` which means that the RP wants to use the Implicit flow, but is not interested in getting
  111. an oAuth2 token too. Select this if you are certain that all necessary claims will be contained in
  112. the ID Token or if the OP doesn't offer a User Info endpoint.
  113. rp.redirect_uri::
  114. The redirect URI where the OP will redirect the browser after authentication. This needs to be
  115. _exactly_ the same as the one <<oidc-guide-op, configured with the OP upon registration>> and will
  116. typically be +$\{kibana-url}/api/security/oidc/callback+ where _$\{kibana-url}_ is the base URL for your {kib} instance
  117. op.issuer::
  118. A verifiable Identifier for your OpenID Connect Provider. An Issuer Identifier is usually a case sensitive URL.
  119. The value for this setting should be provided by your OpenID Connect Provider.
  120. op.authorization_endpoint::
  121. The URL for the Authorization Endpoint in the OP. This is where the user's browser
  122. will be redirected to start the authentication process. The value for this setting should be provided by your
  123. OpenID Connect Provider.
  124. op.token_endpoint::
  125. The URL for the Token Endpoint in the OpenID Connect Provider. This is the endpoint where
  126. {es} will send a request to exchange the code for an ID Token. This setting is optional when
  127. you use the implicit flow. The value for this setting should be provided by your OpenID Connect Provider.
  128. op.jwkset_path::
  129. The path to a file or a URL containing a JSON Web Key Set with the key material that the OpenID Connect
  130. Provider uses for signing tokens and claims responses. If a path is set, it is resolved relative to the {es}
  131. config directory.
  132. {es} will automatically monitor this file for changes and will reload the configuration whenever
  133. it is updated. Your OpenID Connect Provider should provide you with this file or a URL where it is available.
  134. op.userinfo_endpoint::
  135. (Optional) The URL for the UserInfo Endpoint in the OpenID Connect Provider. This is the endpoint of the OP that
  136. can be queried to get further user information, if required. The value for this setting should be provided by your
  137. OpenID Connect Provider.
  138. op.endsession_endpoint::
  139. (Optional) The URL to the End Session Endpoint in the OpenID Connect Provider. This is the endpoint where the user's
  140. browser will be redirected after local logout, if the realm is configured for RP initiated Single Logout and
  141. the OP supports it. The value for this setting should be provided by your OpenID Connect Provider.
  142. rp.post_logout_redirect_uri::
  143. (Optional) The Redirect URL where the OpenID Connect Provider should redirect the user after a
  144. successful Single Logout (assuming `op.endsession_endpoint` above is also set). This should be set to a value that
  145. will not trigger a new OpenID Connect Authentication, such as +$\{kibana-url}/security/logged_out+ or
  146. +$\{kibana-url}/login?msg=LOGGED_OUT+ where _$\{kibana-url}_ is the base URL for your {kib} instance.
  147. claims.principal:: See <<oidc-claims-mappings>>.
  148. claims.groups:: See <<oidc-claims-mappings>>.
  149. A final piece of configuration of the OpenID Connect realm is to set the `Client Secret` that was assigned
  150. to the RP during registration in the OP. This is a secure setting and as such is not defined in the realm
  151. configuration in `elasticsearch.yml` but added to the
  152. <<secure-settings,elasticsearch keystore>>.
  153. For instance
  154. [source,sh]
  155. ----
  156. bin/elasticsearch-keystore add xpack.security.authc.realms.oidc.oidc1.rp.client_secret
  157. ----
  158. NOTE: Changes to the `client_secret` requires a restart of the {es} nodes to pick up the change.
  159. NOTE: According to the OpenID Connect specification, the OP should also make their configuration
  160. available at a well known URL, which is the concatenation of their `Issuer` value with the
  161. `.well-known/openid-configuration` string. For example: `https://op.org.com/.well-known/openid-configuration`
  162. That document should contain all the necessary information to configure the OpenID Connect realm in {es}.
  163. [[oidc-claims-mappings]]
  164. ==== Claims mapping
  165. ===== Claims and scopes
  166. When authenticating to {kib} using OpenID Connect, the OP will provide information about the user
  167. in the form of OpenID Connect Claims, that can be included either in the ID Token, or be retrieved from the
  168. UserInfo endpoint of the OP. The claim is defined as a piece of information asserted by the OP
  169. for the authenticated user. Simply put, a claim is a name/value pair that contains information about
  170. the user. Related to claims, we also have the notion of OpenID Connect Scopes. Scopes are identifiers
  171. that are used to request access to specific lists of claims. The standard defines a set of scope
  172. identifiers that can be requested. The only mandatory one is `openid`, while commonly used ones are
  173. `profile` and `email`. The `profile` scope requests access to the `name`,`family_name`,`given_name`,`middle_name`,`nickname`,
  174. `preferred_username`,`profile`,`picture`,`website`,`gender`,`birthdate`,`zoneinfo`,`locale`, and `updated_at` claims.
  175. The `email` scope requests access to the `email` and `email_verified` claims. The process is that
  176. the RP requests specific scopes during the authentication request. If the OP Privacy Policy
  177. allows it and the authenticating user consents to it, the related claims are returned to the
  178. RP (either in the ID Token or as a UserInfo response).
  179. The list of the supported claims will vary depending on the OP you are using, but you can expect
  180. the https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims[Standard Claims] to be
  181. largely supported.
  182. [[oidc-claim-to-property]]
  183. ===== Mapping claims to user properties
  184. The goal of claims mapping is to configure {es} in such a way as to be able to map the values of
  185. specified returned claims to one of the <<oidc-user-properties, user properties>> that are supported
  186. by {es}. These user properties are then utilized to identify the user in the {kib} UI or the audit
  187. logs, and can also be used to create <<oidc-role-mappings, role mapping>> rules.
  188. The recommended steps for configuring OpenID Claims mapping are as follows:
  189. . Consult your OP configuration to see what claims it might support. Note that
  190. the list provided in the OP's metadata or in the configuration page of the OP
  191. is a list of potentially supported claims. However, for privacy reasons it might
  192. not be a complete one, or not all supported claims will be available for all
  193. authenticated users.
  194. . Read through the list of <<oidc-user-properties, user properties>> that {es}
  195. supports, and decide which of them are useful to you, and can be provided by
  196. your OP in the form of claims. At a _minimum_, the `principal` user property
  197. is required.
  198. . Configure your OP to "release" those claims to your {stack} Relying
  199. party. This process greatly varies by provider. You can use a static
  200. configuration while others will support that the RP requests the scopes that
  201. correspond to the claims to be "released" on authentication time. See
  202. <<ref-oidc-settings,`rp.requested_scopes`>> for details about how
  203. to configure the scopes to request. To ensure interoperability and minimize
  204. the errors, you should only request scopes that the OP supports, and which you
  205. intend to map to {es} user properties.
  206. NOTE: You can only map claims with values that are strings, numbers, boolean values or an array
  207. of the aforementioned.
  208. . Configure the OpenID Connect realm in {es} to associate the {es} user properties (see
  209. <<oidc-user-properties, the listing>> below), to the name of the claims that your
  210. OP will release. In the example above, we have configured the `principal` and
  211. `groups` user properties as follows:
  212. .. `claims.principal: sub` : This instructs {es} to look for the OpenID Connect claim named `sub`
  213. in the ID Token that the OP issued for the user ( or in the UserInfo response ) and assign the
  214. value of this claim to the `principal` user property. `sub` is a commonly used claim for the
  215. principal property as it is an identifier of the user in the OP and it is also a required
  216. claim of the ID Token, thus offering guarantees that it will be available. It is, however,
  217. only used as an example here, the OP may provide another claim that is a better fit for your needs.
  218. .. `claims.groups: "http://example.info/claims/groups"` : Similarly, this instructs {es} to look
  219. for the claim with the name `http://example.info/claims/groups` (note that this is a URI - an
  220. identifier, treated as a string and not a URL pointing to a location that will be retrieved)
  221. either in the ID Token or in the UserInfo response, and map the value(s) of it to the user
  222. property `groups` in {es}. There is no standard claim in the specification that is used for
  223. expressing roles or group memberships of the authenticated user in the OP, so the name of the
  224. claim that should be mapped here, will vary greatly between providers. Consult your OP
  225. documentation for more details.
  226. [[oidc-user-properties]]
  227. ===== {es} user properties
  228. The {es} OpenID Connect realm can be configured to map OpenID Connect claims to the
  229. following properties on the authenticated user:
  230. principal:: _(Required)_
  231. This is the _username_ that will be applied to a user that authenticates
  232. against this realm.
  233. The `principal` appears in places such as the {es} audit logs.
  234. NOTE: If the principal property fails to be mapped from a claim, the authentication fails.
  235. groups:: _(Recommended)_
  236. If you wish to use your OP's concept of groups or roles as the basis for a
  237. user's {es} privileges, you should map them with this property.
  238. The `groups` are passed directly to your <<oidc-role-mappings, role mapping rules>>.
  239. name:: _(Optional)_ The user's full name.
  240. mail:: _(Optional)_ The user's email address.
  241. dn:: _(Optional)_ The user's X.500 _Distinguished Name_.
  242. ===== Extracting partial values from OpenID Connect claims
  243. There are some occasions where the value of a claim may contain more information
  244. than you wish to use within {es}. A common example of this is one where the
  245. OP works exclusively with email addresses, but you would like the user's
  246. `principal` to use the _local-name_ part of the email address.
  247. For example if their email address was `james.wong@staff.example.com`, then you
  248. would like their principal to simply be `james.wong`.
  249. This can be achieved using the `claim_patterns` setting in the {es}
  250. realm, as demonstrated in the realm configuration below:
  251. [source, yaml]
  252. -------------------------------------------------------------------------------------
  253. xpack.security.authc.realms.oidc.oidc1:
  254. order: 2
  255. rp.client_id: "the_client_id"
  256. rp.response_type: code
  257. rp.redirect_uri: "https://kibana.example.org:5601/api/security/oidc/callback"
  258. op.authorization_endpoint: "https://op.example.org/oauth2/v1/authorize"
  259. op.token_endpoint: "https://op.example.org/oauth2/v1/token"
  260. op.userinfo_endpoint: "https://op.example.org/oauth2/v1/userinfo"
  261. op.endsession_endpoint: "https://op.example.org/oauth2/v1/logout"
  262. op.issuer: "https://op.example.org"
  263. op.jwkset_path: oidc/jwkset.json
  264. claims.principal: email_verified
  265. claim_patterns.principal: "^([^@]+)@staff\\.example\\.com$"
  266. -------------------------------------------------------------------------------------
  267. In this case, the user's `principal` is mapped from the `email_verified` claim, but a
  268. regular expression is applied to the value before it is assigned to the user.
  269. If the regular expression matches, then the result of the first group is used as the
  270. effective value. If the regular expression does not match then the claim
  271. mapping fails.
  272. In this example, the email address must belong to the `staff.example.com` domain,
  273. and then the local-part (anything before the `@`) is used as the principal.
  274. Any users who try to login using a different email domain will fail because the
  275. regular expression will not match against their email address, and thus their
  276. principal user property - which is mandatory - will not be populated.
  277. IMPORTANT: Small mistakes in these regular expressions can have significant
  278. security consequences. For example, if we accidentally left off the trailing
  279. `$` from the example above, then we would match any email address where the
  280. domain starts with `staff.example.com`, and this would accept an email
  281. address such as `admin@staff.example.com.attacker.net`. It is important that
  282. you make sure your regular expressions are as precise as possible so that
  283. you do not inadvertently open an avenue for user impersonation attacks.
  284. [[third-party-login]]
  285. ==== Third party initiated single sign-on
  286. The Open ID Connect realm in {es} supports 3rd party initiated login as described in the
  287. https://openid.net/specs/openid-connect-core-1_0.html#ThirdPartyInitiatedLogin[relevant specification].
  288. This allows the OP itself or another, third party other than the RP, to initiate the authentication
  289. process while requesting the OP to be used for the authentication. Please note that the Elastic
  290. Stack RP should already be configured for this OP, in order for this process to succeed.
  291. [[oidc-logout]]
  292. ==== OpenID Connect Logout
  293. The OpenID Connect realm in {es} supports RP-Initiated Logout Functionality as
  294. described in the
  295. https://openid.net/specs/openid-connect-session-1_0.html#RPLogout[relevant part of the specification]
  296. In this process, the OpenID Connect RP (the Elastic Stack in this case) will redirect the user's
  297. browser to predefined URL of the OP after successfully completing a local logout. The OP can then
  298. logout the user also, depending on the configuration, and should finally redirect the user back to the
  299. RP. The `op.endsession_endpoint` in the realm configuration determines the URL in the OP that the browser
  300. will be redirected to. The `rp.post_logout_redirect_uri` setting determines the URL to redirect
  301. the user back to after the OP logs them out.
  302. When configuring `rp.post_logout_redirect_uri`, care should be taken to not point this to a URL that
  303. will trigger re-authentication of the user. For instance, when using OpenID Connect to support
  304. single sign-on to {kib}, this could be set to either +$\{kibana-url}/security/logged_out+, which will show a
  305. user-friendly message to the user or +$\{kibana-url}/login?msg=LOGGED_OUT+ which will take the user to the login selector in {kib}.
  306. [[oidc-ssl-config]]
  307. ==== OpenID Connect Realm SSL Configuration
  308. OpenID Connect depends on TLS to provide security properties such as encryption in transit and endpoint authentication. The RP
  309. is required to establish back-channel communication with the OP in order to exchange the code for an ID Token during the
  310. Authorization code grant flow and in order to get additional user information from the UserInfo endpoint. Furthermore, if
  311. you configure `op.jwks_path` as a URL, {es} will need to get the OP's signing keys from the file hosted there. As such, it is
  312. important that {es} can validate and trust the server certificate that the OP uses for TLS. Since the system truststore is
  313. used for the client context of outgoing https connections, if your OP is using a certificate from a trusted CA, no additional
  314. configuration is needed.
  315. However, if the issuer of your OP's certificate is not trusted by the JVM on which {es} is running (e.g it uses a organization CA), then you must configure
  316. {es} to trust that CA. Assuming that you have the CA certificate that has signed the certificate that the OP uses for TLS
  317. stored in the /oidc/company-ca.pem` file stored in the configuration directory of {es}, you need to set the following
  318. property in the realm configuration:
  319. [source, yaml]
  320. -------------------------------------------------------------------------------------
  321. xpack.security.authc.realms.oidc.oidc1:
  322. order: 1
  323. ...
  324. ssl.certificate_authorities: ["/oidc/company-ca.pem"]
  325. -------------------------------------------------------------------------------------
  326. [[oidc-role-mappings]]
  327. === Configuring role mappings
  328. When a user authenticates using OpenID Connect, they are identified to the Elastic Stack,
  329. but this does not automatically grant them access to perform any actions or
  330. access any data.
  331. Your OpenID Connect users cannot do anything until they are assigned roles. This can be done
  332. through either the
  333. <<security-api-put-role-mapping,add role mapping API>> or with
  334. <<authorization_realms,authorization realms>>.
  335. NOTE: You cannot use <<mapping-roles-file,role mapping files>>
  336. to grant roles to users authenticating via OpenID Connect.
  337. This is an example of a simple role mapping that grants the `example_role` role
  338. to any user who authenticates against the `oidc1` OpenID Connect realm:
  339. [source,console]
  340. --------------------------------------------------
  341. PUT /_security/role_mapping/oidc-example
  342. {
  343. "roles": [ "example_role" ], <1>
  344. "enabled": true,
  345. "rules": {
  346. "field": { "realm.name": "oidc1" }
  347. }
  348. }
  349. --------------------------------------------------
  350. <1> The `example_role` role is *not* a builtin Elasticsearch role.
  351. This example assumes that you have created a custom role of your own, with
  352. appropriate access to your <<roles-indices-priv,data streams, indices,>> and
  353. {kibana-ref}/kibana-privileges.html#kibana-feature-privileges[Kibana features].
  354. The user properties that are mapped via the realm configuration are used to process
  355. role mapping rules, and these rules determine which roles a user is granted.
  356. The user fields that are provided to the role
  357. mapping are derived from the OpenID Connect claims as follows:
  358. - `username`: The `principal` user property
  359. - `dn`: The `dn` user property
  360. - `groups`: The `groups` user property
  361. - `metadata`: See <<oidc-user-metadata>>
  362. For more information, see <<mapping-roles>> and
  363. <<security-role-mapping-apis>>.
  364. If your OP has the ability to provide groups or roles to RPs via tha use of
  365. an OpenID Claim, then you should map this claim to the `claims.groups` setting in
  366. the {es} realm (see <<oidc-claim-to-property>>), and then make use of it in a role mapping
  367. as per the example below.
  368. This mapping grants the {es} `finance_data` role, to any users who authenticate
  369. via the `oidc1` realm with the `finance-team` group membership.
  370. [source,console]
  371. --------------------------------------------------
  372. PUT /_security/role_mapping/oidc-finance
  373. {
  374. "roles": [ "finance_data" ],
  375. "enabled": true,
  376. "rules": { "all": [
  377. { "field": { "realm.name": "oidc1" } },
  378. { "field": { "groups": "finance-team" } }
  379. ] }
  380. }
  381. --------------------------------------------------
  382. If your users also exist in a repository that can be directly accessed by {es}
  383. (such as an LDAP directory) then you can use
  384. <<authorization_realms, authorization realms>> instead of role mappings.
  385. In this case, you perform the following steps:
  386. 1. In your OpenID Connect realm, assign a claim to act as the lookup userid,
  387. by configuring the `claims.principal` setting.
  388. 2. Create a new realm that can look up users from your local repository (e.g. an
  389. `ldap` realm)
  390. 3. In your OpenID Connect realm, set `authorization_realms` to the name of the
  391. realm you created in step 2.
  392. [[oidc-user-metadata]]
  393. === User metadata
  394. By default users who authenticate via OpenID Connect will have some additional metadata
  395. fields. These fields will include every OpenID Claim that is provided in the authentication response
  396. (regardless of whether it is mapped to an {es} user property). For example,
  397. in the metadata field `oidc(claim_name)`, "claim_name" is the name of the
  398. claim as it was contained in the ID Token or in the User Info response. Note that these will
  399. include all the https://openid.net/specs/openid-connect-core-1_0.html#IDToken[ID Token claims]
  400. that pertain to the authentication event, rather than the user themselves.
  401. This behaviour can be disabled by adding `populate_user_metadata: false` as
  402. a setting in the oidc realm.
  403. [[oidc-configure-kibana]]
  404. === Configuring {kib}
  405. OpenID Connect authentication in {kib} requires a small number of additional settings
  406. in addition to the standard {kib} security configuration. The
  407. {kibana-ref}/using-kibana-with-security.html[{kib} security documentation]
  408. provides details on the available configuration options that you can apply.
  409. In particular, since your {es} nodes have been configured to use TLS on the HTTP
  410. interface, you must configure {kib} to use a `https` URL to connect to {es}, and
  411. you may need to configure `elasticsearch.ssl.certificateAuthorities` to trust
  412. the certificates that {es} has been configured to use.
  413. OpenID Connect authentication in {kib} is subject to the following timeout settings in
  414. `kibana.yml`:
  415. - {kibana-ref}/xpack-security-session-management.html#session-idle-timeout[`xpack.security.session.idleTimeout`]
  416. - {kibana-ref}/xpack-security-session-management.html#session-lifespan[`xpack.security.session.lifespan`]
  417. You may want to adjust these timeouts based on your security requirements.
  418. The three additional settings that are required for OpenID Connect support are shown below:
  419. [source, yaml]
  420. ------------------------------------------------------------
  421. xpack.security.authc.providers:
  422. oidc.oidc1:
  423. order: 0
  424. realm: "oidc1"
  425. ------------------------------------------------------------
  426. The configuration values used in the example above are:
  427. `xpack.security.authc.providers`::
  428. Add `oidc` provider to instruct {kib} to use OpenID Connect single sign-on as the
  429. authentication method. This instructs Kibana to attempt to initiate an SSO flow
  430. everytime a user attempts to access a URL in Kibana, if the user is not already
  431. authenticated. If you also want to allow users to login with a username and password,
  432. you must enable the `basic` authentication provider too. For example:
  433. [source, yaml]
  434. ------------------------------------------------------------
  435. xpack.security.authc.providers:
  436. oidc.oidc1:
  437. order: 0
  438. realm: "oidc1"
  439. basic.basic1:
  440. order: 1
  441. ------------------------------------------------------------
  442. This will allow users that haven't already authenticated with OpenID Connect to
  443. log in using the {kib} login form.
  444. `xpack.security.authc.providers.oidc.<provider-name>.realm`::
  445. The name of the OpenID Connect realm in {es} that should handle authentication
  446. for this Kibana instance.
  447. [[oidc-without-kibana]]
  448. === OpenID Connect without {kib}
  449. The OpenID Connect realm is designed to allow users to authenticate to {kib} and as
  450. such, most of the parts of the guide above make the assumption that {kib} is used.
  451. This section describes how a custom web application could use the relevant OpenID
  452. Connect REST APIs in order to authenticate the users to {es}, with OpenID Connect.
  453. Single sign-on realms such as OpenID Connect and SAML make use of the Token Service in
  454. {es} and in principle exchange a SAML or OpenID Connect Authentication response for
  455. an {es} access token and a refresh token. The access token is used as credentials for subsequent calls to {es}. The
  456. refresh token enables the user to get new {es} access tokens after the current one
  457. expires.
  458. NOTE: The {es} Token Service can be seen as a minimal oAuth2 authorization server
  459. and the access token and refresh token mentioned above are tokens that pertain
  460. _only_ to this authorization server. They are generated and consumed _only_ by {es}
  461. and are in no way related to the tokens ( access token and ID Token ) that the
  462. OpenID Connect Provider issues.
  463. ==== Register the RP with an OpenID Connect Provider
  464. The Relying Party ( {es} and the custom web app ) will need to be registered as
  465. client with the OpenID Connect Provider. Note that when registering the
  466. `Redirect URI`, it needs to be a URL in the custom web app.
  467. ==== OpenID Connect Realm
  468. An OpenID Connect realm needs to be created and configured accordingly
  469. in {es}. See <<oidc-elasticsearch-authentication>>
  470. ==== Service Account user for accessing the APIs
  471. The realm is designed with the assumption that there needs to be a privileged entity
  472. acting as an authentication proxy. In this case, the custom web application is the
  473. authentication proxy handling the authentication of end users ( more correctly,
  474. "delegating" the authentication to the OpenID Connect Provider ). The OpenID Connect
  475. APIs require authentication and the necessary authorization level for the authenticated
  476. user. For this reason, a Service Account user needs to be created and assigned a role
  477. that gives them the `manage_oidc` cluster privilege. The use of the `manage_token`
  478. cluster privilege will be necessary after the authentication takes place, so that the
  479. user can maintain access or be subsequently logged out.
  480. [source,console]
  481. --------------------------------------------------
  482. POST /_security/role/facilitator-role
  483. {
  484. "cluster" : ["manage_oidc", "manage_token"]
  485. }
  486. --------------------------------------------------
  487. [source,console]
  488. --------------------------------------------------
  489. POST /_security/user/facilitator
  490. {
  491. "password" : "<somePasswordHere>",
  492. "roles" : [ "facilitator-role"]
  493. }
  494. --------------------------------------------------
  495. ==== Handling the authentication flow
  496. On a high level, the custom web application would need to perform the following steps in order to
  497. authenticate a user with OpenID Connect:
  498. . Make an HTTP POST request to `_security/oidc/prepare`, authenticating as the `facilitator` user, using the name of the
  499. OpenID Connect realm in the {es} configuration in the request body. For more
  500. details, see
  501. <<security-api-oidc-prepare-authentication>>.
  502. +
  503. [source,console]
  504. --------------------------------------------------
  505. POST /_security/oidc/prepare
  506. {
  507. "realm" : "oidc1"
  508. }
  509. --------------------------------------------------
  510. +
  511. . Handle the response to `/_security/oidc/prepare`. The response from {es} will contain 3 parameters:
  512. `redirect`, `state`, `nonce`. The custom web application would need to store the values for `state`
  513. and `nonce` in the user's session (client side in a cookie or server side if session information is
  514. persisted this way) and redirect the user's browser to the URL that will be contained in the
  515. `redirect` value.
  516. . Handle a subsequent response from the OP. After the user is successfully authenticated with the
  517. OpenID Connect Provider, they will be redirected back to the callback/redirect URI. Upon receiving
  518. this HTTP GET request, the custom web app will need to make an HTTP POST request to
  519. `_security/oidc/authenticate`, again - authenticating as the `facilitator` user - passing the URL
  520. where the user's browser was redirected to, as a parameter, along with the
  521. values for `nonce` and `state` it had saved in the user's session previously. If more than one
  522. OpenID Connect realms are configured, the custom web app can specify the name of the realm to be
  523. used for handling this, but this parameter is optional. For more details, see
  524. <<security-api-oidc-authenticate>>.
  525. +
  526. [source,console]
  527. -----------------------------------------------------------------------
  528. POST /_security/oidc/authenticate
  529. {
  530. "redirect_uri" : "https://oidc-kibana.elastic.co:5603/api/security/oidc/callback?code=jtI3Ntt8v3_XvcLzCFGq&state=4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I",
  531. "state" : "4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I",
  532. "nonce" : "WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM",
  533. "realm" : "oidc1"
  534. }
  535. -----------------------------------------------------------------------
  536. // TEST[catch:unauthorized]
  537. +
  538. Elasticsearch will validate this and if all is correct will respond with an access token that can be used
  539. as a `Bearer` token for subsequent requests and a refresh token that can be later used to refresh the given
  540. access token as described in <<security-api-get-token>>.
  541. . At some point, if necessary, the custom web application can log the user out by using the
  542. <<security-api-oidc-logout,OIDC logout API>> passing the access token and refresh token as parameters. For example:
  543. +
  544. [source,console]
  545. --------------------------------------------------
  546. POST /_security/oidc/logout
  547. {
  548. "token" : "dGhpcyBpcyBub3QgYSByZWFsIHRva2VuIGJ1dCBpdCBpcyBvbmx5IHRlc3QgZGF0YS4gZG8gbm90IHRyeSB0byByZWFkIHRva2VuIQ==",
  549. "refresh_token": "vLBPvmAB6KvwvJZr27cS"
  550. }
  551. --------------------------------------------------
  552. // TEST[catch:request]
  553. +
  554. If the realm is configured accordingly, this may result in a response with a `redirect` parameter indicating where
  555. the user needs to be redirected in the OP in order to complete the logout process.