← Back to Pull requests

CXH-2390: fix org-level role grants in baton-slack-enterprise #40

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 28, 2026 23:58 UTC
Author

Connectors could never grant Enterprise Grid organization-level roles: any provisioning of organization_admin or organization_owner failed immediately with Slack's invalid_arguments error, because the connector sent those role names to a Slack endpoint that only accepts the 15 granular system role IDs. Two production occurrences are confirmed in Datadog (tenant action IDs 3Ijrm2kqVKkxhfJb3jB7kHmaK0p and 3IiyNvynQnoXspiOqaiyTe7mQak, team E0B87GJMZJP, both fatal). This PR routes organization-level roles through the org-level Slack API — admin.users.setAdmin / admin.users.setOwner / admin.users.setRegular, which Slack documents as acting at workspace or organization level depending on which ID you pass — and leaves every granular role on the path it already used.

Context

Linear: CXH-2390

The write path (enterpriseRoleType.Grant()/Revoke()) treated all 18 enterprise roles identically and POSTed every one to admin.roles.addAssignments/admin.roles.removeAssignments with role_id: organization_admin (or its siblings). Slack's docs for that endpoint accept only granular Rl0X role IDs from admin.roles.list; the org-level names fail top-level parameter validation — the empty rejected_users/rejected_entities in the prod error confirm Slack rejected the request before evaluating any user or team. The read path already treated these three roles as a special case (they are derived from each user's IsAdmin/IsOwner/IsPrimaryOwner flags), but the write path never got the matching branch.

What changed

pkg/connector/enterprise_roles.go (~L267-340)

Where: Grant(), Revoke(), and the new orgRoleToWorkspaceRole helper.

