-
Notifications
You must be signed in to change notification settings - Fork 393
/
testdata.go
138 lines (120 loc) · 3.55 KB
/
testdata.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
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/base64"
"errors"
"github.com/zeebo/errs"
"golang.org/x/crypto/bcrypt"
"storj.io/common/macaroon"
"storj.io/common/memory"
"storj.io/common/uuid"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/console/consolewasm"
)
const (
fullMigration = "full"
snapshotMigration = "snapshot"
testDataCreation = "testdata"
noMigration = "none"
)
var migrationTypes = []string{fullMigration, snapshotMigration, testDataCreation, noMigration}
var (
projectID = uuid.UUID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})
apiKeyID = uuid.UUID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2})
head = []byte{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
}
secret = []byte{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
}
password = "password"
)
// createTestData creates predefined test account to make the integration tests easier.
func createTestData(ctx context.Context, db satellite.DB) error {
userID, err := uuid.FromString("be041c3c-0658-40d1-8f7c-e70a0a26cc12")
if err != nil {
return err
}
_, err = db.Console().Users().Get(ctx, userID)
if errors.Is(err, sql.ErrNoRows) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), 0)
if err != nil {
return err
}
_, err = db.Console().Users().Insert(ctx, &console.User{
ID: userID,
FullName: "Hiro Protagonist",
Email: "test@storj.io",
ProjectLimit: 5,
ProjectStorageLimit: (memory.GB * 150).Int64(),
ProjectBandwidthLimit: (memory.GB * 150).Int64(),
PasswordHash: hash,
})
if err != nil {
return err
}
active := console.Active
err = db.Console().Users().Update(ctx, userID, console.UpdateUserRequest{
Status: &active,
})
if err != nil {
return err
}
} else if err != nil {
return err
}
_, err = db.Console().Projects().Get(ctx, projectID)
if errors.Is(err, sql.ErrNoRows) {
_, err := db.Console().Projects().Insert(ctx, &console.Project{
ID: projectID,
OwnerID: userID,
Name: "testproject",
})
if err != nil {
return err
}
_, err = db.Console().ProjectMembers().Insert(ctx, userID, projectID)
if err != nil {
return err
}
} else if err != nil {
return err
}
_, err = db.Console().APIKeys().GetByNameAndProjectID(ctx, "testkey", projectID)
if errors.Is(err, sql.ErrNoRows) {
_, err = db.Console().APIKeys().Create(ctx, head, console.APIKeyInfo{
ID: apiKeyID,
ProjectID: projectID,
Name: "testkey",
Secret: secret,
})
} else if err != nil {
return err
}
return err
}
// GetTestApiKey can calculate an access grant for the predefined test users/project.
func GetTestApiKey(satelliteId string) (string, error) {
key, err := macaroon.FromParts(head, secret)
if err != nil {
return "", errs.Wrap(err)
}
idHash := sha256.Sum256(projectID[:])
base64Salt := base64.StdEncoding.EncodeToString(idHash[:])
accessGrant, err := consolewasm.GenAccessGrant(satelliteId, key.Serialize(), password, base64Salt)
if err != nil {
return "", errs.Wrap(err)
}
return accessGrant, nil
}