-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcshandler.go
317 lines (282 loc) · 9.33 KB
/
gcshandler.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
package gcssessions
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"context"
"fmt"
"io"
"net/url"
"path/filepath"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/api/iterator"
"google.golang.org/grpc"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/session"
"cloud.google.com/go/storage"
"google.golang.org/api/option"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
)
var (
uploadRequests = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gcs_event_storage_uploads",
Help: "Number of uploads to the GCS backend",
},
)
downloadRequests = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gcs_event_storage_downloads",
Help: "Number of downloads from the GCS backend",
},
)
uploadLatencies = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "gcs_event_storage_uploads_seconds",
Help: "Latency for GCS upload operations",
// lowest bucket start of upper bound 0.001 sec (1 ms) with factor 2
// highest bucket start of 0.001 sec * 2^15 == 32.768 sec
Buckets: prometheus.ExponentialBuckets(0.001, 2, 16),
},
)
downloadLatencies = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "gcs_event_storage_downloads_seconds",
Help: "Latency for GCS download operations",
// lowest bucket start of upper bound 0.001 sec (1 ms) with factor 2
// highest bucket start of 0.001 sec * 2^15 == 32.768 sec
Buckets: prometheus.ExponentialBuckets(0.001, 2, 16),
},
)
)
func init() {
prometheus.MustRegister(uploadRequests)
prometheus.MustRegister(downloadRequests)
prometheus.MustRegister(uploadLatencies)
prometheus.MustRegister(downloadLatencies)
}
const (
// endpointPropertyKey
endpointPropertyKey = "endpoint"
// credentialsPath is used to supply credentials to teleport via JSON-typed service account key file
credentialsPath = "credentialsPath"
// projectID is used to to lookup GCS resources for a given GCP project
projectID = "projectID"
// kmsKeyName
kmsKeyName = "keyName"
// pathPropertyKey
pathPropertyKey = "path"
)
// Config is handler configuration
type Config struct {
// Bucket is GCS bucket name
Bucket string
// Path is an optional bucket path
Path string
// Path to the credentials file
CredentialsPath string
// The GCS project ID
ProjectID string
// KMS key name
KMSKeyName string
// Endpoint
Endpoint string
}
// SetFromURL sets values on the Config from the supplied URI
func (cfg *Config) SetFromURL(url *url.URL) error {
kmsKeyNameParamString := url.Query().Get(kmsKeyName)
if len(kmsKeyNameParamString) > 0 {
cfg.KMSKeyName = kmsKeyNameParamString
}
endpointParamString := url.Query().Get(endpointPropertyKey)
if len(endpointParamString) > 0 {
cfg.Endpoint = endpointParamString
}
pathParamString := url.Query().Get(pathPropertyKey)
if len(pathParamString) > 0 {
cfg.Path = pathParamString
}
credentialsPathParamString := url.Query().Get(credentialsPath)
if len(credentialsPathParamString) > 0 {
cfg.CredentialsPath = credentialsPathParamString
}
projectIDParamString := url.Query().Get(projectID)
if projectIDParamString == "" {
return trace.BadParameter("parameter %s with value '%s' is invalid",
projectID, projectIDParamString)
} else {
cfg.ProjectID = projectIDParamString
}
if url.Host == "" {
return trace.BadParameter("host should be set to the bucket name for recording storage")
} else {
cfg.Bucket = url.Host
}
return nil
}
// DefaultNewHandler returns a new handler with default GCS client settings derived from the config
func DefaultNewHandler(cfg Config) (*Handler, error) {
var args []option.ClientOption
if len(cfg.Endpoint) != 0 {
args = append(args, option.WithoutAuthentication(), option.WithEndpoint(cfg.Endpoint), option.WithGRPCDialOption(grpc.WithInsecure()))
} else if len(cfg.CredentialsPath) != 0 {
args = append(args, option.WithCredentialsFile(cfg.CredentialsPath))
}
ctx, cancelFunc := context.WithCancel(context.Background())
client, err := storage.NewClient(ctx, args...)
if err != nil {
cancelFunc()
return nil, trace.Wrap(convertGCSError(err), "error creating GCS gcsClient")
}
return NewHandler(ctx, cancelFunc, cfg, client)
}
// NewHandler returns a new handler with specific context, cancelFunc, and client
func NewHandler(ctx context.Context, cancelFunc context.CancelFunc, cfg Config, client *storage.Client) (*Handler, error) {
h := &Handler{
Entry: log.WithFields(log.Fields{
trace.Component: teleport.Component(teleport.SchemeGCS),
}),
Config: cfg,
gcsClient: client,
clientContext: ctx,
clientCancel: cancelFunc,
}
start := time.Now()
h.Infof("Setting up bucket %q, sessions path %q.", h.Bucket, h.Path)
if err := h.ensureBucket(); err != nil {
return nil, trace.Wrap(err)
}
h.WithFields(log.Fields{"duration": time.Now().Sub(start)}).Infof("Setup bucket %q completed.", h.Bucket)
return h, nil
}
// Handler handles upload and downloads to GCS object storage
type Handler struct {
// Config is handler configuration
Config
// Entry is a logging entry
*log.Entry
// gcsClient is the google cloud storage client used for persistence
gcsClient *storage.Client
// clientContext is used for non-request operations and cleanup
clientContext context.Context
// clientCancel is a function that will cancel the clientContext
clientCancel context.CancelFunc
}
// Closer releases connection and resources associated with log if any
func (h *Handler) Close() error {
h.clientCancel()
return h.gcsClient.Close()
}
// Upload uploads object to GCS bucket, reads the contents of the object from reader
// and returns the target GCS bucket path in case of successful upload.
func (h *Handler) Upload(ctx context.Context, sessionID session.ID, reader io.Reader) (string, error) {
path := h.path(sessionID)
h.Logger.Debugf("uploading %s", path)
writer := h.gcsClient.Bucket(h.Config.Bucket).Object(path).NewWriter(ctx)
start := time.Now()
_, err := io.Copy(writer, reader)
if err == nil {
err = writer.Close()
}
uploadLatencies.Observe(time.Since(start).Seconds())
uploadRequests.Inc()
if err != nil {
return "", convertGCSError(err)
}
return fmt.Sprintf("%v://%v/%v", teleport.SchemeGCS, h.Bucket, path), nil
}
// Download downloads recorded session from GCS bucket and writes the results into writer
// return trace.NotFound error is object is not found
func (h *Handler) Download(ctx context.Context, sessionID session.ID, writer io.WriterAt) error {
path := h.path(sessionID)
h.Logger.Debugf("downloading %s", path)
reader, err := h.gcsClient.Bucket(h.Config.Bucket).Object(path).NewReader(ctx)
if err != nil {
return convertGCSError(err)
}
start := time.Now()
written, err := io.Copy(writer.(io.Writer), reader)
if err != nil {
return convertGCSError(err)
}
downloadLatencies.Observe(time.Since(start).Seconds())
downloadRequests.Inc()
if written == 0 {
return trace.NotFound("recording for %v is not found", sessionID)
}
return nil
}
// delete bucket deletes bucket and all it's contents and is used in tests
// this app should not have the authority to create/destroy resources
func (h *Handler) deleteBucket() error {
objectsIterator := h.gcsClient.Bucket(h.Config.Bucket).Objects(h.clientContext, nil)
for {
attrs, err := objectsIterator.Next()
if err == iterator.Done {
break
}
if err != nil {
return convertGCSError(err)
}
err = h.gcsClient.Bucket(h.Config.Bucket).Object(attrs.Name).Delete(h.clientContext)
if err != nil {
return convertGCSError(err)
}
}
err := h.gcsClient.Bucket(h.Config.Bucket).Delete(h.clientContext)
return convertGCSError(err)
}
func (h *Handler) path(sessionID session.ID) string {
if h.Path == "" {
return string(sessionID) + ".tar"
}
return strings.TrimPrefix(filepath.Join(h.Path, string(sessionID)+".tar"), "/")
}
// ensureBucket makes sure bucket exists, and if it does not, creates it
// this app should not have the authority to create/destroy resources
func (h *Handler) ensureBucket() error {
_, err := h.gcsClient.Bucket(h.Config.Bucket).Attrs(h.clientContext)
err = convertGCSError(err)
// assumes that bucket is administered by other entity
if err == nil {
return nil
}
if !trace.IsNotFound(err) {
return trace.Wrap(err)
}
err = h.gcsClient.Bucket(h.Config.Bucket).Create(h.clientContext, h.Config.ProjectID, &storage.BucketAttrs{
VersioningEnabled: true,
Encryption: &storage.BucketEncryption{DefaultKMSKeyName: h.Config.KMSKeyName},
})
err = convertGCSError(err)
if err != nil {
if !trace.IsAlreadyExists(err) {
return trace.Wrap(err)
}
// if this gcsClient has not created the bucket, don't reconfigure it
return nil
}
return nil
}
func convertGCSError(err error, args ...interface{}) error {
if err == nil {
return nil
}
switch err {
case storage.ErrBucketNotExist, storage.ErrObjectNotExist:
return trace.NotFound(err.Error(), args...)
default:
return trace.BadParameter(err.Error(), args...)
}
}