-
Notifications
You must be signed in to change notification settings - Fork 261
/
search_path.go
67 lines (58 loc) · 1.86 KB
/
search_path.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
package db_local
import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/spf13/viper"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/db/db_common"
"log"
"strings"
)
func setUserSearchPath(ctx context.Context, conn *pgx.Conn, foreignSchemaNames []string) error {
var searchPath []string
// is there a user search path in the config?
// check ConfigKeyDatabaseSearchPath config (this is the value specified in the database config)
if viper.IsSet(constants.ConfigKeyDatabaseSearchPath) {
searchPath = viper.GetStringSlice(constants.ConfigKeyDatabaseSearchPath)
// add 'internal' schema as last schema in the search path
searchPath = append(searchPath, constants.FunctionSchema)
} else {
// no config set - set user search path to default
// - which is all the connection names, book-ended with public and internal
searchPath = db_common.GetDefaultSearchPath(ctx, foreignSchemaNames)
}
// escape the schema names
escapedSearchPath := db_common.PgEscapeSearchPath(searchPath)
log.Println("[TRACE] setting user search path to", searchPath)
// get all roles which are a member of steampipe_users
query := fmt.Sprintf(`select usename from pg_user where pg_has_role(usename, '%s', 'member')`, constants.DatabaseUsersRole)
rows, err := conn.Query(ctx, query)
if err != nil {
return err
}
// set the search path for all these roles
var queries = []string{
"lock table pg_user;",
}
for rows.Next() {
var user string
if err := rows.Scan(&user); err != nil {
return err
}
if user == "root" {
continue
}
queries = append(queries, fmt.Sprintf(
"alter user %s set search_path to %s;",
db_common.PgEscapeName(user),
strings.Join(escapedSearchPath, ","),
))
}
log.Printf("[TRACE] user search path sql: %v", queries)
_, err = executeSqlAsRoot(ctx, queries...)
if err != nil {
return err
}
return nil
}