-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preserve REST client auth despite 401 response #30558
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9993f36
Preserve REST client auth despite 401 response
tvernum e32d26e
Address feedback
tvernum 26cfd20
Merge branch 'master' into rest-client/auth-cache
tvernum 06ba63f
Merge branch 'master' into rest-client/auth-cache
tvernum 925679c
Merge branch 'master' into rest-client/auth-cache
tvernum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...t/src/main/java/org/elasticsearch/client/PersistentCredentialsAuthenticationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* | ||
*/ | ||
|
||
package org.elasticsearch.client; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.http.HttpHost; | ||
import org.apache.http.auth.AuthScheme; | ||
import org.apache.http.impl.client.TargetAuthenticationStrategy; | ||
import org.apache.http.protocol.HttpContext; | ||
|
||
/** | ||
* An {@link org.apache.http.client.AuthenticationStrategy} implementation that does <em>not</em> perform | ||
* any special handling if authentication fails. | ||
* The default handler in Apache HTTP client mimics standard browser behaviour of clearing authentication | ||
* credentials if it receives a 401 response from the server. While this can be useful for browser, it is | ||
* rarely the desired behaviour with the Elasticsearch REST API. | ||
* If the code using the REST client has configured credentials for the REST API, then we can and should | ||
* assume that this is intentional, and those credentials represent the best possible authentication | ||
* mechanism to the Elasticsearch node. | ||
* If we receive a 401 status, a probably cause is that the authentication mechanism in place was unable | ||
* to perform the requisite password checks (the node has not yet recovered its state, or an external | ||
* authentication provider was unavailable). | ||
* If this occurs, then the desired behaviour is for the Rest client to retry with the same credentials | ||
* (rather than trying with no credentials, or expecting the calling code to provide alternate credentials). | ||
*/ | ||
final class PersistentCredentialsAuthenticationStrategy extends TargetAuthenticationStrategy { | ||
|
||
private final Log logger = LogFactory.getLog(PersistentCredentialsAuthenticationStrategy.class); | ||
|
||
@Override | ||
public void authFailed(HttpHost host, AuthScheme authScheme, HttpContext context) { | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("Authentication to " + host + " failed (scheme: " + authScheme.getSchemeName() | ||
+ "). Preserving credentials for next request"); | ||
} | ||
// Do nothing. | ||
// The superclass implementation of method will clear the credentials from the cache, but we don't | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this good as a default in every case? Thinking of installation without x-pack installed and a different authentication method (e.g proxy or something along those lines)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is, but I'm keen to hear other opinions.
Given that we default to preemptive authentication it seems right to default to preemptive authentication that actually works.
If the server rejects the supplied credentials, then you're going to need some custom behaviour to provide alternative credentials - which probably means a custom auth strategy, although I think it could be possible to use a custom credentials provider and rely on the clear-cache behaviour.
Note the http client has separate strategies for proxy auth and target auth, though a reverse proxy would look like the target to the client.