Files
computer-playbook/roles/web-app-erpnext/files/python/apply/oidc_settings.py
kevinveenbirkenbachandClaude Opus 5 575a5a87ac refactor(layout): sort shipped scripts by language and split the unit suite
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).

This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.

Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:

  * 7 `src:` values built from a role variable holding a bare filename
    (svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
    The variable doubles as the destination name inside the container, so the
    language segment belongs at the call site, not in the variable.
  * `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
    through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
    18 roles ship exactly that and have no template fallback: the play would
    have died on each.
  * `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
    interface now reads `files/javascript/javascript.js`.
  * `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
    path through `path_absolute(... 'files', ...)`.
  * 4 `ansible.builtin.script:` invocations naming a bare command
    (pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
  * `dev-python/files/shell/install.sh` pointed at a sibling role's script.

Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-22 01:31:09 +02:00

106 lines
3.2 KiB
Python

# nocheck: mirrored-unit-test - imports frappe and frappe.utils.password at module level
# and runs inside `bench execute`; the framework is not installable outside the ERPNext
# image
import json
import os
import frappe
from frappe.utils.password import get_decrypted_password
SITE_NAME = os.environ["SITE_NAME"]
PROVIDER_NAME = os.environ["PROVIDER_NAME"]
BUTTON_TEXT = os.environ["BUTTON_TEXT"]
CLIENT_ID = os.environ["CLIENT_ID"]
CLIENT_SECRET = os.environ["CLIENT_SECRET"]
ISSUER_URL = os.environ["ISSUER_URL"]
GROUP_ROLE_MAP = json.loads(os.environ["GROUP_ROLE_MAP_JSON"])
TRACKED_FIELDS = (
"enable_social_login",
"social_login_provider",
"client_id",
"base_url",
"custom_base_url",
"authorize_url",
"access_token_url",
"api_endpoint",
"redirect_url",
"auth_url_data",
"user_id_property",
"sign_ups",
"icon",
)
def _tracked(doc):
return {field: doc.get(field) for field in TRACKED_FIELDS}
def upsert_social_login_key():
existed = bool(frappe.db.exists("Social Login Key", PROVIDER_NAME))
if existed:
existing = frappe.get_doc("Social Login Key", PROVIDER_NAME)
if (existing.social_login_provider or "Custom") != "Keycloak":
frappe.delete_doc(
"Social Login Key", PROVIDER_NAME, force=True, ignore_permissions=True
)
frappe.db.commit()
existed = False
if not existed:
slk = frappe.new_doc("Social Login Key")
slk.provider_name = PROVIDER_NAME
slk.social_login_provider = "Keycloak"
before_fields = None
before_secret = None
else:
slk = frappe.get_doc("Social Login Key", PROVIDER_NAME)
before_fields = _tracked(slk)
before_secret = get_decrypted_password(
"Social Login Key", PROVIDER_NAME, "client_secret", raise_exception=False
)
slk.enable_social_login = 1
slk.sign_ups = "Allow"
slk.icon = "fa fa-key"
slk.client_id = CLIENT_ID
slk.client_secret = CLIENT_SECRET
slk.base_url = ISSUER_URL
slk.custom_base_url = 1
slk.authorize_url = "/protocol/openid-connect/auth"
slk.access_token_url = "/protocol/openid-connect/token" # noqa: S105
slk.api_endpoint = "/protocol/openid-connect/userinfo"
slk.redirect_url = (
"/api/method/frappe.integrations.oauth2_logins.login_via_keycloak/keycloak"
)
slk.auth_url_data = json.dumps({"response_type": "code", "scope": "openid"})
slk.user_id_property = "preferred_username"
slk.custom_button_text = BUTTON_TEXT
slk.flags.ignore_permissions = True
after_fields = _tracked(slk)
slk.save()
return (
before_fields is None
or before_fields != after_fields
or before_secret != CLIENT_SECRET
)
def store_group_role_map():
frappe.db.set_default(
"erpnext_oidc_group_role_map",
json.dumps(GROUP_ROLE_MAP),
)
frappe.init(site=SITE_NAME, sites_path="/home/frappe/frappe-bench/sites")
frappe.connect()
changed = False
try:
changed = upsert_social_login_key()
store_group_role_map()
frappe.db.commit()
frappe.clear_cache()
finally:
frappe.destroy()
print("OIDC_SLK_CHANGED" if changed else "OIDC_SLK_UNCHANGED")