/
showcertificates.go
92 lines (85 loc) · 3.11 KB
/
showcertificates.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
// Copyright © 2020 Attestant Limited.
// 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 cmd
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/wealdtech/go-majordomo"
)
// ShowCertificates shows information about the certificates configured for Dirk.
func ShowCertificates(ctx context.Context, majordomo majordomo.Service) error {
certPEMBlock, err := majordomo.Fetch(ctx, viper.GetString("certificates.server-cert"))
if err != nil {
return errors.Wrap(err, "failed to obtain server certificate")
}
fmt.Printf("Server certificate obtained from %s\n", viper.GetString("certificates.server-cert"))
keyPEMBlock, err := majordomo.Fetch(ctx, viper.GetString("certificates.server-key"))
if err != nil {
return errors.Wrap(err, "failed to obtain server key")
}
fmt.Printf("Server key obtained from %s\n", viper.GetString("certificates.server-key"))
var caPEMBlock []byte
if viper.GetString("certificates.ca-cert") != "" {
caPEMBlock, err = majordomo.Fetch(ctx, viper.GetString("certificates.ca-cert"))
if err != nil {
return errors.Wrap(err, "failed to obtain client CA certificate")
}
fmt.Printf("CA certificate obtained from %s\n", viper.GetString("certificates.ca-cert"))
}
fmt.Println()
serverCert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
return errors.Wrap(err, "invalid server certificate/key")
}
if len(serverCert.Certificate) == 0 {
return errors.New("certificate file does not contain a certificate")
}
cert, err := x509.ParseCertificate(serverCert.Certificate[0])
if err != nil {
return errors.Wrap(err, "could not read certificate")
}
fmt.Printf("Server certificate issued by: %s\n", cert.Issuer.CommonName)
if cert.NotAfter.Before(time.Now()) {
fmt.Printf("WARNING: server certificate expired at: %v\n", cert.NotAfter)
} else {
fmt.Printf("Server certificate expires: %v\n", cert.NotAfter)
}
fmt.Printf("Server certificate issued to: %s\n", cert.Subject.CommonName)
for len(caPEMBlock) > 0 {
var block *pem.Block
block, caPEMBlock = pem.Decode(caPEMBlock)
if block == nil {
break
}
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
continue
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
continue
}
fmt.Printf("\nCertificate authority certificate is: %s\n", cert.Subject.CommonName)
if cert.NotAfter.Before(time.Now()) {
fmt.Printf("WARNING: certificate authority certificate expired at: %v\n", cert.NotAfter)
} else {
fmt.Printf("Certificate authority certificate expires: %v\n", cert.NotAfter)
}
}
return nil
}