-
Notifications
You must be signed in to change notification settings - Fork 43
/
azblob.go
236 lines (201 loc) · 6.17 KB
/
azblob.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
// Copyright 2021 Nitric Pty Ltd.
//
// 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.
package azblob_service
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/url"
"time"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/nitric-dev/membrane/pkg/plugins/errors"
"github.com/nitric-dev/membrane/pkg/plugins/errors/codes"
"github.com/nitric-dev/membrane/pkg/plugins/storage"
azblob_service_iface "github.com/nitric-dev/membrane/pkg/plugins/storage/azblob/iface"
azureutils "github.com/nitric-dev/membrane/pkg/providers/azure/utils"
"github.com/nitric-dev/membrane/pkg/utils"
)
// AzblobStorageService - Nitric membrane storage plugin implementation for Azure Storage
type AzblobStorageService struct {
client azblob_service_iface.AzblobServiceUrlIface
storage.UnimplementedStoragePlugin
}
func (a *AzblobStorageService) getBlobUrl(bucket string, key string) azblob_service_iface.AzblobBlockBlobUrlIface {
cUrl := a.client.NewContainerURL(bucket)
// Get a new blob for the key name
return cUrl.NewBlockBlobURL(key)
}
func (a *AzblobStorageService) Read(bucket string, key string) ([]byte, error) {
newErr := errors.ErrorsWithScope(
"AzblobStorageService.Read",
map[string]interface{}{
"bucket": bucket,
"key": key,
},
)
// Get the bucket for this bucket name
blob := a.getBlobUrl(bucket, key)
//// download the blob
r, err := blob.Download(
context.TODO(),
0,
azblob.CountToEnd,
azblob.BlobAccessConditions{},
false,
azblob.ClientProvidedKeyOptions{},
)
if err != nil {
return nil, newErr(
codes.Internal,
"Unable to download blob",
err,
)
}
// TODO: Configure retries
data := r.Body(azblob.RetryReaderOptions{MaxRetryRequests: 20})
return ioutil.ReadAll(data)
}
func (a *AzblobStorageService) Write(bucket string, key string, object []byte) error {
newErr := errors.ErrorsWithScope(
"AzblobStorageService.Write",
map[string]interface{}{
"bucket": bucket,
"key": key,
},
)
blob := a.getBlobUrl(bucket, key)
if _, err := blob.Upload(
context.TODO(),
bytes.NewReader(object),
azblob.BlobHTTPHeaders{},
azblob.Metadata{},
azblob.BlobAccessConditions{},
azblob.DefaultAccessTier,
nil,
azblob.ClientProvidedKeyOptions{},
); err != nil {
return newErr(
codes.Internal,
"Unable to write blob data",
err,
)
}
return nil
}
func (a *AzblobStorageService) Delete(bucket string, key string) error {
newErr := errors.ErrorsWithScope(
"AzblobStorageService.Delete",
map[string]interface{}{
"bucket": bucket,
"key": key,
},
)
// Get the bucket for this bucket name
blob := a.getBlobUrl(bucket, key)
if _, err := blob.Delete(
context.TODO(),
azblob.DeleteSnapshotsOptionInclude,
azblob.BlobAccessConditions{},
); err != nil {
return newErr(
codes.Internal,
"Unable to delete blob",
err,
)
}
return nil
}
func (s *AzblobStorageService) PreSignUrl(bucket string, key string, operation storage.Operation, expiry uint32) (string, error) {
newErr := errors.ErrorsWithScope(
"AzblobStorageService.PreSignUrl",
map[string]interface{}{
"bucket": bucket,
"key": key,
"operation": operation.String(),
},
)
blobUrlParts := azblob.NewBlobURLParts(s.getBlobUrl(bucket, key).Url())
currentTime := time.Now().UTC()
validDuration := currentTime.Add(time.Duration(expiry) * time.Second)
cred, err := s.client.GetUserDelegationCredential(context.TODO(), azblob.NewKeyInfo(currentTime, validDuration), nil, nil)
if err != nil {
return "", newErr(
codes.Internal,
"could not get user delegation credential",
err,
)
}
sigOpts := azblob.BlobSASSignatureValues{
Protocol: azblob.SASProtocolHTTPS,
ExpiryTime: validDuration,
Permissions: azblob.BlobSASPermissions{
Read: operation == storage.READ,
Write: operation == storage.WRITE,
}.String(),
BlobName: key,
ContainerName: bucket,
}
queryParams, err := sigOpts.NewSASQueryParameters(cred)
if err != nil {
return "", newErr(
codes.Internal,
"error signing query params for URL",
err,
)
}
blobUrlParts.SAS = queryParams
url := blobUrlParts.URL()
return url.String(), nil
}
const expiryBuffer = 2 * time.Minute
func tokenRefresherFromSpt(spt *adal.ServicePrincipalToken) azblob.TokenRefresher {
return func(credential azblob.TokenCredential) time.Duration {
if err := spt.Refresh(); err != nil {
fmt.Println("Error refreshing token: ", err)
} else {
tkn := spt.Token()
credential.SetToken(tkn.AccessToken)
return tkn.Expires().Sub(time.Now().Add(expiryBuffer))
}
// Mark the token as already expired
return time.Duration(0)
}
}
// New - Creates a new instance of the AzblobStorageService
func New() (storage.StorageService, error) {
// TODO: Create a default storage account for the stack???
// XXX: This will limit a membrane wrapped application
// to accessing a single storage account
blobEndpoint := utils.GetEnv(azureutils.AZURE_STORAGE_BLOB_ENDPOINT, "")
if blobEndpoint == "" {
return nil, fmt.Errorf("failed to determine Azure Storage Blob endpoint, environment variable %s not set", azureutils.AZURE_STORAGE_BLOB_ENDPOINT)
}
spt, err := azureutils.GetServicePrincipalToken(azure.PublicCloud.ResourceIdentifiers.Storage)
if err != nil {
return nil, err
}
cTkn := azblob.NewTokenCredential(spt.Token().AccessToken, tokenRefresherFromSpt(spt))
var accountURL *url.URL
if accountURL, err = url.Parse(blobEndpoint); err != nil {
return nil, err
}
pipeline := azblob.NewPipeline(cTkn, azblob.PipelineOptions{})
client := azblob.NewServiceURL(*accountURL, pipeline)
return &AzblobStorageService{
client: azblob_service_iface.AdaptServiceUrl(client),
}, nil
}