What changed

  • Both methods now branch before the existing granular body. Organization roles take a new org arm; everything else falls through to the previous code untouched (the diff is insertions-only — the granular path is byte-identical to main).

  • Granting organization_admin / organization_owner first reads the user's current standing: if the user already holds the role (an organization owner counts as holding admin too, since the sync emits both grants for owners), the grant completes as GrantAlreadyExists without any write — Slack's setAdmin would otherwise demote an owner to plain admin. Otherwise the grant calls SetWorkspaceRole(ctx, orgID, userToSet, "admin"/"owner"), routing to admin.users.setAdmin / admin.users.setOwner with team_id set to the enterprise ID the builder already carried but never read. No users.info call is needed to resolve a team anymore (the old path resolved the user's home team only to feed the granular endpoint).

  • Revoking organization_owner demotes the user to organization admin via admin.users.setAdmin with the org ID — it revokes only the owner role, not the admin role that org owners also carry. Revoking organization_admin first checks the user's current ownership: if the user still holds the owner role, the revoke is refused with a clear "revoke the owner role first" error (revoking both at once would silently strip ownership an access review did not ask for); otherwise the user is demoted via admin.users.setRegular. Neither path ever touches admin.roles.removeAssignments with an org role name.

  • Grant or revoke of organization_primary_owner returns a PermissionDenied error explaining Slack does not allow modifying the primary owner via the API, before any HTTP call.

  • If the connector was constructed without an enterprise ID, the org arm fails fast with InvalidArgument (mirroring the guard the granular client method already has) — it never falls through to the granular path with an org role name.

  • An "already in that state" answer from Slack is treated as success: on the grant path already_admin / already_owner map to GrantAlreadyExists; on the revoke path each arm maps its own already-downgraded signal (already_admin on the owner-revoke arm, already_regular on the admin-revoke arm) to GrantAlreadyRevoked. Slack's invalid_role_for_user is deliberately NOT treated as success — Slack documents it as a refusal of the demotion, so it surfaces as a real error rather than silently reporting a revoke that did not happen. Unmatched errors fail closed with the connector-prefixed wrapped error and gRPC code.
  • pkg/connector/enterprise_roles_guard_test.go (new file)

    Where: whole file; test functions TestEnterpriseRoleGrant_OrgRolesUseOrgLevelAPI, TestEnterpriseRoleRevoke_OrgRolesDowngradeToRegular, TestEnterpriseRole_PrimaryOwnerRefusedBeforeHTTP, TestEnterpriseRoleGrant_GranularRolesUnchanged, idempotency and guard-order tests, and the classification tripwire.

    Why

  • A mock Slack server records every request the connector makes, so each test pins the exact endpoint and parameters for its role class: org grants hit setAdmin/setOwner with the enterprise ID, the owner revoke hits setAdmin, the admin revoke hits setRegular (after the ownership check) and is refused while the user still owns, the primary owner produces zero HTTP requests, and granular roles still reach admin.roles.addAssignments with their Rl… role ID and the user's home team.

  • The tripwire iterates every role constant and asserts its routing, so a role added later without a matching branch fails CI instead of surfacing as another production invalid_arguments.
  • How far this is verified

  • Targeted build + vet + tests of ./pkg/connector pass (scripts/validate-changes.sh) at every pushed head; the new guard tests pass fresh, including with -race.

  • connector-eval (advisory): clean — a full sync of this branch was replayed against a Slack API mock built from Slack's official docs and produced exactly the same objects (0 lost / 0 gained resources, entitlements, grants) as current main, with no new static findings. (A mock proves the connector walks the documented API, not that Slack behaves; the provisioning arms are covered by the guard tests and a mock-backed end-to-end probe, not by a sync recording.)

  • The smoke check built cmd/baton-slack-enterprise and ran it (--help) successfully; it exercised no sync because the repo declares no smoke mock.

  • An adversarial code review against this repository's published review criteria reached SHIP with no code changes required (head 840f114); two later review passes on the resolved bot threads kept it clean.

  • CI is green and this repository's review bot accepts the current head (b2c3d642d), after three fix passes during review: the bot caught that revoking one org role stripped both roles (revokes are now per-arm — owner revoke demotes to admin only), that revoking organization_owner from a non-owner would have promoted the user (an ownership precheck now returns GrantAlreadyRevoked with no write), and a compile error one of those fixes briefly introduced. All five bot threads are replied and resolved.

  • An independent deep review (a fresh reviewer environment that saw none of the development) ran two rounds: round 1 raised 2 major findings — invalid_role_for_user was being treated as "already revoked" when Slack documents it as a refusal (now fails closed), and granting an org role to a current owner would have demoted them (now an idempotent GrantAlreadyExists no-op) — both fixed; round 2 verified those fixes and raised 2 minor ones (a stale comment that could mislead a future maintainer, and a fail-open helper default) — both fixed. Every deep-review thread is replied and resolved.

  • A fresh-clone test pass (VERA) verdicts all 8 acceptance criteria WORKS at the final head, with mock-backed evidence for each org/granular/primary-owner routing and refusal, and confirms a full sync against a Slack mock emits the same objects as main.

  • Slack docs note (verification detail): the "already in state" error strings above are not in Slack's current documented error tables for these methods (absence, not contradiction); the connector treats any unrecognized error as a failure rather than guessing, so an undocumented Slack change fails loudly rather than silently mis-annotating. invalid_role_for_user IS documented (a refusal) and deliberately produces a real error.
  • Risk — what to look at hardest

  • The org-level calls reuse the workspace-role primitive (SetWorkspaceRole) with the enterprise ID in the team_id field. That three-string argument order is the highest-risk swap site, and the guard tests pin the exact form parameters to catch a transposition.

  • The admin revoke now makes one extra users.info read to check ownership before demoting; that lookup failing fails the revoke closed rather than guessing.

  • The idempotency match is a string check on Slack's error text; if Slack phrases an already-in-state case differently than expected, the request still fails closed (an error, not a wrong success).
  • Release Notes

    Granting or revoking the Enterprise Grid Organization Admin and Organization Owner roles through the connector now works: those requests go to Slack's org-level admin API instead of an endpoint that rejected them with invalid_arguments. Revoking Organization Owner demotes the user to organization admin; revoking Organization Admin demotes the user to a regular member — and is refused with a clear error while the user still holds the owner role, so an access review can no longer silently strip ownership it did not ask to revoke. The Organization Primary Owner role remains read-only — attempts to grant or revoke it return a clear error, since Slack's API does not allow modifying the primary owner.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: c1-squire-dev[bot] Review duration: 49693m
    All Vouch review confidence checks passed
    Residual risk 0.35 = Change Risk (0.59) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.59 · 650 lines diff across 8 files
    0.59
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    40%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #40"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 40.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 59% (MEDIUM) · Higher = larger blast radius
    59%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 40.9% · Lower = rushed or shallow review
    40.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 34.8% · Safe to merge
    34.8%