-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathoracle.go
469 lines (395 loc) · 12.4 KB
/
oracle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package oracle
import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"strings"
"time"
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
"github.com/hashicorp/vault/sdk/database/helper/connutil"
"github.com/hashicorp/vault/sdk/database/helper/dbutil"
"github.com/hashicorp/vault/sdk/helper/dbtxn"
"github.com/hashicorp/vault/sdk/helper/strutil"
"github.com/hashicorp/vault/sdk/helper/template"
_ "github.com/mattn/go-oci8"
)
const (
oracleTypeName = "oci8"
defaultRotateCredsSql = `ALTER USER {{username}} IDENTIFIED BY "{{password}}"`
defaultUsernameTemplate = `{{ printf "V_%s_%s_%s_%s" (.DisplayName | truncate 8) (.RoleName | truncate 8) (random 20) (unix_time) | truncate 30 | uppercase | replace "-" "_" | replace "." "_" }}`
)
var (
defaultRevocationStatements = []string{
`REVOKE CONNECT FROM {{username}}`,
`REVOKE CREATE SESSION FROM {{username}}`,
`DROP USER {{username}}`,
}
defaultSessionRevocationStatements = []string{
`ALTER USER {{username}} ACCOUNT LOCK`,
`begin
for x in ( select inst_id, sid, serial# from gv$session where username="{{username}}" )
loop
execute immediate ( 'alter system kill session '''|| x.Sid || ',' || x.Serial# || '@' || x.inst_id ''' immediate' );
end loop;
dbms_lock.sleep(1);
end;`,
`DROP USER {{username}}`,
}
)
var _ dbplugin.Database = (*Oracle)(nil)
type Oracle struct {
*connutil.SQLConnectionProducer
usernameProducer template.StringTemplate
splitStatements bool
disconnectSessions bool
}
func New() (interface{}, error) {
db := new()
// Wrap the plugin with middleware to sanitize errors
dbType := dbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.secretValues)
return dbType, nil
}
func new() *Oracle {
connProducer := &connutil.SQLConnectionProducer{}
connProducer.Type = oracleTypeName
dbType := &Oracle{
SQLConnectionProducer: connProducer,
}
return dbType
}
func (o *Oracle) Initialize(ctx context.Context, req dbplugin.InitializeRequest) (dbplugin.InitializeResponse, error) {
usernameTemplate, err := strutil.GetString(req.Config, "username_template")
if err != nil {
return dbplugin.InitializeResponse{}, fmt.Errorf("failed to retrieve username_template: %w", err)
}
if usernameTemplate == "" {
usernameTemplate = defaultUsernameTemplate
}
splitStatements, err := coerceToBool(req.Config, "split_statements", true)
if err != nil {
return dbplugin.InitializeResponse{}, fmt.Errorf("failed to parse 'split_statements' field: %w", err)
}
o.splitStatements = splitStatements
disconnectSessions, err := coerceToBool(req.Config, "disconnect_sessions", true)
if err != nil {
return dbplugin.InitializeResponse{}, fmt.Errorf("failed to parse 'disconnect_sessions' field: %w", err)
}
o.disconnectSessions = disconnectSessions
up, err := template.NewTemplate(template.Template(usernameTemplate))
if err != nil {
return dbplugin.InitializeResponse{}, fmt.Errorf("unable to initialize username template: %w", err)
}
o.usernameProducer = up
_, err = o.usernameProducer.Generate(dbplugin.UsernameMetadata{})
if err != nil {
return dbplugin.InitializeResponse{}, fmt.Errorf("invalid username template: %w", err)
}
err = o.SQLConnectionProducer.Initialize(ctx, req.Config, req.VerifyConnection)
if err != nil {
return dbplugin.InitializeResponse{}, err
}
resp := dbplugin.InitializeResponse{
Config: req.Config,
}
return resp, nil
}
func coerceToBool(m map[string]interface{}, key string, def bool) (bool, error) {
rawVal, ok := m[key]
if !ok {
return def, nil
}
switch val := rawVal.(type) {
case bool:
return val, nil
case string:
return strconv.ParseBool(val)
}
return false, fmt.Errorf("invalid type for key [%s]", key)
}
func (o *Oracle) NewUser(ctx context.Context, req dbplugin.NewUserRequest) (dbplugin.NewUserResponse, error) {
o.Lock()
defer o.Unlock()
username, err := o.usernameProducer.Generate(req.UsernameConfig)
if err != nil {
return dbplugin.NewUserResponse{}, fmt.Errorf("failed to generate username: %w", err)
}
db, err := o.getConnection(ctx)
if err != nil {
return dbplugin.NewUserResponse{}, fmt.Errorf("failed to get connection: %w", err)
}
err = o.newUser(ctx, db, username, req.Password, req.Expiration, req.Statements.Commands)
if err != nil {
return dbplugin.NewUserResponse{}, err
}
resp := dbplugin.NewUserResponse{
Username: username,
}
return resp, nil
}
func (o *Oracle) newUser(ctx context.Context, db *sql.DB, username, password string, expiration time.Time, commands []string) error {
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("failed to start a transaction: %w", err)
}
// Effectively a no-op if the transaction commits successfully
defer tx.Rollback()
statements := o.parseStatements(commands)
if len(statements) == 0 {
return dbutil.ErrEmptyCreationStatement
}
for _, query := range statements {
m := map[string]string{
"username": username,
"name": username, // backwards compatibility
"password": password,
"expiration": expiration.Format("2006-01-02 15:04:05-0700"),
}
err = dbtxn.ExecuteTxQuery(ctx, tx, m, query)
if err != nil {
return fmt.Errorf("failed to execute query: %w", err)
}
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}
func (o *Oracle) UpdateUser(ctx context.Context, req dbplugin.UpdateUserRequest) (dbplugin.UpdateUserResponse, error) {
if req.Password == nil && req.Expiration == nil {
return dbplugin.UpdateUserResponse{}, fmt.Errorf("no change requested")
}
if req.Password != nil {
err := o.changeUserPassword(ctx, req.Username, req.Password.NewPassword, req.Password.Statements.Commands, req.SelfManagedPassword)
if err != nil {
return dbplugin.UpdateUserResponse{}, fmt.Errorf("failed to change password: %w", err)
}
return dbplugin.UpdateUserResponse{}, nil
}
// Expiration change is a no-op
return dbplugin.UpdateUserResponse{}, nil
}
func (o *Oracle) changeUserPassword(ctx context.Context, username string, newPassword string, rotateStatements []string, selfManagedPassword string) error {
if len(rotateStatements) == 0 {
rotateStatements = []string{defaultRotateCredsSql}
}
if username == "" || newPassword == "" {
return errors.New("must provide both username and password")
}
variables := map[string]string{
"username": username,
"name": username, // backwards compatibility
"password": newPassword,
}
statements := o.parseStatements(rotateStatements)
if len(statements) == 0 { // Extra check to protect against future changes
return errors.New("no rotation statements found")
}
o.Lock()
defer o.Unlock()
var db *sql.DB
var err error
if selfManagedPassword != "" {
db, err = o.getStaticConnection(ctx, username, selfManagedPassword)
if err != nil {
return fmt.Errorf("unable to get static connection from cache: %w", err)
}
} else {
db, err = o.getConnection(ctx)
if err != nil {
return fmt.Errorf("unable to get database connection: %w", err)
}
}
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("unable to create database transaction: %w", err)
}
// Effectively a no-op if the transaction commits successfully
defer tx.Rollback()
for _, query := range statements {
parsedQuery := dbutil.QueryHelper(query, variables)
err := dbtxn.ExecuteTxQuery(ctx, tx, nil, parsedQuery)
if err != nil {
return fmt.Errorf("unable to execute query [%s]: %w", query, err)
}
}
err = tx.Commit()
if err != nil {
return fmt.Errorf("unable to commit statements: %w", err)
}
return nil
}
func (o *Oracle) DeleteUser(ctx context.Context, req dbplugin.DeleteUserRequest) (dbplugin.DeleteUserResponse, error) {
o.Lock()
defer o.Unlock()
db, err := o.getConnection(ctx)
if err != nil {
return dbplugin.DeleteUserResponse{}, fmt.Errorf("failed to make connection: %w", err)
}
tx, err := db.Begin()
if err != nil {
return dbplugin.DeleteUserResponse{}, fmt.Errorf("failed to start transaction: %w", err)
}
// Effectively a no-op if the transaction commits successfully
defer tx.Rollback()
if o.disconnectSessions {
err = o.disconnectSession(db, req.Username)
if err != nil {
return dbplugin.DeleteUserResponse{}, fmt.Errorf("failed to disconnect user %s: %w", req.Username, err)
}
}
revocationStatements := o.getRevocationStatements(req.Statements.Commands)
if len(revocationStatements) == 0 {
return dbplugin.DeleteUserResponse{}, fmt.Errorf("empty revocation statements")
}
// We can't use a transaction here, because Oracle treats DROP USER as a DDL statement, which commits immediately.
for _, query := range revocationStatements {
m := map[string]string{
"username": req.Username,
"name": req.Username, // backwards compatibility
}
if err := dbtxn.ExecuteTxQuery(ctx, tx, m, query); err != nil {
return dbplugin.DeleteUserResponse{}, fmt.Errorf("failed to execute query: %w", err)
}
}
return dbplugin.DeleteUserResponse{}, nil
}
func (o *Oracle) getRevocationStatements(statements []string) []string {
if len(statements) > 0 {
statements = o.parseStatements(statements)
return statements
}
if !o.splitStatements || !o.disconnectSessions {
return defaultSessionRevocationStatements
} else {
return defaultRevocationStatements
}
}
func (o *Oracle) Type() (string, error) {
return oracleTypeName, nil
}
func (o *Oracle) secretValues() map[string]string {
return map[string]string{
o.Password: "[password]",
}
}
// parseStatements conditionally splits the list of commands on semi-colons. If `split_statements` is true, this
// will return the provided slice of commands without altering them
func (o *Oracle) parseStatements(rawStatements []string) []string {
if !o.splitStatements {
statements := []string{}
for _, rawQ := range rawStatements {
newQ := strings.TrimSpace(rawQ)
if newQ == "" {
continue
}
statements = append(statements, newQ)
}
return statements
}
statements := []string{}
for _, rawQ := range rawStatements {
split := strutil.ParseArbitraryStringSlice(rawQ, ";")
for _, newQ := range split {
newQ = strings.TrimSpace(newQ)
if newQ == "" {
continue
}
statements = append(statements, newQ)
}
}
return statements
}
func (o *Oracle) disconnectSession(db *sql.DB, username string) error {
err := o.disconnectFromCluster(db, username)
if err == nil {
return nil
}
return o.disconnectLocal(db, username)
}
func (o *Oracle) disconnectFromCluster(db *sql.DB, username string) error {
disconnectVars := map[string]string{
"username": username,
}
query := dbutil.QueryHelper(`SELECT inst_id, sid, serial#, username FROM gv$session WHERE username = UPPER('{{username}}')`, disconnectVars)
disconnectStmt, err := db.Prepare(query)
if err != nil {
return err
}
defer disconnectStmt.Close()
rows, err := disconnectStmt.Query()
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var instID, sessionID, serialNumber int
var username sql.NullString
err = rows.Scan(&instID, &sessionID, &serialNumber, &username)
if err != nil {
return err
}
killStatement := fmt.Sprintf(`ALTER SYSTEM KILL SESSION '%d,%d,@%d' IMMEDIATE`, sessionID, serialNumber, instID)
_, err = db.Exec(killStatement)
if err != nil {
return err
}
}
err = rows.Err()
if err != nil {
return err
}
return nil
}
func (o *Oracle) disconnectLocal(db *sql.DB, username string) error {
disconnectVars := map[string]string{
"username": username,
}
query := dbutil.QueryHelper(`SELECT sid, serial#, username FROM v$session WHERE username = UPPER('{{username}}')`, disconnectVars)
disconnectStmt, err := db.Prepare(query)
if err != nil {
return err
}
defer disconnectStmt.Close()
rows, err := disconnectStmt.Query()
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var sessionID, serialNumber int
var username sql.NullString
err = rows.Scan(&sessionID, &serialNumber, &username)
if err != nil {
return err
}
killStatement := fmt.Sprintf(`ALTER SYSTEM KILL SESSION '%d,%d' IMMEDIATE`, sessionID, serialNumber)
_, err = db.Exec(killStatement)
if err != nil {
return err
}
}
err = rows.Err()
if err != nil {
return err
}
return nil
}
func (o *Oracle) getConnection(ctx context.Context) (*sql.DB, error) {
db, err := o.Connection(ctx)
if err != nil {
return nil, err
}
return db.(*sql.DB), nil
}
func (o *Oracle) getStaticConnection(ctx context.Context, username, password string) (*sql.DB, error) {
db, err := o.StaticConnection(ctx, username, password)
if err != nil {
return nil, err
}
return db, nil
}