|
| 1 | +# IM - Infrastructure Manager |
| 2 | +# Copyright (C) 2011 - GRyCAP - Universitat Politecnica de Valencia |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | + |
| 18 | +import base64 |
| 19 | +import requests |
| 20 | +from .CloudConnector import CloudConnector |
| 21 | + |
| 22 | + |
| 23 | +class EGICloudConnector(CloudConnector): |
| 24 | + """ |
| 25 | + Cloud connector for the EGI cloud provider, that allows to manage the DNS entries with DyDNS service |
| 26 | + """ |
| 27 | + |
| 28 | + type = "EGI" |
| 29 | + """str with the name of the provider.""" |
| 30 | + DYDNS_URL = "https://nsupdate.fedcloud.eu" |
| 31 | + DEFAULT_TIMEOUT = 10 |
| 32 | + |
| 33 | + def _get_domains(self, token): |
| 34 | + """ |
| 35 | + List the domains available in the DyDNS service |
| 36 | + """ |
| 37 | + url = f'{self.DYDNS_URL}/nic/domains' |
| 38 | + resp = requests.get(url, headers={'Authorization': f'Bearer {token}'}, timeout=self.DEFAULT_TIMEOUT) |
| 39 | + if resp.status_code != 200: |
| 40 | + self.log_error(f"Error getting domains: {resp.text}") |
| 41 | + return None |
| 42 | + output = resp.json() |
| 43 | + if output.get("status") != "ok": |
| 44 | + self.log_error(f"Error getting domains: {output.get('message', 'Unknown error')}") |
| 45 | + return None |
| 46 | + domains = [] |
| 47 | + for domain in output.get("private", []) + output.get("public", []): |
| 48 | + if domain.get("available"): |
| 49 | + domains.append(domain["name"]) |
| 50 | + return domains |
| 51 | + |
| 52 | + def _get_host(self, hostname, domain, token): |
| 53 | + """ |
| 54 | + Look for a host registered in the DyDNS service |
| 55 | + """ |
| 56 | + if hostname == "*": |
| 57 | + parts = domain.split(".") |
| 58 | + domain = ".".join(parts[1:]) |
| 59 | + hostname = parts[0] |
| 60 | + url = f'{self.DYDNS_URL}/nic/hosts?domain={domain}' |
| 61 | + resp = requests.get(url, headers={'Authorization': f'Bearer {token}'}, timeout=self.DEFAULT_TIMEOUT) |
| 62 | + if resp.status_code != 200: |
| 63 | + self.log_error(f"Error getting host {hostname}.{domain}: {resp.text}") |
| 64 | + return None |
| 65 | + |
| 66 | + output = resp.json() |
| 67 | + if output.get("status") != "ok": |
| 68 | + self.log_error(f"Error getting host {hostname}.{domain}: {output.get('message', 'Unknown error')}") |
| 69 | + return None |
| 70 | + |
| 71 | + for host in output.get("hosts", []): |
| 72 | + if host.get("name") == hostname: |
| 73 | + return host |
| 74 | + |
| 75 | + return None |
| 76 | + |
| 77 | + def add_dns_entry(self, hostname, domain, ip, auth_data, extra_args=None): |
| 78 | + """ |
| 79 | + Add a DNS entry to the DNS server |
| 80 | + """ |
| 81 | + im_auth = auth_data.getAuthInfo("InfrastructureManager") |
| 82 | + try: |
| 83 | + secret = None |
| 84 | + if im_auth and im_auth[0].get("token"): |
| 85 | + self.log_debug(f"Registering DNS entry {hostname}.{domain} with DyDNS oauth token") |
| 86 | + token = im_auth[0].get("token") |
| 87 | + # Check if the host already exists |
| 88 | + host = self._get_host(hostname, domain, token) |
| 89 | + if host: |
| 90 | + self.log_debug(f"DNS entry {hostname}.{domain} already exists") |
| 91 | + if ip in [host.get("ipv4"), host.get("ipv6")]: |
| 92 | + print(f"DNS entry {hostname}.{domain} already has the IP {ip}") |
| 93 | + return True |
| 94 | + else: |
| 95 | + commennt = 'IM created DNS entry' |
| 96 | + if hostname == "*": |
| 97 | + url = f'{self.DYDNS_URL}/nic/register?fqdn={domain}&comment={commennt}&wildcard=true' |
| 98 | + else: |
| 99 | + url = f'{self.DYDNS_URL}/nic/register?fqdn={hostname}.{domain}&comment={commennt}' |
| 100 | + resp = requests.get(url, headers={'Authorization': f'Bearer {token}'}, timeout=self.DEFAULT_TIMEOUT) |
| 101 | + if resp.status_code != 200: |
| 102 | + self.log_error(f"Error registering DNS entry {hostname}.{domain}: {resp.text}") |
| 103 | + return False |
| 104 | + |
| 105 | + resp_json = resp.json() |
| 106 | + if resp_json.get("status") != "ok": |
| 107 | + self.log_error(f"Error registering DNS entry {hostname}.{domain}:" |
| 108 | + f" {resp_json.get('message', 'Unknown error')}") |
| 109 | + return False |
| 110 | + elif hostname.startswith("dydns:") and "@" in hostname: |
| 111 | + self.log_debug(f"Updating DNS entry {hostname}.{domain} with secret") |
| 112 | + parts = hostname[6:].split("@") |
| 113 | + secret = parts[0] |
| 114 | + hostname = parts[1] |
| 115 | + domain = domain[:-1] |
| 116 | + else: |
| 117 | + self.log_error(f"Error updating DNS entry {hostname}.{domain}: No secret nor token provided") |
| 118 | + return False |
| 119 | + |
| 120 | + if secret: |
| 121 | + auth = f"{hostname}.{domain}:{secret}" |
| 122 | + headers = {"Authorization": "Basic %s" % base64.b64encode(auth.encode()).decode()} |
| 123 | + else: |
| 124 | + headers = {'Authorization': f'Bearer {token}'} |
| 125 | + |
| 126 | + fqdn = f'{hostname}.{domain}' |
| 127 | + if hostname == "*": |
| 128 | + fqdn = domain |
| 129 | + url = f'{self.DYDNS_URL}/nic/update?hostname={fqdn}&myip={ip}' |
| 130 | + resp = requests.get(url, headers=headers, timeout=self.DEFAULT_TIMEOUT) |
| 131 | + if resp.status_code != 200: |
| 132 | + self.log_error(f"Error updating DNS entry {hostname}.{domain}: {resp.text}") |
| 133 | + return False |
| 134 | + return True |
| 135 | + except Exception as e: |
| 136 | + self.log_error(f"Error registering DNS entry {hostname}.{domain}: {str(e)}") |
| 137 | + return False |
| 138 | + |
| 139 | + def del_dns_entry(self, hostname, domain, ip, auth_data, extra_args=None): |
| 140 | + """ |
| 141 | + Delete a DNS entry from the DNS server |
| 142 | + """ |
| 143 | + im_auth = auth_data.getAuthInfo("InfrastructureManager") |
| 144 | + try: |
| 145 | + if im_auth and im_auth[0].get("token"): |
| 146 | + self.log_debug(f"Deleting DNS entry {hostname}.{domain} with DyDNS oauth token") |
| 147 | + token = im_auth[0].get("token") |
| 148 | + |
| 149 | + host = self._get_host(hostname, domain, token) |
| 150 | + if not host: |
| 151 | + self.log_debug(f"DNS entry {hostname}.{domain} does not exist. Do not need to delete.") |
| 152 | + return True |
| 153 | + |
| 154 | + if hostname == "*": |
| 155 | + url = f'{self.DYDNS_URL}/nic/unregister?fqdn={domain}' |
| 156 | + else: |
| 157 | + url = f'{self.DYDNS_URL}/nic/unregister?fqdn={hostname}.{domain}' |
| 158 | + resp = requests.get(url, headers={'Authorization': f'Bearer {token}'}, timeout=self.DEFAULT_TIMEOUT) |
| 159 | + if resp.status_code != 200: |
| 160 | + self.log_error(f"Error deleting DNS entry {hostname}.{domain}: {resp.text}") |
| 161 | + return False |
| 162 | + else: |
| 163 | + self.log_error(f"Error updating DNS entry {hostname}.{domain}: No token provided") |
| 164 | + return False |
| 165 | + except Exception as e: |
| 166 | + self.log_error(f"Error deleting DNS entry {hostname}.{domain}: {str(e)}") |
| 167 | + return False |
| 168 | + |
| 169 | + return True |
0 commit comments