-
Notifications
You must be signed in to change notification settings - Fork 402
/
client.go
95 lines (77 loc) · 2.26 KB
/
client.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package certificateclient
import (
"context"
"time"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"storj.io/common/identity"
"storj.io/common/pb"
"storj.io/common/peertls/tlsopts"
"storj.io/common/rpc"
)
var mon = monkit.Package()
// Config is a config struct for use with a certificate signing service client.
type Config struct {
Address string `help:"address of the certificate signing rpc service"`
TLS tlsopts.Config
}
// Client implements pb.DRPCCertificatesClient
type Client struct {
conn *rpc.Conn
client pb.DRPCCertificatesClient
}
// New creates a new certificate signing rpc client.
func New(ctx context.Context, dialer rpc.Dialer, address string) (_ *Client, err error) {
defer mon.Task()(&ctx, address)(&err)
conn, err := dialer.DialAddressInsecure(ctx, address)
if err != nil {
return nil, err
}
return &Client{
conn: conn,
client: pb.NewDRPCCertificatesClient(conn),
}, nil
}
// NewClientFrom creates a new certificate signing client from an existing
// cert signing client.
func NewClientFrom(client pb.DRPCCertificatesClient) *Client {
return &Client{
client: client,
}
}
// Sign submits a certificate signing request given the config.
func (config Config) Sign(ctx context.Context, ident *identity.FullIdentity, authToken string) (_ [][]byte, err error) {
defer mon.Task()(&ctx)(&err)
tlsOptions, err := tlsopts.NewOptions(ident, config.TLS, nil)
if err != nil {
return nil, err
}
client, err := New(ctx, rpc.NewDefaultDialer(tlsOptions), config.Address)
if err != nil {
return nil, err
}
defer func() { err = errs.Combine(err, client.Close()) }()
return client.Sign(ctx, authToken)
}
// Sign claims an authorization using the token string and returns a signed
// copy of the client's CA certificate.
func (client *Client) Sign(ctx context.Context, tokenStr string) (_ [][]byte, err error) {
defer mon.Task()(&ctx)(&err)
res, err := client.client.Sign(ctx, &pb.SigningRequest{
AuthToken: tokenStr,
Timestamp: time.Now().Unix(),
})
if err != nil {
return nil, err
}
return res.Chain, nil
}
// Close closes the client.
func (client *Client) Close() error {
if client.conn != nil {
return client.conn.Close()
}
return nil
}