1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/plugins/module_utils/source_control/bitbucket.py

96 lines
3.3 KiB
Python
Raw Normal View History

2020-03-09 10:11:07 +01:00
# -*- coding: utf-8 -*-
# Copyright (c) Ansible project
# Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
# SPDX-License-Identifier: BSD-2-Clause
2020-03-09 10:11:07 +01:00
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
2020-03-09 10:11:07 +01:00
import json
from ansible.module_utils.common.text.converters import to_text
2020-03-09 10:11:07 +01:00
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.urls import fetch_url, basic_auth_header
class BitbucketHelper:
BITBUCKET_API_URL = 'https://api.bitbucket.org'
def __init__(self, module):
self.module = module
self.access_token = None
@staticmethod
def bitbucket_argument_spec():
return dict(
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>
2021-10-31 19:09:25 +01:00
client_id=dict(type='str', fallback=(env_fallback, ['BITBUCKET_CLIENT_ID'])),
2020-03-09 10:11:07 +01:00
client_secret=dict(type='str', no_log=True, fallback=(env_fallback, ['BITBUCKET_CLIENT_SECRET'])),
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>
2021-10-31 19:09:25 +01:00
# TODO:
# - Rename user to username once current usage of username is removed
# - Alias user to username and deprecate it
user=dict(type='str', aliases=['username'], fallback=(env_fallback, ['BITBUCKET_USERNAME'])),
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>
2021-10-31 19:09:25 +01:00
password=dict(type='str', no_log=True, fallback=(env_fallback, ['BITBUCKET_PASSWORD'])),
2020-03-09 10:11:07 +01:00
)
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>
2021-10-31 19:09:25 +01:00
@staticmethod
def bitbucket_required_one_of():
return [['client_id', 'client_secret', 'user', 'password']]
2020-03-09 10:11:07 +01:00
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>
2021-10-31 19:09:25 +01:00
@staticmethod
def bitbucket_required_together():
return [['client_id', 'client_secret'], ['user', 'password']]
2020-03-09 10:11:07 +01:00
def fetch_access_token(self):
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>
2021-10-31 19:09:25 +01:00
if self.module.params['client_id'] and self.module.params['client_secret']:
headers = {
'Authorization': basic_auth_header(self.module.params['client_id'], self.module.params['client_secret']),
}
info, content = self.request(
api_url='https://bitbucket.org/site/oauth2/access_token',
method='POST',
data='grant_type=client_credentials',
headers=headers,
)
if info['status'] == 200:
self.access_token = content['access_token']
else:
self.module.fail_json(msg='Failed to retrieve access token: {0}'.format(info))
2020-03-09 10:11:07 +01:00
def request(self, api_url, method, data=None, headers=None):
headers = headers or {}
if self.access_token:
headers.update({
'Authorization': 'Bearer {0}'.format(self.access_token),
})
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>
2021-10-31 19:09:25 +01:00
elif self.module.params['user'] and self.module.params['password']:
headers.update({
'Authorization': basic_auth_header(self.module.params['user'], self.module.params['password']),
})
2020-03-09 10:11:07 +01:00
if isinstance(data, dict):
data = self.module.jsonify(data)
headers.update({
'Content-type': 'application/json',
})
response, info = fetch_url(
module=self.module,
url=api_url,
method=method,
headers=headers,
data=data,
force=True,
)
content = {}
if response is not None:
body = to_text(response.read())
if body:
content = json.loads(body)
return info, content