/
plugin.go
258 lines (217 loc) · 5.66 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
package main
import (
"io"
"net/http"
"os"
"strings"
"time"
fmt "github.com/jhunt/go-ansi"
"golang.org/x/oauth2"
oauthgoogle "golang.org/x/oauth2/google"
"google.golang.org/api/storage/v1"
"github.com/shieldproject/shield/plugin"
)
const (
DefaultJsonKey = ""
DefaultPrefix = ""
)
func main() {
p := GooglePlugin{
Name: "Google Cloud Storage Plugin",
Author: "SHIELD Core Team",
Version: "0.0.1",
Features: plugin.PluginFeatures{
Target: "no",
Store: "yes",
},
Example: `
{
"json_key": "your-gcs-json-key", # optional, Google Cloud JSON key
"bucket": "name-of-your-bucket", # REQUIRED
"prefix" "/path/in/bucket" # optional, where to store archives inside the bucket
}
`,
Defaults: `
{
# there are no defaults.
}
`,
Fields: []plugin.Field{
plugin.Field{
Mode: "store",
Name: "json_key",
Type: "string",
Title: "Google Cloud JSON key",
Help: "Your Google Cloud Store JSON key, which should be available via the Google Cloud Platform web UI.",
},
plugin.Field{
Mode: "store",
Name: "bucket",
Type: "string",
Title: "Bucket Name",
Help: "Name of the bucket to store backup archives in.",
Required: true,
},
plugin.Field{
Mode: "store",
Name: "prefix",
Type: "string",
Title: "Bucket Path Prefix",
Help: "An optional sub-path of the bucket to use for storing archives. By default, archives are stored in the root of the bucket.",
},
},
}
plugin.Run(p)
}
type GooglePlugin plugin.PluginInfo
type GoogleConnectionInfo struct {
JsonKey string
Bucket string
Prefix string
}
func (p GooglePlugin) Meta() plugin.PluginInfo {
return plugin.PluginInfo(p)
}
func (p GooglePlugin) Validate(endpoint plugin.ShieldEndpoint) error {
var (
s string
err error
fail bool
)
s, err = endpoint.StringValueDefault("json_key", DefaultJsonKey)
if err != nil {
fmt.Printf("@R{\u2717 json_key %s}\n", err)
fail = true
} else if s == "" {
fmt.Printf("@G{\u2713 json_key} (using Google Application Default Credentials)\n")
} else {
fmt.Printf("@G{\u2713 json_key} @C{%s}\n", plugin.Redact(s))
}
s, err = endpoint.StringValue("bucket")
if err != nil {
fmt.Printf("@R{\u2717 bucket %s}\n", err)
fail = true
} else {
fmt.Printf("@G{\u2713 bucket} @C{%s}\n", plugin.Redact(s))
}
s, err = endpoint.StringValueDefault("prefix", DefaultPrefix)
if err != nil {
fmt.Printf("@R{\u2717 prefix %s}\n", err)
fail = true
} else if s == "" {
fmt.Printf("@G{\u2713 prefix} (none)\n")
} else {
s = strings.TrimLeft(s, "/")
fmt.Printf("@G{\u2713 prefix} @C{%s}\n", s)
}
if fail {
return fmt.Errorf("google: invalid configuration")
}
return nil
}
func (p GooglePlugin) Backup(endpoint plugin.ShieldEndpoint) error {
return plugin.UNIMPLEMENTED
}
func (p GooglePlugin) Restore(endpoint plugin.ShieldEndpoint) error {
return plugin.UNIMPLEMENTED
}
func (p GooglePlugin) Store(endpoint plugin.ShieldEndpoint) (string, int64, error) {
gcs, err := getGoogleConnInfo(endpoint)
if err != nil {
return "", 0, err
}
client, err := gcs.Connect()
if err != nil {
return "", 0, err
}
path := gcs.genBackupPath()
plugin.DEBUG("Storing data in %s", path)
object, err := client.Objects.Insert(gcs.Bucket, &storage.Object{Name: path}).Media(os.Stdin).Do()
if err != nil {
return "", 0, err
}
return path, int64(object.Size), nil
}
func (p GooglePlugin) Retrieve(endpoint plugin.ShieldEndpoint, file string) error {
gcs, err := getGoogleConnInfo(endpoint)
if err != nil {
return err
}
client, err := gcs.Connect()
if err != nil {
return err
}
res, err := client.Objects.Get(gcs.Bucket, file).Download()
if err != nil {
return err
}
defer res.Body.Close()
if _, err = io.Copy(os.Stdout, res.Body); err != nil {
return err
}
return nil
}
func (p GooglePlugin) Purge(endpoint plugin.ShieldEndpoint, file string) error {
gcs, err := getGoogleConnInfo(endpoint)
if err != nil {
return err
}
client, err := gcs.Connect()
if err != nil {
return err
}
if err := client.Objects.Delete(gcs.Bucket, file).Do(); err != nil {
return err
}
return nil
}
func getGoogleConnInfo(e plugin.ShieldEndpoint) (GoogleConnectionInfo, error) {
jsonKey, err := e.StringValueDefault("json_key", DefaultJsonKey)
if err != nil {
return GoogleConnectionInfo{}, err
}
bucket, err := e.StringValue("bucket")
if err != nil {
return GoogleConnectionInfo{}, err
}
prefix, err := e.StringValueDefault("prefix", DefaultPrefix)
if err != nil {
return GoogleConnectionInfo{}, err
}
prefix = strings.TrimLeft(prefix, "/")
return GoogleConnectionInfo{
JsonKey: jsonKey,
Bucket: bucket,
Prefix: prefix,
}, nil
}
func (gcs GoogleConnectionInfo) genBackupPath() string {
t := time.Now()
year, mon, day := t.Date()
hour, min, sec := t.Clock()
uuid := plugin.GenUUID()
path := fmt.Sprintf("%s/%04d/%02d/%02d/%04d-%02d-%02d-%02d%02d%02d-%s", gcs.Prefix, year, mon, day, year, mon, day, hour, min, sec, uuid)
path = strings.Replace(path, "//", "/", -1)
return path
}
func (gcs GoogleConnectionInfo) Connect() (*storage.Service, error) {
var err error
var storageClient *http.Client
if gcs.JsonKey != "" {
storageJwtConf, err := oauthgoogle.JWTConfigFromJSON([]byte(gcs.JsonKey), storage.DevstorageFullControlScope)
if err != nil {
return nil, err
}
storageClient = storageJwtConf.Client(oauth2.NoContext)
} else {
storageClient, err = oauthgoogle.DefaultClient(oauth2.NoContext, storage.DevstorageFullControlScope)
if err != nil {
return nil, err
}
}
storageService, err := storage.New(storageClient)
if err != nil {
return nil, err
}
return storageService, nil
}