Fix isApiKey test and apply it consistently#84396
Merged
ywangd merged 3 commits intoelastic:masterfrom Feb 28, 2022
Merged
Conversation
Creating tokens using API keys is not properly supported till elastic#80926. Previously the created token always has no previlege. Now the token has the same privilege as the API key itself (similar to user created tokens). Authenticating using the token is considered equivalent to the API key itself. Therefore the "isApiKey" check needs to be updated to cater for both authentications of API key itself and the token created by the API key. This PR updated the isApiKey check and apply it consistently to ensure the behaviour is consistent between an API key and a token created by it. The only exception is for supporting run-as. API key itself can run-as another user. But a token created by the API key cannot perform run-as (elastic#84336) similar to how user/token works.
Collaborator
|
Pinging @elastic/es-security (Team:Security) |
Member
Author
|
Label as |
ywangd
commented
Feb 28, 2022
Comment on lines
253
to
258
| public boolean isServiceAccount() { | ||
| return isAuthenticatedWithServiceAccount() && false == getUser().isRunAs(); | ||
| final boolean result = ServiceAccountSettings.REALM_TYPE.equals(getSourceRealm().getType()); | ||
| assert false == result || ServiceAccountSettings.REALM_NAME.equals(getSourceRealm().getName()) | ||
| : "service account realm name mismatch"; | ||
| return result; | ||
| } |
Member
Author
There was a problem hiding this comment.
Not strictly needed since Service Account cannot create tokens. But updated for consistency.
ywangd
commented
Feb 28, 2022
Comment on lines
-1233
to
+1250
| // Non API Key | ||
| final Authentication authentication3 = randomFrom( | ||
| AuthenticationTests.randomRealmAuthentication(false), | ||
| AuthenticationTests.randomServiceAccountAuthentication(), | ||
| AuthenticationTests.randomAnonymousAuthentication(), | ||
| AuthenticationTests.randomInternalAuthentication() | ||
| ); | ||
|
|
||
| // Realm | ||
| final Authentication authentication3 = AuthenticationTests.randomRealmAuthentication(false); | ||
| assertThat(ApiKeyService.getCreatorRealmName(authentication3), equalTo(authentication3.getSourceRealm().getName())); | ||
| assertThat(ApiKeyService.getCreatorRealmType(authentication3), equalTo(authentication3.getSourceRealm().getType())); | ||
|
|
||
| // Non API Key run-as | ||
| // Realm run-as | ||
| final Authentication authentication4 = authentication3.runAs(AuthenticationTests.randomUser(), lookupRealmRef); | ||
| assertThat(ApiKeyService.getCreatorRealmName(authentication4), equalTo(lookupRealmRef.getName())); | ||
| assertThat(ApiKeyService.getCreatorRealmType(authentication4), equalTo(lookupRealmRef.getType())); | ||
|
|
||
| // Others (cannot run-as) | ||
| final Authentication authentication5 = randomFrom( | ||
| AuthenticationTests.randomServiceAccountAuthentication(), | ||
| AuthenticationTests.randomAnonymousAuthentication(), | ||
| AuthenticationTests.randomInternalAuthentication() | ||
| ); | ||
| assertThat(ApiKeyService.getCreatorRealmName(authentication5), equalTo(authentication5.getSourceRealm().getName())); | ||
| assertThat(ApiKeyService.getCreatorRealmType(authentication5), equalTo(authentication5.getSourceRealm().getType())); |
Member
Author
There was a problem hiding this comment.
This is to fix a test failure introduced by #84336
Member
Author
|
@elasticmachine run elasticsearch-ci/part-1 |
tvernum
approved these changes
Feb 28, 2022
ywangd
added a commit
to ywangd/elasticsearch
that referenced
this pull request
Feb 28, 2022
Creating tokens using API keys is not properly supported till elastic#80926. Previously the created token always has no previlege. Now the token has the same privilege as the API key itself (similar to user created tokens). Authenticating using the token is considered equivalent to the API key itself. Therefore the "isApiKey" check needs to be updated to cater for both authentications of API key itself and the token created by the API key. This PR updates the isApiKey check and apply it consistently to ensure the behaviour is consistent between an API key and a token created by it. The only exception is for supporting run-as. API key itself can run-as another user. But a token created by the API key cannot perform run-as (elastic#84336) similar to how user/token works.
elasticsearchmachine
pushed a commit
that referenced
this pull request
Feb 28, 2022
Creating tokens using API keys is not properly supported till #80926. Previously the created token always has no previlege. Now the token has the same privilege as the API key itself (similar to user created tokens). Authenticating using the token is considered equivalent to the API key itself. Therefore the "isApiKey" check needs to be updated to cater for both authentications of API key itself and the token created by the API key. This PR updates the isApiKey check and apply it consistently to ensure the behaviour is consistent between an API key and a token created by it. The only exception is for supporting run-as. API key itself can run-as another user. But a token created by the API key cannot perform run-as (#84336) similar to how user/token works.
tvernum
added a commit
to tvernum/elasticsearch
that referenced
this pull request
Mar 1, 2022
This test would create objects with an authentication_type of "API_KEY", but with a real authentication realm (rather than the API key synthetic realm). It is not possible to create an object like that on the server, so the test was asserting behaviour that cannot exist, and should not be subject to test constraints. This commit fixes the mock object creation to always create more realistic objects. Relates: elastic#84396 Resolves: elastic#84433
elasticsearchmachine
pushed a commit
that referenced
this pull request
Mar 1, 2022
This test would create objects with an authentication_type of "API_KEY", but with a real authentication realm (rather than the API key synthetic realm). It is not possible to create an object like that on the server, so the test was asserting behaviour that cannot exist, and should not be subject to test constraints. This commit fixes the mock object creation to always create more realistic objects. Relates: #84396 Resolves: #84433
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Creating tokens using API keys is not properly supported till #80926.
Previously the created token always has no previlege. Now the token has
the same privilege as the API key itself (similar to user created
tokens). Authenticating using the token is considered equivalent to the
API key itself. Therefore the "isApiKey" check needs to be updated to
cater for both authentications of API key itself and the token created
by the API key.
This PR updated the isApiKey check and apply it consistently to ensure
the behaviour is consistent between an API key and a token created by
it.
The only exception is for supporting run-as. API key itself can run-as
another user. But a token created by the API key cannot perform run-as
(#84336) similar to how user/token works.