2019-09-05 16:11:21 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package certificateclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-09-10 17:24:41 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-09-05 16:11:21 +01:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
2019-09-05 16:11:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Client implements pb.DRPCCertificatesClient.
|
2019-09-05 16:11:21 +01:00
|
|
|
type Client struct {
|
2019-09-19 05:46:39 +01:00
|
|
|
conn *rpc.Conn
|
2019-12-22 14:52:55 +00:00
|
|
|
client pb.DRPCCertificatesClient
|
2019-09-05 16:11:21 +01:00
|
|
|
}
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
// New creates a new certificate signing rpc client.
|
|
|
|
func New(ctx context.Context, dialer rpc.Dialer, address string) (_ *Client, err error) {
|
2019-09-05 16:11:21 +01:00
|
|
|
defer mon.Task()(&ctx, address)(&err)
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
conn, err := dialer.DialAddressInsecure(ctx, address)
|
2019-09-05 16:11:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
conn: conn,
|
2020-03-25 12:15:27 +00:00
|
|
|
client: pb.NewDRPCCertificatesClient(conn),
|
2019-09-05 16:11:21 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:54:14 +01:00
|
|
|
// NewClientFrom creates a new certificate signing client from an existing
|
|
|
|
// cert signing client.
|
2019-12-22 14:52:55 +00:00
|
|
|
func NewClientFrom(client pb.DRPCCertificatesClient) *Client {
|
2019-09-05 16:11:21 +01:00
|
|
|
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)
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
tlsOptions, err := tlsopts.NewOptions(ident, config.TLS, nil)
|
2019-09-05 16:11:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-19 05:46:39 +01:00
|
|
|
client, err := New(ctx, rpc.NewDefaultDialer(tlsOptions), config.Address)
|
2019-09-05 16:11:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-19 05:46:39 +01:00
|
|
|
defer func() { err = errs.Combine(err, client.Close()) }()
|
2019-09-05 16:11:21 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|