id: surrealdb-access-grant-expiry-sentinel
namespace: company.team
description: |
Run INFO FOR DB against a SurrealDB instance, flag any ACCESS method
configured with DURATION FOR GRANT NONE (a refresh token grant that never
expires), alert Slack on breach, and conditionally write an audit-trail
record for each flagged access method. auto_remediate and dry_run are two
independent gates; no ACCESS method definition is ever altered.
triggers:
- id: periodic_grant_check
type: io.kestra.plugin.core.trigger.Schedule
description: Check every 6 hours. Shipped disabled; enable it once
surrealdb_host/surrealdb_namespace/surrealdb_database point at a real
instance and a manual run has been verified. Always fires in safe mode
(auto_remediate false, dry_run true) regardless of this flow's own
defaults.
cron: "0 */6 * * *"
disabled: true
inputs:
auto_remediate: "false"
dry_run: "true"
inputs:
- id: surrealdb_host
type: STRING
displayName: "SurrealDB Host"
defaults: "localhost"
description: Hostname of the SurrealDB instance, matching this plugin's own
documented connection pattern.
- id: surrealdb_namespace
type: STRING
displayName: "SurrealDB Namespace"
defaults: "test"
description: SurrealDB namespace to connect to (not the Kestra flow namespace).
- id: surrealdb_database
type: STRING
displayName: "SurrealDB Database"
defaults: "test"
description: SurrealDB database audited by INFO FOR DB.
- id: surrealdb_username
type: STRING
displayName: "SurrealDB Username"
defaults: "root"
description: Must have rights to run INFO FOR DB and, if remediation runs,
CREATE records.
- id: max_unbounded_grants
type: INT
displayName: "Max Allowed Unbounded Grants"
defaults: 0
description: Breach when the count of ACCESS methods with DURATION FOR GRANT
NONE exceeds this number. Default 0 means any such access method at all is
a breach.
- id: auto_remediate
type: SELECT
displayName: "Auto Remediate"
defaults: "false"
values:
- "false"
- "true"
description: If "true" and unbounded-grant access methods are found, write an
audit-trail record for each one (still gated by dry_run below). If
"false", the risk is only reported.
- id: dry_run
type: SELECT
displayName: "Dry Run"
defaults: "true"
values:
- "true"
- "false"
description: If "true", remediation is only logged, no audit record is written.
Must be explicitly "false", together with auto_remediate "true", for the
record to actually be created.
tasks:
- id: audit_access_grants
type: io.kestra.plugin.surrealdb.Query
description: Runs INFO FOR DB, SurrealDB's own system-introspection statement,
which returns an accesses map keyed by access method name, each value
being the re-serialized DEFINE ACCESS statement text - verified from
SurrealDB's own documentation. fetchType STORE is used because this
plugin's blueprint example documents STORE as the mode that exposes a
downstream-readable uri output.
host: "{{ inputs.surrealdb_host }}"
namespace: "{{ inputs.surrealdb_namespace }}"
database: "{{ inputs.surrealdb_database }}"
username: "{{ inputs.surrealdb_username }}"
password: "{{ secret('SURREALDB_PASSWORD') }}"
query: "INFO FOR DB;"
fetchType: STORE
- id: evaluate_access_risk
type: io.kestra.plugin.scripts.python.Script
description: Reads the stored INFO FOR DB result and flags any access method
whose definition text contains "FOR GRANT NONE" - SurrealDB's own
documented syntax for a refresh-token grant that never expires. The exact
JSON shape this task's STORE output wraps the query result in was not
confirmed against a live instance, so the script handles both a bare
result object and a list-of-statement-results defensively; see this
blueprint's Pitfalls.
inputFiles:
info.json: "{{ outputs.audit_access_grants.uri }}"
script: |
import json
from kestra import Kestra
with open("info.json") as f:
raw = json.load(f)
if isinstance(raw, list):
result = (raw[0].get("result", raw[0]) if isinstance(raw[0], dict) else raw[0]) if raw else {}
else:
result = raw
accesses = (result or {}).get("accesses", {}) or {}
unbounded_accesses = sorted(
name for name, definition in accesses.items()
if "FOR GRANT NONE" in str(definition)
)
Kestra.outputs({
"unbounded_count": len(unbounded_accesses),
"unbounded_accesses": unbounded_accesses,
})
- id: evaluate_access_gate
type: io.kestra.plugin.core.flow.If
description: Breach when at least one access method grants refresh tokens that
never expire.
condition: "{{ outputs.evaluate_access_risk.vars.unbounded_count >
inputs.max_unbounded_grants }}"
then:
- id: route_remediation
type: io.kestra.plugin.core.flow.Switch
description: auto_remediate only decides whether remediation is considered at
all; dry_run, checked separately below, decides whether the audit
record is actually written.
value: "{{ inputs.auto_remediate }}"
cases:
"true":
- id: check_dry_run
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.dry_run == \"false\" }}"
then:
- id: record_flagged_access
type: io.kestra.plugin.core.flow.Loop
description: Write one audit-trail record per flagged access method, so one
failed write never blocks the rest of the batch.
values: "{{ outputs.evaluate_access_risk.vars.unbounded_accesses }}"
concurrencyLimit: 4
tasks:
- id: create_audit_record
type: io.kestra.plugin.surrealdb.Query
description: A plain CREATE, matching this plugin's own documented example. It
only adds a new security_audit_log record; it never runs
ALTER ACCESS or touches the flagged access method's own
definition - this blueprint does not attempt to change
DURATION FOR GRANT, since ALTER ACCESS's support for
modifying DURATION was not confirmed from available
documentation.
host: "{{ inputs.surrealdb_host }}"
namespace: "{{ inputs.surrealdb_namespace }}"
database: "{{ inputs.surrealdb_database }}"
username: "{{ inputs.surrealdb_username }}"
password: "{{ secret('SURREALDB_PASSWORD') }}"
query: "CREATE security_audit_log SET access_name = '{{ item.value }}', risk =
'unbounded_grant_duration', flagged_at = time::now();"
- id: log_remediation_applied
type: io.kestra.plugin.core.log.Log
message: "Recorded {{ outputs.evaluate_access_risk.vars.unbounded_count }}
unbounded-grant access method(s) to security_audit_log in {{
inputs.surrealdb_database }}."
else:
- id: log_dry_run_remediation
type: io.kestra.plugin.core.log.Log
message: "DRY RUN: would record {{
outputs.evaluate_access_risk.vars.unbounded_count }}
unbounded-grant access method(s) to security_audit_log in {{
inputs.surrealdb_database }} (dry_run is true). No record
written."
"false":
- id: log_remediation_disabled
type: io.kestra.plugin.core.log.Log
message: "auto_remediate is false: {{
outputs.evaluate_access_risk.vars.unbounded_count }} access
method(s) in {{ inputs.surrealdb_database }} grant refresh
tokens that never expire, but no audit record was written."
- id: alert_access_risk
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Always alert on any risk, independent of whether it was recorded.
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{
"text": "SurrealDB access grant expiry sentinel: {{ outputs.evaluate_access_risk.vars.unbounded_count }} access method(s) in database {{ inputs.surrealdb_database }} on {{ inputs.surrealdb_host }} grant refresh tokens that never expire (DURATION FOR GRANT NONE). auto_remediate: {{ inputs.auto_remediate }}. dry_run: {{ inputs.dry_run }}. Recorded: {{ inputs.auto_remediate == \"true\" and inputs.dry_run == \"false\" }}. Execution {{ execution.id }}."
}
else:
- id: log_access_healthy
type: io.kestra.plugin.core.log.Log
message: "{{ inputs.surrealdb_database }} is healthy: 0 access methods grant
refresh tokens that never expire."
- id: log_audit_complete
type: io.kestra.plugin.core.log.Log
description: Final status line for every run, independent of which branch fired,
so the execution history always shows the headline numbers.
message: "Access grant audit complete for {{ inputs.surrealdb_database }}.
unbounded_count={{ outputs.evaluate_access_risk.vars.unbounded_count }}.
threshold={{ inputs.max_unbounded_grants }}. auto_remediate={{
inputs.auto_remediate }}. dry_run={{ inputs.dry_run }}."
errors:
- id: alert_execution_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alert when the flow itself fails outside the handled branches, for
example an unreachable SurrealDB instance or insufficient privileges to
run INFO FOR DB.
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{
"text": "SurrealDB access grant expiry sentinel FAILED in execution {{ execution.id }} for {{ inputs.surrealdb_database }} on {{ inputs.surrealdb_host }}. The access audit did not complete; investigate before trusting the last known grant status."
}