|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import sys |
| 3 | +import json |
| 4 | +import re |
| 5 | +from urllib.parse import urlparse |
| 6 | +from ruamel.yaml import YAML |
| 7 | + |
| 8 | +def remove_ids_and_containerids(obj): |
| 9 | + if isinstance(obj, dict): |
| 10 | + return {k: remove_ids_and_containerids(v) for k, v in obj.items() if k not in ("id", "containerId")} |
| 11 | + elif isinstance(obj, list): |
| 12 | + return [remove_ids_and_containerids(el) for el in obj] |
| 13 | + else: |
| 14 | + return obj |
| 15 | + |
| 16 | +def sort_lists(obj): |
| 17 | + if isinstance(obj, dict): |
| 18 | + return {k: sort_lists(v) for k, v in obj.items()} |
| 19 | + elif isinstance(obj, list): |
| 20 | + if all(not isinstance(el, (dict, list)) for el in obj): |
| 21 | + return sorted(obj, key=lambda x: str(x)) |
| 22 | + if all(isinstance(el, dict) for el in obj) and obj: |
| 23 | + recursed = [sort_lists(el) for el in obj] |
| 24 | + def sort_key(el): |
| 25 | + clientId = str(el.get("clientId", "")) |
| 26 | + priority = el.get("priority") |
| 27 | + if isinstance(priority, str) and priority.isdigit(): |
| 28 | + priority = int(priority) |
| 29 | + elif not isinstance(priority, int): |
| 30 | + priority = float("inf") |
| 31 | + name = str(el.get("name", "")) |
| 32 | + return (clientId, priority, name) |
| 33 | + return sorted(recursed, key=sort_key) |
| 34 | + return [sort_lists(el) for el in obj] |
| 35 | + else: |
| 36 | + return obj |
| 37 | + |
| 38 | +def extract_realm_name(obj): |
| 39 | + # Try data['realm']['realm'] or data['realm'] as string |
| 40 | + if isinstance(obj.get("realm"), dict): |
| 41 | + return obj["realm"].get("realm") |
| 42 | + elif isinstance(obj.get("realm"), str): |
| 43 | + return obj["realm"] |
| 44 | + return None |
| 45 | + |
| 46 | +def extract_platform_domain(obj): |
| 47 | + # Scan all rootUrl/adminUrl values and pick domain |
| 48 | + domains = set() |
| 49 | + def scan(o): |
| 50 | + if isinstance(o, dict): |
| 51 | + for k, v in o.items(): |
| 52 | + if k in ("rootUrl", "adminUrl") and isinstance(v, str) and v.startswith("https://"): |
| 53 | + parsed = urlparse(v) |
| 54 | + host = parsed.hostname |
| 55 | + if host and "." in host: |
| 56 | + # remove subdomain (keep domain) |
| 57 | + parts = host.split(".") |
| 58 | + domains.add(".".join(parts[1:])) |
| 59 | + else: |
| 60 | + scan(v) |
| 61 | + elif isinstance(o, list): |
| 62 | + for el in o: |
| 63 | + scan(el) |
| 64 | + scan(obj) |
| 65 | + return list(domains)[0] |
| 66 | + |
| 67 | +def inject_realm_variables(obj, original_realm, variable="{{ keycloak.realm.name }}"): |
| 68 | + if isinstance(obj, dict): |
| 69 | + return {k: inject_realm_variables(v, original_realm, variable) for k, v in obj.items()} |
| 70 | + elif isinstance(obj, list): |
| 71 | + return [inject_realm_variables(el, original_realm, variable) for el in obj] |
| 72 | + elif isinstance(obj, str): |
| 73 | + s = obj |
| 74 | + s = s.replace(f"/realms/{original_realm}/", f"/realms/{variable}/") |
| 75 | + s = s.replace(f"/admin/{original_realm}/console/", f"/admin/{variable}/console/") |
| 76 | + return s |
| 77 | + else: |
| 78 | + return obj |
| 79 | + |
| 80 | +def inject_platform_variables(obj, original_domain, variable="{{ platform_domain_name }}"): |
| 81 | + if isinstance(obj, dict): |
| 82 | + return {k: inject_platform_variables(v, original_domain, variable) for k, v in obj.items()} |
| 83 | + elif isinstance(obj, list): |
| 84 | + return [inject_platform_variables(el, original_domain, variable) for el in obj] |
| 85 | + elif isinstance(obj, str): |
| 86 | + s = re.sub( |
| 87 | + r"https://([a-zA-Z0-9_-]+)\." + re.escape(original_domain), |
| 88 | + r"https://\1." + variable, |
| 89 | + obj |
| 90 | + ) |
| 91 | + return s |
| 92 | + else: |
| 93 | + return obj |
| 94 | + |
| 95 | +def inject_client_secrets(obj): |
| 96 | + if isinstance(obj, dict): |
| 97 | + new_obj = {} |
| 98 | + client_id = obj.get("clientId") |
| 99 | + for k, v in obj.items(): |
| 100 | + if k == "secret" and isinstance(v, str) and v == "**********" and client_id: |
| 101 | + new_obj[k] = f"{{{{ {client_id}_oidc_client_secret }}}}" |
| 102 | + else: |
| 103 | + new_obj[k] = inject_client_secrets(v) |
| 104 | + return new_obj |
| 105 | + elif isinstance(obj, list): |
| 106 | + return [inject_client_secrets(el) for el in obj] |
| 107 | + else: |
| 108 | + return obj |
| 109 | + |
| 110 | +def inject_smtp_variables(realm_map): |
| 111 | + """ |
| 112 | + Replace specific SMTP keys with variables in spec.realm.smtpServer. |
| 113 | + Only handles the case where smtpServer is a dict under spec.realm. |
| 114 | + Other keys are left unchanged. |
| 115 | + """ |
| 116 | + smtp_server = realm_map.get("smtpServer") |
| 117 | + smtp_map = { |
| 118 | + "from": "{{ keycloak.smtp.from }}", |
| 119 | + "host": "{{ keycloak.smtp.host }}", |
| 120 | + "password": "{{ keycloak.smtp.password }}", |
| 121 | + "port": "{{ keycloak.smtp.port }}", |
| 122 | + "ssl": "{{ keycloak.smtp.ssl }}", |
| 123 | + "starttls": "{{ keycloak.smtp.starttls }}", |
| 124 | + "user": "{{ keycloak.smtp.user }}" |
| 125 | + } |
| 126 | + # Replace only the keys specified in smtp_map |
| 127 | + for key, var in smtp_map.items(): |
| 128 | + if key in smtp_server: |
| 129 | + smtp_server[key] = var |
| 130 | + # Sort keys alphabetically |
| 131 | + realm_map["smtpServer"] = dict(sorted(smtp_server.items())) |
| 132 | + return realm_map |
| 133 | + |
| 134 | +def main(inpath, outpath): |
| 135 | + with open(inpath, "r", encoding="utf-8") as f: |
| 136 | + data = json.load(f) |
| 137 | + |
| 138 | + cleaned = remove_ids_and_containerids(data) |
| 139 | + sorted_cleaned = sort_lists(cleaned) |
| 140 | + |
| 141 | + realm_map = sorted_cleaned if isinstance(sorted_cleaned, dict) else {"data": sorted_cleaned} |
| 142 | + |
| 143 | + original_realm = extract_realm_name(realm_map) |
| 144 | + original_domain = extract_platform_domain(realm_map) |
| 145 | + |
| 146 | + realm_map = inject_realm_variables(realm_map, original_realm) |
| 147 | + realm_map = inject_platform_variables(realm_map, original_domain) |
| 148 | + realm_map = inject_client_secrets(realm_map) |
| 149 | + realm_map = inject_smtp_variables(realm_map) |
| 150 | + |
| 151 | + final = { |
| 152 | + "apiVersion": "k8s.keycloak.org/v2alpha1", |
| 153 | + "kind": "KeycloakRealmImport", |
| 154 | + "metadata": {"name": "rspy", "namespace": "iam", "labels": {"wait-for-deployment": "Done"}}, |
| 155 | + "spec": {"keycloakCRName": "keycloak", "realm": realm_map}, |
| 156 | + } |
| 157 | + |
| 158 | + yaml = YAML() |
| 159 | + yaml.indent(mapping=2, sequence=4, offset=2) |
| 160 | + yaml.width = 130 |
| 161 | + yaml.preserve_quotes = False |
| 162 | + |
| 163 | + license_header = """# Copyright 2024 CS Group |
| 164 | +# |
| 165 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 166 | +# you may not use this file except in compliance with the License. |
| 167 | +# You may obtain a copy of the License at |
| 168 | +# |
| 169 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 170 | +# |
| 171 | +# Unless required by applicable law or agreed to in writing, software |
| 172 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 173 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 174 | +# See the License for the specific language governing permissions and |
| 175 | +# limitations under the License. |
| 176 | +""" |
| 177 | + |
| 178 | + with open(outpath, "w", encoding="utf-8") as f: |
| 179 | + f.write(license_header + "\n") |
| 180 | + yaml.dump(final, f) |
| 181 | + |
| 182 | + print(f"Transformation completed: {outpath}") |
| 183 | + |
| 184 | +if __name__ == "__main__": |
| 185 | + if len(sys.argv) != 3: |
| 186 | + print(f"Usage: {sys.argv[0]} <input.json> <output.yaml>", file=sys.stderr) |
| 187 | + sys.exit(1) |
| 188 | + main(sys.argv[1], sys.argv[2]) |
0 commit comments