Skip to content

Commit 42b3cd0

Browse files
authored
Remove unnecessary details logged for OIDC (#48746)
This commit removes unnecessary details logged for OIDC. Co-Authored-By: Ioannis Kakavas <ikakavas@protonmail.com>
1 parent ec9d8cf commit 42b3cd0

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthenticator.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import org.elasticsearch.common.collect.Tuple;
8080
import org.elasticsearch.common.util.concurrent.EsExecutors;
8181
import org.elasticsearch.common.util.concurrent.ListenableFuture;
82+
import org.elasticsearch.rest.RestStatus;
8283
import org.elasticsearch.watcher.FileChangesListener;
8384
import org.elasticsearch.watcher.FileWatcher;
8485
import org.elasticsearch.watcher.ResourceWatcherService;
@@ -514,29 +515,31 @@ private void handleTokenResponse(HttpResponse httpResponse, ActionListener<Tuple
514515
return;
515516
}
516517
final Charset encoding = encodingHeader == null ? StandardCharsets.UTF_8 : Charsets.toCharset(encodingHeader.getValue());
517-
final String json = EntityUtils.toString(entity, encoding);
518-
if (LOGGER.isTraceEnabled()) {
519-
LOGGER.trace("Received Token Response from OP with status [{}] and content [{}] ",
520-
httpResponse.getStatusLine().getStatusCode(), json);
521-
}
522-
final OIDCTokenResponse oidcTokenResponse = OIDCTokenResponse.parse(JSONObjectUtils.parse(json));
523-
if (oidcTokenResponse.indicatesSuccess() == false) {
524-
TokenErrorResponse errorResponse = oidcTokenResponse.toErrorResponse();
525-
tokensListener.onFailure(
526-
new ElasticsearchSecurityException("Failed to exchange code for Id Token. Code=[{}], Description=[{}]",
527-
errorResponse.getErrorObject().getCode(), errorResponse.getErrorObject().getDescription()));
518+
final RestStatus responseStatus = RestStatus.fromCode(httpResponse.getStatusLine().getStatusCode());
519+
if (RestStatus.OK != responseStatus) {
520+
final String json = EntityUtils.toString(entity, encoding);
521+
LOGGER.warn("Received Token Response from OP with status [{}] and content [{}]", responseStatus, json);
522+
if (RestStatus.BAD_REQUEST == responseStatus) {
523+
final TokenErrorResponse tokenErrorResponse = TokenErrorResponse.parse(JSONObjectUtils.parse(json));
524+
tokensListener.onFailure(
525+
new ElasticsearchSecurityException("Failed to exchange code for Id Token. Code=[{}], Description=[{}]",
526+
tokenErrorResponse.getErrorObject().getCode(), tokenErrorResponse.getErrorObject().getDescription()));
527+
} else {
528+
tokensListener.onFailure(new ElasticsearchSecurityException("Failed to exchange code for Id Token"));
529+
}
528530
} else {
529-
OIDCTokenResponse successResponse = oidcTokenResponse.toSuccessResponse();
530-
final OIDCTokens oidcTokens = successResponse.getOIDCTokens();
531+
final OIDCTokenResponse oidcTokenResponse = OIDCTokenResponse.parse(
532+
JSONObjectUtils.parse(EntityUtils.toString(entity, encoding)));
533+
final OIDCTokens oidcTokens = oidcTokenResponse.getOIDCTokens();
531534
final AccessToken accessToken = oidcTokens.getAccessToken();
532535
final JWT idToken = oidcTokens.getIDToken();
533536
if (LOGGER.isTraceEnabled()) {
534-
LOGGER.trace("Successfully exchanged code for ID Token: [{}] and Access Token [{}]",
535-
idToken, accessToken);
537+
LOGGER.trace("Successfully exchanged code for ID Token [{}] and Access Token [{}]", idToken,
538+
truncateToken(accessToken.toString()));
536539
}
537540
if (idToken == null) {
538-
tokensListener.onFailure(new ElasticsearchSecurityException("Token Response did not contain an ID Token or parsing of" +
539-
" the JWT failed."));
541+
tokensListener.onFailure(
542+
new ElasticsearchSecurityException("Token Response did not contain an ID Token or parsing of the JWT failed."));
540543
return;
541544
}
542545
tokensListener.onResponse(new Tuple<>(accessToken, idToken));
@@ -548,6 +551,13 @@ private void handleTokenResponse(HttpResponse httpResponse, ActionListener<Tuple
548551
}
549552
}
550553

554+
private static String truncateToken(String input) {
555+
if (Strings.hasText(input) == false || input.length() <= 4) {
556+
return input;
557+
}
558+
return input.substring(0, 2) + "***" + input.substring(input.length() - 2);
559+
}
560+
551561
/**
552562
* Creates a {@link CloseableHttpAsyncClient} that uses a {@link PoolingNHttpClientConnectionManager}
553563
*/

0 commit comments

Comments
 (0)