Allows using different ways to retrieve hosts IP address
This commit is contained in:
parent
97cf1a8f5c
commit
84558826fb
15 changed files with 861 additions and 710 deletions
|
|
@ -13,20 +13,15 @@ from cryptography.hazmat.primitives import serialization
|
|||
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
|
||||
from cryptography.hazmat.primitives.hashes import SHA512
|
||||
|
||||
from transip_client.utils import import_string
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _get_ip(service: str) -> str:
|
||||
try:
|
||||
response = requests.get(service, timeout=10)
|
||||
response.raise_for_status()
|
||||
except requests.RequestException as e:
|
||||
raise OSError(f"Unable to retrieve current IP from {service}") from e
|
||||
|
||||
return response.text
|
||||
API_URL = "https://api.transip.nl/v6"
|
||||
|
||||
|
||||
# Note that tokens cannot be recreated when an existing token is present
|
||||
def _get_token(private_key_path: str, login: str, api_url: str) -> str:
|
||||
request = requests.Request(
|
||||
"POST",
|
||||
|
|
@ -36,7 +31,7 @@ def _get_token(private_key_path: str, login: str, api_url: str) -> str:
|
|||
"nonce": str(int(time.time() * 1000)),
|
||||
"read_only": False,
|
||||
"expiration_time": "30 minutes",
|
||||
"label": "Custom token",
|
||||
"label": "Trans IP client",
|
||||
"global_key": True,
|
||||
},
|
||||
)
|
||||
|
|
@ -53,6 +48,8 @@ def _get_token(private_key_path: str, login: str, api_url: str) -> str:
|
|||
|
||||
prepped_request.headers["Signature"] = signature.decode("ascii")
|
||||
|
||||
logger.info(f"Retrieving token from {api_url} for {login}")
|
||||
|
||||
with requests.Session() as session:
|
||||
response = session.send(prepped_request)
|
||||
|
||||
|
|
@ -65,6 +62,8 @@ def _get_token(private_key_path: str, login: str, api_url: str) -> str:
|
|||
|
||||
|
||||
def _get_domain(domain: str, token: str, api_url: str) -> requests.Response:
|
||||
logger.info(f"Retrieving domain information for {domain} from {api_url}")
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
return requests.get(f"{api_url}/domains/{domain}/dns", headers=headers)
|
||||
|
||||
|
|
@ -94,8 +93,9 @@ def _get_domain_data(
|
|||
def _update_domain(
|
||||
domain: str, payload: dict, api_url: str, token: str
|
||||
) -> requests.Response:
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
logger.info(f"Updating domain {domain} at {api_url}")
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
return requests.put(
|
||||
f"{api_url}/domains/{domain}/dns", data=json.dumps(payload), headers=headers
|
||||
)
|
||||
|
|
@ -123,23 +123,22 @@ def _update_domains(
|
|||
logger.exception(f"Unable to update domain {domain}")
|
||||
continue
|
||||
|
||||
logger.info(f"Updated domain {domain}")
|
||||
logger.info(f"Updated domain {domain} at {api_url}")
|
||||
|
||||
|
||||
def detect(
|
||||
domains: list[str],
|
||||
service: str,
|
||||
credentials: tuple[str, str],
|
||||
token: str,
|
||||
api_url: str,
|
||||
adapter_class: str,
|
||||
read_only: bool,
|
||||
) -> None:
|
||||
ip = _get_ip(service)
|
||||
_adapter_class = import_string(adapter_class)
|
||||
adapter = _adapter_class()
|
||||
ip = adapter.get_ip()
|
||||
|
||||
if all(credentials):
|
||||
token = _get_token(*credentials, api_url)
|
||||
token = _get_token(*credentials, API_URL)
|
||||
|
||||
domain_data = _get_domain_data(domains, token, api_url)
|
||||
domain_data = _get_domain_data(domains, token, API_URL)
|
||||
updated_domains = {}
|
||||
|
||||
for data in domain_data:
|
||||
|
|
@ -156,8 +155,9 @@ def detect(
|
|||
)
|
||||
|
||||
if dns_entries == updated_entries:
|
||||
logger.info(f"No changes detected for {domain}, skipping...")
|
||||
continue
|
||||
|
||||
updated_domains[domain] = {"dnsEntries": updated_entries}
|
||||
|
||||
_update_domains(updated_domains, api_url, token, read_only)
|
||||
_update_domains(updated_domains, API_URL, token, read_only)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue