Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ jobs:
psql -h localhost --user postgres -d batondb -t -c "SELECT attr_first_name, attr_job_title FROM users WHERE username = 'jane.doe'" | grep -q "Janet.*Senior Software Engineer"
env:
PGPASSWORD: secretpassword
# Uses the baton CLI that the sync-test action installs.
- name: Run event feed smoke test
run: test/event_feed_smoke.sh postgres
env:
PGPASSWORD: secretpassword
CHANGED_USER: jane.doe
SQL_CMD: psql -h localhost --user postgres -d batondb -t -A
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ Resource types and grants queries can define an `event_feed` section so Conducto

The overlap window re-reads recent rows so rows with tied or late-committed timestamps are not missed. Re-read rows produce the same event IDs, and ConductorOne ignores the duplicates. Resource types with an `event_feed` also need a `get` query that uses `?<resource_id>`. See [`examples/example.yml`](examples/example.yml) for a complete configuration.

The cursor column (for example `updated_at`) must change on every insert and update, including updates made by provisioning and actions. Use a trigger or an `ON UPDATE` default; otherwise every writer has to set it explicitly.

#### Grant revokes and soft deletes

A grants `event_feed` reports revokes through `revokes_query`, which can only see rows that are soft-deleted (for example, rows with a `deleted_at` timestamp). A hard `DELETE` leaves nothing to read, so the revoke is only picked up at the next full sync. If your table hard-deletes, add a soft-delete column or an audit table filled by a trigger. When you configure `revokes_query`:

- The full-sync grants `query` must exclude soft-deleted rows (for example `WHERE deleted_at IS NULL`). Otherwise every full sync restores the grants the feed revoked.
- Revoke provisioning must soft-delete the row (set `deleted_at` and `updated_at`) instead of deleting it.
- Grant provisioning must revive a soft-deleted row (clear `deleted_at`, update `updated_at`) instead of inserting a duplicate or doing nothing.

The test examples (`postgres-test.yml`, `sqlserver-test.yml`, `mysql-test.yml`, `vertica-test.yml`) follow these rules, and `test/event_feed_smoke.sh` checks them against the matching `test/*-init.sql` database.

**Multi-database limitation:** event feed queries and `get` queries run only against the primary database (the lexicographically first one), even when `connect.databases` is configured. Full syncs still cover every database, but between full syncs:

- Changes in any other database produce no events.
Expand Down
6 changes: 5 additions & 1 deletion baton_capabilities.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
},
"capabilities": [
"CAPABILITY_SYNC",
"CAPABILITY_TARGETED_SYNC",
"CAPABILITY_PROVISION"
],
"permissions": {}
}
],
"connectorCapabilities": [
"CAPABILITY_PROVISION",
"CAPABILITY_SYNC"
"CAPABILITY_SYNC",
"CAPABILITY_TARGETED_SYNC",
"CAPABILITY_EVENT_FEED_V2",
"CAPABILITY_SERVICE_MODE_TARGETED_SYNC"
],
"credentialDetails": {}
}
23 changes: 19 additions & 4 deletions examples/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,27 @@ resource_types:
vars:
user_id: "principal.ID"
access_level: "'basic'"
# Grant revives a soft-deleted row, or inserts a new one. Upsert syntax depends on
# the database (e.g. ON CONFLICT in PostgreSQL, MERGE in SQL Server or Oracle).
grant:
queries:
- |
UPDATE user_access
SET deleted_at = NULL, updated_at = CURRENT_TIMESTAMP
WHERE user_id = ?<user_id> AND level = ?<access_level> AND deleted_at IS NOT NULL
- |
INSERT INTO user_access (user_id, level)
VALUES (?<user_id>, ?<access_level>)
SELECT ?<user_id>, ?<access_level> FROM DUAL
WHERE NOT EXISTS (
SELECT 1 FROM user_access WHERE user_id = ?<user_id> AND level = ?<access_level>
)
# Revoke soft-deletes so the grants event feed's revokes_query can report it.
revoke:
queries:
- |
DELETE FROM user_access
WHERE user_id = ?<user_id>
UPDATE user_access
SET deleted_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP
WHERE user_id = ?<user_id> AND deleted_at IS NULL

# Grants Query Configuration
# --------------------------
Expand All @@ -157,6 +168,7 @@ resource_types:
access_level,
updated_at
FROM user_access
WHERE deleted_at IS NULL
LIMIT ?<Limit> OFFSET ?<Offset>
map:
- skip_if: ".access_level != 'basic'"
Expand All @@ -183,9 +195,12 @@ resource_types:
AND updated_at >= ?<since>
ORDER BY updated_at ASC, id ASC
LIMIT ?<Limit> OFFSET ?<Offset>
# updated_at must change on every insert and update (a trigger or ON UPDATE default).
changes_cursor_column: updated_at

