/
aws.go
169 lines (139 loc) · 3.54 KB
/
aws.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
package postgres
import (
"fmt"
"github.com/lib/pq"
)
const (
CreateDBWithoutOwnerSQLTemplate = `CREATE DATABASE "%s"`
AlterDBOwnerSQLTemplate = `ALTER DATABASE "%s" OWNER TO "%s"`
)
type awspg struct {
pg
}
func newAWSPG(postgres *pg) PG {
return &awspg{
*postgres,
}
}
func (c *awspg) AlterDefaultLoginRole(role, setRole string) error {
// On AWS RDS the postgres user isn't really superuser so he doesn't have permissions
// to ALTER USER unless he belongs to both roles
err := c.GrantRole(role, c.user, false)
if err != nil {
return err
}
defer func() {
err := c.RevokeRole(role, c.user)
// Check error
if err != nil {
c.log.Error(err, "error in revoke role")
}
}()
return c.pg.AlterDefaultLoginRole(role, setRole)
}
func (c *awspg) CreateDB(dbname, role string) error {
err := c.connect(c.defaultDatabase)
if err != nil {
return err
}
_, err = c.db.Exec(fmt.Sprintf(CreateDBWithoutOwnerSQLTemplate, dbname))
if err != nil {
// eat DUPLICATE DATABASE ERROR
// Try to cast error
pqErr, ok := err.(*pq.Error)
if !ok || pqErr.Code != DuplicateDatabaseErrorCode {
return err
}
}
_, err = c.db.Exec(fmt.Sprintf(AlterDBOwnerSQLTemplate, dbname, role))
if err != nil {
return err
}
return nil
}
func (c *awspg) DropRoleAndDropAndChangeOwnedBy(role, newOwner, database string) error {
// On AWS RDS the postgres user isn't really superuser so he doesn't have permissions
// to REASSIGN OWNED BY unless he belongs to both roles
err := c.GrantRole(role, c.user, false)
// Check error
if err != nil {
// Try to cast error
pqErr, ok := err.(*pq.Error)
if !ok {
return err
}
if pqErr.Code == RoleNotFoundErrorCode {
return nil
}
if pqErr.Code != InvalidGrantOperationErrorCode {
return err
}
}
err = c.GrantRole(newOwner, c.user, false)
// Check error
if err != nil {
// Try to cast error
pqErr, ok := err.(*pq.Error)
if !ok {
return err
}
if pqErr.Code == RoleNotFoundErrorCode {
// The group role does not exist, no point of granting roles
c.log.Info(fmt.Sprintf("not granting %s to %s as %s does not exist", role, newOwner, newOwner))
return nil
}
if pqErr.Code != InvalidGrantOperationErrorCode {
return err
}
}
defer func() {
err := c.RevokeRole(newOwner, c.user)
if err != nil {
c.log.Error(err, "error in revoke role")
}
}()
return c.pg.DropRoleAndDropAndChangeOwnedBy(role, newOwner, database)
}
func (c *awspg) ChangeAndDropOwnedBy(role, newOwner, database string) error {
// On AWS RDS the postgres user isn't really superuser so he doesn't have permissions
// to REASSIGN OWNED BY unless he belongs to both roles
err := c.GrantRole(role, c.user, false)
// Check error
if err != nil {
// Try to cast error
pqErr, ok := err.(*pq.Error)
if !ok {
return err
}
if pqErr.Code == RoleNotFoundErrorCode {
return nil
}
if pqErr.Code != InvalidGrantOperationErrorCode {
return err
}
}
err = c.GrantRole(newOwner, c.user, false)
// Check error
if err != nil {
// Try to cast error
pqErr, ok := err.(*pq.Error)
if !ok {
return err
}
if pqErr.Code == RoleNotFoundErrorCode {
// The group role does not exist, no point of granting roles
c.log.Info(fmt.Sprintf("not granting %s to %s as %s does not exist", role, newOwner, newOwner))
return nil
}
if pqErr.Code != InvalidGrantOperationErrorCode {
return err
}
}
defer func() {
err := c.RevokeRole(newOwner, c.user)
if err != nil {
c.log.Error(err, "error in revoke role")
}
}()
return c.pg.ChangeAndDropOwnedBy(role, newOwner, database)
}