forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcs.go
205 lines (164 loc) · 4.89 KB
/
gcs.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
package gcs
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strconv"
"strings"
"time"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/physical"
log "github.com/mgutz/logxi/v1"
"cloud.google.com/go/storage"
"github.com/armon/go-metrics"
"golang.org/x/net/context"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
// GCSBackend is a physical backend that stores data
// within an Google Cloud Storage bucket.
type GCSBackend struct {
bucketName string
client *storage.Client
permitPool *physical.PermitPool
logger log.Logger
}
// NewGCSBackend constructs a Google Cloud Storage backend using a pre-existing
// bucket. Credentials can be provided to the backend, sourced
// from environment variables or a service account file
func NewGCSBackend(conf map[string]string, logger log.Logger) (physical.Backend, error) {
bucketName := os.Getenv("GOOGLE_STORAGE_BUCKET")
if bucketName == "" {
bucketName = conf["bucket"]
if bucketName == "" {
return nil, fmt.Errorf("env var GOOGLE_STORAGE_BUCKET or configuration parameter 'bucket' must be set")
}
}
// path to service account JSON file
credentialsFile := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if credentialsFile == "" {
credentialsFile = conf["credentials_file"]
if credentialsFile == "" {
return nil, fmt.Errorf("env var GOOGLE_APPLICATION_CREDENTIALS or configuration parameter 'credentials_file' must be set")
}
}
client, err := storage.NewClient(
context.Background(),
option.WithServiceAccountFile(credentialsFile),
)
if err != nil {
return nil, fmt.Errorf("error establishing storage client: '%v'", err)
}
// check client connectivity by getting bucket attributes
_, err = client.Bucket(bucketName).Attrs(context.Background())
if err != nil {
return nil, fmt.Errorf("unable to access bucket '%s': '%v'", bucketName, err)
}
maxParStr, ok := conf["max_parallel"]
var maxParInt int
if ok {
maxParInt, err = strconv.Atoi(maxParStr)
if err != nil {
return nil, errwrap.Wrapf("failed parsing max_parallel parameter: {{err}}", err)
}
if logger.IsDebug() {
logger.Debug("physical/gcs: max_parallel set", "max_parallel", maxParInt)
}
}
g := GCSBackend{
bucketName: bucketName,
client: client,
permitPool: physical.NewPermitPool(maxParInt),
logger: logger,
}
return &g, nil
}
// Put is used to insert or update an entry
func (g *GCSBackend) Put(entry *physical.Entry) error {
defer metrics.MeasureSince([]string{"gcs", "put"}, time.Now())
bucket := g.client.Bucket(g.bucketName)
writer := bucket.Object(entry.Key).NewWriter(context.Background())
g.permitPool.Acquire()
defer g.permitPool.Release()
defer writer.Close()
_, err := writer.Write(entry.Value)
return err
}
// Get is used to fetch an entry
func (g *GCSBackend) Get(key string) (*physical.Entry, error) {
defer metrics.MeasureSince([]string{"gcs", "get"}, time.Now())
bucket := g.client.Bucket(g.bucketName)
reader, err := bucket.Object(key).NewReader(context.Background())
// return (nil, nil) if object doesn't exist
if err == storage.ErrObjectNotExist {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("error creating bucket reader: '%v'", err)
}
g.permitPool.Acquire()
defer g.permitPool.Release()
defer reader.Close()
value, err := ioutil.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("error reading object '%v': '%v'", key, err)
}
ent := physical.Entry{
Key: key,
Value: value,
}
return &ent, nil
}
// Delete is used to permanently delete an entry
func (g *GCSBackend) Delete(key string) error {
defer metrics.MeasureSince([]string{"gcs", "delete"}, time.Now())
bucket := g.client.Bucket(g.bucketName)
g.permitPool.Acquire()
defer g.permitPool.Release()
err := bucket.Object(key).Delete(context.Background())
// deletion of non existent object is OK
if err == storage.ErrObjectNotExist {
return nil
} else if err != nil {
return fmt.Errorf("error deleting object '%v': '%v'", key, err)
}
return nil
}
// List is used to list all the keys under a given
// prefix, up to the next prefix.
func (g *GCSBackend) List(prefix string) ([]string, error) {
defer metrics.MeasureSince([]string{"gcs", "list"}, time.Now())
bucket := g.client.Bucket(g.bucketName)
objects_it := bucket.Objects(
context.Background(),
&storage.Query{
Prefix: prefix,
Delimiter: "/",
Versions: false,
})
keys := []string{}
g.permitPool.Acquire()
defer g.permitPool.Release()
for {
objAttrs, err := objects_it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, fmt.Errorf("error listing bucket '%v': '%v'", g.bucketName, err)
}
path := ""
if objAttrs.Prefix != "" {
// "subdirectory"
path = objAttrs.Prefix
} else {
// file
path = objAttrs.Name
}
// get relative file/dir just like "basename"
key := strings.TrimPrefix(path, prefix)
keys = append(keys, key)
}
sort.Strings(keys)
return keys, nil
}