# Optional: only configure when soft-delete is available in the schema.
# Optional: only configure when the table soft-deletes rows. Hard deletes can't be
# detected. When set, the full-sync grants query must exclude soft-deleted rows, revoke
# must soft-delete, and grant must revive a soft-deleted row (see provisioning above).
revokes_query: |
SELECT id AS grant_id, user_id, access_level, deleted_at
FROM user_access
Expand Down
4 changes: 3 additions & 1 deletion examples/mysql-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,13 @@ resource_types:
user_roles ur ON u.id = ur.user_id
JOIN
roles r ON r.id = ur.role_id
WHERE
ur.deleted_at IS NULL
pagination:
strategy: "offset"
primary_key: "username"
map:
- skip_if: ".role_name != resource.ID"
- skip_if: "string(.role_name) != resource.ID"
principal_id: ".username"
principal_type: "user"
entitlement_id: "member"
Expand Down
7 changes: 6 additions & 1 deletion examples/postgres-test-exclusion-group.yml
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ resource_types:
JOIN
roles r ON r.id = ur.role_id
WHERE u.username = ?<username>
AND ur.deleted_at IS NULL
map:
- principal_id: ".username"
principal_type: "user"
Expand All @@ -423,7 +424,9 @@ resource_types:
FROM users u, roles r
WHERE u.username = ?<username>
AND r.role_name = ?<role_name>
ON CONFLICT (user_id, role_id) DO NOTHING
ON CONFLICT (user_id, role_id) DO UPDATE
SET deleted_at = NULL, updated_at = CURRENT_TIMESTAMP
WHERE user_roles.deleted_at IS NOT NULL
revoke:
validation_queries:
- |
Expand Down Expand Up @@ -456,6 +459,8 @@ resource_types:
user_roles ur ON u.id = ur.user_id
JOIN
roles r ON r.id = ur.role_id
WHERE
ur.deleted_at IS NULL
pagination:
strategy: "offset"
primary_key: "username"
Expand Down
12 changes: 9 additions & 3 deletions examples/postgres-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,9 @@ resource_types:
FROM users u, roles r
WHERE u.username = ?<username>
AND r.role_name = ?<role_name>
ON CONFLICT (user_id, role_id) DO NOTHING
ON CONFLICT (user_id, role_id) DO UPDATE
SET deleted_at = NULL, updated_at = CURRENT_TIMESTAMP
WHERE user_roles.deleted_at IS NOT NULL
revoke:
validation_queries:
- |
Expand All @@ -499,16 +501,18 @@ resource_types:
FROM users u, roles r
WHERE u.username = ?<username>
AND r.role_name = ?<role_name>
# Soft delete so the event feed's revokes_query can report the revoke.
queries:
- |
DELETE FROM user_roles
UPDATE user_roles
SET deleted_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP
WHERE user_id IN (
SELECT id FROM users
WHERE username = ?<username>
) AND role_id IN (
SELECT id FROM roles
WHERE role_name = ?<role_name>
)
) AND deleted_at IS NULL

