Skip to content

✨ Add os.date resource with time and timezone fields#6972

Merged
tas50 merged 6 commits intomainfrom
add-os-date-resource
Mar 19, 2026
Merged

✨ Add os.date resource with time and timezone fields#6972
tas50 merged 6 commits intomainfrom
add-os-date-resource

Conversation

@tas50
Copy link
Copy Markdown
Member

@tas50 tas50 commented Mar 18, 2026

Summary

  • Adds new os.date resource to the OS provider with time() and timezone() computed fields
  • Queries the remote system's actual time and timezone (unlike core time.now() which returns the local workstation's time)
  • Pure Go timezone detection via filesystem — works on EBS snapshots, Docker images, and other static targets without command execution:
    1. readlink /etc/localtime → extract IANA name from symlink target
    2. Read /etc/timezone (Debian/Ubuntu)
    3. Parse TZ= from /etc/TIMEZONE (Solaris/AIX)
    4. Binary-match /etc/localtime against zoneinfo database (handles Docker images that copy the TZif file)
  • Falls back to date +%Z command only for the abbreviated timezone name (the one thing that can't be read from files)
  • time returns nil on static targets (no fake local time for EBS snapshots/Docker images)
  • Windows support via PowerShell Get-Date + Get-TimeZone
  • Uses double-check locking to batch-fetch both fields from a single operation

Timezone Resolution Logic (TZif matching)

When /etc/localtime is a regular file (not a symlink) and /etc/timezone doesn't exist — common in Docker images — the resolver uses a three-tier strategy to identify the timezone:

  1. TZif footer parsing (fastest): TZif v2/v3/v4 files contain a POSIX TZ string in their footer (e.g., EST5EDT,M3.2.0,M11.1.0). This is parsed and mapped to an IANA name via a lookup table covering the most common timezones. No filesystem reads beyond /etc/localtime itself.

  2. Common timezone path matching (fast): Reads a curated list of ~50 common timezone files (e.g., /usr/share/zoneinfo/America/New_York) and does a byte comparison against /etc/localtime. This avoids walking the entire zoneinfo tree while covering the vast majority of real-world deployments.

  3. Capped zoneinfo tree walk (fallback): Walks /usr/share/zoneinfo comparing file contents, but caps the number of files read at 600 (well above the ~350 real IANA zones). This prevents pathological performance on tar-backed filesystems where each ReadFile extracts from a tar archive.

This tiered approach is critical for performance on tar-backed filesystems (Docker image scans, EBS snapshots) where a full directory walk would extract every file from the archive.

Usage

mql run local -c "os.date { time timezone }"
mql run ssh user@host -c "os.date.timezone"

Test plan

  • Unit tests for Unix and Windows date/timezone parsing pass
  • Filesystem-based timezone detection tests (symlink, /etc/timezone, /etc/TIMEZONE, TZif binary matching)
  • TZif footer parsing tests (v1 rejection, UTC variants, mapped POSIX strings, unmapped strings)
  • Common path matching tests (match found, no match)
  • Docker image simulation tests (UTC and non-UTC via TZif footer)
  • OS provider compiles cleanly
  • Interactive verification with mql shell localos.date { time timezone }
  • Interactive verification over SSH to Linux host
  • Interactive verification on Docker image (filesystem-only, no RunCommand)
  • Interactive verification over WinRM to Windows host

🤖 Generated with Claude Code

Copy link
Copy Markdown

@mondoo-code-review mondoo-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New os.date resource may fail on connections that don't support running commands (e.g., filesystem-only transports).

@mondoo-code-review mondoo-code-review bot dismissed their stale review March 18, 2026 05:30

Superseded by new review

Copy link
Copy Markdown

@mondoo-code-review mondoo-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New os.date resource exposes system time and timezone for queried assets.

@tas50 tas50 force-pushed the add-os-date-resource branch from 10ec633 to 2848267 Compare March 18, 2026 05:36
Copy link
Copy Markdown

@mondoo-code-review mondoo-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New os.date resource exposes system time and timezone; version numbering is correct (13.2.4 follows provider 13.2.3).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 18, 2026

Test Results

5 546 tests  +42   5 542 ✅ +42   2m 4s ⏱️ -1s
  412 suites + 1       4 💤 ± 0 
   31 files   ± 0       0 ❌ ± 0 

Results for commit d99cb0c. ± Comparison against base commit 70b85e8.

♻️ This comment has been updated with latest results.

Copy link
Copy Markdown

@mondoo-code-review mondoo-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Timezone detection is now much faster on tar-backed filesystems via footer parsing and common-path probing.

Copy link
Copy Markdown

@mondoo-code-review mondoo-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup commit addresses prior feedback; one previous warning about silent failure on file-count limit still applies.

@tas50
Copy link
Copy Markdown
Member Author

tas50 commented Mar 18, 2026

/review

Copy link
Copy Markdown

@mondoo-code-review mondoo-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New os.date resource adds system time and timezone detection for Unix and Windows targets.

@mondoo-code-review
Copy link
Copy Markdown

Unable to complete the code review.

Reason: The AI service is temporarily overloaded. Please retry in a few minutes.

You can try /review again or reduce the PR size.

tas50 and others added 5 commits March 18, 2026 14:05
Adds a new os.date resource to the OS provider that queries the remote
system's current time and timezone. Unlike the core time.now() which
returns the local workstation's time, os.date fetches from the connected
asset via SSH/WinRM.

Cross-platform support:
- Unix (Linux, macOS, FreeBSD, AIX, Solaris): uses date -u for UTC time
  and /etc/localtime, /etc/timezone, /etc/TIMEZONE for timezone detection
- Windows: uses PowerShell Get-Date and Get-TimeZone

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Add fast paths to avoid walking the entire zoneinfo tree when resolving
/etc/localtime, which is extremely slow on tar-backed filesystems (Docker
images, EBS snapshots). Three strategies are tried in order: parsing the
TZif v2/v3 footer for a POSIX TZ string, direct reads of common timezone
paths, and finally a capped directory walk.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- Rename errMatchFound → errWalkDone (clearer when hit by file count limit)
- Use bytes.Equal instead of string conversion for byte slice comparison
- Add comment documenting ambiguous POSIX→IANA mappings in fast path

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…ard for Windows

Strip posix/ and right/ prefixes from zoneinfo symlink targets so
/etc/localtime → .../zoneinfo/posix/Asia/Tokyo correctly returns
"Asia/Tokyo" instead of the invalid "posix/Asia/Tokyo".

Add Capability_RunCommand check to Windows date provider, matching
the Unix implementation, so static targets don't error.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@tas50 tas50 force-pushed the add-os-date-resource branch from 35a12c3 to 5704c51 Compare March 18, 2026 21:05
Copy link
Copy Markdown

@mondoo-code-review mondoo-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Querying os.date.time on static targets (EBS snapshots, Docker images) will panic or loop because nil resource return is not handled correctly.

Handle nil res.Time by setting StateIsNull|StateIsSet before returning,
preventing runtime panics on static targets (EBS snapshots, Docker images).
Update os.date version entries from 13.2.4 to 13.2.6 (next after provider 13.2.5).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@mondoo-code-review mondoo-code-review bot dismissed their stale review March 18, 2026 21:12

Superseded by new review

Copy link
Copy Markdown

@mondoo-code-review mondoo-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes nil return safety issue for os.date.time on capability-limited connections.

@tas50 tas50 added the manually-tested PR content has been manually tested against real assets label Mar 19, 2026
Copy link
Copy Markdown
Contributor

@czunker czunker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tas50 tas50 merged commit b66190c into main Mar 19, 2026
23 checks passed
@tas50 tas50 deleted the add-os-date-resource branch March 19, 2026 14:33
@github-actions github-actions bot locked and limited conversation to collaborators Mar 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

manually-tested PR content has been manually tested against real assets

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants