84 lines
5.8 KiB
Python
84 lines
5.8 KiB
Python
import argparse
|
|
import json
|
|
import os
|
|
import secrets
|
|
from pathlib import Path
|
|
|
|
parser = argparse.ArgumentParser(description='Prepare the secrets for a new Item Atlas public preview.')
|
|
parser.add_argument('directory', type=Path, help='A directory on the SSD outside the Git worktree')
|
|
parser.add_argument('--owner', required=True, help='First account username')
|
|
parser.add_argument('--ollama-url', required=True, help='Internal Ollama URL')
|
|
args = parser.parse_args()
|
|
os.umask(0o077)
|
|
root = args.directory.resolve()
|
|
root.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
vault = root / 'generated.json'
|
|
if not vault.exists():
|
|
values = {key: secrets.token_urlsafe(30) for key in ('pg', 'inventory', 'intake', 'dbkeycloak', 'admin', 'owner', 'delivery')}
|
|
values['media_key'] = 'GK' + secrets.token_hex(12)
|
|
values['media_secret'] = secrets.token_hex(32)
|
|
vault.write_text(json.dumps(values))
|
|
values = json.loads(vault.read_text())
|
|
|
|
def env(name, data):
|
|
(root / (name + '.env')).write_text(''.join(f'{k}={v}\n' for k,v in data.items()))
|
|
|
|
hostname = 'https://api-atlas.tutudog.com/auth/realms/item-atlas'
|
|
internal = 'http://atlas-keycloak:8080/auth/realms/item-atlas/protocol/openid-connect'
|
|
issuer = {'ATLAS_OIDC_ISSUER_URI': hostname, 'ATLAS_OIDC_AUDIENCE': 'item-atlas-api',
|
|
'SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_JWK_SET_URI': internal + '/certs',
|
|
'SPRING_DATASOURCE_HIKARI_MAXIMUM_POOL_SIZE': 6,
|
|
'MANAGEMENT_OTLP_METRICS_EXPORT_ENABLED': 'false'}
|
|
env('atlas-public-postgres', {'POSTGRES_PASSWORD': values['pg'], 'INVENTORY_PASSWORD': values['inventory'],
|
|
'INTAKE_PASSWORD': values['intake'], 'IDENTITY_PASSWORD': values['dbkeycloak']})
|
|
env('atlas-public-keycloak', {'KC_DB_PASSWORD': values['dbkeycloak'], 'KC_BOOTSTRAP_ADMIN_USERNAME': 'atlas-bootstrap',
|
|
'KC_BOOTSTRAP_ADMIN_PASSWORD': values['admin']})
|
|
for service in ('inventory','intake'):
|
|
settings = issuer | {'ATLAS_DB_URL': f'jdbc:postgresql://atlas-postgres:5432/itematlas_{service}',
|
|
'ATLAS_DB_USER': 'atlas_' + service, 'ATLAS_DB_PASSWORD': values[service]}
|
|
if service == 'intake':
|
|
settings.update(ATLAS_OAUTH_TOKEN_URI=internal+'/token', ATLAS_OAUTH_CLIENT_ID='atlas-intake',
|
|
ATLAS_OAUTH_CLIENT_SECRET=values['delivery'], ATLAS_OLLAMA_ENABLED='true',
|
|
ATLAS_OLLAMA_URL=args.ollama_url, ATLAS_OLLAMA_MODEL='gemma4:12b')
|
|
env('item-atlas-'+service, settings)
|
|
|
|
env('atlas-public-media', {'ATLAS_MEDIA_ENDPOINT': 'http://atlas-media.item-atlas-staging.svc:3900',
|
|
'ATLAS_MEDIA_BUCKET': 'jianwu-public-photos', 'ATLAS_MEDIA_REGION': 'garage',
|
|
'ATLAS_MEDIA_ACCESS_KEY': values['media_key'], 'ATLAS_MEDIA_SECRET_KEY': values['media_secret']})
|
|
|
|
audience = {'name': 'item-atlas-api audience', 'protocol': 'openid-connect', 'protocolMapper': 'oidc-audience-mapper',
|
|
'config': {'included.custom.audience': 'item-atlas-api', 'access.token.claim': 'true'}}
|
|
role_mapper = {'name': 'Atlas public admin role', 'protocol': 'openid-connect', 'protocolMapper': 'oidc-usermodel-realm-role-mapper',
|
|
'config': {'claim.name': 'atlas_roles', 'multivalued': 'true', 'jsonType.label': 'String', 'access.token.claim': 'true'}}
|
|
|
|
native = {'clientId': 'atlas-ios', 'enabled': True, 'protocol': 'openid-connect', 'publicClient': True,
|
|
'standardFlowEnabled': True, 'implicitFlowEnabled': False, 'directAccessGrantsEnabled': False,
|
|
'attributes': {'pkce.code.challenge.method': 'S256'}, 'defaultClientScopes': ['profile'], 'protocolMappers': [audience]}
|
|
admin = native | {'clientId': 'atlas-admin', 'redirectUris': ['https://admin-atlas.tutudog.com/'],
|
|
'webOrigins': ['https://admin-atlas.tutudog.com'], 'protocolMappers': [audience, role_mapper]}
|
|
profile = {
|
|
'name': 'profile', 'protocol': 'openid-connect', 'attributes': {'include.in.token.scope': 'true'},
|
|
'protocolMappers': [
|
|
{'name': claim, 'protocol': 'openid-connect', 'protocolMapper': 'oidc-usermodel-property-mapper',
|
|
'config': {'user.attribute': prop, 'claim.name': claim, 'jsonType.label': 'String',
|
|
'access.token.claim': 'true', 'id.token.claim': 'true', 'userinfo.token.claim': 'true'}}
|
|
for prop, claim in (('username', 'preferred_username'), ('firstName', 'given_name'), ('lastName', 'family_name'))
|
|
],
|
|
}
|
|
realm = {'realm': 'item-atlas', 'displayName': '见物', 'enabled': True, 'sslRequired': 'external',
|
|
'registrationAllowed': False, 'resetPasswordAllowed': False, 'bruteForceProtected': True,
|
|
'accessTokenLifespan': 300, 'internationalizationEnabled': True, 'supportedLocales': ['en', 'zh-CN'],
|
|
'defaultLocale': 'zh-CN', 'roles': {'realm': [{'name': 'atlas-space-admin'}]},
|
|
'clientScopes': [profile, {'name': 'atlas.deliver', 'protocol': 'openid-connect', 'attributes': {'include.in.token.scope': 'true'}}],
|
|
'clients': [native | {'redirectUris': ['com.tutudog.itematlas:/oauth2/callback'],
|
|
'attributes': native['attributes'] | {'login_theme': 'jianwu'}}, admin,
|
|
{'clientId': 'atlas-intake', 'enabled': True, 'protocol': 'openid-connect', 'publicClient': False,
|
|
'clientAuthenticatorType': 'client-secret', 'secret': values['delivery'], 'standardFlowEnabled': False,
|
|
'directAccessGrantsEnabled': False, 'serviceAccountsEnabled': True,
|
|
'optionalClientScopes': ['atlas.deliver'], 'protocolMappers': [audience]}],
|
|
'users': [{'username': args.owner, 'enabled': True, 'firstName': '见物', 'lastName': '所有者',
|
|
'email': f'{args.owner}@example.invalid', 'emailVerified': False,
|
|
'credentials': [{'type': 'password', 'value': values['owner'], 'temporary': False}]}]}
|
|
(root/'item-atlas-realm.json').write_text(json.dumps(realm,ensure_ascii=False))
|
|
print('Private environment files and realm import prepared.')
|