Bind CMS qualification to its private registered runner identity
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
"""The qualification runner is allocated at registration, never Device Core's ID."""
|
||||
from contextlib import ExitStack
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
from types import SimpleNamespace
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from test_runner import r
|
||||
|
||||
|
||||
class RegisteredRunnerTests(unittest.TestCase):
|
||||
def fixture(self, root):
|
||||
data=root/'data';data.mkdir(mode=0o700)
|
||||
registration=data/'.runner'
|
||||
value={'id':2,'name':'synology-cms-build','address':'https://git.dcserve.ru',
|
||||
'uuid':'fixture-uuid','token':'fixture-private-token'}
|
||||
registration.write_text(json.dumps(value));registration.chmod(0o600)
|
||||
return registration,value
|
||||
|
||||
def read(self, root, *, file_owner=0, directory_owner=0, links=None):
|
||||
# Local tests run without root. Only ownership is emulated; modes, file
|
||||
# descriptors, symlink protection and content reads use real temp files.
|
||||
original_lstat=Path.lstat;original_fstat=os.fstat
|
||||
def fields(value, **changes):
|
||||
result={name:getattr(value,name) for name in ('st_mode','st_uid','st_nlink','st_size','st_mtime_ns','st_ctime_ns')}
|
||||
return SimpleNamespace(**{**result,**changes})
|
||||
def lstat(path, *args, **kwargs):
|
||||
value=original_lstat(path,*args,**kwargs)
|
||||
return fields(value,st_uid=directory_owner) if path==root/'data' else value
|
||||
def fstat(fd):
|
||||
value=original_fstat(fd)
|
||||
return fields(value,st_uid=file_owner,st_nlink=value.st_nlink if links is None else links)
|
||||
with patch.object(r,'CMS_CI_ROOT',root),patch.object(Path,'lstat',lstat),patch.object(r.os,'fstat',fstat):
|
||||
return r.cms_ci_registered_runner_id()
|
||||
|
||||
def test_allocated_id_is_read_from_registered_private_identity(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
root=Path(d).resolve();p,value=self.fixture(root)
|
||||
for identity in (2,17,293):
|
||||
value['id']=identity;p.write_text(json.dumps(value))
|
||||
self.assertEqual(self.read(root),identity)
|
||||
|
||||
def test_identity_metadata_must_remain_private_and_single_link(self):
|
||||
for kwargs in ({'file_owner':501},{'directory_owner':501},{'links':2}):
|
||||
with self.subTest(kwargs=kwargs),tempfile.TemporaryDirectory() as d:
|
||||
root=Path(d).resolve();self.fixture(root)
|
||||
with self.assertRaises(r.DeployError):self.read(root,**kwargs)
|
||||
for rel,mode in (('data',0o755),('data/.runner',0o644)):
|
||||
with self.subTest(path=rel),tempfile.TemporaryDirectory() as d:
|
||||
root=Path(d).resolve();self.fixture(root);(root/rel).chmod(mode)
|
||||
with self.assertRaises(r.DeployError):self.read(root)
|
||||
|
||||
def test_other_daemon_or_origin_and_invalid_id_are_rejected(self):
|
||||
for key,value in (('id',True),('id',0),('id','2'),('id',2**53),('name','synology-device-core-build'),
|
||||
('address','https://other.example'),('uuid',''),('token','')):
|
||||
with self.subTest(key=key,value=value),tempfile.TemporaryDirectory() as d:
|
||||
root=Path(d).resolve();p,identity=self.fixture(root)
|
||||
identity[key]=value;p.write_text(json.dumps(identity))
|
||||
with self.assertRaises(r.DeployError):self.read(root)
|
||||
|
||||
def test_registration_symlink_is_rejected_without_reading_target(self):
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
root=Path(d).resolve();p,_=self.fixture(root)
|
||||
target=root/'other';p.rename(target);p.symlink_to(target)
|
||||
with self.assertRaises(OSError):self.read(root)
|
||||
|
||||
def test_registration_input_is_bounded_and_malformed_data_is_rejected(self):
|
||||
for data in ('x'*16385,'not JSON','[]'):
|
||||
with self.subTest(length=len(data)),tempfile.TemporaryDirectory() as d:
|
||||
root=Path(d).resolve();p,_=self.fixture(root);p.write_text(data)
|
||||
with self.assertRaises(r.DeployError):self.read(root)
|
||||
|
||||
|
||||
if __name__=='__main__':unittest.main()
|
||||
Reference in New Issue
Block a user