Run approved FreeBSD root jobs through independently pinned and synchronized local helper
Rust development slice / linux (push) Failing after 11s
Rust development slice / linux (push) Failing after 11s
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
"""Register an explicitly pinned FreeBSD helper in the existing managed-script policy."""
|
||||
import argparse
|
||||
import fcntl
|
||||
import inspect
|
||||
import json
|
||||
import re
|
||||
import stat
|
||||
import subprocess
|
||||
import uuid
|
||||
from dev_linux_helper_rollover import POLICY, encoded, protected, replace, require
|
||||
from dev_stage import ssh_prefix
|
||||
|
||||
|
||||
def register(path, scope):
|
||||
uid=(path.parent.parent/'jobs/ed25519.pem').lstat().st_uid
|
||||
lock=path.with_name('operator.lock')
|
||||
info=lock.lstat()
|
||||
require(stat.S_ISREG(info.st_mode) and info.st_uid in (0,uid) and not info.st_mode&0o022,'bad operator lock')
|
||||
with lock.open('rb') as guard:
|
||||
fcntl.flock(guard,fcntl.LOCK_EX)
|
||||
info=protected(path,uid)
|
||||
require(stat.S_IMODE(info.st_mode)==0o600 and info.st_size<=65536,'unsafe policy')
|
||||
p=json.loads(path.read_bytes())
|
||||
require(p['schema_version']==1,'unsupported policy')
|
||||
rows=p.setdefault('opnsense',[])
|
||||
existing=[r for r in rows if r['agent_id']==scope['agent_id']]
|
||||
if existing:
|
||||
require(existing==[scope],'existing rule differs')
|
||||
else:
|
||||
rows.append(scope)
|
||||
data=encoded(p)
|
||||
require(len(data)<=65536 and sum(len(p[k]) for k in ('windows','linux','opnsense'))<=64,'policy capacity')
|
||||
replace(path,data)
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__)
|
||||
for k in ('tenant','asset','agent','principal'): parser.add_argument('--'+k,required=True,type=uuid.UUID)
|
||||
for k in ('policy','binary'): parser.add_argument('--'+k,required=True)
|
||||
for k in ('enrollment-generation','assignment-version'):parser.add_argument('--'+k,required=True,type=int)
|
||||
a=parser.parse_args()
|
||||
require(all(re.fullmatch('[a-f0-9]{64}',s) for s in [a.policy,a.binary]) and
|
||||
min(a.enrollment_generation,a.assignment_version)>0,'invalid pins or generations')
|
||||
scope=dict(tenant_id=str(a.tenant),asset_id=str(a.asset),agent_id=str(a.agent),
|
||||
enrollment_generation=a.enrollment_generation,assignment_version=a.assignment_version,
|
||||
principals=['local:'+str(a.principal)],helper_policy_sha256=a.policy,helper_binary_sha256=a.binary,
|
||||
allowed_script_profiles=['full_maintenance_v1'],allow_root_scripts=True,allow_reboot=False,
|
||||
allowed_services=[],acceptance_evidence_id='opnsense-root-'+str(a.agent))
|
||||
program='import fcntl,json,os,stat,tempfile\nfrom pathlib import Path\n'
|
||||
for f in (require,protected,encoded,replace,register):program+=inspect.getsource(f)+'\n'
|
||||
subprocess.run([*ssh_prefix('server'),'python3 -I -'],input=program+f'register(Path({str(POLICY)!r}),{scope!r})\n',text=True,check=True)
|
||||
|
||||
|
||||
if __name__=='__main__': main()
|
||||
Reference in New Issue
Block a user