Skip to content
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

Merged
merged 3 commits into from
Feb 28, 2022

Conversation

ywangd
Copy link
Member

@ywangd ywangd commented 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 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.

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.
@ywangd ywangd added >non-issue :Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) v8.1.0 v8.2.0 labels Feb 28, 2022
@ywangd ywangd requested a review from tvernum February 28, 2022 03:22
@elasticmachine elasticmachine added the Team:Security Meta label for security team label Feb 28, 2022
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-security (Team:Security)

@ywangd
Copy link
Member Author

ywangd commented Feb 28, 2022

Label as >non-issue because the changes are not released yet.

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;
}
Copy link
Member Author

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.

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()));
Copy link
Member Author

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

@ywangd
Copy link
Member Author

ywangd commented Feb 28, 2022

@elasticmachine run elasticsearch-ci/part-1

@ywangd ywangd merged commit a6340a5 into elastic:master 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>non-issue :Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) Team:Security Meta label for security team v8.1.0 v8.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants