8
8
"fmt"
9
9
"strings"
10
10
11
+ "github.com/lib/pq"
11
12
"github.com/pkg/errors"
12
13
13
14
"gitlab.com/postgres-ai/database-lab/v2/pkg/log"
@@ -95,23 +96,23 @@ func CreateUser(c *resources.AppConfig, user resources.EphemeralUser) error {
95
96
}
96
97
97
98
func superuserQuery (username , password string ) string {
98
- return fmt .Sprintf (`create user "%s" with password '%s' login superuser;` , username , password )
99
+ return fmt .Sprintf (`create user %s with password %s login superuser;` , pq . QuoteIdentifier ( username ), pq . QuoteLiteral ( password ) )
99
100
}
100
101
101
102
const restrictionTemplate = `
102
103
-- create a new user
103
- create user %[1]s with password '%s' login;
104
+ create user @username with password @password login;
104
105
105
106
-- change a database owner
106
- alter database %s owner to %[1]s ;
107
+ alter database @database owner to @username ;
107
108
108
109
do $$
109
110
declare
110
111
new_owner text;
111
112
object_type record;
112
113
r record;
113
114
begin
114
- new_owner := '%[1]s' ;
115
+ new_owner := @usernameStr ;
115
116
116
117
-- c: composite type
117
118
-- p: partitioned table
@@ -133,16 +134,16 @@ begin
133
134
join pg_namespace n on
134
135
n.oid = c.relnamespace
135
136
and not n.nspname in ('pg_catalog', 'information_schema')
136
- and c.relkind = %% L
137
+ and c.relkind = %L
137
138
order by c.relname
138
139
$sql,ドル
139
140
object_type.code
140
141
)
141
142
loop
142
- raise debug 'Changing ownership of %% %%.%% to % %',
143
+ raise debug 'Changing ownership of % %.% to %',
143
144
object_type.type_name, r.nspname, r.relname, new_owner;
144
145
execute format(
145
- 'alter %% s %% I.%% I owner to % %I;',
146
+ 'alter %s %I.%I owner to %I;',
146
147
object_type.type_name,
147
148
r.nspname,
148
149
r.relname,
@@ -160,12 +161,12 @@ begin
160
161
from pg_catalog.pg_namespace as n
161
162
join pg_catalog.pg_proc as p on p.pronamespace = n.oid
162
163
where not n.nspname in ('pg_catalog', 'information_schema')
163
- and p.proname not ilike 'dblink%% ' -- We do not want dblink to be involved (exclusion)
164
+ and p.proname not ilike 'dblink%' -- We do not want dblink to be involved (exclusion)
164
165
loop
165
- raise debug 'Changing ownership of function %%.%%(%% ) to % %',
166
+ raise debug 'Changing ownership of function %.%(% ) to %',
166
167
r.nspname, r.proname, r.args, new_owner;
167
168
execute format(
168
- 'alter function %% I.%% I(%% s) owner to % %I', -- todo: check support CamelStyle r.args
169
+ 'alter function %I.%I(%s) owner to %I', -- todo: check support CamelStyle r.args
169
170
r.nspname,
170
171
r.proname,
171
172
r.args,
@@ -181,10 +182,10 @@ begin
181
182
join pg_catalog.pg_ts_dict d on d.dictnamespace = n.oid
182
183
where not n.nspname in ('pg_catalog', 'information_schema')
183
184
loop
184
- raise debug 'Changing ownership of text search dictionary %%.%% to % %',
185
+ raise debug 'Changing ownership of text search dictionary %.% to %',
185
186
r.nspname, r.dictname, new_owner;
186
187
execute format(
187
- 'alter text search dictionary %% I.%% I owner to % %I',
188
+ 'alter text search dictionary %I.%I owner to %I',
188
189
r.nspname,
189
190
r.dictname,
190
191
new_owner
@@ -198,22 +199,29 @@ begin
198
199
join pg_catalog.pg_namespace on pg_namespace.oid = pg_type.typnamespace
199
200
where typtype = 'd' and not nspname in ('pg_catalog', 'information_schema')
200
201
loop
201
- raise debug 'Changing ownership of domain %%.%% to % %',
202
+ raise debug 'Changing ownership of domain %.% to %',
202
203
r.nspname, r.typname, new_owner;
203
204
execute format(
204
- 'alter domain %% I.%% I owner to % %I',
205
+ 'alter domain %I.%I owner to %I',
205
206
r.nspname,
206
207
r.typname,
207
208
new_owner
208
209
);
209
210
end loop;
210
211
211
- grant select on pg_stat_activity to %[1]s ;
212
+ grant select on pg_stat_activity to @username ;
212
213
213
214
end
214
215
$$;
215
216
`
216
217
217
218
func restrictedUserQuery (username , password , database string ) string {
218
- return fmt .Sprintf (restrictionTemplate , username , password , database )
219
+ repl := strings .NewReplacer (
220
+ "@usernameStr" , pq .QuoteLiteral (username ),
221
+ "@username" , pq .QuoteIdentifier (username ),
222
+ "@password" , pq .QuoteLiteral (password ),
223
+ "@database" , pq .QuoteIdentifier (database ),
224
+ )
225
+
226
+ return repl .Replace (restrictionTemplate )
219
227
}
0 commit comments