forked from vmware-archive/atc
/
team_factory.go
204 lines (163 loc) · 3.84 KB
/
team_factory.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
package dbng
import (
"database/sql"
"errors"
"fmt"
"strings"
"encoding/json"
sq "github.com/Masterminds/squirrel"
"github.com/concourse/atc"
"github.com/concourse/atc/db/lock"
)
//go:generate counterfeiter . TeamFactory
type TeamFactory interface {
CreateTeam(atc.Team) (Team, error)
FindTeam(string) (Team, bool, error)
GetTeams() ([]Team, error)
GetByID(teamID int) Team
}
var ErrDataIsEncrypted = errors.New("failed to decrypt data that is encrypted")
var ErrDataIsNotEncrypted = errors.New("failed to decrypt data that is not encrypted")
//go:generate counterfeiter . EncryptionStrategy
type EncryptionStrategy interface {
Encrypt([]byte) (string, *string, error)
Decrypt(string, *string) ([]byte, error)
}
type teamFactory struct {
conn Conn
lockFactory lock.LockFactory
}
func NewTeamFactory(conn Conn, lockFactory lock.LockFactory) TeamFactory {
return &teamFactory{
conn: conn,
lockFactory: lockFactory,
}
}
func (factory *teamFactory) CreateTeam(t atc.Team) (Team, error) {
tx, err := factory.conn.Begin()
if err != nil {
return nil, err
}
defer tx.Rollback()
encryptedBasicAuthJSON, err := encryptedJSON(t.BasicAuth)
if err != nil {
return nil, err
}
auth, err := json.Marshal(t.Auth)
if err != nil {
return nil, err
}
es := factory.conn.EncryptionStrategy()
encryptedAuth, nonce, err := es.Encrypt(auth)
if err != nil {
return nil, err
}
row := psql.Insert("teams").
Columns("name, basic_auth, auth, nonce").
Values(t.Name, encryptedBasicAuthJSON, encryptedAuth, nonce).
Suffix("RETURNING id, name, admin, basic_auth, auth, nonce").
RunWith(tx).
QueryRow()
team := &team{
conn: factory.conn,
lockFactory: factory.lockFactory,
}
err = factory.scanTeam(team, row)
if err != nil {
return nil, err
}
createTableString := fmt.Sprintf(`
CREATE TABLE team_build_events_%d ()
INHERITS (build_events);`, team.ID())
_, err = tx.Exec(createTableString)
if err != nil {
return nil, err
}
err = tx.Commit()
if err != nil {
return nil, err
}
return team, nil
}
func (factory *teamFactory) GetByID(teamID int) Team {
return &team{
id: teamID,
conn: factory.conn,
lockFactory: factory.lockFactory,
}
}
func (factory *teamFactory) FindTeam(teamName string) (Team, bool, error) {
team := &team{
conn: factory.conn,
lockFactory: factory.lockFactory,
}
row := psql.Select("id, name, admin, basic_auth, auth, nonce").
From("teams").
Where(sq.Eq{"LOWER(name)": strings.ToLower(teamName)}).
RunWith(factory.conn).
QueryRow()
err := factory.scanTeam(team, row)
if err != nil {
if err == sql.ErrNoRows {
return nil, false, nil
}
return nil, false, err
}
return team, true, nil
}
func (factory *teamFactory) GetTeams() ([]Team, error) {
rows, err := psql.Select("id, name, admin, basic_auth, auth, nonce").
From("teams").
RunWith(factory.conn).
Query()
if err != nil {
return nil, err
}
defer rows.Close()
teams := []Team{}
for rows.Next() {
team := &team{
conn: factory.conn,
lockFactory: factory.lockFactory,
}
err = factory.scanTeam(team, rows)
if err != nil {
return nil, err
}
teams = append(teams, team)
}
return teams, nil
}
func (factory *teamFactory) scanTeam(t *team, rows scannable) error {
var basicAuth, providerAuth, nonce sql.NullString
err := rows.Scan(
&t.id,
&t.name,
&t.admin,
&basicAuth,
&providerAuth,
&nonce,
)
if basicAuth.Valid {
err = json.Unmarshal([]byte(basicAuth.String), &t.basicAuth)
if err != nil {
return err
}
}
if providerAuth.Valid {
es := factory.conn.EncryptionStrategy()
var noncense *string
if nonce.Valid {
noncense = &nonce.String
}
pAuth, err := es.Decrypt(providerAuth.String, noncense)
if err != nil {
return err
}
err = json.Unmarshal(pAuth, &t.auth)
if err != nil {
return err
}
}
return err
}