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

[Test] Add a dedicate helper class for randomizing Authentication #85590

Merged
merged 2 commits into from
Apr 13, 2022

Conversation

ywangd
Copy link
Member

@ywangd ywangd commented Apr 1, 2022

The Authentication class has many internal logics about what values can
or cannot be combined together. It is not straightforward to always get
the logic right when trying to create such an object for tests. This
could lead to spurious failures or incomplete test coverage.

This PR adds a helper class for creating such an object with necessary
configurabililty. The relevant methods in AuthenticationTests now
delegate to the new class to avoid having to touch too many files in one
PR. The ultimate goal is to have it used in every place where an
Authentication object is needed for test to replace any calls to
constructors or mocking.

The Authentication class has many internal logics about what values can
or cannot be combined together. It is not straightforward to always get
the logic right when trying to create such an object for tests. This
could lead to spurious failures or incomplete test coverage.

This PR adds a helper class for creating such an object with necessary
configurabililty. The relevant methods in AuthenticationTests now
delegate to the new class to avoid having to touch too many files in one
PR. The ultimate goal is to have it used in every place where an
Authentication object is needed for test to replace any calls to
constructors or mocking.
@ywangd ywangd added >test Issues or PRs that are addressing/adding tests :Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) v8.3.0 labels Apr 1, 2022
@ywangd ywangd requested a review from albertzaharovits April 1, 2022 03:12
@elasticmachine elasticmachine added the Team:Security Meta label for security team label Apr 1, 2022
@elasticmachine
Copy link
Collaborator

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

Copy link
Contributor

@albertzaharovits albertzaharovits left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
It looks to me that all sorts possible of Authentications can be built with this new AuthenticatioinTestHelper class.

@ywangd ywangd merged commit df5cb3d into elastic:master Apr 13, 2022
ywangd added a commit to ywangd/elasticsearch that referenced this pull request Apr 19, 2022
This PR is another step towards locking down how Authentication object
can be instantiated: It should be created using dedicated convenient
methods instead of constructors.

Production usage of constructors are mostly removed. But lots of test
code still uses them. This PR replaces one form of the usage with the
newly introduced test helper and removes the corresponding constructor.

Relates: elastic#85590
Relates: elastic#85905
ywangd added a commit that referenced this pull request Apr 21, 2022
This PR is another step towards locking down how Authentication object
can be instantiated: It should be created using dedicated convenient
methods instead of constructors.

Production usage of constructors are mostly removed. But lots of test
code still uses them. This PR replaces one form of the usage with the
newly introduced test helper and removes the corresponding constructor.

Relates: #85590
Relates: #85905
ywangd added a commit to ywangd/elasticsearch that referenced this pull request Apr 26, 2022
In elastic#85255, some mocking of Authentication class got replaced by randomly
creating actual Authentication objects. This is a general direction we
want to head towards because Authentication object has plenty internal
logics which makes it hard to mock correctly (and also undesirable). The
recent change in elastic#85590 adds a test helper which makes randomising
Authentication object easier for tests.

For ApiKeyServiceTests.testGetApiKeyMetadata, the randomisation is
however too broad (broader then what the mocking provided) and can
sometimes creates authentication object that does not pass the
assertion.

The assertion expects no API key authentication. But the randomisation
can generate such one because it randomises whether the authentication
has run-as even when the effective user is from a realm. Since API keys
can run-as, the resulted Authentication object can be an overall API key
authentication object.

This PR reduces the randomness by not allow run-as so that the resulted
Authentication cannot be API keys.

Relates: elastic#85255
Resolves: elastic#86179
ywangd added a commit that referenced this pull request Apr 26, 2022
In #85255, some mocking of Authentication class got replaced by randomly
creating actual Authentication objects. This is a general direction we
want to head towards because Authentication object has plenty internal
logics which makes it hard to mock correctly (and also undesirable). The
recent change in #85590 adds a test helper which makes randomising
Authentication object easier for tests.

For ApiKeyServiceTests.testGetApiKeyMetadata, the randomisation is
however too broad (broader then what the mocking provided) and can
sometimes creates authentication object that does not pass the
assertion.

The assertion expects no API key authentication. But the randomisation
can generate such one because it randomises whether the authentication
has run-as even when the effective user is from a realm. Since API keys
can run-as, the resulted Authentication object can be an overall API key
authentication object.

This PR reduces the randomness by not allow run-as so that the resulted
Authentication cannot be API keys.

Relates: #85255
Resolves: #86179
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Security/Authentication Logging in, Usernames/passwords, Realms (Native/LDAP/AD/SAML/PKI/etc) Team:Security Meta label for security team >test Issues or PRs that are addressing/adding tests v8.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants