-
Notifications
You must be signed in to change notification settings - Fork 372
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
Changing default values for sys.initialize
parameters secret_shares
and secret_threshold
#1063
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5caf907
move mock adapter and fixture to conftest
briantist 0d44cc7
update sys.initialize with conditional defaults and warnings
briantist 1a8b2d7
add integration test for sys.is_initialized
briantist a59fb22
add unit tests for sys.init module
briantist 6ad5f9f
add init value passing test
briantist File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import pytest | ||
|
||
from hvac.exceptions import ParamValidationError | ||
from hvac.api.system_backend.init import Init | ||
|
||
|
||
@pytest.fixture | ||
def sys_init(mock_adapter): | ||
return Init(mock_adapter) | ||
|
||
|
||
INIT_SECRET_PGP_ERROR_MSG = ( | ||
r"length of pgp_keys list argument must equal secret_shares value" | ||
) | ||
INIT_RECOVERY_PGP_ERROR_MSG = ( | ||
r"length of recovery_pgp_keys list argument must equal recovery_shares value" | ||
) | ||
INIT_RECOVERY_SHARES_ERROR_MSG = r"value for recovery_threshold argument must be less than or equal to recovery_shares argument" | ||
INIT_STORED_SHARES_ERROR_MSG = ( | ||
r"value for stored_shares argument must equal secret_shares argument" | ||
) | ||
|
||
|
||
class TestInit: | ||
@pytest.mark.parametrize( | ||
["secret_shares", "recovery_shares", "expected_value", "expected_warn"], | ||
[ | ||
(None, None, 5, True), | ||
(3, None, 3, False), | ||
(5, 7, 5, False), | ||
(None, 9, None, False), | ||
], | ||
) | ||
def test_initialize_default_secret_shares( | ||
self, | ||
sys_init, | ||
mock_warn, | ||
secret_shares, | ||
recovery_shares, | ||
expected_value, | ||
expected_warn, | ||
): | ||
(r_args, r_kwargs) = sys_init.initialize( | ||
secret_shares=secret_shares, | ||
recovery_shares=recovery_shares, | ||
recovery_threshold=0, | ||
) | ||
params = r_kwargs["json"] | ||
assert params["secret_shares"] == expected_value | ||
|
||
if expected_warn: | ||
mock_warn.assert_called_once() | ||
else: | ||
mock_warn.assert_not_called() | ||
|
||
@pytest.mark.parametrize( | ||
["secret_threshold", "recovery_threshold", "expected_value", "expected_warn"], | ||
[ | ||
(None, None, 3, True), | ||
(3, None, 3, False), | ||
(5, 7, 5, False), | ||
(None, 9, None, False), | ||
], | ||
) | ||
def test_initialize_default_secret_threshold( | ||
self, | ||
sys_init, | ||
mock_warn, | ||
secret_threshold, | ||
recovery_threshold, | ||
expected_value, | ||
expected_warn, | ||
): | ||
(r_args, r_kwargs) = sys_init.initialize( | ||
secret_threshold=secret_threshold, | ||
recovery_threshold=recovery_threshold, | ||
secret_shares=0, | ||
) | ||
params = r_kwargs["json"] | ||
assert params["secret_threshold"] == expected_value | ||
|
||
if expected_warn: | ||
mock_warn.assert_called_once() | ||
else: | ||
mock_warn.assert_not_called() | ||
|
||
@pytest.mark.parametrize( | ||
[ | ||
"secret_shares", | ||
"pgp_keys", | ||
"stored_shares", | ||
"recovery_shares", | ||
"recovery_pgp_keys", | ||
"recovery_threshold", | ||
"exc_msg", | ||
], | ||
[ | ||
( | ||
2, | ||
[1, 2, 3], | ||
2, | ||
None, | ||
None, | ||
None, | ||
INIT_SECRET_PGP_ERROR_MSG, | ||
), | ||
( | ||
2, | ||
[1, 2, 3], | ||
3, | ||
None, | ||
None, | ||
None, | ||
INIT_SECRET_PGP_ERROR_MSG, | ||
), | ||
( | ||
2, | ||
[1, 2], | ||
3, | ||
None, | ||
None, | ||
None, | ||
INIT_STORED_SHARES_ERROR_MSG, | ||
), | ||
(2, [1, 2], 2, 3, [1, 2], None, INIT_RECOVERY_PGP_ERROR_MSG), | ||
(2, [1, 2], 2, 3, [1, 2], 1, INIT_RECOVERY_PGP_ERROR_MSG), | ||
(2, [1, 2], 2, 3, [1, 2], 9, INIT_RECOVERY_SHARES_ERROR_MSG), | ||
], | ||
) | ||
def test_initialize_errors( | ||
self, | ||
sys_init, | ||
mock_adapter, | ||
secret_shares, | ||
pgp_keys, | ||
stored_shares, | ||
recovery_shares, | ||
recovery_pgp_keys, | ||
recovery_threshold, | ||
exc_msg, | ||
): | ||
with pytest.raises(ParamValidationError, match=exc_msg): | ||
sys_init.initialize( | ||
secret_threshold=0, # TODO(v3.0.0): remove this, only set to suppress warning | ||
secret_shares=secret_shares, | ||
pgp_keys=pgp_keys, | ||
stored_shares=stored_shares, | ||
recovery_shares=recovery_shares, | ||
recovery_pgp_keys=recovery_pgp_keys, | ||
recovery_threshold=recovery_threshold, | ||
) | ||
|
||
mock_adapter.request.assert_not_called() | ||
|
||
def test_initialize_value_pass(self, sys_init): | ||
(r_args, r_kwargs) = sys_init.initialize( | ||
secret_threshold=0, | ||
secret_shares=2, | ||
root_token_pgp_key="abc", | ||
pgp_keys=[1, 2], | ||
stored_shares=2, | ||
recovery_shares=3, | ||
recovery_pgp_keys=[1, 2, 3], | ||
recovery_threshold=3, | ||
) | ||
params = r_kwargs["json"] | ||
|
||
assert params["secret_threshold"] == 0 | ||
assert params["secret_shares"] == 2 | ||
assert params["root_token_pgp_key"] == "abc" | ||
assert params["pgp_keys"] == [1, 2] | ||
assert params["stored_shares"] == 2 | ||
assert params["recovery_shares"] == 3 | ||
assert params["recovery_pgp_keys"] == [1, 2, 3] | ||
assert params["recovery_threshold"] == 3 |
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,31 @@ | ||
import pytest | ||
|
||
from unittest import mock | ||
|
||
from hvac.adapters import Adapter | ||
|
||
|
||
class MockAdapter(Adapter): | ||
def __init__(self, *args, **kwargs): | ||
if "session" not in kwargs: | ||
kwargs["session"] = mock.MagicMock() | ||
super().__init__(*args, **kwargs) | ||
|
||
def request(self, *args, **kwargs): | ||
return (args, kwargs) | ||
|
||
def get_login_token(self, response): | ||
raise NotImplementedError() | ||
|
||
|
||
@pytest.fixture | ||
def mock_adapter(): | ||
adapter = MockAdapter() | ||
with mock.patch.object(adapter, "request", mock.Mock(wraps=MockAdapter.request)): | ||
yield adapter | ||
|
||
|
||
@pytest.fixture | ||
def mock_warn(): | ||
with mock.patch("warnings.warn") as warn: | ||
yield warn |
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.
Why use an explicit error_msg variable here when the current file has previously not done so? Its not a breaking instance of code just stylistic.
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.
looking at left side in the side-by-side diff, it seems it's because the previous code did in fact use this variable, and I just changed the message minimally but didn't remove use of the var. I don't feel strongly about it, if you want to put in a suggested change in the comment that removes the variable I'll probably accept it.