| 1 |
# Copyright (c) Ansible project |
| 2 |
# Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 3 |
# SPDX-License-Identifier: BSD-2-Clause |
| 4 |
|
| 5 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 6 |
# Do not use this from other collections or standalone plugins/modules! |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import json |
| 11 |
import typing as t |
| 12 |
from http import HTTPStatus |
| 13 |
|
| 14 |
from ansible.module_utils.basic import env_fallback |
| 15 |
from ansible.module_utils.urls import basic_auth_header, fetch_url |
| 16 |
|
| 17 |
if t.TYPE_CHECKING: |
| 18 |
from ansible.module_utils.basic import AnsibleModule |
| 19 |
|
| 20 |
|
| 21 |
class BitbucketHelper: |
| 22 |
BITBUCKET_API_URL = "https://api.bitbucket.org" |
| 23 |
|
| 24 |
def __init__(self, module: AnsibleModule) -> None: |
| 25 |
self.module = module |
| 26 |
self.access_token: str | None = None |
| 27 |
|
| 28 |
@staticmethod |
| 29 |
def bitbucket_argument_spec() -> dict[str, t.Any]: |
| 30 |
return dict( |
| 31 |
client_id=dict(type="str", fallback=(env_fallback, ["BITBUCKET_CLIENT_ID"])), |
| 32 |
client_secret=dict(type="str", no_log=True, fallback=(env_fallback, ["BITBUCKET_CLIENT_SECRET"])), |
| 33 |
# TODO: |
| 34 |
# - Rename user to username once current usage of username is removed |
| 35 |
# - Alias user to username and deprecate it |
| 36 |
user=dict(type="str", aliases=["username"], fallback=(env_fallback, ["BITBUCKET_USERNAME"])), |
| 37 |
password=dict(type="str", no_log=True, fallback=(env_fallback, ["BITBUCKET_PASSWORD"])), |
| 38 |
) |
| 39 |
|
| 40 |
@staticmethod |
| 41 |
def bitbucket_required_one_of() -> list[list[str]]: |
| 42 |
return [["client_id", "client_secret", "user", "password"]] |
| 43 |
|
| 44 |
@staticmethod |
| 45 |
def bitbucket_required_together() -> list[list[str]]: |
| 46 |
return [["client_id", "client_secret"], ["user", "password"]] |
| 47 |
|
| 48 |
def fetch_access_token(self) -> None: |
| 49 |
if self.module.params["client_id"] and self.module.params["client_secret"]: |
| 50 |
headers = { |
| 51 |
"Authorization": basic_auth_header( |
| 52 |
self.module.params["client_id"], self.module.params["client_secret"] |
| 53 |
), |
| 54 |
} |
| 55 |
|
| 56 |
info, content = self.request( |
| 57 |
api_url="https://bitbucket.org/site/oauth2/access_token", |
| 58 |
method="POST", |
| 59 |
data="grant_type=client_credentials", |
| 60 |
headers=headers, |
| 61 |
) |
| 62 |
|
| 63 |
if info["status"] == HTTPStatus.OK: |
| 64 |
self.access_token = content["access_token"] |
| 65 |
else: |
| 66 |
self.module.fail_json(msg=f"Failed to retrieve access token: {info}") |
| 67 |
|
| 68 |
def request( |
| 69 |
self, api_url: str, method: str, data: bytes | str | dict | None = None, headers: dict[str, str] | None = None |
| 70 |
): |
| 71 |
headers = headers or {} |
| 72 |
|
| 73 |
if self.access_token: |
| 74 |
headers.update( |
| 75 |
{ |
| 76 |
"Authorization": f"Bearer {self.access_token}", |
| 77 |
} |
| 78 |
) |
| 79 |
elif self.module.params["user"] and self.module.params["password"]: |
| 80 |
headers.update( |
| 81 |
{ |
| 82 |
"Authorization": basic_auth_header(self.module.params["user"], self.module.params["password"]), |
| 83 |
} |
| 84 |
) |
| 85 |
|
| 86 |
if isinstance(data, dict): |
| 87 |
data = self.module.jsonify(data) |
| 88 |
headers.update( |
| 89 |
{ |
| 90 |
"Content-type": "application/json", |
| 91 |
} |
| 92 |
) |
| 93 |
|
| 94 |
response, info = fetch_url( |
| 95 |
module=self.module, |
| 96 |
url=api_url, |
| 97 |
method=method, |
| 98 |
headers=headers, |
| 99 |
data=data, |
| 100 |
force=True, |
| 101 |
) |
| 102 |
|
| 103 |
content = {} |
| 104 |
|
| 105 |
if response is not None: |
| 106 |
body = response.read() |
| 107 |
if body: |
| 108 |
content = json.loads(body) |
| 109 |
|
| 110 |
return info, content |