-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-sheets.go
403 lines (333 loc) · 9.83 KB
/
google-sheets.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
package ds
import (
"bytes"
"context"
"github.com/fanonwue/go-short-link/internal/state"
"github.com/fanonwue/go-short-link/internal/util"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
"google.golang.org/api/drive/v3"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
type GoogleAuthConfig struct {
ProjectId string
ServiceAccountMail string
ServiceAccountKey []byte
ServiceAccountKeyId string
}
type GoogleSheetsConfig struct {
SpreadsheetId string
SkipFirstRow bool
ApiKey string
Auth *GoogleAuthConfig
}
type GoogleSheetsDataSource struct {
lastUpdate time.Time
lastUpdateMutex sync.RWMutex
lastModified time.Time
lastModifiedMutex sync.RWMutex
config GoogleSheetsConfig
httpClient *http.Client
sheetsService *sheets.Service
driveService *drive.Service
}
const (
// If enabled, the read key will be validated to make sure it is a PEM-formatted key
validateKeyContent = false
defaultKeyFilePath = "secret/privateKey.pem"
keyColumn = 0
targetColumn = 1
isActiveColumn = 2
)
func createSheetsConfig() GoogleSheetsConfig {
config := GoogleSheetsConfig{
SpreadsheetId: os.Getenv(util.PrefixedEnvVar("SPREADSHEET_ID")),
SkipFirstRow: true,
}
apiKey := os.Getenv(util.PrefixedEnvVar("API_KEY"))
if len(apiKey) == 0 {
auth := GoogleAuthConfig{
ProjectId: os.Getenv(util.PrefixedEnvVar("PROJECT_ID")),
ServiceAccountMail: os.Getenv(util.PrefixedEnvVar("SERVICE_ACCOUNT_CLIENT_EMAIL")),
ServiceAccountKey: getServiceAccountPrivateKey(),
ServiceAccountKeyId: os.Getenv(util.PrefixedEnvVar("SERVICE_ACCOUNT_PRIVATE_KEY_ID")),
}
config.Auth = &auth
} else {
config.ApiKey = apiKey
}
return config
}
func getServiceAccountPrivateKey() []byte {
// Read private key from env or file, and trim whitespace just to be sure
var keyData = readPrivateKeyData(true)
if len(keyData) < 100 {
util.Logger().Warnf("Keyfile smaller than 100 Bytes! This is probably unintended. Length: %d", len(keyData))
}
if validateKeyContent {
util.Logger().Info("Key validation enabled, performing checks")
keyString := string(keyData)
keyPrefix := "-----BEGIN PRIVATE KEY-----"
if !strings.HasPrefix(keyString, keyPrefix) {
util.Logger().Panicf("Key does not start with expected prefix: %s", keyPrefix)
}
keySuffix := "-----END PRIVATE KEY-----"
if !strings.HasSuffix(keyString, keySuffix) {
util.Logger().Panicf("Key does not end with expected suffix: %s", keySuffix)
}
} else {
util.Logger().Info("Key validation disabled, skipping checks")
}
return keyData
}
func readPrivateKeyData(trimWhitespace bool) []byte {
var keyData []byte
rawKey := os.Getenv(util.PrefixedEnvVar("SERVICE_ACCOUNT_PRIVATE_KEY"))
if len(rawKey) > 0 {
key := strings.Replace(rawKey, "\\n", "\n", -1)
keyData = []byte(key)
} else {
keyFile := os.Getenv(util.PrefixedEnvVar("SERVICE_ACCOUNT_PRIVATE_KEY_FILE"))
if len(keyFile) == 0 {
// Assume standard keyfile location
util.Logger().Debugf("Keyfile location not set, assuming default location: %s", defaultKeyFilePath)
keyFile = defaultKeyFilePath
}
keyFile, err := filepath.Abs(keyFile)
if err != nil {
util.Logger().Panicf("Could not create absolute path from path: %v", err)
}
util.Logger().Infof("Trying to read keyfile from path: %s", keyFile)
fileContent, err := os.ReadFile(keyFile)
if err != nil {
util.Logger().Panicf("Error reading keyfile: %v", err)
}
util.Logger().Debugf("Read keyfile, length: %d bytes", len(fileContent))
keyData = fileContent
}
if trimWhitespace {
return bytes.TrimSpace(keyData)
} else {
return keyData
}
}
func CreateSheetsDataSource() *GoogleSheetsDataSource {
_ds := GoogleSheetsDataSource{
config: createSheetsConfig(),
}
_ds.postCreate()
return &_ds
}
func (dsc *GoogleSheetsConfig) UseServiceAccount() bool {
return dsc.Auth != nil
}
func (ds *GoogleSheetsDataSource) apiScopes() []string {
return []string{
drive.DriveMetadataReadonlyScope,
sheets.SpreadsheetsReadonlyScope,
}
}
func (ds *GoogleSheetsDataSource) postCreate() {
fileWebLink, err := ds.SpreadsheetWebLink()
if err == nil {
util.Logger().Infof("Using document available at: %s", fileWebLink)
}
}
func (ds *GoogleSheetsDataSource) getClient() *http.Client {
if ds.httpClient == nil {
if ds.config.UseServiceAccount() {
jwtConfig := &jwt.Config{
Email: ds.config.Auth.ServiceAccountMail,
PrivateKey: ds.config.Auth.ServiceAccountKey,
TokenURL: google.JWTTokenURL,
Scopes: ds.apiScopes(),
}
keyId := ds.config.Auth.ServiceAccountKeyId
if len(keyId) > 0 {
jwtConfig.PrivateKeyID = keyId
}
ds.httpClient = jwtConfig.Client(context.Background())
} else {
ds.httpClient = &http.Client{}
}
}
return ds.httpClient
}
func (ds *GoogleSheetsDataSource) serviceClientOpts() []option.ClientOption {
var opts []option.ClientOption
if ds.config.UseServiceAccount() {
opts = append(opts, option.WithHTTPClient(ds.getClient()))
} else {
opts = append(opts,
option.WithAPIKey(os.Getenv(util.PrefixedEnvVar("API_KEY"))),
option.WithScopes(ds.apiScopes()...),
)
}
return opts
}
func (ds *GoogleSheetsDataSource) DriveService() *drive.Service {
if ds.driveService == nil {
newService, err := drive.NewService(context.Background(), ds.serviceClientOpts()...)
if err != nil {
util.Logger().Panicf("Could not create drive service: %v", err)
} else {
ds.driveService = newService
}
}
return ds.driveService
}
func (ds *GoogleSheetsDataSource) SheetsService() *sheets.Service {
if ds.sheetsService == nil {
newService, err := sheets.NewService(context.Background(), ds.serviceClientOpts()...)
if err != nil {
util.Logger().Panicf("Could not create sheets service: %v", err)
} else {
ds.sheetsService = newService
}
}
return ds.sheetsService
}
func (ds *GoogleSheetsDataSource) SpreadsheetId() string {
return ds.config.SpreadsheetId
}
func (ds *GoogleSheetsDataSource) Id() string {
return ds.SpreadsheetId()
}
func (ds *GoogleSheetsDataSource) LastUpdate() time.Time {
ds.lastUpdateMutex.RLock()
defer ds.lastUpdateMutex.RUnlock()
return ds.lastUpdate
}
func (ds *GoogleSheetsDataSource) SpreadsheetWebLink() (string, error) {
service := ds.DriveService()
file, err := service.Files.Get(ds.config.SpreadsheetId).Fields("webViewLink").Do()
if err != nil {
util.Logger().Warnf("Could not determine webViewLink for Spreadsheet '%s': %v", ds.config.SpreadsheetId, err)
return "", err
}
return file.WebViewLink, nil
}
func (ds *GoogleSheetsDataSource) updateLastUpdate(updateTime time.Time) time.Time {
ds.lastUpdateMutex.Lock()
defer ds.lastUpdateMutex.Unlock()
if updateTime.IsZero() {
updateTime = time.Now()
}
ds.lastUpdate = updateTime.UTC()
return ds.lastUpdate
}
func (ds *GoogleSheetsDataSource) updateLastModified() (time.Time, error) {
ds.lastModifiedMutex.Lock()
defer ds.lastModifiedMutex.Unlock()
service := ds.DriveService()
file, err := service.Files.Get(ds.config.SpreadsheetId).Fields("modifiedTime").Do()
if err != nil {
util.Logger().Errorf("Could not determine modifiedTime for Spreadsheet '%s': %v", ds.config.SpreadsheetId, err)
return time.Time{}, err
}
modifiedTimeRaw := file.ModifiedTime
modifiedTime, err := time.Parse(time.RFC3339, modifiedTimeRaw)
if err != nil {
util.Logger().Errorf("Could not parse RFC3339 timestamp %s: %v", modifiedTimeRaw, err)
return time.Time{}, err
}
modifiedTimeUtc := modifiedTime.UTC()
oldTime := ds.lastModified
ds.lastModified = modifiedTimeUtc
if oldTime.UnixMilli() != modifiedTimeUtc.UnixMilli() {
util.Logger().Debugf("Updated last modified time to %v", modifiedTimeUtc)
}
return ds.lastModified, nil
}
func (ds *GoogleSheetsDataSource) LastModified() time.Time {
ds.lastModifiedMutex.RLock()
if ds.lastModified.IsZero() {
// Release read lock to allow upgrade to a write lock
ds.lastModifiedMutex.RUnlock()
modified, _ := ds.updateLastModified()
return modified
}
defer ds.lastModifiedMutex.RUnlock()
return ds.lastModified
}
func (ds *GoogleSheetsDataSource) NeedsUpdate() bool {
if ds.lastUpdate.IsZero() {
return true
}
modifiedTime, err := ds.updateLastModified()
if err != nil {
return true
}
return modifiedTime.After(ds.lastUpdate)
}
func (ds *GoogleSheetsDataSource) fetchRedirectMappingInternal() (state.RedirectMap, time.Time, error) {
service := ds.SheetsService()
sheetsRange := "A2:C"
if !ds.config.SkipFirstRow {
sheetsRange = "A:C"
}
mapping := state.RedirectMap{}
updateTime := time.Now().UTC()
result, err := service.Spreadsheets.Values.Get(ds.config.SpreadsheetId, sheetsRange).
ValueRenderOption("UNFORMATTED_VALUE").
Do()
if err != nil {
util.Logger().Errorf("Unable to retrieve data from sheet: %v", err)
return nil, time.Time{}, err
}
if len(result.Values) == 0 {
return mapping, time.Time{}, nil
}
for _, row := range result.Values {
if len(row) < 2 {
continue
}
if len(row) > isActiveColumn {
isActive, ok := row[isActiveColumn].(bool)
if !ok {
rawIsActive, ok := row[isActiveColumn].(string)
if !ok {
continue
}
isActive, err = strconv.ParseBool(rawIsActive)
}
if !isActive || err != nil {
continue
}
}
key, ok := row[keyColumn].(string)
if !ok {
// Check if the key is a number instead
intKey, ok := row[keyColumn].(int)
if ok {
key = strconv.Itoa(intKey)
} else {
continue
}
}
value, ok := row[targetColumn].(string)
if !ok {
continue
}
if len(value) == 0 || len(key) == 0 {
continue
}
mapping[key] = value
}
return mapping, updateTime, nil
}
func (ds *GoogleSheetsDataSource) FetchRedirectMapping() (state.RedirectMap, error) {
mapping, updateTime, err := ds.fetchRedirectMappingInternal()
if err == nil {
ds.updateLastUpdate(updateTime)
}
return mapping, err
}