• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codenotary / immudb / 6876333693

15 Nov 2023 10:56AM UTC coverage: 89.514% (+0.2%) from 89.267%
6876333693

push

gh-ci

jeroiraz
test(embedded/sql): cover sql catalog stmt edge cases

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

33931 of 37906 relevant lines covered (89.51%)

143149.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.03
/pkg/server/multidb_handler.go
1
/*
2
Copyright 2022 Codenotary Inc. All rights reserved.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
        http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package server
18

19
import (
20
        "context"
21
        "fmt"
22

23
        "github.com/codenotary/immudb/embedded/sql"
24
        "github.com/codenotary/immudb/pkg/api/schema"
25
        "github.com/codenotary/immudb/pkg/auth"
26
)
27

28
type multidbHandler struct {
29
        s *ImmuServer
30
}
31

32
func (s *ImmuServer) multidbHandler() sql.MultiDBHandler {
591✔
33
        return &multidbHandler{s}
591✔
34
}
591✔
35

36
func (h *multidbHandler) UseDatabase(ctx context.Context, db string) error {
3✔
37
        if auth.GetAuthTypeFromContext(ctx) != auth.SessionAuth {
4✔
38
                return fmt.Errorf("%w: database selection from SQL statements requires session based authentication", ErrNotSupported)
1✔
39
        }
1✔
40

41
        _, err := h.s.UseDatabase(ctx, &schema.Database{DatabaseName: db})
2✔
42
        return err
2✔
43
}
44

45
func (h *multidbHandler) CreateDatabase(ctx context.Context, db string, ifNotExists bool) error {
9✔
46
        _, err := h.s.CreateDatabaseV2(ctx, &schema.CreateDatabaseRequest{
9✔
47
                Name:        db,
9✔
48
                IfNotExists: ifNotExists,
9✔
49
        })
9✔
50
        return err
9✔
51
}
9✔
52

53
func (h *multidbHandler) ListDatabases(ctx context.Context) ([]string, error) {
2✔
54
        res, err := h.s.DatabaseList(ctx, nil)
2✔
55
        if err != nil {
3✔
56
                return nil, err
1✔
57
        }
1✔
58

59
        dbs := make([]string, len(res.Databases))
1✔
60

1✔
61
        for i, db := range res.Databases {
3✔
62
                dbs[i] = db.DatabaseName
2✔
63
        }
2✔
64

65
        return dbs, nil
1✔
66
}
67

68
func (h *multidbHandler) ListUsers(ctx context.Context) ([]sql.User, error) {
6✔
69
        db, err := h.s.getDBFromCtx(ctx, "ListUsers")
6✔
70
        if err != nil {
7✔
71
                return nil, err
1✔
72
        }
1✔
73

74
        res, err := h.s.ListUsers(ctx, nil)
5✔
75
        if err != nil {
5✔
76
                return nil, err
×
77
        }
×
78

79
        users := make([]sql.User, 0, len(res.Users))
5✔
80

5✔
81
        for _, user := range res.Users {
22✔
82
                if !user.Active {
19✔
83
                        continue
2✔
84
                }
85

86
                var hasPermission *schema.Permission
15✔
87

15✔
88
                if string(user.User) == auth.SysAdminUsername {
20✔
89
                        hasPermission = &schema.Permission{Database: db.GetName()}
5✔
90
                } else {
15✔
91
                        for _, perm := range user.Permissions {
20✔
92
                                if perm.Database == db.GetName() {
19✔
93
                                        hasPermission = perm
9✔
94
                                        break
9✔
95
                                }
96
                        }
97
                }
98

99
                if hasPermission != nil {
29✔
100
                        users = append(users, &User{username: string(user.User), perm: hasPermission.Permission})
14✔
101
                }
14✔
102
        }
103

104
        return users, nil
5✔
105
}
106

107
type User struct {
108
        username string
109
        perm     uint32
110
}
111

112
func (usr *User) Username() string {
14✔
113
        return usr.username
14✔
114
}
14✔
115

116
func (usr *User) Permission() uint32 {
14✔
117
        return usr.perm
14✔
118
}
14✔
119

120
func permCode(permission sql.Permission) uint32 {
6✔
121
        switch permission {
6✔
122
        case sql.PermissionReadOnly:
2✔
123
                {
4✔
124
                        return 1
2✔
125
                }
2✔
126
        case sql.PermissionReadWrite:
3✔
127
                {
6✔
128
                        return 2
3✔
129
                }
3✔
130
        case sql.PermissionAdmin:
1✔
131
                {
2✔
132
                        return 254
1✔
133
                }
1✔
134
        }
135
        return 0
×
136
}
137

138
func (h *multidbHandler) CreateUser(ctx context.Context, username, password string, permission sql.Permission) error {
5✔
139
        db, err := h.s.getDBFromCtx(ctx, "CreateUser")
5✔
140
        if err != nil {
6✔
141
                return err
1✔
142
        }
1✔
143

144
        _, err = h.s.CreateUser(ctx, &schema.CreateUserRequest{
4✔
145
                User:       []byte(username),
4✔
146
                Password:   []byte(password),
4✔
147
                Database:   db.GetName(),
4✔
148
                Permission: permCode(permission),
4✔
149
        })
4✔
150

4✔
151
        return err
4✔
152
}
153

154
func (h *multidbHandler) AlterUser(ctx context.Context, username, password string, permission sql.Permission) error {
4✔
155
        _, user, err := h.s.getLoggedInUserdataFromCtx(ctx)
4✔
156
        if err != nil {
5✔
157
                return err
1✔
158
        }
1✔
159

160
        db, err := h.s.getDBFromCtx(ctx, "ChangePassword")
3✔
161
        if err != nil {
3✔
162
                return err
×
163
        }
×
164

165
        _, err = h.s.SetActiveUser(ctx, &schema.SetActiveUserRequest{
3✔
166
                Username: username,
3✔
167
                Active:   true,
3✔
168
        })
3✔
169
        if err != nil {
4✔
170
                return err
1✔
171
        }
1✔
172

173
        _, err = h.s.ChangePassword(ctx, &schema.ChangePasswordRequest{
2✔
174
                User:        []byte(username),
2✔
175
                OldPassword: []byte(user.HashedPassword),
2✔
176
                NewPassword: []byte(password),
2✔
177
        })
2✔
178
        if err != nil {
2✔
179
                return err
×
180
        }
×
181

182
        _, err = h.s.ChangePermission(ctx, &schema.ChangePermissionRequest{
2✔
183
                Username:   username,
2✔
184
                Database:   db.GetName(),
2✔
185
                Action:     schema.PermissionAction_GRANT,
2✔
186
                Permission: permCode(permission),
2✔
187
        })
2✔
188

2✔
189
        return err
2✔
190
}
191

192
func (h *multidbHandler) DropUser(ctx context.Context, username string) error {
1✔
193
        _, err := h.s.SetActiveUser(ctx, &schema.SetActiveUserRequest{
1✔
194
                Username: username,
1✔
195
                Active:   false,
1✔
196
        })
1✔
197
        return err
1✔
198
}
1✔
199

200
func (h *multidbHandler) ExecPreparedStmts(
201
        ctx context.Context,
202
        opts *sql.TxOptions,
203
        stmts []sql.SQLStmt,
204
        params map[string]interface{},
205
) (ntx *sql.SQLTx, committedTxs []*sql.SQLTx, err error) {
1✔
206

1✔
207
        db, err := h.s.getDBFromCtx(ctx, "SQLExec")
1✔
208
        if err != nil {
1✔
209
                return nil, nil, err
×
210
        }
×
211

212
        tx, err := db.NewSQLTx(ctx, opts)
1✔
213
        if err != nil {
1✔
214
                return nil, nil, err
×
215
        }
×
216

217
        return db.SQLExecPrepared(ctx, tx, stmts, params)
1✔
218
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc