Preserve native timestamp precision in passive time closeout
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -22,6 +22,7 @@ FIELDS = {'schema_version', 'agent_pid', 'observed_at', 'role', 'windows_boot_id
|
||||
'connection_generation', 'verifier_incarnation', 'request_sha256', 'response_sha256'}
|
||||
FILES = {'agent': 'agent-time-sample.json', 'system_helper': 'helper-time-sample.json'}
|
||||
HEX = re.compile(r'[0-9a-f]{64}')
|
||||
SECOND_NS = 1_000_000_000
|
||||
|
||||
# No report is requested from a service. The only content reads on the guest
|
||||
# are these two public JSONs and public executable hashes. CIM/process queries
|
||||
@@ -106,9 +107,16 @@ def strict(pairs):
|
||||
|
||||
|
||||
def stamp(value):
|
||||
need(type(value) is str and value.endswith('Z'), 'UTC_TIMESTAMP_REQUIRED')
|
||||
result = dt.datetime.fromisoformat(value[:-1] + '+00:00')
|
||||
return result
|
||||
need(type(value) is str, 'UTC_TIMESTAMP_REQUIRED')
|
||||
match = re.fullmatch(r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,9}))?Z', value)
|
||||
need(match is not None, 'UTC_TIMESTAMP_REQUIRED')
|
||||
base = dt.datetime.strptime(match[1], '%Y-%m-%dT%H:%M:%S').replace(tzinfo=dt.timezone.utc)
|
||||
elapsed = base - dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
|
||||
return (elapsed.days * 86400 + elapsed.seconds) * SECOND_NS + int((match[2] or '').ljust(9, '0'))
|
||||
|
||||
|
||||
def utc_ns(value):
|
||||
return stamp(value.astimezone(dt.timezone.utc).isoformat().replace('+00:00', 'Z'))
|
||||
|
||||
|
||||
def canonical_uuid(value):
|
||||
@@ -138,7 +146,7 @@ def windows_boot(ticks):
|
||||
|
||||
def filetime(value):
|
||||
need(type(value) is str and value.isascii() and value.isdigit(), 'FILETIME_REQUIRED')
|
||||
return dt.datetime(1601, 1, 1, tzinfo=dt.timezone.utc) + dt.timedelta(microseconds=int(value) // 10)
|
||||
return (int(value) - 116444736000000000) * 100
|
||||
|
||||
|
||||
def validate(native, server, since, observed_now):
|
||||
@@ -151,7 +159,7 @@ def validate(native, server, since, observed_now):
|
||||
generation = current['connection_generation']
|
||||
need(type(generation) is int and generation > 0, 'CURRENT_POSITIVE_GENERATION')
|
||||
now = stamp(native['observed_at'])
|
||||
need(abs((now - observed_now).total_seconds()) <= 10, 'CURRENT_WINDOWS_UTC')
|
||||
need(abs(now - utc_ns(observed_now)) <= 10 * SECOND_NS, 'CURRENT_WINDOWS_UTC')
|
||||
records = []
|
||||
for role, filename in FILES.items():
|
||||
wrapped = native['reports'][filename]
|
||||
@@ -168,9 +176,9 @@ def validate(native, server, since, observed_now):
|
||||
observed = stamp(record['observed_at'])
|
||||
modified = filetime(wrapped['metadata']['last_write_filetime'])
|
||||
birth = filetime(native['runtime']['agent']['birth_filetime'])
|
||||
need(since <= observed <= now + dt.timedelta(seconds=2) and birth <= observed, 'SAMPLE_CURRENT_INSTALL_WINDOW')
|
||||
need(0 <= (now - modified).total_seconds() <= 180 and 0 <= (now - observed).total_seconds() <= 180, 'SAMPLE_FILE_AGE')
|
||||
need(-2 <= (modified - observed).total_seconds() <= 90, 'SAMPLE_PUBLICATION_DELAY')
|
||||
need(since <= observed <= now + 2 * SECOND_NS and birth <= observed, 'SAMPLE_CURRENT_INSTALL_WINDOW')
|
||||
need(0 <= now - modified <= 180 * SECOND_NS and 0 <= now - observed <= 180 * SECOND_NS, 'SAMPLE_FILE_AGE')
|
||||
need(-2 * SECOND_NS <= modified - observed <= 90 * SECOND_NS, 'SAMPLE_PUBLICATION_DELAY')
|
||||
need(wrapped['metadata']['dacl_checked'] is True, 'SAMPLE_PROTECTED_METADATA')
|
||||
records.append(record)
|
||||
for field in ('verifier_incarnation', 'request_sha256', 'response_sha256'):
|
||||
@@ -181,7 +189,7 @@ def validate(native, server, since, observed_now):
|
||||
def collect(args):
|
||||
need(HEX.fullmatch(args.agent_sha256) and HEX.fullmatch(args.helper_sha256), 'QUALIFIED_RUNTIME_HASHES_REQUIRED')
|
||||
since = stamp(args.profile_after)
|
||||
need(0 <= (dt.datetime.now(dt.timezone.utc) - since).total_seconds() <= 600, 'RECENT_PROFILE_INSTALL_WINDOW')
|
||||
need(0 <= utc_ns(dt.datetime.now(dt.timezone.utc)) - since <= 600 * SECOND_NS, 'RECENT_PROFILE_INSTALL_WINDOW')
|
||||
STATE.mkdir(parents=True, mode=0o700, exist_ok=True)
|
||||
ident = str(uuid.uuid4())
|
||||
folder = STATE / ident
|
||||
|
||||
@@ -10,7 +10,7 @@ import passive_time_closeout as c
|
||||
class Binding(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.now = dt.datetime(2026,11,15,14,0,tzinfo=dt.timezone.utc)
|
||||
self.since = self.now - dt.timedelta(seconds=50)
|
||||
self.since = c.utc_ns(self.now - dt.timedelta(seconds=50))
|
||||
ticks='638987472000000000'
|
||||
boot=c.windows_boot(ticks)
|
||||
epoch=dt.datetime(1601,1,1,tzinfo=dt.timezone.utc)
|
||||
@@ -50,5 +50,15 @@ class Binding(unittest.TestCase):
|
||||
def test_missing_sample_is_explicit(self):
|
||||
self.native['reports']['helper-time-sample.json']=None
|
||||
with self.assertRaisesRegex(ValueError,'PUBLIC_SERVICE_SAMPLE_MISSING'):c.validate(self.native,self.server,self.since,self.now)
|
||||
def test_native_seven_and_chrono_nine_digits_keep_exact_install_boundary(self):
|
||||
native='2026-11-15T13:59:40.1234567Z'
|
||||
chrono='2026-11-15T13:59:40.123456701Z'
|
||||
self.assertEqual(c.stamp(chrono)-c.stamp(native),1)
|
||||
self.modify('agent-time-sample.json','observed_at',native)
|
||||
self.modify('helper-time-sample.json','observed_at',chrono)
|
||||
since=c.stamp(native)
|
||||
self.assertEqual(len(c.validate(self.native,self.server,since,self.now)),2)
|
||||
with self.assertRaisesRegex(ValueError,'SAMPLE_CURRENT_INSTALL_WINDOW'):
|
||||
c.validate(self.native,self.server,since+1,self.now)
|
||||
|
||||
if __name__=='__main__':unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user