bitbucket: Support Basic Auth (#2045)

* bitbucket: Support Basic Auth

* Rename username to user

* Document user/password options

* Rename username to workspace

* Deprecate username

* Fix credentials_required error_message

* Fix credentials_required error_message

* Test user/password/workspace options and env vars

* Update a test to use user/password/workspace for each module

* Fix check auth arguments

* Use required_one_of/required_together

* Fix required typo

* Fix fetch_access_token

* Fix tests 🤞

* Switch things up in test_bitbucket_access_key

* Fix username/password are None

* Remove username/password properties, use params directly

* Update plugins/doc_fragments/bitbucket.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/module_utils/source_control/bitbucket.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/module_utils/source_control/bitbucket.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/module_utils/source_control/bitbucket.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/source_control/bitbucket/bitbucket_access_key.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/source_control/bitbucket/bitbucket_pipeline_key_pair.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/source_control/bitbucket/bitbucket_pipeline_known_host.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/source_control/bitbucket/bitbucket_pipeline_known_host.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/source_control/bitbucket/bitbucket_pipeline_variable.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/source_control/bitbucket/bitbucket_pipeline_variable.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/source_control/bitbucket/bitbucket_access_key.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/source_control/bitbucket/bitbucket_pipeline_key_pair.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Document OAuth/Basic Auth precedence

* Apply suggestions from code review

Co-authored-by: Felix Fontein <felix@fontein.de>

* Remove no_log=False from user argument

* Add changelog fragment

* Correct wording and formatting in changelog

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update changelogs/fragments/2045-bitbucket_support_basic_auth.yaml

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Maxime Brunet 2021-10-31 11:09:25 -07:00 committed by GitHub
parent 2324f350bc
commit aaa0f39f72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 187 additions and 134 deletions

View file

@ -29,15 +29,14 @@ class TestBucketAccessKeyModule(ModuleTestCase):
self.assertEqual(exec_info.exception.args[0]['msg'], self.module.error_messages['required_key'])
@patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
@patch.object(bitbucket_access_key, 'get_existing_deploy_key', return_value=None)
def test_create_deploy_key(self, *args):
with patch.object(self.module, 'create_deploy_key') as create_deploy_key_mock:
with self.assertRaises(AnsibleExitJson) as exec_info:
set_module_args({
'client_id': 'ABC',
'client_secret': 'XXX',
'username': 'name',
'user': 'ABC',
'password': 'XXX',
'workspace': 'name',
'repository': 'repo',
'key': 'public_key',
'label': 'key name',

View file

@ -28,15 +28,14 @@ class TestBucketPipelineKeyPairModule(ModuleTestCase):
self.assertEqual(exec_info.exception.args[0]['msg'], self.module.error_messages['required_keys'])
@patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
@patch.object(bitbucket_pipeline_key_pair, 'get_existing_ssh_key_pair', return_value=None)
def test_create_keys(self, *args):
with patch.object(self.module, 'update_ssh_key_pair') as update_ssh_key_pair_mock:
with self.assertRaises(AnsibleExitJson) as exec_info:
set_module_args({
'client_id': 'ABC',
'client_secret': 'XXX',
'username': 'name',
'user': 'ABC',
'password': 'XXX',
'workspace': 'name',
'repository': 'repo',
'public_key': 'public',
'private_key': 'PRIVATE',

View file

@ -37,16 +37,15 @@ class TestBucketPipelineKnownHostModule(ModuleTestCase):
self.assertEqual(create_known_host_mock.call_count, 1)
self.assertEqual(exec_info.exception.args[0]['changed'], True)
@patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
@patch.object(BitbucketHelper, 'request', return_value=(dict(status=201), dict()))
@patch.object(bitbucket_pipeline_known_host, 'get_existing_known_host', return_value=None)
def test_create_known_host_with_key(self, *args):
with patch.object(self.module, 'get_host_key') as get_host_key_mock:
with self.assertRaises(AnsibleExitJson) as exec_info:
set_module_args({
'client_id': 'ABC',
'client_secret': 'XXX',
'username': 'name',
'user': 'ABC',
'password': 'XXX',
'workspace': 'name',
'repository': 'repo',
'name': 'bitbucket.org',
'key': 'ssh-rsa public',

View file

@ -25,7 +25,7 @@ class TestBucketPipelineVariableModule(ModuleTestCase):
})
self.module.main()
self.assertEqual(exec_info.exception.args[0]['msg'], BitbucketHelper.error_messages['required_client_id'])
self.assertEqual(exec_info.exception.args[0]['failed'], True)
def test_missing_value_with_present_state(self):
with self.assertRaises(AnsibleFailJson) as exec_info:
@ -47,7 +47,7 @@ class TestBucketPipelineVariableModule(ModuleTestCase):
})
@patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
@patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value=None)
def test_env_vars_params(self, *args):
def test_oauth_env_vars_params(self, *args):
with self.assertRaises(AnsibleExitJson):
set_module_args({
'username': 'name',
@ -57,15 +57,29 @@ class TestBucketPipelineVariableModule(ModuleTestCase):
})
self.module.main()
@patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
@patch.dict('os.environ', {
'BITBUCKET_USERNAME': 'ABC',
'BITBUCKET_PASSWORD': 'XXX',
})
@patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value=None)
def test_basic_auth_env_vars_params(self, *args):
with self.assertRaises(AnsibleExitJson):
set_module_args({
'workspace': 'name',
'repository': 'repo',
'name': 'PIPELINE_VAR_NAME',
'state': 'absent',
})
self.module.main()
@patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value=None)
def test_create_variable(self, *args):
with patch.object(self.module, 'create_pipeline_variable') as create_pipeline_variable_mock:
with self.assertRaises(AnsibleExitJson) as exec_info:
set_module_args({
'client_id': 'ABC',
'client_secret': 'XXX',
'username': 'name',
'user': 'ABC',
'password': 'XXX',
'workspace': 'name',
'repository': 'repo',
'name': 'PIPELINE_VAR_NAME',
'value': '42',