# Dynamic grants based on SQL queries to associate users with roles
grants:
Expand All @@ -522,6 +526,8 @@ resource_types:
user_roles ur ON u.id = ur.user_id
JOIN
roles r ON r.id = ur.role_id
WHERE
ur.deleted_at IS NULL
pagination:
strategy: "offset"
primary_key: "username"
Expand Down
21 changes: 20 additions & 1 deletion examples/sqlserver-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,18 @@ resource_types:
vars:
username: "principal.ID"
role_name: "resource.ID"
# Revives a soft-deleted assignment, or inserts a new one.
grant:
queries:
- |
UPDATE ur
SET IsDeleted = 0, DeletedAt = NULL, UpdatedAt = SYSUTCDATETIME()
FROM UserRoles ur
INNER JOIN Users u ON ur.UserID = u.UserID
INNER JOIN Roles r ON ur.RoleID = r.RoleID
WHERE u.Username = ?<username>
AND r.RoleName = ?<role_name>
AND ur.IsDeleted = 1
- |
INSERT INTO UserRoles (
UserID,
Expand All @@ -420,14 +430,22 @@ resource_types:
FROM Users u, Roles r
WHERE u.Username = ?<username>
AND r.RoleName = ?<role_name>
AND NOT EXISTS (
SELECT 1 FROM UserRoles ur
WHERE ur.UserID = u.UserID AND ur.RoleID = r.RoleID
)
# Soft delete so the event feed's revokes_query can report the revoke.
revoke:
queries:
- |
DELETE ur FROM UserRoles ur
UPDATE ur
SET IsDeleted = 1, DeletedAt = SYSUTCDATETIME(), UpdatedAt = SYSUTCDATETIME()
FROM UserRoles ur
INNER JOIN Users u ON ur.UserID = u.UserID
INNER JOIN Roles r ON ur.RoleID = r.RoleID
WHERE u.Username = ?<username>
AND r.RoleName = ?<role_name>
AND ur.IsDeleted = 0

# Grant discovery for existing role assignments
grants:
Expand All @@ -438,6 +456,7 @@ resource_types:
FROM UserRoles ur
INNER JOIN Users u ON ur.UserID = u.UserID
INNER JOIN Roles r ON ur.RoleID = r.RoleID
WHERE ur.IsDeleted = 0
ORDER BY r.RoleID
OFFSET ?<Offset> ROWS FETCH NEXT ?<Limit> ROWS ONLY
map:
Expand Down
5 changes: 4 additions & 1 deletion examples/vertica-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ resource_types:
LEFT JOIN users m ON u.manager_id = m.id
WHERE u.username = ?<resource_id>

# Detects new and modified users via updated_at.
# Detects new and modified users via updated_at. Vertica has no triggers, so anything
# that writes to these tables must set updated_at (and deleted_at on revoke) itself.
event_feed:
query: |
SELECT
Expand Down Expand Up @@ -158,6 +159,8 @@ resource_types:
user_roles ur ON u.id = ur.user_id
JOIN
roles r ON r.id = ur.role_id
WHERE
ur.deleted_at IS NULL
pagination:
strategy: "offset"
primary_key: "username"
Expand Down
137 changes: 137 additions & 0 deletions test/event_feed_smoke.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/env bash
# Smoke test for the event feed of the shipped examples against a freshly loaded test database.
#
# Usage: test/event_feed_smoke.sh <postgres|mysql|sqlserver|vertica|oracle>
#
# Environment:
# BATON_SQL path to the connector binary (default: ./baton-sql)
# BATON_CONFIG_PATH example config for the engine, plus the DB_* variables it reads
# SQL_CMD command that reads one SQL statement on stdin and prints the result rows.
# Required for engines whose example provisions role grants.
# CHANGED_USER optional user whose row was updated before running; the feed must report it.
set -euo pipefail

ENGINE="${1:?usage: $0 <postgres|mysql|sqlserver|vertica|oracle>}"
BATON_SQL="${BATON_SQL:-./baton-sql}"
WORKDIR="$(mktemp -d)"
trap 'rm -rf "$WORKDIR"' EXIT

# Seeded soft-deleted assignment, and a live assignment to revoke and grant again.
SEEDED_USER="disabled.user"
SEEDED_ROLE="reader"
LIVE_USER="service.acct"
LIVE_ROLE="user"

case "$ENGINE" in
postgres | mysql | vertica)
HAS_GRANTS_FEED=1
deleted_at_sql="SELECT CASE WHEN ur.deleted_at IS NULL THEN 'live' ELSE 'deleted' END FROM user_roles ur JOIN users u ON u.id = ur.user_id JOIN roles r ON r.id = ur.role_id WHERE u.username = '$LIVE_USER' AND r.role_name = '$LIVE_ROLE'"
;;
sqlserver)
HAS_GRANTS_FEED=1
deleted_at_sql="SELECT CASE WHEN ur.IsDeleted = 0 AND ur.DeletedAt IS NULL THEN 'live' ELSE 'deleted' END FROM UserRoles ur JOIN Users u ON u.UserID = ur.UserID JOIN Roles r ON r.RoleID = ur.RoleID WHERE u.Username = '$LIVE_USER' AND r.RoleName = '$LIVE_ROLE'"
;;
oracle)
HAS_GRANTS_FEED=0
;;
*)
echo "unknown engine: $ENGINE" >&2
exit 2
;;
esac

case "$ENGINE" in
postgres | sqlserver) HAS_ROLE_PROVISIONING=1 ;;
*) HAS_ROLE_PROVISIONING=0 ;;
esac

fail() {
echo "FAIL: $*" >&2
exit 1
}

run_feed() {
local out="$WORKDIR/feed.json"
"$BATON_SQL" --event-feed=true --event-feed-id=sql_event_feed --log-level=error >"$out" ||
fail "event feed exited with $?"
echo "$out"
}

