-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathsign_certd.go
777 lines (701 loc) · 24.4 KB
/
sign_certd.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
package main
import (
"bytes"
"crypto/rand"
"encoding/base32"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/cloudtools/ssh-cert-authority/client"
"github.com/cloudtools/ssh-cert-authority/util"
"github.com/cloudtools/ssh-cert-authority/version"
"github.com/codegangsta/cli"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"strings"
"sync"
"time"
)
// Yanked from PROTOCOL.certkeys
var supportedCriticalOptions = []string{
"force-command",
"source-address",
}
func isSupportedOption(x string) bool {
for optionIdx := range supportedCriticalOptions {
if supportedCriticalOptions[optionIdx] == x {
return true
}
}
return false
}
func areCriticalOptionsValid(criticalOptions map[string]string) error {
for optionName := range criticalOptions {
if !isSupportedOption(optionName) {
return fmt.Errorf("Invalid critical option name: '%s'", optionName)
}
}
return nil
}
type certRequest struct {
// This struct tracks state for certificate requests. Imagine this one day
// being stored in a persistent data store.
request *ssh.Certificate
submitTime time.Time
environment string
signatures map[string]bool
certSigned bool
certRejected bool
reason string
}
func compareCerts(one, two *ssh.Certificate) bool {
/* Compare two SSH certificates in a special way.
The specialness is in that we expect these certs to be more or less the
same but they will have been signed by different people. The act of signing
the cert changes the Key, SignatureKey, Signature and Nonce fields of the
Certificate struct so we compare the cert except for those fields.
*/
if one.Serial != two.Serial {
return false
}
if one.CertType != two.CertType {
return false
}
if one.KeyId != two.KeyId {
return false
}
if !reflect.DeepEqual(one.ValidPrincipals, two.ValidPrincipals) {
return false
}
if one.ValidAfter != two.ValidAfter {
return false
}
if one.ValidBefore != two.ValidBefore {
return false
}
if !reflect.DeepEqual(one.CriticalOptions, two.CriticalOptions) {
return false
}
if !reflect.DeepEqual(one.Extensions, two.Extensions) {
return false
}
if !bytes.Equal(one.Reserved, two.Reserved) {
return false
}
if !reflect.DeepEqual(one.Key, two.Key) {
return false
}
return true
}
func newcertRequest() certRequest {
var cr certRequest
cr.submitTime = time.Now()
cr.certSigned = false
cr.signatures = make(map[string]bool)
return cr
}
type certRequestHandler struct {
Config map[string]ssh_ca_util.SignerdConfig
state map[string]certRequest
sshAgentConn io.ReadWriter
stateMutex sync.RWMutex
}
type signingRequest struct {
config *ssh_ca_util.SignerdConfig
environment string
cert *ssh.Certificate
}
func (h *certRequestHandler) setupPrivateKeys(config map[string]ssh_ca_util.SignerdConfig) error {
for env, cfg := range config {
if cfg.PrivateKeyFile == "" {
continue
}
keyUrl, err := url.Parse(cfg.PrivateKeyFile)
if err != nil {
log.Printf("Ignoring invalid private key file: '%s'. Error parsing: %s", cfg.PrivateKeyFile, err)
continue
}
if keyUrl.Scheme == "gcpkms" {
cfg = config[env]
cfg.SigningKeyFingerprint = cfg.PrivateKeyFile
config[env] = cfg
} else if keyUrl.Scheme == "" || keyUrl.Scheme == "file" {
keyContents, err := ioutil.ReadFile(keyUrl.Path)
if err != nil {
return fmt.Errorf("Failed reading private key file %s: %v", keyUrl.Path, err)
}
if strings.HasSuffix(keyUrl.Path, ".kms") {
var region string
if cfg.KmsRegion != "" {
region = cfg.KmsRegion
} else {
region, err = ec2metadata.New(session.New(), aws.NewConfig()).Region()
if err != nil {
return fmt.Errorf("Unable to determine our region: %s", err)
}
}
svc := kms.New(session.New(), aws.NewConfig().WithRegion(region))
params := &kms.DecryptInput{
CiphertextBlob: keyContents,
}
resp, err := svc.Decrypt(params)
if err != nil {
// We try only one time to speak with KMS. If this pukes, and it
// will occasionally because "the cloud", the caller is responsible
// for trying again, possibly after a crash/restart.
return fmt.Errorf("Unable to decrypt CA key: %v\n", err)
}
keyContents = resp.Plaintext
}
key, err := ssh.ParseRawPrivateKey(keyContents)
if err != nil {
return fmt.Errorf("Failed parsing private key %s: %v", keyUrl.Path, err)
}
keyToAdd := agent.AddedKey{
PrivateKey: key,
Comment: fmt.Sprintf("ssh-cert-authority-%s-%s", env, keyUrl.Path),
LifetimeSecs: 0,
}
agentClient := agent.NewClient(h.sshAgentConn)
err = agentClient.Add(keyToAdd)
if err != nil {
return fmt.Errorf("Unable to add private key %s: %v", keyUrl.Path, err)
}
signer, err := ssh.NewSignerFromKey(key)
if err != nil {
return fmt.Errorf("Unable to create signer from pk %s: %v", keyUrl.Path, err)
}
keyFp := ssh_ca_util.MakeFingerprint(signer.PublicKey().Marshal())
log.Printf("Added private key for env %s: %s", env, keyFp)
cfg = config[env]
cfg.SigningKeyFingerprint = keyFp
config[env] = cfg
}
}
return nil
}
func (h *certRequestHandler) createSigningRequest(rw http.ResponseWriter, req *http.Request) {
err := req.ParseForm()
if err != nil {
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
cert, err := h.extractCertFromRequest(req)
if err != nil {
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
environment, ok := cert.Extensions["[email protected]"]
if !ok || environment == "" {
http.Error(rw, "You forgot to send in the environment", http.StatusBadRequest)
return
}
reason, ok := cert.Extensions["[email protected]"]
if !ok || reason == "" {
http.Error(rw, "You forgot to send in a reason", http.StatusBadRequest)
return
}
config, ok := h.Config[environment]
if !ok {
http.Error(rw, "Unknown environment.", http.StatusBadRequest)
return
}
err = h.validateCert(cert, config.AuthorizedUsers)
if err != nil {
log.Printf("Invalid certificate signing request received from %s, ignoring", req.RemoteAddr)
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
// Ideally we put the critical options into the cert and let validateCert
// do the validation. However, this also checks the signature on the cert
// which would fail if we modified it prior to validation. So we validate
// by hand.
if len(config.CriticalOptions) > 0 {
for optionName, optionVal := range config.CriticalOptions {
cert.CriticalOptions[optionName] = optionVal
}
}
requestID := make([]byte, 8)
rand.Reader.Read(requestID)
requestIDStr := base32.StdEncoding.EncodeToString(requestID)
requestIDStr = strings.Replace(requestIDStr, "=", "", 10)
// the serial number is the same as the request id, just encoded differently.
var nextSerial uint64
nextSerial = 0
for _, byteVal := range requestID {
nextSerial <<= 8
nextSerial |= uint64(byteVal)
}
requesterFp := ssh_ca_util.MakeFingerprint(cert.SignatureKey.Marshal())
signed, err := h.saveSigningRequest(config, environment, reason, requestIDStr, nextSerial, cert)
if err != nil {
http.Error(rw, fmt.Sprintf("Request not made: %v", err), http.StatusBadRequest)
return
}
// Serial and id are the same value, just encoded differently. Logging them
// both because they didn't use to be the same value and folks may be
// parsing these log messages and I don't want to break the format.
log.Printf("Cert request serial %d id %s env %s from %s (%s) @ %s principals %v valid from %d to %d for '%s'\n",
cert.Serial, requestIDStr, environment, requesterFp, config.AuthorizedUsers[requesterFp],
req.RemoteAddr, cert.ValidPrincipals, cert.ValidAfter, cert.ValidBefore, reason)
if config.SlackUrl != "" {
slackMsg := fmt.Sprintf("SSH cert request from %s with id %s for %s", config.AuthorizedUsers[requesterFp], requestIDStr, reason)
err = ssh_ca_client.PostToSlack(config.SlackUrl, config.SlackChannel, slackMsg)
if err != nil {
log.Printf("Unable to post to slack: %v", err)
}
}
var returnStatus int
if signed {
slackMsg := fmt.Sprintf("SSH cert request %s auto signed.", requestIDStr)
err := ssh_ca_client.PostToSlack(config.SlackUrl, config.SlackChannel, slackMsg)
if err != nil {
log.Printf("Unable to post to slack for %s: %v", requestIDStr, err)
}
returnStatus = http.StatusAccepted
} else {
returnStatus = http.StatusCreated
}
rw.WriteHeader(returnStatus)
rw.Write([]byte(requestIDStr))
return
}
func (h *certRequestHandler) saveSigningRequest(config ssh_ca_util.SignerdConfig, environment, reason, requestIDStr string, requestSerial uint64, cert *ssh.Certificate) (bool, error) {
requesterFp := ssh_ca_util.MakeFingerprint(cert.SignatureKey.Marshal())
maxValidBefore := uint64(time.Now().Add(time.Duration(config.MaxCertLifetime) * time.Second).Unix())
if config.MaxCertLifetime != 0 && cert.ValidBefore > maxValidBefore {
return false, fmt.Errorf("Certificate is valid longer than maximum permitted by configuration %d > %d",
cert.ValidBefore, maxValidBefore)
}
// We override keyid here so that its a server controlled value. Instead of
// letting a requester attempt to spoof it.
var ok bool
cert.KeyId, ok = config.AuthorizedUsers[requesterFp]
if !ok {
return false, fmt.Errorf("Requester fingerprint (%s) not found in config", requesterFp)
}
if requestSerial == 0 {
return false, fmt.Errorf("Serial number not set.")
}
cert.Serial = requestSerial
certRequest := newcertRequest()
certRequest.request = cert
if environment == "" {
return false, fmt.Errorf("Environment is a required field")
}
certRequest.environment = environment
if reason == "" {
return false, fmt.Errorf("Reason is a required field")
}
certRequest.reason = reason
if len(requestIDStr) < 12 {
return false, fmt.Errorf("Request id is too short to be useful.")
}
h.stateMutex.RLock()
_, ok = h.state[requestIDStr]
h.stateMutex.RUnlock()
if ok {
return false, fmt.Errorf("Request id '%s' already in use.", requestIDStr)
}
h.stateMutex.Lock()
h.state[requestIDStr] = certRequest
h.stateMutex.Unlock()
// This is the special case of supporting auto-signing.
if config.NumberSignersRequired < 0 {
signed, err := h.maybeSignWithCa(requestIDStr, config.NumberSignersRequired, config.SigningKeyFingerprint)
if signed && err == nil {
return true, nil
}
}
return false, nil
}
func (h *certRequestHandler) extractCertFromRequest(req *http.Request) (*ssh.Certificate, error) {
if req.Form["cert"] == nil || len(req.Form["cert"]) == 0 {
err := errors.New("Please specify exactly one cert request")
return nil, err
}
rawCertRequest, err := base64.StdEncoding.DecodeString(req.Form["cert"][0])
if err != nil {
err := errors.New("Unable to base64 decode cert request")
return nil, err
}
pubKey, err := ssh.ParsePublicKey(rawCertRequest)
if err != nil {
err := errors.New("Unable to parse cert request")
return nil, err
}
return pubKey.(*ssh.Certificate), nil
}
func (h *certRequestHandler) validateCert(cert *ssh.Certificate, authorizedSigners map[string]string) error {
var certChecker ssh.CertChecker
certChecker.IsUserAuthority = func(auth ssh.PublicKey) bool {
fingerprint := ssh_ca_util.MakeFingerprint(auth.Marshal())
_, ok := authorizedSigners[fingerprint]
return ok
}
certChecker.SupportedCriticalOptions = supportedCriticalOptions
err := certChecker.CheckCert(cert.ValidPrincipals[0], cert)
if err != nil {
err := fmt.Errorf("Cert not valid: %v", err)
return err
}
if cert.CertType != ssh.UserCert {
err = errors.New("Cert not valid: not a user certificate")
return err
}
// explicitly call IsUserAuthority
if !certChecker.IsUserAuthority(cert.SignatureKey) {
err = errors.New("Cert not valid: not signed by an authorized key")
return err
}
return nil
}
type listResponseElement struct {
Signed bool
Rejected bool
CertBlob string
NumSignatures int
SignaturesRequired int
Serial uint64
Environment string
Reason string
Cert *ssh.Certificate `json:"-"`
}
type certRequestResponse map[string]listResponseElement
func newResponseElement(cert *ssh.Certificate, certBlob string, signed bool, rejected bool, numSignatures, signaturesRequired int, serial uint64, reason string, environment string) listResponseElement {
var element listResponseElement
element.Cert = cert
element.CertBlob = certBlob
element.Signed = signed
element.Rejected = rejected
element.NumSignatures = numSignatures
element.SignaturesRequired = signaturesRequired
element.Serial = serial
element.Reason = reason
element.Environment = environment
return element
}
func (h *certRequestHandler) listEnvironments(rw http.ResponseWriter, req *http.Request) {
var environments []string
for k := range h.Config {
environments = append(environments, k)
}
result, err := json.Marshal(environments)
if err != nil {
http.Error(rw, fmt.Sprintf("Unable to marshal environment names: %v", err), http.StatusInternalServerError)
return
}
log.Printf("List environments received from '%s'\n", req.RemoteAddr)
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
rw.Write(result)
}
func (h *certRequestHandler) listPendingRequests(rw http.ResponseWriter, req *http.Request) {
var certRequestID string
err := req.ParseForm()
if err != nil {
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
certRequestIDs, ok := req.Form["certRequestId"]
if ok {
certRequestID = certRequestIDs[0]
}
matched, _ := regexp.MatchString("^[A-Z2-7=]{10,16}$", certRequestID)
if certRequestID != "" && !matched {
http.Error(rw, "Invalid certRequestId", http.StatusBadRequest)
return
}
log.Printf("List pending requests received from %s for request id '%s'\n",
req.RemoteAddr, certRequestID)
foundSomething := false
results := make(certRequestResponse)
h.stateMutex.RLock()
defer h.stateMutex.RUnlock()
for k, v := range h.state {
encodedCert := base64.StdEncoding.EncodeToString(v.request.Marshal())
element := newResponseElement(v.request, encodedCert, v.certSigned, v.certRejected, len(v.signatures), h.Config[v.environment].NumberSignersRequired, v.request.Serial, v.reason, v.environment)
// Two ways to use this URL. If caller specified a certRequestId
// then we return only that one. Otherwise everything.
if certRequestID == "" {
results[k] = element
foundSomething = true
} else {
if certRequestID == k {
results[k] = element
foundSomething = true
break
}
}
}
if foundSomething {
output, err := json.Marshal(results)
if err != nil {
http.Error(rw, fmt.Sprintf("Trouble marshaling json response %v", err), http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
rw.Write(output)
} else {
http.Error(rw, fmt.Sprintf("No certs found."), http.StatusNotFound)
return
}
}
func (h *certRequestHandler) getRequestStatus(rw http.ResponseWriter, req *http.Request) {
uriVars := mux.Vars(req)
requestID := uriVars["requestID"]
type Response struct {
certSigned bool
certRejected bool
cert string
}
h.stateMutex.RLock()
defer h.stateMutex.RUnlock()
if h.state[requestID].certSigned {
rw.Write([]byte(h.state[requestID].request.Type()))
rw.Write([]byte(" "))
rw.Write([]byte(base64.StdEncoding.EncodeToString(h.state[requestID].request.Marshal())))
rw.Write([]byte("\n"))
} else if h.state[requestID].certRejected {
http.Error(rw, "Cert request was rejected.", http.StatusPreconditionFailed)
} else {
http.Error(rw, "Cert not signed yet.", http.StatusPreconditionFailed)
}
}
func (h *certRequestHandler) signOrRejectRequest(rw http.ResponseWriter, req *http.Request) {
requestID := mux.Vars(req)["requestID"]
h.stateMutex.RLock()
originalRequest, ok := h.state[requestID]
h.stateMutex.RUnlock()
if !ok {
http.Error(rw, "Unknown request id", http.StatusNotFound)
return
}
if originalRequest.certSigned {
http.Error(rw, "Request already signed.", http.StatusConflict)
return
}
if originalRequest.certRejected {
http.Error(rw, "Request already rejected.", http.StatusConflict)
return
}
err := req.ParseForm()
if err != nil {
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
envConfig, ok := h.Config[originalRequest.environment]
if !ok {
http.Error(rw, "Original request found to have an invalid env. Weird.", http.StatusBadRequest)
return
}
signedCert, err := h.extractCertFromRequest(req)
if err != nil {
log.Printf("Unable to extract certificate signing request from %s, ignoring", req.RemoteAddr)
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
err = h.validateCert(signedCert, envConfig.AuthorizedSigners)
if err != nil {
log.Printf("Invalid certificate signing request received from %s, ignoring", req.RemoteAddr)
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
signerFp := ssh_ca_util.MakeFingerprint(signedCert.SignatureKey.Marshal())
// Verifying that the cert being posted to us here matches the one in the
// request. That is, that an attacker isn't using an old signature to sign a
// new/different request id
h.stateMutex.RLock()
requestedCert := h.state[requestID].request
h.stateMutex.RUnlock()
if !compareCerts(requestedCert, signedCert) {
log.Printf("Signature was valid, but cert didn't match from %s.", req.RemoteAddr)
log.Printf("Orig req: %#v\n", requestedCert)
log.Printf("Sign req: %#v\n", signedCert)
http.Error(rw, "Signature was valid, but cert didn't match.", http.StatusBadRequest)
return
}
requesterFp := ssh_ca_util.MakeFingerprint(requestedCert.Key.Marshal())
// Make sure the key attempting to sign the request is not the same as the key in the CSR
if signerFp == requesterFp {
err = errors.New("Signed by the same key as key in request")
http.Error(rw, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
log.Printf("Signature for serial %d id %s received from %s (%s) @ %s and determined valid\n",
signedCert.Serial, requestID, signerFp, envConfig.AuthorizedSigners[signerFp], req.RemoteAddr)
if req.Method == "POST" {
err = h.addConfirmation(requestID, signerFp, envConfig)
} else {
err = h.rejectRequest(requestID, signerFp, envConfig)
}
if err != nil {
http.Error(rw, fmt.Sprintf("%v", err), http.StatusNotFound)
}
}
func (h *certRequestHandler) rejectRequest(requestID string, signerFp string, envConfig ssh_ca_util.SignerdConfig) error {
log.Printf("Reject received for id %s", requestID)
h.stateMutex.Lock()
defer h.stateMutex.Unlock()
stateInfo := h.state[requestID]
stateInfo.certRejected = true
// this is weird. see: https://code.google.com/p/go/issues/detail?id=3117
h.state[requestID] = stateInfo
return nil
}
func (h *certRequestHandler) addConfirmation(requestID string, signerFp string, envConfig ssh_ca_util.SignerdConfig) error {
h.stateMutex.RLock()
certRejected := h.state[requestID].certRejected
h.stateMutex.RUnlock()
if certRejected {
return fmt.Errorf("Attempt to sign a rejected cert.")
}
h.stateMutex.Lock()
h.state[requestID].signatures[signerFp] = true
h.stateMutex.Unlock()
if envConfig.SlackUrl != "" {
slackMsg := fmt.Sprintf("SSH cert %s signed by %s making %d/%d signatures.",
requestID, envConfig.AuthorizedSigners[signerFp],
len(h.state[requestID].signatures), envConfig.NumberSignersRequired)
err := ssh_ca_client.PostToSlack(envConfig.SlackUrl, envConfig.SlackChannel, slackMsg)
if err != nil {
log.Printf("Unable to post to slack for %s: %v", requestID, err)
}
}
signed, err := h.maybeSignWithCa(requestID, envConfig.NumberSignersRequired, envConfig.SigningKeyFingerprint)
if signed && err == nil {
slackMsg := fmt.Sprintf("SSH cert request %s fully signed.", requestID)
err := ssh_ca_client.PostToSlack(envConfig.SlackUrl, envConfig.SlackChannel, slackMsg)
if err != nil {
log.Printf("Unable to post to slack for %s: %v", requestID, err)
}
}
return err
}
func (h *certRequestHandler) maybeSignWithCa(requestID string, numSignersRequired int, signingKeyFingerprint string) (bool, error) {
h.stateMutex.Lock()
defer h.stateMutex.Unlock()
if len(h.state[requestID].signatures) >= numSignersRequired {
if h.sshAgentConn == nil {
// This is used for testing. We're effectively disabling working
// with the ssh agent to avoid needing to mock it.
log.Print("ssh agent uninitialized, will not attempt signing. This is normal in unittests")
return true, nil
}
log.Printf("Received %d signatures for %s, signing now.\n", len(h.state[requestID].signatures), requestID)
signer, err := ssh_ca_util.GetSignerForFingerprintOrUrl(signingKeyFingerprint, h.sshAgentConn)
if err != nil {
log.Printf("Couldn't find signing key for request %s, unable to sign request: %s\n", requestID, err)
return false, fmt.Errorf("Couldn't find signing key, unable to sign. Sorry.")
}
stateInfo := h.state[requestID]
for extensionName := range stateInfo.request.Extensions {
// sshd up to version 6.8 has a bug where optional extensions are
// treated as critical. If a cert contains any non-standard
// extensions, like ours, the server rejects the cert because it
// doesn't understand the extension. To cope with this we simply
// strip our non-standard extensions before doing the final
// signature. https://bugzilla.mindrot.org/show_bug.cgi?id=2387
if strings.Contains(extensionName, "@") {
delete(stateInfo.request.Extensions, extensionName)
}
}
stateInfo.request.SignCert(rand.Reader, signer)
stateInfo.certSigned = true
// this is weird. see: https://code.google.com/p/go/issues/detail?id=3117
h.state[requestID] = stateInfo
return true, nil
}
return false, nil
}
func signdFlags() []cli.Flag {
home := os.Getenv("HOME")
if home == "" {
home = "/"
}
configPath := home + "/.ssh_ca/sign_certd_config.json"
return []cli.Flag{
cli.StringFlag{
Name: "config-file",
Value: configPath,
Usage: "Path to config.json",
},
cli.StringFlag{
Name: "listen-address",
Value: "127.0.0.1:8080",
Usage: "HTTP service address",
},
cli.BoolFlag{
Name: "reverse-proxy",
Usage: "Set when service is behind a reverse proxy, like nginx",
EnvVar: "SSH_CERT_AUTHORITY_PROXY",
},
}
}
func signCertd(c *cli.Context) error {
configPath := c.String("config-file")
config := make(map[string]ssh_ca_util.SignerdConfig)
err := ssh_ca_util.LoadConfig(configPath, &config)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Load Config failed: %s", err), 1)
}
for envName, configObj := range config {
err = areCriticalOptionsValid(configObj.CriticalOptions)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Error validation config for env '%s': %s", envName, err), 1)
}
}
err = runSignCertd(config, c.String("listen-address"), c.Bool("reverse-proxy"))
return err
}
func makeCertRequestHandler(config map[string]ssh_ca_util.SignerdConfig) certRequestHandler {
var requestHandler certRequestHandler
requestHandler.Config = config
requestHandler.state = make(map[string]certRequest)
return requestHandler
}
func runSignCertd(config map[string]ssh_ca_util.SignerdConfig, addr string, is_proxied bool) error {
log.Println("Server running version", version.BuildVersion)
log.Println("Using SSH agent at", os.Getenv("SSH_AUTH_SOCK"))
sshAgentConn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
return cli.NewExitError(fmt.Sprintf("Dial failed: %s", err), 1)
}
requestHandler := makeCertRequestHandler(config)
requestHandler.sshAgentConn = sshAgentConn
err = requestHandler.setupPrivateKeys(config)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed CA key load: %v\n", err), 1)
}
log.Printf("Server started with config %#v\n", config)
r := mux.NewRouter()
requests := r.Path("/cert/requests").Subrouter()
requests.Methods("POST").HandlerFunc(requestHandler.createSigningRequest)
requests.Methods("GET").HandlerFunc(requestHandler.listPendingRequests)
request := r.Path("/cert/requests/{requestID}").Subrouter()
request.Methods("GET").HandlerFunc(requestHandler.getRequestStatus)
request.Methods("POST", "DELETE").HandlerFunc(requestHandler.signOrRejectRequest)
environments := r.Path("/config/environments").Subrouter()
environments.Methods("GET").HandlerFunc(requestHandler.listEnvironments)
if is_proxied {
http.ListenAndServe(addr, handlers.ProxyHeaders(r))
} else {
http.ListenAndServe(addr, r)
}
return nil
}