Skip to content

Commit 8ab58f9

Browse files
committed
Allow Transport Actions to indicate authN realm (elastic#45767)
This commit allows the Transport Actions for the SSO realms to indicate the realm that should be used to authenticate the constructed AuthenticationToken. This is useful in the case that many authentication realms of the same type have been configured and where the caller of the API(Kibana or a custom web app) already know which realm should be used so there is no need to iterate all the realms of the same type. The realm parameter is added in the relevant REST APIs as optional so as not to introduce any breaking change.
1 parent 040a810 commit 8ab58f9

File tree

24 files changed

+340
-108
lines changed

24 files changed

+340
-108
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ task verifyVersions {
179179
* after the backport of the backcompat code is complete.
180180
*/
181181

182-
boolean bwc_tests_enabled = true
183-
final String bwc_tests_disabled_issue = "" /* place a PR link here when committing bwc changes */
182+
boolean bwc_tests_enabled = false
183+
final String bwc_tests_disabled_issue = "https://github.com/elastic/elasticsearch/pull/45767"
184184
if (bwc_tests_enabled == false) {
185185
if (bwc_tests_disabled_issue.isEmpty()) {
186186
throw new GradleException("bwc_tests_disabled_issue must be set when bwc_tests_enabled == false")

x-pack/docs/en/rest-api/security/oidc-authenticate-api.asciidoc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,28 @@ and <<security-api-oidc-logout,OpenID Connect logout API>>
3131
==== {api-request-body-title}
3232

3333
`redirect_uri`::
34-
The URL to which the OpenID Connect Provider redirected the User Agent in
34+
(Required, string) The URL to which the OpenID Connect Provider redirected the User Agent in
3535
response to an authentication request, after a successful authentication. This
3636
URL is expected to be provided as-is (URL encoded), taken from the body of the
3737
response or as the value of a `Location` header in the response from the OpenID
3838
Connect Provider.
3939

4040
`state`::
41-
String value used to maintain state between the authentication request and the
41+
(Required, string) Used to maintain state between the authentication request and the
4242
response. This value needs to be the same as the one that was provided to the
4343
call to `/_security/oidc/prepare` earlier, or the one that was generated by {es}
4444
and included in the response to that call.
4545

4646
`nonce`::
47-
String value used to associate a Client session with an ID Token and to mitigate
47+
(Required, string) Used to associate a Client session with an ID Token and to mitigate
4848
replay attacks. This value needs to be the same as the one that was provided to
4949
the call to `/_security/oidc/prepare` earlier, or the one that was generated by
5050
{es} and included in the response to that call.
5151

52+
`realm`::
53+
(Optional, string) Used to identify the name of the OpenID Connect realm that should
54+
be used to authenticate this. Useful when multiple realms have been defined.
55+
5256
[[security-api-oidc-authenticate-example]]
5357
==== {api-examples-title}
5458

@@ -63,7 +67,8 @@ POST /_security/oidc/authenticate
6367
{
6468
"redirect_uri" : "https://oidc-kibana.elastic.co:5603/api/security/v1/oidc?code=jtI3Ntt8v3_XvcLzCFGq&state=4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I",
6569
"state" : "4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I",
66-
"nonce" : "WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM"
70+
"nonce" : "WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM",
71+
"realm" : "oidc1"
6772
}
6873
--------------------------------------------------
6974
// CONSOLE

x-pack/docs/en/rest-api/security/oidc-logout-api.asciidoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ and
2929
==== {api-request-body-title}
3030

3131
`access_token`::
32-
The value of the access token to be invalidated as part of the logout.
32+
(Required, string) The value of the access token to be invalidated as part of the logout.
3333

3434
`refresh_token`::
35-
(Optional) The value of the refresh token to be invalidated as part of the logout.
35+
(Optional, string) The value of the refresh token to be invalidated as part of the logout.
3636

3737

3838
[[security-api-oidc-logout-example]]

x-pack/docs/en/rest-api/security/oidc-prepare-authentication-api.asciidoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,28 @@ and <<security-api-oidc-logout,OpenID Connect logout API>>.
3333
The following parameters can be specified in the body of the request:
3434

3535
`realm`::
36-
The name of the OpenID Connect realm in {es} the configuration of which should
36+
(Optional, string) The name of the OpenID Connect realm in {es} the configuration of which should
3737
be used in order to generate the authentication request. Cannot be specified
38-
when `iss` is specified.
38+
when `iss` is specified. One of `realm`, `iss` is required.
3939

4040
`state`::
41-
String value used to maintain state between the authentication request and the
41+
(Optional, string) Value used to maintain state between the authentication request and the
4242
response, typically used as a Cross-Site Request Forgery mitigation. If the
4343
caller of the API doesn't provide a value, {es} will generate one with
4444
sufficient entropy itself and return it in the response.
4545

4646
`nonce`::
47-
String value used to associate a Client session with an ID Token and to mitigate
47+
(Optional, string) Value used to associate a Client session with an ID Token and to mitigate
4848
replay attacks. If the caller of the API doesn't provide a value, {es} will
4949
generate one with sufficient entropy itself and return it in the response.
5050

51-
`issuer`::
52-
In the case of a 3rd Party initiated Single Sign On, this is the Issuer
51+
`iss`::
52+
(Optional, string) In the case of a 3rd Party initiated Single Sign On, this is the Issuer
5353
Identifier for the OP that the RP is to send the Authentication Request to.
54-
Cannot be specified when `realm` is specified.
54+
Cannot be specified when `realm` is specified. One of `realm`, `iss` is required.
5555

5656
`login_hint`::
57-
In the case of a 3rd Party initiated Single Sign On, a string value to be
57+
(Optional, string) In the case of a 3rd Party initiated Single Sign On, a string value to be
5858
included in the authentication request, as the `login_hint` parameter. This
5959
parameter is not valid when `realm` is specified
6060

x-pack/docs/en/security/authentication/oidc-guide.asciidoc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,9 @@ POST /_security/oidc/prepare
649649
this HTTP GET request, the custom web app will need to make an HTTP POST request to
650650
`_security/oidc/authenticate`, again - authenticating as the `facilitator` user - passing the URL
651651
where the user's browser was redirected to, as a parameter, along with the
652-
values for `nonce` and `state` it had saved in the user's session previously.
652+
values for `nonce` and `state` it had saved in the user's session previously. If more than one
653+
OpenID Connect realms are configured, the custom web app can specify the name of the realm to be
654+
used for handling this, but this parameter is optional.
653655
See {ref}/security-api-oidc-authenticate.html[OIDC Authenticate API] for more details
654656
+
655657
[source,js]
@@ -658,7 +660,8 @@ POST /_security/oidc/authenticate
658660
{
659661
"redirect_uri" : "https://oidc-kibana.elastic.co:5603/api/security/v1/oidc?code=jtI3Ntt8v3_XvcLzCFGq&state=4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I",
660662
"state" : "4dbrihtIAt3wBTwo6DxK-vdk-sSyDBV8Yf0AjdkdT5I",
661-
"nonce" : "WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM"
663+
"nonce" : "WaBPH0KqPVdG5HHdSxPRjfoZbXMCicm5v1OiAj0DUFM",
664+
"realm" : "oidc1"
662665
}
663666
-----------------------------------------------------------------------
664667
// CONSOLE

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/oidc/OpenIdConnectAuthenticateRequest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.core.security.action.oidc;
77

8+
import org.elasticsearch.Version;
89
import org.elasticsearch.action.ActionRequest;
910
import org.elasticsearch.action.ActionRequestValidationException;
1011
import org.elasticsearch.common.Strings;
@@ -38,6 +39,11 @@ public class OpenIdConnectAuthenticateRequest extends ActionRequest {
3839
*/
3940
private String nonce;
4041

42+
/**
43+
* The name of the OIDC Realm that should consume the authentication request
44+
*/
45+
private String realm;
46+
4147
public OpenIdConnectAuthenticateRequest() {
4248

4349
}
@@ -47,6 +53,10 @@ public OpenIdConnectAuthenticateRequest(StreamInput in) throws IOException {
4753
redirectUri = in.readString();
4854
state = in.readString();
4955
nonce = in.readString();
56+
if (in.getVersion().onOrAfter(Version.V_7_4_0)) {
57+
realm = in.readOptionalString();
58+
}
59+
5060
}
5161

5262
public String getRedirectUri() {
@@ -73,6 +83,14 @@ public void setNonce(String nonce) {
7383
this.nonce = nonce;
7484
}
7585

86+
public String getRealm() {
87+
return realm;
88+
}
89+
90+
public void setRealm(String realm) {
91+
this.realm = realm;
92+
}
93+
7694
@Override
7795
public ActionRequestValidationException validate() {
7896
ActionRequestValidationException validationException = null;
@@ -94,10 +112,13 @@ public void writeTo(StreamOutput out) throws IOException {
94112
out.writeString(redirectUri);
95113
out.writeString(state);
96114
out.writeString(nonce);
115+
if (out.getVersion().onOrAfter(Version.V_7_4_0)) {
116+
out.writeOptionalString(realm);
117+
}
97118
}
98119

99120
public String toString() {
100-
return "{redirectUri=" + redirectUri + ", state=" + state + ", nonce=" + nonce + "}";
121+
return "{redirectUri=" + redirectUri + ", state=" + state + ", nonce=" + nonce + ", realm=" +realm+"}";
101122
}
102123
}
103124

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateRequest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import org.elasticsearch.action.ActionRequest;
99
import org.elasticsearch.action.ActionRequestValidationException;
10+
import org.elasticsearch.common.Nullable;
1011
import org.elasticsearch.common.io.stream.StreamInput;
1112

1213
import java.io.IOException;
@@ -19,6 +20,8 @@ public final class SamlAuthenticateRequest extends ActionRequest {
1920

2021
private byte[] saml;
2122
private List<String> validRequestIds;
23+
@Nullable
24+
private String realm;
2225

2326
public SamlAuthenticateRequest(StreamInput in) throws IOException {
2427
super(in);
@@ -47,4 +50,12 @@ public List<String> getValidRequestIds() {
4750
public void setValidRequestIds(List<String> validRequestIds) {
4851
this.validRequestIds = validRequestIds;
4952
}
53+
54+
public String getRealm() {
55+
return realm;
56+
}
57+
58+
public void setRealm(String realm) {
59+
this.realm = realm;
60+
}
5061
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/saml/SamlAuthenticateRequestBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ public SamlAuthenticateRequestBuilder validRequestIds(List<String> validRequestI
2929
request.setValidRequestIds(validRequestIds);
3030
return this;
3131
}
32+
33+
public SamlAuthenticateRequestBuilder authenticatingRealm(String realm) {
34+
request.setRealm(realm);
35+
return this;
36+
}
3237
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/oidc/TransportOpenIdConnectAuthenticateAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public TransportOpenIdConnectAuthenticateAction(ThreadPool threadPool, Transport
5555
protected void doExecute(Task task, OpenIdConnectAuthenticateRequest request,
5656
ActionListener<OpenIdConnectAuthenticateResponse> listener) {
5757
final OpenIdConnectToken token = new OpenIdConnectToken(request.getRedirectUri(), new State(request.getState()),
58-
new Nonce(request.getNonce()));
58+
new Nonce(request.getNonce()), request.getRealm());
5959
final ThreadContext threadContext = threadPool.getThreadContext();
6060
Authentication originatingAuthentication = Authentication.getAuthentication(threadContext);
6161
try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/saml/TransportSamlAuthenticateAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public TransportSamlAuthenticateAction(ThreadPool threadPool, TransportService t
4848

4949
@Override
5050
protected void doExecute(Task task, SamlAuthenticateRequest request, ActionListener<SamlAuthenticateResponse> listener) {
51-
final SamlToken saml = new SamlToken(request.getSaml(), request.getValidRequestIds());
51+
final SamlToken saml = new SamlToken(request.getSaml(), request.getValidRequestIds(), request.getRealm());
5252
logger.trace("Attempting to authenticate SamlToken [{}]", saml);
5353
final ThreadContext threadContext = threadPool.getThreadContext();
5454
Authentication originatingAuthentication = Authentication.getAuthentication(threadContext);

0 commit comments

Comments
 (0)