Skip to content

Commit 0834409

Browse files
committed
Refactor SQL privileges
- Ensure backward compatibility; - Include database name in schema type; Signed-off-by: Stefano Scafiti <[email protected]>
1 parent c189283 commit 0834409

File tree

11 files changed

+2602
-2612
lines changed

11 files changed

+2602
-2612
lines changed

embedded/sql/engine.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,8 @@ func (e *Engine) checkUserPermissions(ctx context.Context, stmt SQLStmt) error {
535535
return err
536536
}
537537

538-
if user.Permission() == PermissionAdmin {
539-
return nil
540-
}
541-
542538
if !stmt.readOnly() && user.Permission() == PermissionReadOnly {
543-
return ErrAccessDenied
539+
return fmt.Errorf("%w: statement requires %s permission", ErrAccessDenied, PermissionReadWrite)
544540
}
545541

546542
requiredPrivileges := stmt.requiredPrivileges()

embedded/sql/stmt.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ const (
9797
PermissionSysAdmin Permission = "SYSADMIN"
9898
)
9999

100+
func PermissionFromCode(code uint32) Permission {
101+
switch code {
102+
case 1:
103+
{
104+
return PermissionReadOnly
105+
}
106+
case 2:
107+
{
108+
return PermissionReadWrite
109+
}
110+
case 254:
111+
{
112+
return PermissionAdmin
113+
}
114+
}
115+
return PermissionSysAdmin
116+
}
117+
100118
type AggregateFn = string
101119

102120
const (
@@ -5475,7 +5493,7 @@ var allPrivileges = []SQLPrivilege{
54755493

54765494
func DefaultSQLPrivilegesForPermission(p Permission) []SQLPrivilege {
54775495
switch p {
5478-
case PermissionSysAdmin, PermissionAdmin, PermissionReadWrite: // should also contain GRANT/REVOKE
5496+
case PermissionSysAdmin, PermissionAdmin, PermissionReadWrite:
54795497
return allPrivileges
54805498
case PermissionReadOnly:
54815499
return []SQLPrivilege{SQLPrivilegeSelect}

pkg/api/schema/docs.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
- [SQLExecRequest](#immudb.schema.SQLExecRequest)
9292
- [SQLExecResult](#immudb.schema.SQLExecResult)
9393
- [SQLGetRequest](#immudb.schema.SQLGetRequest)
94+
- [SQLPrivilege](#immudb.schema.SQLPrivilege)
9495
- [SQLQueryRequest](#immudb.schema.SQLQueryRequest)
9596
- [SQLQueryResult](#immudb.schema.SQLQueryResult)
9697
- [SQLValue](#immudb.schema.SQLValue)
@@ -142,7 +143,6 @@
142143

143144
- [EntryTypeAction](#immudb.schema.EntryTypeAction)
144145
- [PermissionAction](#immudb.schema.PermissionAction)
145-
- [SQLPrivilege](#immudb.schema.SQLPrivilege)
146146
- [TxMode](#immudb.schema.TxMode)
147147

148148
- [ImmuService](#immudb.schema.ImmuService)
@@ -235,7 +235,7 @@ DEPRECATED
235235
| action | [PermissionAction](#immudb.schema.PermissionAction) | | Action to perform |
236236
| username | [string](#string) | | Name of the user to update |
237237
| database | [string](#string) | | Name of the database |
238-
| privileges | [SQLPrivilege](#immudb.schema.SQLPrivilege) | repeated | SQL privileges to grant / revoke |
238+
| privileges | [string](#string) | repeated | SQL privileges: SELECT, CREATE, INSERT, UPDATE, DELETE, DROP, ALTER |
239239

240240

241241

@@ -1633,6 +1633,22 @@ Only succeed if given key was not modified after given transaction
16331633

16341634

16351635

1636+
<a name="immudb.schema.SQLPrivilege"></a>
1637+
1638+
### SQLPrivilege
1639+
1640+
1641+
1642+
| Field | Type | Label | Description |
1643+
| ----- | ---- | ----- | ----------- |
1644+
| database | [string](#string) | | Database name |
1645+
| privilege | [string](#string) | | Privilege: SELECT, CREATE, INSERT, UPDATE, DELETE, DROP, ALTER |
1646+
1647+
1648+
1649+
1650+
1651+
16361652
<a name="immudb.schema.SQLQueryRequest"></a>
16371653

16381654
### SQLQueryRequest
@@ -2488,24 +2504,6 @@ Reserved to reply with more advanced response later
24882504

24892505

24902506

2491-
<a name="immudb.schema.SQLPrivilege"></a>
2492-
2493-
### SQLPrivilege
2494-
2495-
2496-
| Name | Number | Description |
2497-
| ---- | ------ | ----------- |
2498-
| UNKNOWN | 0 | |
2499-
| SELECT | 1 | |
2500-
| CREATE | 2 | |
2501-
| INSERT | 3 | |
2502-
| UPDATE | 4 | |
2503-
| DELETE | 5 | |
2504-
| DROP | 6 | |
2505-
| ALTER | 7 | |
2506-
2507-
2508-
25092507
<a name="immudb.schema.TxMode"></a>
25102508

25112509
### TxMode

0 commit comments

Comments
 (0)