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
3 changes: 3 additions & 0 deletions backend/src/main/java/com/dbaagent/model/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,52 @@ void assignedChatEditorUserCanUseChatEditor() {
}


// Authorities as production builds them (CustomUserDetailsService): ROLE_<code> 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"))
)
);

assertTrue(accessControlService.currentUserMayMutateSql());
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(
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/components/tabs/Core/SqlRunnerTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -2378,7 +2378,7 @@ export default function SqlRunnerTab({ connectionId }) {
}
>
<span className={styles.executionPolicyNote}>
{mayMutateSql ? (isAdmin ? "Admin mode" : "DBA mode") : "Read-only mode"}
{mayMutateSql ? (isAdmin ? "Admin mode" : "Write mode") : "Read-only mode"}
</span>
</HelpTooltip>
{!connectionId && (
Expand Down
9 changes: 7 additions & 2 deletions src/hooks/useAuth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/lib/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand Down Expand Up @@ -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,
Expand Down
Loading