-
Notifications
You must be signed in to change notification settings - Fork 38
/
gen_certs_migration.go
287 lines (249 loc) · 7.24 KB
/
gen_certs_migration.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
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"regexp"
"strings"
"text/template"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/transcom/mymove/pkg/cli"
)
const (
// FingerprintFlag is the Certificate Fingerprint flag
FingerprintFlag string = "fingerprint"
// SubjectFlag is the Certificate Subject flag
SubjectFlag string = "subject"
// UpdateFlag is the flag to enable Update mode
UpdateFlag string = "update"
// ClientCertIDFlag is the ID flag for Client Cert
ClientCertIDFlag string = "certid"
// template for adding client certificates
createCertsMigration string = `
-- This migration allows a CAC cert to have read/write access to all orders and the prime API.
-- The Orders API and the Prime API use client certificate authentication. Only certificates
-- signed by a trusted CA (such as DISA) are allowed which includes CACs.
-- Using a person's CAC as the certificate is a convenient way to permit a
-- single trusted individual to interact with the Orders API and the Prime API. Eventually
-- this CAC certificate should be removed.
INSERT INTO users (
id,
okta_email,
created_at,
updated_at)
VALUES (
'{{.UserID}}',
'{{.Fingerprint}}' || '@api.move.mil',
now(),
now());
INSERT INTO users_roles (
id,
role_id,
user_id,
created_at,
updated_at)
VALUES (
uuid_generate_v4(),
(SELECT id FROM roles WHERE role_type = 'prime'),
'{{.UserID}}',
now(),
now());
INSERT INTO public.client_certs (
id,
sha256_digest,
subject,
user_id,
allow_orders_api,
allow_prime,
created_at,
updated_at,
allow_air_force_orders_read,
allow_air_force_orders_write,
allow_army_orders_read,
allow_army_orders_write,
allow_coast_guard_orders_read,
allow_coast_guard_orders_write,
allow_marine_corps_orders_read,
allow_marine_corps_orders_write,
allow_navy_orders_read,
allow_navy_orders_write)
VALUES (
'{{.ID}}',
'{{.Fingerprint}}',
'{{.Subject}}',
'{{.UserID}}',
true,
true,
now(),
now(),
true,
true,
true,
true,
true,
true,
true,
true,
true,
true);
`
// template for updating client certificates
updateCertsMigration string = `
-- This migration allows a CAC cert to have read/write access to all orders and the prime API.
-- The Orders API and the Prime API use client certificate authentication. Only certificates
-- signed by a trusted CA (such as DISA) are allowed which includes CACs.
-- Using a person's CAC as the certificate is a convenient way to permit a
-- single trusted individual to interact with the Orders API and the Prime API. Eventually
-- this CAC certificate should be removed.
-- Updating sha256 for new cac
UPDATE client_certs
SET sha256_digest = '{{.Fingerprint}}',
subject = '{{.Subject}}',
updated_at = NOW()
WHERE id = '{{.ID}}';
`
)
// CertsTemplate is a struct that stores the context from which to generate the migration
type CertsTemplate struct {
ID string
Fingerprint string
Subject string
UserID string
}
// InitCertsMigrationFlags initializes certs migration command line flags
func InitCertsMigrationFlags(flag *pflag.FlagSet) {
flag.StringP(FingerprintFlag, "f", "", "Certificate fingerprint in SHA 256 form")
flag.StringP(SubjectFlag, "s", "", "Certificate subject")
flag.Bool(UpdateFlag, false, "Create an update migration")
flag.String(ClientCertIDFlag, "", "Previous ID of client cert entry")
}
// CheckCertsMigration validates command line flags
func CheckCertsMigration(v *viper.Viper) error {
if err := cli.CheckMigration(v); err != nil {
return err
}
if err := cli.CheckMigrationFile(v); err != nil {
return err
}
if err := cli.CheckCAC(v); err != nil {
return err
}
if !v.GetBool(cli.CACFlag) {
fingerprint := v.GetString(FingerprintFlag)
if len(fingerprint) == 0 {
return fmt.Errorf("%s is missing", FingerprintFlag)
}
sha256Pattern := "^[a-f0-9]{64}$"
_, err := regexp.MatchString(sha256Pattern, fingerprint)
if err != nil {
return errors.Errorf("Fingerprint must be a valid SHA 256 hash")
}
subject := v.GetString(SubjectFlag)
if len(subject) == 0 {
return errors.Errorf("%s is missing", SubjectFlag)
}
}
if v.GetBool(UpdateFlag) {
certID := v.GetString(ClientCertIDFlag)
if len(certID) == 0 {
return fmt.Errorf("%s required for generating UPDATE migration", ClientCertIDFlag)
}
uuid.Must(uuid.FromString(certID))
}
return nil
}
func initGenCertsMigrationFlags(flag *pflag.FlagSet) {
// Migration Config
cli.InitMigrationFlags(flag)
// Migration File Config
cli.InitMigrationFileFlags(flag)
// CAC Config
cli.InitCACFlags(flag)
// Init Certs Migration Flags
InitCertsMigrationFlags(flag)
// Don't sort command line flags
flag.SortFlags = false
}
func genCertsMigration(cmd *cobra.Command, args []string) error {
err := cmd.ParseFlags(args)
if err != nil {
return errors.Wrap(err, "could not ParseFlags on args")
}
flag := cmd.Flags()
err = flag.Parse(os.Args[1:])
if err != nil {
return errors.Wrap(err, "could not parse flags")
}
v := viper.New()
err = v.BindPFlags(flag)
if err != nil {
return errors.Wrap(err, "could not bind flags")
}
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
v.AutomaticEnv()
err = CheckCertsMigration(v)
if err != nil {
return err
}
migrationManifest := v.GetString(cli.MigrationManifestFlag)
migrationName := v.GetString(cli.MigrationNameFlag)
migrationVersion := v.GetString(cli.MigrationVersionFlag)
var fingerprint, subject string
if v.GetBool(cli.CACFlag) {
store, errStore := cli.GetCACStore(v)
if errStore != nil {
return fmt.Errorf("Ensure CAC reader and card inserted: %w", errStore)
}
defer func() {
if closeErr := store.Close(); closeErr != nil {
fmt.Println("Failed to close CAC store")
}
}()
cert, errTLSCert := store.TLSCertificate()
if errTLSCert != nil {
return errTLSCert
}
// Get the fingerprint
hash := sha256.Sum256(cert.Certificate[0])
fingerprint = hex.EncodeToString(hash[:])
// Get the subject in RFC2253 format
subject = cert.Leaf.Subject.String()
} else {
fingerprint = v.GetString(FingerprintFlag)
subject = v.GetString(SubjectFlag)
}
certsTemplate := CertsTemplate{
Fingerprint: fingerprint,
Subject: subject,
}
var t1 *template.Template
updateMode := v.GetBool(UpdateFlag)
if updateMode {
certsTemplate.ID = v.GetString(ClientCertIDFlag)
t1 = template.Must(template.New("certs_migration").Parse(updateCertsMigration))
} else {
certsTemplate.ID = uuid.Must(uuid.NewV4()).String()
certsTemplate.UserID = uuid.Must(uuid.NewV4()).String()
t1 = template.Must(template.New("certs_migration").Parse(createCertsMigration))
}
secureMigrationName := fmt.Sprintf("%s_%s.up.sql", migrationVersion, migrationName)
err = createMigration(tempMigrationPath, secureMigrationName, t1, certsTemplate)
if err != nil {
return err
}
t2 := template.Must(template.New("migrations/app/secure").Parse(localMigrationTemplate))
err = createMigration("./migrations/app/secure", secureMigrationName, t2, nil)
if err != nil {
return err
}
err = addMigrationToManifest(migrationManifest, secureMigrationName)
if err != nil {
return err
}
return nil
}