2022-05-02 07:21:07 -04:00
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2022-06-01 09:08:42 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/atls"
|
2022-06-13 05:40:27 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/grpc/atlscredentials"
|
2022-05-02 07:21:07 -04:00
|
|
|
"github.com/edgelesssys/constellation/state/keyservice/keyproto"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// KeyClient wraps a KeyAPI client and the connection to it.
|
|
|
|
type KeyClient struct {
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
keyapi keyproto.APIClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect connects the client to a given server, using the handed
|
|
|
|
// Validators for the attestation of the connection.
|
|
|
|
// The connection must be closed using Close(). If connect is
|
|
|
|
// called on a client that already has a connection, the old
|
|
|
|
// connection is closed.
|
2022-05-06 07:56:02 -04:00
|
|
|
func (c *KeyClient) Connect(endpoint string, validators []atls.Validator) error {
|
2022-06-13 05:40:27 -04:00
|
|
|
creds := atlscredentials.New(nil, validators)
|
2022-05-02 07:21:07 -04:00
|
|
|
|
2022-06-13 05:40:27 -04:00
|
|
|
conn, err := grpc.Dial(endpoint, grpc.WithTransportCredentials(creds))
|
2022-05-02 07:21:07 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = c.Close()
|
|
|
|
c.conn = conn
|
|
|
|
c.keyapi = keyproto.NewAPIClient(conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the grpc connection of the client.
|
|
|
|
// Close is idempotent and can be called on non connected clients
|
|
|
|
// without returning an error.
|
|
|
|
func (c *KeyClient) Close() error {
|
|
|
|
if c.conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := c.conn.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.conn = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushStateDiskKey pushes the state disk key to a constellation instance in recovery mode.
|
|
|
|
// The state disk key must be derived from the UUID of the state disk and the master key.
|
|
|
|
func (c *KeyClient) PushStateDiskKey(ctx context.Context, stateDiskKey []byte) error {
|
|
|
|
if c.keyapi == nil {
|
|
|
|
return errors.New("client is not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &keyproto.PushStateDiskKeyRequest{
|
|
|
|
StateDiskKey: stateDiskKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := c.keyapi.PushStateDiskKey(ctx, req)
|
|
|
|
return err
|
|
|
|
}
|