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

@ -16,29 +16,23 @@ description:
- The host fingerprint will be retrieved automatically, but in case of an error, one can use I(key) field to specify it manually.
author:
- Evgeniy Krysanov (@catcombo)
extends_documentation_fragment:
- community.general.bitbucket
requirements:
- paramiko
options:
client_id:
description:
- The OAuth consumer key.
- If not set the environment variable C(BITBUCKET_CLIENT_ID) will be used.
type: str
client_secret:
description:
- The OAuth consumer secret.
- If not set the environment variable C(BITBUCKET_CLIENT_SECRET) will be used.
type: str
repository:
description:
- The repository name.
type: str
required: true
username:
workspace:
description:
- The repository owner.
- Alias I(username) has been deprecated and will become an alias of I(user) in community.general 6.0.0.
type: str
required: true
aliases: [ username ]
name:
description:
- The FQDN of the known host.
@ -55,7 +49,6 @@ options:
required: true
choices: [ absent, present ]
notes:
- Bitbucket OAuth consumer key and secret can be obtained from Bitbucket profile -> Settings -> Access Management -> OAuth.
- Check mode is supported.
'''
@ -63,7 +56,7 @@ EXAMPLES = r'''
- name: Create known hosts from the list
community.general.bitbucket_pipeline_known_host:
repository: 'bitbucket-repo'
username: bitbucket_username
workspace: bitbucket_workspace
name: '{{ item }}'
state: present
with_items:
@ -73,14 +66,14 @@ EXAMPLES = r'''
- name: Remove known host
community.general.bitbucket_pipeline_known_host:
repository: bitbucket-repo
username: bitbucket_username
workspace: bitbucket_workspace
name: bitbucket.org
state: absent
- name: Specify public key file
community.general.bitbucket_pipeline_known_host:
repository: bitbucket-repo
username: bitbucket_username
workspace: bitbucket_workspace
name: bitbucket.org
key: '{{lookup("file", "bitbucket.pub") }}'
state: absent
@ -105,8 +98,8 @@ error_messages = {
}
BITBUCKET_API_ENDPOINTS = {
'known-host-list': '%s/2.0/repositories/{username}/{repo_slug}/pipelines_config/ssh/known_hosts/' % BitbucketHelper.BITBUCKET_API_URL,
'known-host-detail': '%s/2.0/repositories/{username}/{repo_slug}/pipelines_config/ssh/known_hosts/{known_host_uuid}' % BitbucketHelper.BITBUCKET_API_URL,
'known-host-list': '%s/2.0/repositories/{workspace}/{repo_slug}/pipelines_config/ssh/known_hosts/' % BitbucketHelper.BITBUCKET_API_URL,
'known-host-detail': '%s/2.0/repositories/{workspace}/{repo_slug}/pipelines_config/ssh/known_hosts/{known_host_uuid}' % BitbucketHelper.BITBUCKET_API_URL,
}
@ -137,7 +130,7 @@ def get_existing_known_host(module, bitbucket):
"""
content = {
'next': BITBUCKET_API_ENDPOINTS['known-host-list'].format(
username=module.params['username'],
workspace=module.params['workspace'],
repo_slug=module.params['repository'],
)
}
@ -150,7 +143,7 @@ def get_existing_known_host(module, bitbucket):
)
if info['status'] == 404:
module.fail_json(msg='Invalid `repository` or `username`.')
module.fail_json(msg='Invalid `repository` or `workspace`.')
if info['status'] != 200:
module.fail_json(msg='Failed to retrieve list of known hosts: {0}'.format(info))
@ -214,7 +207,7 @@ def create_known_host(module, bitbucket):
info, content = bitbucket.request(
api_url=BITBUCKET_API_ENDPOINTS['known-host-list'].format(
username=module.params['username'],
workspace=module.params['workspace'],
repo_slug=module.params['repository'],
),
method='POST',
@ -240,7 +233,7 @@ def create_known_host(module, bitbucket):
def delete_known_host(module, bitbucket, known_host_uuid):
info, content = bitbucket.request(
api_url=BITBUCKET_API_ENDPOINTS['known-host-detail'].format(
username=module.params['username'],
workspace=module.params['workspace'],
repo_slug=module.params['repository'],
known_host_uuid=known_host_uuid,
),
@ -261,7 +254,10 @@ def main():
argument_spec = BitbucketHelper.bitbucket_argument_spec()
argument_spec.update(
repository=dict(type='str', required=True),
username=dict(type='str', required=True),
workspace=dict(
type='str', aliases=['username'], required=True,
deprecated_aliases=[dict(name='username', version='6.0.0', collection_name='community.general')],
),
name=dict(type='str', required=True),
key=dict(type='str', no_log=False),
state=dict(type='str', choices=['present', 'absent'], required=True),
@ -269,6 +265,8 @@ def main():
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
required_one_of=BitbucketHelper.bitbucket_required_one_of(),
required_together=BitbucketHelper.bitbucket_required_together(),
)
if (module.params['key'] is None) and (not HAS_PARAMIKO):