Fixed PEM Keys issue

This commit is contained in:
2026-08-01 09:51:36 +05:30
parent f264f90f3b
commit dcb6850306
15 changed files with 217 additions and 150 deletions

View File

@@ -228,60 +228,120 @@ register_client_in_database() {
-d "${DB_NAME_VALUE}" \
-X -v ON_ERROR_STOP=1 \
-c "
WITH account AS (
WITH registration AS (
SELECT registration_id
FROM identity.client_registration_details
WHERE lower(client_code) = lower(
convert_from(decode('${client_id_b64}', 'base64'), 'UTF8'))
AND status = 'ACTIVE'
ORDER BY created_at
LIMIT 1
), account AS (
INSERT INTO identity.client_account
(tenant_id, client_slug, client_name, status)
VALUES (
(tenant_id, registration_id, client_slug, client_name, status)
SELECT
gen_random_uuid(),
registration_id,
convert_from(decode('${client_id_b64}', 'base64'), 'UTF8'),
convert_from(decode('${client_name_b64}', 'base64'), 'UTF8'),
'ACTIVE')
'ACTIVE'
FROM registration
ON CONFLICT (client_slug) DO UPDATE SET
registration_id = EXCLUDED.registration_id,
client_name = EXCLUDED.client_name,
status = 'ACTIVE',
updated_at = now()
RETURNING tenant_id
), installation AS (
INSERT INTO identity.client_installation
(installation_id, tenant_id, client_id, installation_code,
assertion_public_key, allowed_scopes, enabled)
), new_license AS (
INSERT INTO identity.client_license
(license_id, tenant_id, license_type, package_code,
valid_from, valid_until, status)
SELECT
gen_random_uuid(),
tenant_id,
convert_from(decode('${license_type_b64}', 'base64'), 'UTF8'),
convert_from(decode('${package_code_b64}', 'base64'), 'UTF8'),
now(),
now() + make_interval(months => ${LICENSE_MONTHS}),
'ACTIVE'
FROM account
WHERE NOT EXISTS (
SELECT 1
FROM identity.client_license existing
WHERE existing.tenant_id = account.tenant_id
AND existing.status = 'ACTIVE'
AND existing.valid_until > now()
)
RETURNING license_id, tenant_id
), selected_license AS (
SELECT existing.license_id, existing.tenant_id
FROM identity.client_license existing
JOIN account ON account.tenant_id = existing.tenant_id
WHERE existing.status = 'ACTIVE'
AND existing.valid_until > now()
UNION ALL
SELECT license_id, tenant_id
FROM new_license
ORDER BY license_id
LIMIT 1
), installation AS (
INSERT INTO identity.client_installation
(installation_id, tenant_id, client_id, installation_code,
assertion_public_key, allowed_scopes, enabled, license_id,
installation_uuid, installation_name, status,
software_version, environment)
SELECT
gen_random_uuid(),
account.tenant_id,
convert_from(decode('${client_id_b64}', 'base64'), 'UTF8'),
convert_from(decode('${installation_b64}', 'base64'), 'UTF8'),
convert_from(decode('${public_key_b64}', 'base64'), 'UTF8'),
ARRAY['identity.login']::text[],
true
true,
selected_license.license_id,
gen_random_uuid(),
convert_from(decode('${client_name_b64}', 'base64'), 'UTF8')
|| ' ' ||
convert_from(decode('${installation_b64}', 'base64'), 'UTF8'),
'ACTIVE',
'development',
'development'
FROM account
JOIN selected_license
ON selected_license.tenant_id = account.tenant_id
ON CONFLICT (client_id, installation_code) DO UPDATE SET
tenant_id = EXCLUDED.tenant_id,
assertion_public_key = EXCLUDED.assertion_public_key,
allowed_scopes = EXCLUDED.allowed_scopes,
enabled = true,
license_id = EXCLUDED.license_id,
status = 'ACTIVE',
security_version = identity.client_installation.security_version + 1,
updated_at = now()
RETURNING tenant_id
)
INSERT INTO identity.client_license
(license_id, tenant_id, license_type, package_code,
valid_from, valid_until, status)
SELECT
gen_random_uuid(),
tenant_id,
convert_from(decode('${license_type_b64}', 'base64'), 'UTF8'),
convert_from(decode('${package_code_b64}', 'base64'), 'UTF8'),
now(),
now() + make_interval(months => ${LICENSE_MONTHS}),
'ACTIVE'
FROM installation
WHERE NOT EXISTS (
SELECT 1
FROM identity.client_license existing
WHERE existing.tenant_id = installation.tenant_id
AND existing.status = 'ACTIVE'
AND existing.valid_until > now()
);"
SELECT count(*) AS provisioned_installations
FROM installation;"
local installation_count
installation_count="$(
PGPASSWORD="${DB_PASSWORD_VALUE}" "${PSQL_BIN}" \
-h "${DB_HOST_VALUE}" \
-p "${DB_PORT_VALUE}" \
-U "${DB_USER_VALUE}" \
-d "${DB_NAME_VALUE}" \
-X -A -t \
-c "
SELECT count(*)
FROM identity.client_installation
WHERE client_id =
convert_from(decode('${client_id_b64}', 'base64'), 'UTF8')
AND installation_code =
convert_from(decode('${installation_b64}', 'base64'), 'UTF8')
AND enabled = true;"
)"
[[ "${installation_count//[[:space:]]/}" == "1" ]] || fail \
"No active registration/account was found for '${CLIENT_ID}'. Create or activate client_registration_details first."
}
generate_machine_assertion() {