diff --git a/backend/src/main/java/com/dbaagent/model/Permission.java b/backend/src/main/java/com/dbaagent/model/Permission.java index d792a9f..0c44936 100644 --- a/backend/src/main/java/com/dbaagent/model/Permission.java +++ b/backend/src/main/java/com/dbaagent/model/Permission.java @@ -44,6 +44,9 @@ public enum Permission { // schema changes are not, and one ALTER can queue every write on a table. // ADMIN-only by default; grant it to a DBA via an override or a custom role. EXECUTE_DDL("Run schema changes (CREATE, ALTER, DROP, RENAME, index apply) on connected databases", Role.ADMIN), + // Was a hard-coded "built-in ADMIN or DBA" check, so a custom role could never write at + // all — and so could not be given EXECUTE_DDL either. Same default holders as before. + EXECUTE_WRITES("Run data changes (INSERT, UPDATE, DELETE) on connected databases", Role.ADMIN, Role.DBA), USE_INDEX_ADVISOR("Apply Index Advisor recommendations", Role.ADMIN, Role.DBA), MANAGE_ALERTS("Acknowledge and manage alerts", Role.ADMIN, Role.DBA), diff --git a/backend/src/main/java/com/dbaagent/service/security/AccessControlService.java b/backend/src/main/java/com/dbaagent/service/security/AccessControlService.java index 026ea04..bd4b1e1 100644 --- a/backend/src/main/java/com/dbaagent/service/security/AccessControlService.java +++ b/backend/src/main/java/com/dbaagent/service/security/AccessControlService.java @@ -305,29 +305,14 @@ public boolean isCurrentUserAdmin() { * DBA must not receive MANAGE_USERS or other admin-only product controls. */ public boolean currentUserMayMutateSql() { - if (ImpersonationContext.isActive()) { - return ImpersonationContext.current() - .map(state -> { - if (state.target() == null) { - return false; - } - Role role = state.target().getRoleEnum(); - return role == Role.ADMIN || role == Role.DBA; - }) - .orElse(false); - } - if (!authEnabled) { + // EXECUTE_WRITES (default: ADMIN, DBA), so a custom role can be granted writes — + // and with EXECUTE_DDL, schema changes. Every authentication path carries the + // effective permissions as authorities: JWT and MCP-token requests load them per + // request, and View-as swaps in the target's, so this reads the target's grant. + if (!ImpersonationContext.isActive() && !authEnabled) { return true; } - Authentication authentication = currentAuthentication(); - if (authentication == null || !authentication.isAuthenticated()) { - return false; - } - return authentication.getAuthorities().stream() - .anyMatch(authority -> { - String value = authority.getAuthority(); - return "ROLE_ADMIN".equals(value) || "ROLE_DBA".equals(value); - }); + return hasPermission(Permission.EXECUTE_WRITES); } private Authentication currentAuthentication() { diff --git a/backend/src/test/java/com/dbaagent/service/security/AccessControlServiceTest.java b/backend/src/test/java/com/dbaagent/service/security/AccessControlServiceTest.java index afc149f..b8a6fe7 100644 --- a/backend/src/test/java/com/dbaagent/service/security/AccessControlServiceTest.java +++ b/backend/src/test/java/com/dbaagent/service/security/AccessControlServiceTest.java @@ -296,13 +296,15 @@ void assignedChatEditorUserCanUseChatEditor() { } + // Authorities as production builds them (CustomUserDetailsService): ROLE_ plus the + // role's effective permission names. Writes follow EXECUTE_WRITES, not the role name. @Test void dbaMayMutateSqlButIsNotAdmin() { SecurityContextHolder.getContext().setAuthentication( new UsernamePasswordAuthenticationToken( "dba-user", null, - List.of(new SimpleGrantedAuthority("ROLE_DBA")) + List.of(new SimpleGrantedAuthority("ROLE_DBA"), new SimpleGrantedAuthority("EXECUTE_WRITES")) ) ); @@ -310,6 +312,36 @@ void dbaMayMutateSqlButIsNotAdmin() { assertFalse(accessControlService.isCurrentUserAdmin()); } + @Test + void aCustomRoleGrantedWritesMayMutateSql() { + // Was impossible: the gate accepted only built-in ADMIN/DBA, so a custom + // "DBA + schema changes" role lost writes entirely. + SecurityContextHolder.getContext().setAuthentication( + new UsernamePasswordAuthenticationToken( + "custom-user", + null, + List.of(new SimpleGrantedAuthority("ROLE_DBA_SCHEMA"), + new SimpleGrantedAuthority("EXECUTE_WRITES"), new SimpleGrantedAuthority("EXECUTE_DDL")) + ) + ); + + assertTrue(accessControlService.currentUserMayMutateSql()); + } + + @Test + void theRoleNameAloneGrantsNoWrites() { + // A DBA whose EXECUTE_WRITES was revoked by an override must not keep writing. + SecurityContextHolder.getContext().setAuthentication( + new UsernamePasswordAuthenticationToken( + "dba-revoked", + null, + List.of(new SimpleGrantedAuthority("ROLE_DBA")) + ) + ); + + assertFalse(accessControlService.currentUserMayMutateSql()); + } + @Test void adminMayMutateSql() { SecurityContextHolder.getContext().setAuthentication( @@ -351,8 +383,10 @@ void impersonatingDbaAllowsMutateSql() { com.dbaagent.security.ImpersonationContext.enter( new com.dbaagent.security.ImpersonationContext.State(impersonator, target) ); + // ImpersonationService swaps in the target's authorities (role + permissions). SecurityContextHolder.getContext().setAuthentication( - new UsernamePasswordAuthenticationToken("dba-target", null, List.of()) + new UsernamePasswordAuthenticationToken("dba-target", null, + List.of(new SimpleGrantedAuthority("ROLE_DBA"), new SimpleGrantedAuthority("EXECUTE_WRITES"))) ); assertTrue(accessControlService.currentUserMayMutateSql()); diff --git a/src/components/tabs/Core/SqlRunnerTab.js b/src/components/tabs/Core/SqlRunnerTab.js index 03b7e03..c96d9fc 100644 --- a/src/components/tabs/Core/SqlRunnerTab.js +++ b/src/components/tabs/Core/SqlRunnerTab.js @@ -2378,7 +2378,7 @@ export default function SqlRunnerTab({ connectionId }) { } > - {mayMutateSql ? (isAdmin ? "Admin mode" : "DBA mode") : "Read-only mode"} + {mayMutateSql ? (isAdmin ? "Admin mode" : "Write mode") : "Read-only mode"} {!connectionId && ( diff --git a/src/hooks/useAuth.jsx b/src/hooks/useAuth.jsx index f3c1adf..ecbc8d7 100644 --- a/src/hooks/useAuth.jsx +++ b/src/hooks/useAuth.jsx @@ -11,7 +11,7 @@ import { useDashboardStore } from '@/lib/stores/useDashboardStore' import { useNavStore } from '@/lib/stores/useNavStore' export { PERMISSIONS, ROLES, ROLE_LABELS, normalizeRole, roleLabel, isAdminRole, mayMutateSqlRole, isBuiltInRole } from '@/lib/permissions' -import { PERMISSIONS, ROLES, ROLE_BASELINE_PERMISSIONS, normalizeRole, isAdminRole, mayMutateSqlRole, roleLabel } from '@/lib/permissions' +import { PERMISSIONS, ROLES, ROLE_BASELINE_PERMISSIONS, normalizeRole, isAdminRole, roleLabel } from '@/lib/permissions' const AuthContext = createContext(null) @@ -291,7 +291,12 @@ export function AuthProvider({ children }) { }, [role, permissions]) const isAdmin = useMemo(() => isAdminRole(role), [role]) - const mayMutateSql = useMemo(() => mayMutateSqlRole(role), [role]) + // The permission, not the role name: a custom role granted EXECUTE_WRITES must get the + // Editor's write/confirm flow (the backend gate is the same permission). + const mayMutateSql = useMemo( + () => isAdminRole(role) || permissions.has(PERMISSIONS.EXECUTE_WRITES), + [role, permissions] + ) const isDeveloper = useMemo(() => normalizeRole(role) === ROLES.DEVELOPER, [role]) const roleDisplayName = useMemo(() => roleLabel(role, user?.roleName), [role, user?.roleName]) const impersonating = Boolean(user?.impersonating) diff --git a/src/lib/permissions.js b/src/lib/permissions.js index a5f7240..fa89518 100644 --- a/src/lib/permissions.js +++ b/src/lib/permissions.js @@ -31,6 +31,7 @@ export const PERMISSIONS = { RUN_INGESTION: 'RUN_INGESTION', EXECUTE_PLAYBOOKS: 'EXECUTE_PLAYBOOKS', EXECUTE_DDL: 'EXECUTE_DDL', + EXECUTE_WRITES: 'EXECUTE_WRITES', USE_INDEX_ADVISOR: 'USE_INDEX_ADVISOR', MANAGE_ALERTS: 'MANAGE_ALERTS', @@ -76,6 +77,7 @@ export const ROLE_BASELINE_PERMISSIONS = { PERMISSIONS.VIEW_AGENT, PERMISSIONS.VIEW_DASHBOARDS, PERMISSIONS.VIEW_DIGEST, PERMISSIONS.VIEW_BRAIN, PERMISSIONS.VIEW_PERFORMANCE, PERMISSIONS.VIEW_EDITOR, PERMISSIONS.MANAGE_CONNECTIONS, PERMISSIONS.MANAGE_SETTINGS, + PERMISSIONS.EXECUTE_WRITES, ], [ROLES.DATA_ENGINEER]: [ PERMISSIONS.VIEW_AGENT, PERMISSIONS.VIEW_DASHBOARDS, PERMISSIONS.VIEW_EDITOR,