-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
server.go
58 lines (51 loc) · 1.51 KB
/
server.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
package grpc
import (
"context"
"crypto"
"crypto/x509"
"time"
"github.com/karrick/tparse/v2"
"github.com/plgd-dev/hub/v2/certificate-authority/pb"
"github.com/plgd-dev/hub/v2/certificate-authority/store/mongodb"
"github.com/plgd-dev/hub/v2/pkg/log"
"github.com/plgd-dev/kit/v2/security"
)
type CertificateSigner interface {
// csr is encoded by PEM and returns PEM
Sign(ctx context.Context, csr []byte) ([]byte, error)
}
// CertificateAuthorityServer handles incoming requests.
type CertificateAuthorityServer struct {
pb.UnimplementedCertificateAuthorityServer
validFrom func() time.Time
validFor time.Duration
certificate []*x509.Certificate
privateKey crypto.PrivateKey
signerConfig SignerConfig
logger log.Logger
ownerClaim string
store *mongodb.Store
}
func NewCertificateAuthorityServer(ownerClaim string, signerConfig SignerConfig, store *mongodb.Store, logger log.Logger) (*CertificateAuthorityServer, error) {
certificate, err := security.LoadX509(signerConfig.CertFile)
if err != nil {
return nil, err
}
privateKey, err := security.LoadX509PrivateKey(signerConfig.KeyFile)
if err != nil {
return nil, err
}
return &CertificateAuthorityServer{
validFrom: func() time.Time {
t, _ := tparse.ParseNow(time.RFC3339, signerConfig.ValidFrom)
return t
},
validFor: signerConfig.ExpiresIn,
certificate: certificate,
privateKey: privateKey,
signerConfig: signerConfig,
logger: logger,
ownerClaim: ownerClaim,
store: store,
}, nil
}