id: user-access-review-sod
namespace: company.team
description: |
Quarterly user access review (SOX ITGC, ISO 27001 A.5.18): pull every account from each
in-scope application, match it to the HR file, and flag terminated users still active,
orphan and shared accounts, dormant access, privileged roles without a ticket, and
segregation-of-duties conflicts across applications. Each manager certifies or revokes
the access of their reports; anything not certified by the deadline is revoked. The run
produces the revocation list, the evidence for the auditors and the open exceptions.
inputs:
- id: review_id
type: STRING
displayName: Review (YYYY-Qn)
defaults: "2026-Q3"
validator: ^\d{4}-Q[1-4]$
- id: as_of
type: STRING
displayName: As of
defaults: "2026-09-30"
validator: ^\d{4}-\d{2}-\d{2}$
- id: phase
type: SELECT
displayName: Phase
description: >
OPEN builds the campaign and the certification list for managers. CLOSE
applies the decisions received, revokes everything not certified, and
locks the review.
values:
- OPEN
- CLOSE
defaults: OPEN
- id: decisions
type: JSON
displayName: Manager decisions
description: >-
Decisions keyed "application|account", value KEEP or REVOKE, for example
{"ERP|jdoe": "KEEP"}. A SoD conflict can only be kept with a mitigating
control, written as "KEEP: <control>".
defaults: "{}"
- id: dormant_days
type: INT
displayName: Dormant after (days)
defaults: 90
- id: scenario
type: SELECT
displayName: Demo scenario
description: ISSUES adds an application extract with no export date and an
account list that is empty, which must stop the review.
values:
- CLEAN
- ISSUES
defaults: CLEAN
- id: notify_slack
type: BOOL
displayName: Notify Slack
description: Post a failure alert to Slack. Needs the SLACK_WEBHOOK_URL secret.
defaults: false
variables:
state_key: access_reviews
pack_dir: "access-review/{{ inputs.review_id }}"
concurrency:
limit: 1
tasks:
- id: state
type: io.kestra.plugin.core.output.OutputValues
values:
closed: "{{ (kv(vars.state_key, errorOnMissing=false) ?? {'closed': []}) |
jq('.closed') | first | toJson }}"
- id: closed_gate
type: io.kestra.plugin.core.flow.If
condition: "{{ fromJson(outputs.state.values.closed) contains inputs.review_id }}"
then:
- id: already_closed
type: io.kestra.plugin.core.execution.Fail
errorMessage: "The {{ inputs.review_id }} access review is closed. Start a new
review instead of changing a certified one."
- id: review
type: io.kestra.plugin.jdbc.duckdb.Queries
description: >
One DuckDB session: HR file, application account extracts, role catalog
with privilege flags, SoD rule set and access tickets (demo generator or
your IAM, HRIS and ITSM), the findings, the certification list per
manager, the decisions and the revocation list.
outputFiles:
- findings
- certification
- revocations
- evidence
- blockers
fetchType: FETCH_ONE
sql: |
SET VARIABLE d = DATE '{{ inputs.as_of }}';
-- =======================================================================
-- HR file. Replace with your HRIS export.
-- =======================================================================
CREATE TABLE hr AS
SELECT 'u' || lpad(i::VARCHAR, 3, '0') AS user_id, 'Employee ' || i AS name,
['Finance', 'Finance', 'Procurement', 'Sales', 'Engineering', 'Warehouse'][1 + i % 6] AS department,
'u' || lpad((1 + (i - 1) // 10)::VARCHAR, 3, '0') AS manager,
CASE WHEN i IN (23, 47, 71) THEN getvariable('d') - (10 + i % 40)::INT ELSE NULL END AS terminated_on
FROM range(1, 91) t(i);
-- Applications in scope, the date each extract was taken, and the role catalog.
CREATE TABLE apps (app VARCHAR, owner VARCHAR, extracted_on DATE);
INSERT INTO apps VALUES ('ERP', 'u002', getvariable('d')), ('BANK', 'u003', getvariable('d')), ('CRM', 'u004', getvariable('d')), ('CLOUD', 'u005', getvariable('d'));
{% if inputs.scenario == 'ISSUES' %}
INSERT INTO apps VALUES ('HRIS', 'u006', NULL);
{% endif %}
CREATE TABLE roles (app VARCHAR, role VARCHAR, privileged BOOLEAN, duty VARCHAR);
INSERT INTO roles VALUES
('ERP', 'AP_CLERK', false, 'ENTER_INVOICE'), ('ERP', 'AP_APPROVER', false, 'APPROVE_PAYMENT'), ('ERP', 'VENDOR_MASTER', false, 'MAINTAIN_VENDOR'),
('ERP', 'PO_BUYER', false, 'CREATE_PO'), ('ERP', 'GOODS_RECEIPT', false, 'RECEIVE_GOODS'), ('ERP', 'GL_ACCOUNTANT', false, 'POST_JOURNAL'),
('ERP', 'SYS_ADMIN', true, 'ADMINISTER'), ('BANK', 'PAYMENT_RELEASE', false, 'RELEASE_PAYMENT'), ('BANK', 'BANK_ADMIN', true, 'ADMINISTER'),
('CRM', 'SALES_REP', false, 'SELL'), ('CRM', 'CRM_ADMIN', true, 'ADMINISTER'), ('CLOUD', 'DEVELOPER', false, 'DEPLOY'), ('CLOUD', 'ORG_OWNER', true, 'ADMINISTER');
-- Segregation of duties: pairs of duties no one person may hold, across applications.
CREATE TABLE sod_rules (rule VARCHAR, duty_a VARCHAR, duty_b VARCHAR, risk VARCHAR);
INSERT INTO sod_rules VALUES
('SOD-01', 'MAINTAIN_VENDOR', 'APPROVE_PAYMENT', 'Create a fake vendor and pay it'),
('SOD-02', 'ENTER_INVOICE', 'RELEASE_PAYMENT', 'Enter and pay an invoice alone'),
('SOD-03', 'CREATE_PO', 'RECEIVE_GOODS', 'Order and confirm receipt of goods never delivered'),
('SOD-04', 'APPROVE_PAYMENT', 'RELEASE_PAYMENT', 'Approve and release the same payment'),
('SOD-05', 'POST_JOURNAL', 'ADMINISTER', 'Post entries and change the audit trail');
-- =======================================================================
-- Account extracts: one row per account and role. Replace with each
-- application's user export.
-- =======================================================================
CREATE TABLE accounts AS
SELECT a.app, h.user_id AS account, h.user_id, r.role, (getvariable('d') - (hash(h.user_id || r.role) % 60)::INT) AS last_login, 'TKT-' || (hash(h.user_id || r.role) % 9000 + 1000) AS ticket
FROM hr h JOIN roles r ON (
(h.department = 'Finance' AND r.role IN ('AP_CLERK', 'GL_ACCOUNTANT')) OR
(h.department = 'Procurement' AND r.role IN ('PO_BUYER')) OR
(h.department = 'Warehouse' AND r.role IN ('GOODS_RECEIPT')) OR
(h.department = 'Sales' AND r.role = 'SALES_REP') OR
(h.department = 'Engineering' AND r.role = 'DEVELOPER'))
JOIN apps a ON a.app = r.app;
-- The quarter's findings, planted on fixed users so every run sees the same review.
INSERT INTO accounts VALUES
('ERP', 'u013', 'u013', 'VENDOR_MASTER', getvariable('d') - 3, 'TKT-4410'), -- finance clerk can also maintain vendors...
('ERP', 'u013', 'u013', 'AP_APPROVER', getvariable('d') - 3, 'TKT-4411'), -- ...and approve payments: SOD-01
('BANK', 'u007', 'u007', 'PAYMENT_RELEASE', getvariable('d') - 1, 'TKT-3305'), -- AP clerk who releases payments: SOD-02
('ERP', 'u014', 'u014', 'GOODS_RECEIPT', getvariable('d') - 8, 'TKT-5120'), -- buyer who receives goods: SOD-03
('ERP', 'u019', 'u019', 'SYS_ADMIN', getvariable('d') - 2, NULL), -- accountant with admin and no ticket: SOD-05 + privileged without ticket
('CLOUD', 'u029', 'u029', 'ORG_OWNER', getvariable('d') - 120, 'TKT-2001'), -- dormant privileged
('CRM', 'sales-shared', NULL, 'SALES_REP', getvariable('d') - 2, NULL), -- shared account, no owner
('BANK', 'contractor7', NULL, 'PAYMENT_RELEASE', getvariable('d') - 30, NULL); -- orphan: no one in HR
UPDATE accounts SET last_login = getvariable('d') - 140 WHERE account IN ('u034', 'u040') AND app = 'CLOUD';
{% if inputs.scenario == 'ISSUES' %}
DELETE FROM accounts WHERE app = 'CRM';
{% endif %}
-- =======================================================================
-- Checks on the extracts themselves.
-- =======================================================================
CREATE TABLE blockers AS
SELECT 'EXTRACT_NOT_DATED' AS code, app AS ref, 'No export date: the auditors cannot rely on an extract of unknown date' AS detail FROM apps WHERE extracted_on IS NULL
UNION ALL SELECT 'EXTRACT_STALE', app, 'Extract taken on ' || extracted_on || ', more than 7 days before the review date' FROM apps WHERE extracted_on < getvariable('d') - 7
UNION ALL SELECT 'EMPTY_EXTRACT', app, 'No account in the extract' FROM apps a WHERE extracted_on IS NOT NULL AND NOT EXISTS (SELECT 1 FROM accounts x WHERE x.app = a.app)
UNION ALL SELECT 'UNKNOWN_ROLE', app || ' ' || role, 'Role not in the role catalog' FROM accounts x WHERE NOT EXISTS (SELECT 1 FROM roles r WHERE r.app = x.app AND r.role = x.role);
-- =======================================================================
-- Findings.
-- =======================================================================
CREATE TABLE acc AS
SELECT x.*, r.privileged, r.duty, h.name, h.manager, h.terminated_on
FROM accounts x LEFT JOIN roles r USING (app, role) LEFT JOIN hr h ON h.user_id = x.user_id;
CREATE TABLE findings AS
SELECT 'TERMINATED_USER_ACTIVE' AS finding, 'HIGH' AS severity, app, account, role, 'Left on ' || terminated_on || ', account still active' AS detail, 'REVOKE' AS forced
FROM acc WHERE terminated_on IS NOT NULL
UNION ALL SELECT 'ORPHAN_ACCOUNT', 'HIGH', app, account, role, 'No matching employee in the HR file', 'REVOKE' FROM acc WHERE user_id IS NULL AND account NOT LIKE '%shared%'
UNION ALL SELECT 'SHARED_ACCOUNT', 'HIGH', app, account, role, 'Generic account: actions cannot be traced to a person', 'REVOKE' FROM acc WHERE account LIKE '%shared%'
UNION ALL SELECT 'PRIVILEGED_WITHOUT_TICKET', 'HIGH', app, account, role, 'Privileged role with no approved access request', NULL FROM acc WHERE privileged AND ticket IS NULL
UNION ALL SELECT 'DORMANT', CASE WHEN privileged THEN 'HIGH' ELSE 'MEDIUM' END, app, account, role, 'No login for ' || date_diff('day', last_login, getvariable('d')) || ' days', NULL
FROM acc WHERE last_login < getvariable('d') - {{ inputs.dormant_days }} AND terminated_on IS NULL
UNION ALL
SELECT 'SOD_CONFLICT', 'HIGH', a.app || '+' || b.app, a.user_id, a.role || '+' || b.role, s.rule || ': ' || s.risk, NULL
FROM acc a JOIN acc b ON a.user_id = b.user_id AND (a.app, a.role) < (b.app, b.role)
JOIN sod_rules s ON (s.duty_a = a.duty AND s.duty_b = b.duty) OR (s.duty_a = b.duty AND s.duty_b = a.duty)
WHERE a.user_id IS NOT NULL;
-- =======================================================================
-- Certification list: every account, grouped by the manager who certifies.
-- Orphans and shared accounts go to the application owner.
-- =======================================================================
CREATE TABLE certification AS
SELECT coalesce(x.manager, (SELECT owner FROM apps p WHERE p.app = x.app)) AS certifier, x.app, x.account, x.user_id, x.name, x.role, x.privileged, x.last_login,
(SELECT string_agg(f.finding, ', ') FROM findings f WHERE f.account = x.account AND (f.app = x.app OR f.app LIKE '%' || x.app || '%')
AND (f.role = x.role OR f.role LIKE '%' || x.role || '%')) AS findings,
(SELECT max(f.forced) FROM findings f WHERE f.account = x.account AND f.app = x.app AND f.role = x.role) AS forced
FROM acc x;
-- Decisions received. Keys are "application|account" (all roles of the account in that app).
CREATE TABLE given AS SELECT split_part(key, '|', 1) AS app, split_part(key, '|', 2) AS account, trim(value::VARCHAR, '"') AS decision
FROM json_each('{{ inputs.decisions | toJson | replace({"'": "''"}) }}');
CREATE TABLE decided AS
SELECT c.*, g.decision,
CASE
WHEN c.forced = 'REVOKE' THEN 'REVOKE'
WHEN g.decision IS NULL THEN CASE WHEN '{{ inputs.phase }}' = 'CLOSE' THEN 'REVOKE' ELSE 'PENDING' END
WHEN upper(g.decision) LIKE 'REVOKE%' THEN 'REVOKE'
WHEN c.findings LIKE '%SOD_CONFLICT%' AND NOT regexp_matches(g.decision, '^KEEP:\s*\S') THEN CASE WHEN '{{ inputs.phase }}' = 'CLOSE' THEN 'REVOKE' ELSE 'PENDING' END
ELSE 'KEEP' END AS outcome,
CASE
WHEN c.forced = 'REVOKE' THEN 'Forced: ' || c.findings
WHEN g.decision IS NULL THEN CASE WHEN '{{ inputs.phase }}' = 'CLOSE' THEN 'Not certified by the deadline' ELSE 'Waiting for ' || c.certifier END
WHEN c.findings LIKE '%SOD_CONFLICT%' AND NOT regexp_matches(g.decision, '^KEEP:\s*\S') AND upper(g.decision) NOT LIKE 'REVOKE%'
THEN 'SoD conflict kept without a mitigating control'
ELSE 'Decision of ' || c.certifier || ': ' || g.decision END AS reason
FROM certification c LEFT JOIN given g ON g.app = c.app AND g.account = c.account;
CREATE TABLE revocations AS
SELECT app, account, role, reason FROM decided WHERE outcome = 'REVOKE' ORDER BY app, account, role;
-- Evidence: population per application, completeness of certification, findings, outcome.
CREATE TABLE evidence AS
SELECT p.app, p.extracted_on, (SELECT count(*) FROM accounts x WHERE x.app = p.app) AS accounts,
(SELECT count(*) FROM decided x WHERE x.app = p.app AND x.outcome <> 'PENDING') AS decided,
(SELECT count(*) FROM decided x WHERE x.app = p.app AND x.outcome = 'REVOKE') AS revoked,
(SELECT count(*) FROM findings f WHERE f.app LIKE '%' || p.app || '%') AS findings
FROM apps p ORDER BY p.app;
COPY (SELECT * FROM findings ORDER BY severity, finding, app, account) TO '{{ outputFiles.findings }}' (HEADER, DELIMITER ',');
COPY (SELECT certifier, app, account, name, role, privileged, last_login, findings, decision, outcome, reason FROM decided ORDER BY certifier, app, account, role) TO '{{ outputFiles.certification }}' (HEADER, DELIMITER ',');
COPY revocations TO '{{ outputFiles.revocations }}' (HEADER, DELIMITER ',');
COPY evidence TO '{{ outputFiles.evidence }}' (HEADER, DELIMITER ',');
COPY blockers TO '{{ outputFiles.blockers }}' (HEADER, DELIMITER ',');
SELECT
(SELECT count(*) FROM accounts)::INT AS accounts, (SELECT count(DISTINCT coalesce(user_id, account)) FROM accounts)::INT AS identities,
(SELECT string_agg(finding || ' ' || n, ', ' ORDER BY n DESC) FROM (SELECT finding, count(*) AS n FROM findings GROUP BY finding)) AS findings,
(SELECT coalesce(string_agg(account || ' ' || role || ' (' || detail || ')', '; '), '') FROM findings WHERE finding = 'SOD_CONFLICT') AS sod_detail,
(SELECT count(*) FROM decided WHERE outcome = 'PENDING')::INT AS pending,
(SELECT count(DISTINCT certifier) FROM decided WHERE outcome = 'PENDING')::INT AS pending_certifiers,
(SELECT count(*) FROM decided WHERE outcome = 'KEEP')::INT AS kept,
(SELECT count(*) FROM revocations)::INT AS revoked,
(SELECT coalesce(string_agg(DISTINCT reason, '; '), '') FROM decided WHERE reason LIKE 'SoD conflict kept%' OR reason = 'Not certified by the deadline') AS revoke_reasons,
(SELECT count(*) FROM blockers)::INT AS blockers,
(SELECT coalesce(string_agg(code || ' ' || ref || ': ' || detail, '; ' ORDER BY code, ref), '') FROM blockers) AS blocker_detail;
- id: result
type: io.kestra.plugin.core.output.OutputValues
values:
r: "{{ (outputs.review.outputs | last).row | toJson }}"
- id: log_review
type: io.kestra.plugin.core.log.Log
message: |
Access review {{ inputs.review_id }}, phase {{ inputs.phase }} ({{ inputs.scenario }}).
{{ outputs.result.values.r | jq('"\(.accounts) accounts for \(.identities) identities. Findings: \(.findings)."') | first }}
{{ outputs.result.values.r | jq('"SoD conflicts: \(.sod_detail)."') | first }}
{{ outputs.result.values.r | jq('"Kept \(.kept), revoked \(.revoked), pending \(.pending) with \(.pending_certifiers) certifiers. \(.revoke_reasons)"') | first }}
{{ outputs.result.values.r | jq('"Blockers: \(.blockers). \(.blocker_detail)"') | first }}
- id: evidence_blockers
type: io.kestra.plugin.core.namespace.UploadFiles
namespace: "{{ flow.namespace }}"
filesMap:
"{{ render(vars.pack_dir) }}/{{ inputs.phase | lower }}/blockers.csv": "{{ outputs.review.outputFiles.blockers }}"
- id: gate
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.result.values.r | jq('.blockers > 0') | first }}"
then:
- id: blocked
type: io.kestra.plugin.core.execution.Fail
errorMessage: "The access review cannot rely on these extracts: {{
outputs.result.values.r | jq('.blocker_detail') | first }}"
- id: evidence
type: io.kestra.plugin.core.namespace.UploadFiles
namespace: "{{ flow.namespace }}"
filesMap:
"{{ render(vars.pack_dir) }}/{{ inputs.phase | lower }}/findings.csv": "{{ outputs.review.outputFiles.findings }}"
"{{ render(vars.pack_dir) }}/{{ inputs.phase | lower }}/certification.csv": "{{ outputs.review.outputFiles.certification }}"
"{{ render(vars.pack_dir) }}/{{ inputs.phase | lower }}/revocations.csv": "{{ outputs.review.outputFiles.revocations }}"
"{{ render(vars.pack_dir) }}/{{ inputs.phase | lower }}/evidence.csv": "{{ outputs.review.outputFiles.evidence }}"
- id: decide
type: io.kestra.plugin.core.flow.Switch
value: "{{ inputs.phase }}"
cases:
OPEN:
- id: log_open
type: io.kestra.plugin.core.log.Log
message: >-
Campaign open: {{ outputs.result.values.r | jq('.pending') | first
}} accounts to certify by {{ outputs.result.values.r |
jq('.pending_certifiers') | first }} managers, list in {{
render(vars.pack_dir) }}/open/certification.csv. Forced revocations
are already in revocations.csv. Run CLOSE with the decisions.
CLOSE:
- id: lock
type: io.kestra.plugin.core.kv.Set
key: "{{ vars.state_key }}"
kvType: JSON
value: >-
{{ {'old': (kv(vars.state_key, errorOnMissing=false) ?? {'closed':
[], 'reviews': {}}), 'id': inputs.review_id, 'r':
fromJson(outputs.result.values.r), 'at': now()}
| jq('{closed: (.old.closed + [.id]), reviews: (.old.reviews + {(.id): {accounts: .r.accounts, kept: .r.kept, revoked: .r.revoked, closed_at: .at}})}') | first | toJson }}
- id: log_closed
type: io.kestra.plugin.core.log.Log
message: "Review {{ inputs.review_id }} closed: {{ outputs.result.values.r |
jq('.revoked') | first }} revocations to apply from {{
render(vars.pack_dir) }}/close/revocations.csv."
errors:
- id: alert_flow_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
runIf: '{{ inputs.notify_slack }}'
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{{ {'text': 'User access review ' ~ inputs.review_id ~ ' (' ~ inputs.phase ~ ') failed (execution ' ~ execution.id ~ '). Check the logs: the review is not complete.'} | toJson }}