Organize device inventory by customer and restrict token access to its audience

This commit is contained in:
2026-11-17 21:55:24 +01:00
parent aeb9adc649
commit 0afaca1d64
39 changed files with 1950 additions and 225 deletions
+24 -3
View File
@@ -38,6 +38,21 @@ def on_server(payload):
work.mkdir(mode=0o700,exist_ok=True)
with (work/'deployment.lock').open('a') as lock:
fcntl.flock(lock,fcntl.LOCK_EX)
if payload['action']=='grant-customer-admin':
principal = payload['principal']
if 'local:' + str(uuid.UUID(principal.removeprefix('local:'))) != principal:
raise ValueError('Invalid bootstrap principal')
config_path = root / 'devstate/public-auth/users.json'
configured = json.loads(config_path.read_bytes())['users']
if not any(u['principal'] == principal and u['tenant_id'] == TENANT
and u['username'] == 'kevin' and not u['disabled'] for u in configured):
raise ValueError('Active public bootstrap account differs')
query = f"SELECT provision_customer_admin('{TENANT}','{principal}');"
subprocess.run(['docker','exec','-i','rust-discovery-server-postgres-1','psql','-XqAt',
'-v','ON_ERROR_STOP=1','-U','postgres','-d','rmm'],
input=query,text=True,check=True,stdout=subprocess.DEVNULL)
print(json.dumps({'customer_admin':principal}))
return
if payload['action']=='prepare':
config=payload['accounts']
user,=config['users']
@@ -142,11 +157,11 @@ def on_server(payload):
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('action',choices=('prepare','activate','publish'))
parser.add_argument('action',choices=('prepare','activate','publish','grant-customer-admin'))
args=parser.parse_args()
os.umask(0o077)
payload={'action':args.action}
if args.action=='prepare':
if args.action in ('prepare','grant-customer-admin'):
path=ROOT/'credentials/public-webui-login.json'
if path.exists():
info=path.lstat()
@@ -154,6 +169,8 @@ def main():
raise ValueError('Unsafe bootstrap credential file')
credentials=json.loads(path.read_bytes())
else:
if args.action=='grant-customer-admin':
raise ValueError('Provision the local login before granting customer administration')
secret=base64.b32encode(secrets.token_bytes(20)).decode().rstrip('=')
password=secrets.token_urlsafe(32);salt=secrets.token_bytes(32)
derived=hashlib.scrypt(password.encode(),salt=salt,n=1<<17,r=8,p=1,dklen=64,maxmem=256*1024*1024)
@@ -164,7 +181,11 @@ def main():
'accounts':{'schema_version':1,'users':[account]}}
with path.open('x') as stream:json.dump(credentials,stream,indent=2)
path.chmod(0o600)
payload['accounts']=credentials['accounts']
if args.action=='grant-customer-admin':
account,=credentials['accounts']['users']
payload['principal']=account['principal']
else:
payload['accounts']=credentials['accounts']
else:
names=['server.public-access.compose.yaml'] if args.action=='activate' else ['server-bootstrap-ingress.conf','server-bootstrap-ingress.service']
payload['files']={name:base64.b64encode((ROOT/'deploy'/name).read_bytes()).decode() for name in names}