forked from grafeas/grafeas
/
pgsqlstore.go
573 lines (534 loc) · 18.8 KB
/
pgsqlstore.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// Copyright 2017 The Grafeas Authors. All rights reserved.
//
// 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 storage
import (
"database/sql"
"fmt"
"log"
"strconv"
"time"
"github.com/fernet/fernet-go"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/google/uuid"
"github.com/grafeas/grafeas/go/config"
"github.com/grafeas/grafeas/go/name"
pb "github.com/grafeas/grafeas/proto/v1beta1/grafeas_go_proto"
prpb "github.com/grafeas/grafeas/proto/v1beta1/project_go_proto"
"github.com/lib/pq"
"golang.org/x/net/context"
fieldmaskpb "google.golang.org/genproto/protobuf/field_mask"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type PgSQLStore struct {
*sql.DB
paginationKey string
}
func NewPgSQLStore(config *config.PgSQLConfig) *PgSQLStore {
err := createDatabase(CreateSourceString(config.User, config.Password, config.Host, "postgres", config.SSLMode), config.DbName)
if err != nil {
log.Fatal(err.Error())
}
db, err := sql.Open("postgres", CreateSourceString(config.User, config.Password, config.Host, config.DbName, config.SSLMode))
if err != nil {
log.Fatal(err.Error())
}
if db.Ping() != nil {
log.Fatal("Database server is not alive")
}
if _, err := db.Exec(createTables); err != nil {
db.Close()
log.Fatal(err.Error())
}
return &PgSQLStore{
DB: db,
paginationKey: config.PaginationKey,
}
}
func createDatabase(source, dbName string) error {
db, err := sql.Open("postgres", source)
if err != nil {
return err
}
defer db.Close()
// Check if db exists
res, err := db.Exec(
fmt.Sprintf("SELECT * FROM pg_catalog.pg_database WHERE datname='%s'", dbName))
if err != nil {
return err
}
rowCnt, err := res.RowsAffected()
if err != nil {
return err
}
// Create database if it doesn't exist
if rowCnt == 0 {
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
if err != nil {
return err
}
}
return nil
}
// CreateProject adds the specified project to the store
func (pg *PgSQLStore) CreateProject(ctx context.Context, pID string, p *prpb.Project) (*prpb.Project, error) {
_, err := pg.DB.Exec(insertProject, name.FormatProject(pID))
if err, ok := err.(*pq.Error); ok {
// Check for unique_violation
if err.Code == "23505" {
return nil, status.Errorf(codes.AlreadyExists, "Project with name %q already exists", pID)
} else {
log.Println("Failed to insert Project in database", err)
return nil, status.Error(codes.Internal, "Failed to insert Project in database")
}
}
return p, nil
}
// DeleteProject deletes the project with the given pID from the store
func (pg *PgSQLStore) DeleteProject(ctx context.Context, pID string) error {
pName := name.FormatProject(pID)
result, err := pg.DB.Exec(deleteProject, pName)
if err != nil {
return status.Error(codes.Internal, "Failed to delete Project from database")
}
count, err := result.RowsAffected()
if err != nil {
return status.Error(codes.Internal, "Failed to delete Project from database")
}
if count == 0 {
return status.Errorf(codes.NotFound, "Project with name %q does not Exist", pName)
}
return nil
}
// GetProject returns the project with the given pID from the store
func (pg *PgSQLStore) GetProject(ctx context.Context, pID string) (*prpb.Project, error) {
pName := name.FormatProject(pID)
var exists bool
err := pg.DB.QueryRow(projectExists, pName).Scan(&exists)
if err != nil {
return nil, status.Error(codes.Internal, "Failed to query Project from database")
}
if !exists {
return nil, status.Errorf(codes.NotFound, "Project with name %q does not Exist", pName)
}
return &prpb.Project{Name: pName}, nil
}
// ListProjects returns up to pageSize number of projects beginning at pageToken (or from
// start if pageToken is the empty string).
func (pg *PgSQLStore) ListProjects(ctx context.Context, filter string, pageSize int, pageToken string) ([]*prpb.Project, string, error) {
var rows *sql.Rows
id := decryptInt64(pageToken, pg.paginationKey, 0)
rows, err := pg.DB.Query(listProjects, id, pageSize)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to list Projects from database")
}
count, err := pg.count(projectCount)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to count Projects from database")
}
var projects []*prpb.Project
var lastId int64
for rows.Next() {
var name string
err := rows.Scan(&lastId, &name)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to scan Project row")
}
projects = append(projects, &prpb.Project{Name: name})
}
if count == lastId {
return projects, "", nil
}
encryptedPage, err := encryptInt64(lastId, pg.paginationKey)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to paginate projects")
}
return projects, encryptedPage, nil
}
// CreateOccurrence adds the specified occurrence
func (pg *PgSQLStore) CreateOccurrence(ctx context.Context, pID, uID string, o *pb.Occurrence) (*pb.Occurrence, error) {
o = proto.Clone(o).(*pb.Occurrence)
o.CreateTime = ptypes.TimestampNow()
var id string
if nr, err := uuid.NewRandom(); err != nil {
return nil, status.Error(codes.Internal, "Failed to generate UUID")
} else {
id = nr.String()
}
o.Name = fmt.Sprintf("projects/%s/occurrences/%s", pID, id)
nPID, nID, err := name.ParseNote(o.NoteName)
if err != nil {
log.Printf("Invalid note name: %v", o.NoteName)
return nil, status.Error(codes.InvalidArgument, "Invalid note name")
}
_, err = pg.DB.Exec(insertOccurrence, pID, id, nPID, nID, proto.MarshalTextString(o))
if err, ok := err.(*pq.Error); ok {
// Check for unique_violation
if err.Code == "23505" {
return nil, status.Errorf(codes.AlreadyExists, "Occurrence with name %q already exists", o.Name)
} else {
log.Println("Failed to insert Occurrence in database", err)
return nil, status.Error(codes.Internal, "Failed to insert Occurrence in database")
}
}
return o, nil
}
// BatchCreateOccurrence batch creates the specified occurrences in PostreSQL.
func (pg *PgSQLStore) BatchCreateOccurrences(ctx context.Context, pID string, uID string, occs []*pb.Occurrence) ([]*pb.Occurrence, []error) {
clonedOccs := []*pb.Occurrence{}
for _, o := range occs {
clonedOccs = append(clonedOccs, proto.Clone(o).(*pb.Occurrence))
}
occs = clonedOccs
errs := []error{}
created := []*pb.Occurrence{}
for _, o := range occs {
occ, err := pg.CreateOccurrence(ctx, pID, uID, o)
if err != nil {
// Occurrence already exists, skipping.
continue
} else {
created = append(created, occ)
}
}
return created, errs
}
// DeleteOccurrence deletes the occurrence with the given pID and oID
func (pg *PgSQLStore) DeleteOccurrence(ctx context.Context, pID, oID string) error {
result, err := pg.DB.Exec(deleteOccurrence, pID, oID)
if err != nil {
return status.Error(codes.Internal, "Failed to delete Occurrence from database")
}
count, err := result.RowsAffected()
if err != nil {
return status.Error(codes.Internal, "Failed to delete Occurrence from database")
}
if count == 0 {
return status.Errorf(codes.NotFound, "Occurrence with name %q/%q does not Exist", pID, oID)
}
return nil
}
// UpdateOccurrence updates the existing occurrence with the given projectID and occurrenceID
func (pg *PgSQLStore) UpdateOccurrence(ctx context.Context, pID, oID string, o *pb.Occurrence, mask *fieldmaskpb.FieldMask) (*pb.Occurrence, error) {
o = proto.Clone(o).(*pb.Occurrence)
// TODO(#312): implement the update operation
o.UpdateTime = ptypes.TimestampNow()
result, err := pg.DB.Exec(updateOccurrence, pID, oID, proto.MarshalTextString(o))
if err != nil {
return nil, status.Error(codes.Internal, "Failed to update Occurrence")
}
count, err := result.RowsAffected()
if err != nil {
return nil, status.Error(codes.Internal, "Failed to update Occurrence")
}
if count == 0 {
return nil, status.Errorf(codes.NotFound, "Occurrence with name %q/%q does not Exist", pID, oID)
}
return o, nil
}
// GetOccurrence returns the occurrence with pID and oID
func (pg *PgSQLStore) GetOccurrence(ctx context.Context, pID, oID string) (*pb.Occurrence, error) {
var data string
err := pg.DB.QueryRow(searchOccurrence, pID, oID).Scan(&data)
switch {
case err == sql.ErrNoRows:
return nil, status.Errorf(codes.NotFound, "Occurrence with name %q/%q does not Exist", pID, oID)
case err != nil:
return nil, status.Error(codes.Internal, "Failed to query Occurrence from database")
}
var o pb.Occurrence
proto.UnmarshalText(data, &o)
if err != nil {
return nil, status.Error(codes.Internal, "Failed to unmarshal Occurrence from database")
}
// Set the output-only field before returning
o.Name = name.FormatOccurrence(pID, oID)
return &o, nil
}
// ListOccurrences returns up to pageSize number of occurrences for this project beginning
// at pageToken, or from start if pageToken is the empty string.
func (pg *PgSQLStore) ListOccurrences(ctx context.Context, pID, filter, pageToken string, pageSize int32) ([]*pb.Occurrence, string, error) {
var rows *sql.Rows
id := decryptInt64(pageToken, pg.paginationKey, 0)
rows, err := pg.DB.Query(listOccurrences, pID, id, pageSize)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to list Occurrences from database")
}
count, err := pg.count(occurrenceCount, pID)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to count Occurrences from database")
}
var os []*pb.Occurrence
var lastId int64
for rows.Next() {
var data string
err := rows.Scan(&lastId, &data)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to scan Occurrences row")
}
var o pb.Occurrence
proto.UnmarshalText(data, &o)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to unmarshal Occurrence from database")
}
os = append(os, &o)
}
if count == lastId {
return os, "", nil
}
encryptedPage, err := encryptInt64(lastId, pg.paginationKey)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to paginate projects")
}
return os, encryptedPage, nil
}
// CreateNote adds the specified note
func (pg *PgSQLStore) CreateNote(ctx context.Context, pID, nID, uID string, n *pb.Note) (*pb.Note, error) {
n = proto.Clone(n).(*pb.Note)
nName := name.FormatNote(pID, nID)
n.Name = nName
n.CreateTime = ptypes.TimestampNow()
_, err := pg.DB.Exec(insertNote, pID, nID, proto.MarshalTextString(n))
if err, ok := err.(*pq.Error); ok {
// Check for unique_violation
if err.Code == "23505" {
return nil, status.Errorf(codes.AlreadyExists, "Note with name %q already exists", n.Name)
} else {
log.Println("Failed to insert Note in database", err)
return nil, status.Error(codes.Internal, "Failed to insert Note in database")
}
}
return n, nil
}
// BatchCreateNotes batch creates the specified notes in memstore.
func (pg *PgSQLStore) BatchCreateNotes(ctx context.Context, pID, uID string, notes map[string]*pb.Note) ([]*pb.Note, []error) {
clonedNotes := map[string]*pb.Note{}
for nID, n := range notes {
clonedNotes[nID] = proto.Clone(n).(*pb.Note)
}
notes = clonedNotes
errs := []error{}
created := []*pb.Note{}
for nID, n := range notes {
note, err := pg.CreateNote(ctx, pID, nID, uID, n)
if err != nil {
// Note already exists, skipping.
continue
} else {
created = append(created, note)
}
}
return created, errs
}
// DeleteNote deletes the note with the given pID and nID
func (pg *PgSQLStore) DeleteNote(ctx context.Context, pID, nID string) error {
result, err := pg.DB.Exec(deleteNote, pID, nID)
if err != nil {
return status.Error(codes.Internal, "Failed to delete Note from database")
}
count, err := result.RowsAffected()
if err != nil {
return status.Error(codes.Internal, "Failed to delete Note from database")
}
if count == 0 {
return status.Errorf(codes.NotFound, "Note with name %q/%q does not Exist", pID, nID)
}
return nil
}
// UpdateNote updates the existing note with the given pID and nID
func (pg *PgSQLStore) UpdateNote(ctx context.Context, pID, nID string, n *pb.Note, mask *fieldmaskpb.FieldMask) (*pb.Note, error) {
n = proto.Clone(n).(*pb.Note)
nName := name.FormatNote(pID, nID)
n.Name = nName
// TODO(#312): implement the update operation
n.UpdateTime = ptypes.TimestampNow()
result, err := pg.DB.Exec(updateNote, pID, nID, proto.MarshalTextString(n))
if err != nil {
return nil, status.Error(codes.Internal, "Failed to update Note")
}
count, err := result.RowsAffected()
if err != nil {
return nil, status.Error(codes.Internal, "Failed to update Note")
}
if count == 0 {
return nil, status.Errorf(codes.NotFound, "Note with name %q/%q does not Exist", pID, nID)
}
return n, nil
}
// GetNote returns the note with project (pID) and note ID (nID)
func (pg *PgSQLStore) GetNote(ctx context.Context, pID, nID string) (*pb.Note, error) {
var data string
err := pg.DB.QueryRow(searchNote, pID, nID).Scan(&data)
switch {
case err == sql.ErrNoRows:
return nil, status.Errorf(codes.NotFound, "Note with name %q/%q does not Exist", pID, nID)
case err != nil:
return nil, status.Error(codes.Internal, "Failed to query Note from database")
}
var note pb.Note
proto.UnmarshalText(data, ¬e)
if err != nil {
return nil, status.Error(codes.Internal, "Failed to unmarshal Note from database")
}
// Set the output-only field before returning
note.Name = name.FormatNote(pID, nID)
return ¬e, nil
}
// GetOccurrenceNote gets the note for the specified occurrence from PostgreSQL.
func (pg *PgSQLStore) GetOccurrenceNote(ctx context.Context, pID, oID string) (*pb.Note, error) {
o, err := pg.GetOccurrence(ctx, pID, oID)
if err != nil {
return nil, err
}
nPID, nID, err := name.ParseNote(o.NoteName)
if err != nil {
log.Printf("Error parsing name: %v", o.NoteName)
return nil, status.Error(codes.InvalidArgument, "Invalid Note name")
}
n, err := pg.GetNote(ctx, nPID, nID)
if err != nil {
return nil, err
}
// Set the output-only field before returning
n.Name = name.FormatNote(nPID, nID)
return n, nil
}
// ListNotes returns up to pageSize number of notes for this project (pID) beginning
// at pageToken (or from start if pageToken is the empty string).
func (pg *PgSQLStore) ListNotes(ctx context.Context, pID, filter, pageToken string, pageSize int32) ([]*pb.Note, string, error) {
var rows *sql.Rows
id := decryptInt64(pageToken, pg.paginationKey, 0)
rows, err := pg.DB.Query(listNotes, pID, id, pageSize)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to list Notes from database")
}
count, err := pg.count(noteCount, pID)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to count Notes from database")
}
var ns []*pb.Note
var lastId int64
for rows.Next() {
var data string
err := rows.Scan(&lastId, &data)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to scan Notes row")
}
var n pb.Note
proto.UnmarshalText(data, &n)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to unmarshal Note from database")
}
ns = append(ns, &n)
}
if count == lastId {
return ns, "", nil
}
encryptedPage, err := encryptInt64(lastId, pg.paginationKey)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to paginate projects")
}
return ns, encryptedPage, nil
}
// ListNoteOccurrences returns up to pageSize number of occcurrences on the particular note (nID)
// for this project (pID) projects beginning at pageToken (or from start if pageToken is the empty string).
func (pg *PgSQLStore) ListNoteOccurrences(ctx context.Context, pID, nID, filter, pageToken string, pageSize int32) ([]*pb.Occurrence, string, error) {
// Verify that note exists
if _, err := pg.GetNote(ctx, pID, nID); err != nil {
return nil, "", err
}
var rows *sql.Rows
id := decryptInt64(pageToken, pg.paginationKey, 0)
rows, err := pg.DB.Query(listNoteOccurrences, pID, nID, id, pageSize)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to list Occurrences from database")
}
count, err := pg.count(noteOccurrencesCount, pID, nID)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to count Occurrences from database")
}
var os []*pb.Occurrence
var lastId int64
for rows.Next() {
var data string
err := rows.Scan(&lastId, &data)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to scan Occurrences row")
}
var o pb.Occurrence
proto.UnmarshalText(data, &o)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to unmarshal Occurrence from database")
}
os = append(os, &o)
}
if count == lastId {
return os, "", nil
}
encryptedPage, err := encryptInt64(lastId, pg.paginationKey)
if err != nil {
return nil, "", status.Error(codes.Internal, "Failed to paginate projects")
}
return os, encryptedPage, nil
}
// GetVulnerabilityOccurrencesSummary gets a summary of vulnerability occurrences from storage.
func (pg *PgSQLStore) GetVulnerabilityOccurrencesSummary(ctx context.Context, projectID, filter string) (*pb.VulnerabilityOccurrencesSummary, error) {
return &pb.VulnerabilityOccurrencesSummary{}, nil
}
// CreateSourceString generates DB source path.
func CreateSourceString(user, password, host, dbName, SSLMode string) string {
if user == "" {
return fmt.Sprintf("postgres://%s/%s?sslmode=%s", host, dbName, SSLMode)
}
return fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s", user, password, host, dbName, SSLMode)
}
// count returns the total number of entries for the specified query (assuming SELECT(*) is used)
func (pg *PgSQLStore) count(query string, args ...interface{}) (int64, error) {
row := pg.DB.QueryRow(query, args...)
var count int64
err := row.Scan(&count)
if err != nil {
return 0, err
}
return count, err
}
// Encrypt int64 using provided key
func encryptInt64(v int64, key string) (string, error) {
k, err := fernet.DecodeKey(key)
if err != nil {
return "", err
}
bytes, err := fernet.EncryptAndSign([]byte(strconv.FormatInt(v, 10)), k)
if err != nil {
return "", err
}
return string(bytes), nil
}
// Decrypts encrypted int64 using provided key. Returns defaultValue if decryption fails.
func decryptInt64(encrypted string, key string, defaultValue int64) int64 {
k, err := fernet.DecodeKey(key)
if err != nil {
return defaultValue
}
bytes := fernet.VerifyAndDecrypt([]byte(encrypted), time.Hour, []*fernet.Key{k})
if bytes == nil {
return defaultValue
}
decryptedValue, err := strconv.ParseInt(string(bytes), 10, 64)
if err != nil {
return defaultValue
}
return decryptedValue
}