# protojson randomly adds a space after ':' and ',', so the patterns allow an optional one.
has_event() {
local file="$1" kind="$2" role="$3" user="$4"
grep "\"$kind\"" "$file" | grep -E "\"resource\": ?\"$role\"" |
grep -qE "\"resourceType\": ?\"user\", ?\"resource\": ?\"$user\""
}

run_sync() {
local c1z="$WORKDIR/sync.c1z"
rm -f "$c1z"
"$BATON_SQL" -f "$c1z" --log-level=error >/dev/null 2>&1 || fail "full sync exited with $?"
echo "$c1z"
}

synced_grant() {
synced_grant_id "$1" "role:$LIVE_ROLE:member:user:$LIVE_USER"
}

synced_grant_id() {
local grants="$WORKDIR/grants.json"
baton grants -f "$1" -o json >"$grants" || fail "baton grants exited with $?"
grep -q "\"$2\"" "$grants"
}

db_state() {
echo "$deleted_at_sql" | $SQL_CMD | awk 'NF { print $1; exit }'
}

echo "== feed runs and reports seeded changes"
feed="$(run_feed)"
grep -q '"resourceChangeEvent"' "$feed" || fail "no resource change events"
if [ -n "${CHANGED_USER:-}" ]; then
grep '"resourceChangeEvent"' "$feed" | grep -qE "\"resourceType\": ?\"user\", ?\"resource\": ?\"$CHANGED_USER\"" ||
fail "no resource change event for $CHANGED_USER"
fi
if [ "$HAS_GRANTS_FEED" = 1 ]; then
has_event "$feed" createRevokeEvent "$SEEDED_ROLE" "$SEEDED_USER" || fail "no revoke event for seeded $SEEDED_USER/$SEEDED_ROLE"
has_event "$feed" createGrantEvent "$SEEDED_ROLE" "$SEEDED_USER" && fail "grant event for soft-deleted $SEEDED_USER/$SEEDED_ROLE"
c1z="$(run_sync)"
synced_grant_id "$c1z" "role:$SEEDED_ROLE:member:user:$SEEDED_USER" &&
fail "full sync returned soft-deleted grant $SEEDED_USER/$SEEDED_ROLE"
fi

if [ "$HAS_ROLE_PROVISIONING" = 0 ]; then
echo "PASS ($ENGINE: feed only)"
exit 0
fi
: "${SQL_CMD:?SQL_CMD is required for $ENGINE}"

echo "== revoke through the connector soft-deletes"
c1z="$(run_sync)"
synced_grant "$c1z" || fail "expected live grant $LIVE_USER/$LIVE_ROLE before revoke"
"$BATON_SQL" -f "$c1z" --revoke-grant="role:$LIVE_ROLE:member:user:$LIVE_USER" --log-level=error >/dev/null 2>&1 ||
fail "revoke failed"
[ "$(db_state)" = "deleted" ] || fail "row for $LIVE_USER/$LIVE_ROLE is not soft-deleted"
"$BATON_SQL" -f "$c1z" --revoke-grant="role:$LIVE_ROLE:member:user:$LIVE_USER" --log-level=error >/dev/null 2>&1 ||
fail "revoking an already revoked grant failed"
feed="$(run_feed)"
has_event "$feed" createRevokeEvent "$LIVE_ROLE" "$LIVE_USER" || fail "no revoke event after revoke"
has_event "$feed" createGrantEvent "$LIVE_ROLE" "$LIVE_USER" && fail "grant event after revoke"

echo "== full sync keeps it revoked"
c1z="$(run_sync)"
synced_grant "$c1z" && fail "full sync restored revoked grant"

echo "== grant through the connector revives the row"
"$BATON_SQL" -f "$c1z" --grant-entitlement="role:$LIVE_ROLE:member" --grant-principal="$LIVE_USER" \
--grant-principal-type=user --log-level=error >/dev/null 2>&1 || fail "grant failed"
[ "$(db_state)" = "live" ] || fail "row for $LIVE_USER/$LIVE_ROLE was not revived"
"$BATON_SQL" -f "$c1z" --grant-entitlement="role:$LIVE_ROLE:member" --grant-principal="$LIVE_USER" \
--grant-principal-type=user --log-level=error >/dev/null 2>&1 || fail "granting an already live grant failed"
feed="$(run_feed)"
has_event "$feed" createGrantEvent "$LIVE_ROLE" "$LIVE_USER" || fail "no grant event after grant"
has_event "$feed" createRevokeEvent "$LIVE_ROLE" "$LIVE_USER" && fail "revoke event after grant"
c1z="$(run_sync)"
synced_grant "$c1z" || fail "full sync is missing the granted $LIVE_USER/$LIVE_ROLE"

echo "PASS ($ENGINE)"
Loading
Loading