/
plugin.go
406 lines (364 loc) · 10.9 KB
/
plugin.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
package main
import (
"bufio"
"io"
"os"
"os/exec"
"regexp"
fmt "github.com/jhunt/go-ansi"
"github.com/shieldproject/shield/plugin"
)
var (
DefaultPort = "5432"
)
func main() {
p := PostgresPlugin{
Name: "PostgreSQL Backup Plugin",
Author: "SHIELD Core Team",
Version: "0.0.1",
Features: plugin.PluginFeatures{
Target: "yes",
Store: "no",
},
Example: `
{
"pg_user" : "username", # REQUIRED
"pg_password" : "password",
"pg_host" : "10.0.0.1",
"pg_port" : "5432", # Port that PostgreSQL is listening on
"pg_database" : "db1", # Limit backup/restore operation to this database
"pg_bindir" : "/path/to/pg/bin" # Where to find the psql command
"pg_options" : "", # optional
}
`,
Defaults: `
{
"pg_port" : "5432",
"pg_bindir" : "/var/vcap/packages/postgres-9.4/bin"
}
`,
Fields: []plugin.Field{
plugin.Field{
Mode: "target",
Name: "pg_host",
Type: "string",
Title: "PostgreSQL Host",
Help: "The hostname or IP address of your PostgreSQL server.",
},
plugin.Field{
Mode: "target",
Name: "pg_port",
Type: "port",
Title: "PostgreSQL Port",
Help: "The TCP port that PostgreSQL is bound to, listening for incoming connections.",
Default: "5432",
},
plugin.Field{
Mode: "target",
Name: "pg_user",
Type: "string",
Title: "PostgreSQL Username",
Help: "Username to authenticate to PostgreSQL as.",
Required: true,
},
plugin.Field{
Mode: "target",
Name: "pg_password",
Type: "password",
Title: "PostgreSQL Password",
Help: "Password to authenticate to PostgreSQL as.",
},
plugin.Field{
Mode: "target",
Name: "pg_database",
Type: "string",
Title: "Database to Backup",
Help: "Limit scope of the backup to include only this database. By default, all databases will be backed up.",
},
plugin.Field{
Mode: "target",
Name: "pg_read_replica_host",
Type: "string",
Title: "Postgres Read Replica (Host)",
Help: "An optional Postgres replica (possibly readonly) to use for backups, instead of the canonical host. Restore operations will still be conducted against the real database host.",
},
plugin.Field{
Mode: "target",
Name: "pg_read_replica_port",
Type: "string",
Title: "Postgres Read Replica (Port)",
Help: "An optional Postgres replica (possibly readonly) to use for backups, instead of the canonical host. Restore operations will still be conducted against the real database host.",
},
plugin.Field{
Mode: "target",
Name: "pg_options",
Type: "string",
Title: "Additional `pg_dump` options",
Help: "You can tune `pg_dump` (which performs the backup) by specifying additional options and command-line arguments. If you don't know why you might need this, leave it blank.",
Example: "--data-only",
},
plugin.Field{
Mode: "target",
Name: "pg_bindir",
Type: "abspath",
Title: "Path to PostgreSQL bin/ directory",
Help: "The absolute path to the bin/ directory that contains the `psql` command.",
Default: "/var/vcap/packages/postgres-9.4/bin",
},
},
}
plugin.Run(p)
}
type PostgresPlugin plugin.PluginInfo
type PostgresConnectionInfo struct {
Host string
Port string
User string
Password string
Bin string
ReplicaHost string
ReplicaPort string
Database string
Options string
}
func (p PostgresPlugin) Meta() plugin.PluginInfo {
return plugin.PluginInfo(p)
}
func (p PostgresPlugin) Validate(endpoint plugin.ShieldEndpoint) error {
var (
s string
err error
fail bool
)
s, err = endpoint.StringValueDefault("pg_host", "")
if err != nil {
fmt.Printf("@R{\u2717 pg_host %s}\n", err)
fail = true
} else if s == "" {
fmt.Printf("@G{\u2713 pg_host} using @C{localhost}\n")
} else {
fmt.Printf("@G{\u2713 pg_host} @C{%s}\n", s)
}
s, err = endpoint.StringValueDefault("pg_port", "")
if err != nil {
fmt.Printf("@R{\u2717 pg_port %s}\n", err)
fail = true
} else if s == "" {
fmt.Printf("@G{\u2713 pg_port} using default port @C{%s}\n", DefaultPort)
} else {
fmt.Printf("@G{\u2713 pg_port} @C{%s}\n", s)
}
s, err = endpoint.StringValue("pg_user")
if err != nil {
fmt.Printf("@R{\u2717 pg_user %s}\n", err)
fail = true
} else {
fmt.Printf("@G{\u2713 pg_user} @C{%s}\n", plugin.Redact(s))
}
s, err = endpoint.StringValueDefault("pg_password", "")
if err != nil {
fmt.Printf("@R{\u2717 pg_password %s}\n", err)
fail = true
} else if s == "" {
fmt.Printf("@G{\u2713 pg_password} none (no credentials will be sent)\n")
} else {
fmt.Printf("@G{\u2713 pg_password} @C{%s}\n", plugin.Redact(s))
}
s, err = endpoint.StringValueDefault("pg_database", "")
if err != nil {
fmt.Printf("@R{\u2717 pg_database %s}\n", err)
} else if s == "" {
fmt.Printf("@G{\u2713 pg_database} none (all databases will be backed up)\n")
} else {
fmt.Printf("@G{\u2713 pg_database} @C{%s}\n", s)
}
s, err = endpoint.StringValueDefault("pg_read_replica_host", "")
if err != nil {
fmt.Printf("@R{\u2717 pg_read_replica_host %s}\n", err)
fail = true
} else if s == "" {
fmt.Printf("@G{\u2713 pg_read_replica_host} no read replica given\n")
} else {
fmt.Printf("@G{\u2713 pg_read_replica_host} @C{%s}\n", plugin.Redact(s))
}
s, err = endpoint.StringValueDefault("pg_read_replica_port", "")
if err != nil {
fmt.Printf("@R{\u2717 pg_read_replica_port %s}\n", err)
fail = true
} else if s == "" {
fmt.Printf("@G{\u2713 pg_read_replica_port} no read replica given\n")
} else {
fmt.Printf("@G{\u2713 pg_read_replica_port} @C{%s}\n", plugin.Redact(s))
}
s, err = endpoint.StringValueDefault("pg_options", "")
if err != nil {
fmt.Printf("@R{\u2717 pg_options %s}\n", err)
fail = true
} else if s == "" {
fmt.Printf("@G{\u2713 pg_options} no options given\n")
} else {
fmt.Printf("@G{\u2713 pg_options} @C{%s}\n", s)
}
if fail {
return fmt.Errorf("postgres: invalid configuration")
}
return nil
}
func (p PostgresPlugin) Backup(endpoint plugin.ShieldEndpoint) error {
pg, err := pgConnectionInfo(endpoint)
if err != nil {
return err
}
if pg.ReplicaHost != "" && pg.ReplicaPort != "" {
plugin.DEBUG("Using readonly replica -> `%s`", plugin.Redact(fmt.Sprintf("%s:%s", pg.Host, pg.Port)))
pg.Host = pg.ReplicaHost
pg.Port = pg.ReplicaPort
}
setupEnvironmentVariables(pg)
cmd := ""
if pg.Database != "" {
// Run dump all on the specified db
cmd = fmt.Sprintf("%s/pg_dump %s -C -c --no-password %s", pg.Bin, pg.Database, pg.Options)
} else {
// Else run dump on all
cmd = fmt.Sprintf("%s/pg_dumpall -c --no-password %s", pg.Bin, pg.Options)
}
plugin.DEBUG("Executing: `%s`", cmd)
return plugin.Exec(cmd, plugin.STDOUT)
}
func (p PostgresPlugin) Restore(endpoint plugin.ShieldEndpoint) error {
pg, err := pgConnectionInfo(endpoint)
if err != nil {
return err
}
setupEnvironmentVariables(pg)
cmd := exec.Command(fmt.Sprintf("%s/psql", pg.Bin), "-d", "postgres")
plugin.DEBUG("Exec: %s/psql -d postgres", pg.Bin)
plugin.DEBUG("Redirecting stdout and stderr to stderr")
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
scanErr := make(chan error)
go func(out io.WriteCloser, in io.Reader, errChan chan<- error) {
plugin.DEBUG("Starting to read SQL statements from stdin...")
r := bufio.NewReader(in)
reg := regexp.MustCompile("^DROP DATABASE (.*);$")
i := 0
for {
thisLine := []byte{}
isPrefix := true
var err error
for isPrefix {
var tmpLine []byte
tmpLine, isPrefix, err = r.ReadLine()
if err != nil {
if err == io.EOF {
goto eof
}
errChan <- err
return
}
thisLine = append(thisLine, tmpLine...)
}
m := reg.FindStringSubmatch(string(thisLine))
if len(m) > 0 {
plugin.DEBUG("Found dropped database '%s' on line %d", m[1], i)
out.Write([]byte(fmt.Sprintf("UPDATE pg_database SET datallowconn = 'false' WHERE datname = '%s';\n", m[1])))
out.Write([]byte(fmt.Sprintf("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '%s';\n", m[1])))
}
_, err = out.Write([]byte(string(thisLine) + "\n"))
if err != nil {
plugin.DEBUG("Error when writing to output: %s", err)
errChan <- err
return
}
i++
}
eof:
plugin.DEBUG("Completed restore with %d lines of SQL", i)
out.Close()
errChan <- nil
}(stdin, os.Stdin, scanErr)
err = cmd.Run()
if err != nil {
return err
}
return <-scanErr
}
func (p PostgresPlugin) Store(endpoint plugin.ShieldEndpoint) (string, int64, error) {
return "", 0, plugin.UNIMPLEMENTED
}
func (p PostgresPlugin) Retrieve(endpoint plugin.ShieldEndpoint, file string) error {
return plugin.UNIMPLEMENTED
}
func (p PostgresPlugin) Purge(endpoint plugin.ShieldEndpoint, file string) error {
return plugin.UNIMPLEMENTED
}
func setupEnvironmentVariables(pg *PostgresConnectionInfo) {
plugin.DEBUG("Setting up env:\n PGUSER=%s, PGPASSWORD=%s, PGHOST=%s, PGPORT=%s", pg.User, pg.Password, pg.Host, pg.Port)
os.Setenv("PGUSER", pg.User)
os.Setenv("PGPASSWORD", pg.Password)
os.Setenv("PGHOST", pg.Host)
os.Setenv("PGPORT", pg.Port)
}
func pgConnectionInfo(endpoint plugin.ShieldEndpoint) (*PostgresConnectionInfo, error) {
user, err := endpoint.StringValue("pg_user")
if err != nil {
return nil, err
}
plugin.DEBUG("PGUSER: '%s'", user)
password, err := endpoint.StringValueDefault("pg_password", "")
if err != nil {
return nil, err
}
plugin.DEBUG("PGPASSWORD: '%s'", password)
host, err := endpoint.StringValueDefault("pg_host", "")
if err != nil {
return nil, err
}
plugin.DEBUG("PGHOST: '%s'", host)
port, err := endpoint.StringValueDefault("pg_port", DefaultPort)
if err != nil {
return nil, err
}
plugin.DEBUG("PGPORT: '%s'", port)
replicahost, err := endpoint.StringValueDefault("pg_read_replica_host", "")
if err != nil {
return nil, err
}
plugin.DEBUG("PG_READ_REPLICA_HOST: '%s'", replicahost)
replicaport, err := endpoint.StringValueDefault("pg_read_replica_port", "")
if err != nil {
return nil, err
}
plugin.DEBUG("PG_READ_REPLICA_PORT: '%s'", replicaport)
options, err := endpoint.StringValueDefault("pg_options", "")
if err != nil {
return nil, err
}
plugin.DEBUG("PG_OPTIONS: '%s'", options)
database, err := endpoint.StringValueDefault("pg_database", "")
if err != nil {
return nil, err
}
plugin.DEBUG("PGDATABASE: '%s'", database)
bin, err := endpoint.StringValueDefault("pg_bindir", "/var/vcap/packages/postgres-9.4/bin")
if err != nil {
return nil, err
}
plugin.DEBUG("PGBINDIR: '%s'", bin)
return &PostgresConnectionInfo{
Host: host,
Port: port,
User: user,
Password: password,
ReplicaHost: replicahost,
ReplicaPort: replicaport,
Bin: bin,
Database: database,
Options: options,
}, nil
}