-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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
Fix isApiKey test and apply it consistently #84396
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.
Pinging @elastic/es-security (Team:Security) |
Label as |
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; | ||
} |
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.
Not strictly needed since Service Account cannot create tokens. But updated for consistency.
// 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())); |
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.
This is to fix a test failure introduced by #84336
@elasticmachine run elasticsearch-ci/part-1 |
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.
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.
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
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
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.