2022-03-22 11:03:15 -04:00
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/pubapi/pubproto"
|
2022-04-20 09:32:54 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/state"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/connectivity"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
"google.golang.org/grpc/test/bufconn"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClose(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-04-13 09:01:02 -04:00
|
|
|
client := Client{}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
// Create a connection.
|
|
|
|
listener := bufconn.Listen(4)
|
|
|
|
defer listener.Close()
|
|
|
|
ctx := context.Background()
|
|
|
|
conn, err := grpc.DialContext(ctx, "", grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
|
|
|
|
return listener.Dial()
|
|
|
|
}), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
require.NoError(err)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Wait for connection to reach 'connecting' state.
|
|
|
|
// Connection is not yet usable in this state, but we just need
|
|
|
|
// any stable non 'shutdown' state to validate that the state
|
|
|
|
// previous to calling close isn't already 'shutdown'.
|
|
|
|
err = func() error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
for {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
if connectivity.Connecting == conn.GetState() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
client.conn = conn
|
|
|
|
|
|
|
|
// Close connection.
|
|
|
|
assert.NoError(client.Close())
|
|
|
|
assert.Empty(client.conn)
|
|
|
|
assert.Equal(connectivity.Shutdown, conn.GetState())
|
|
|
|
|
|
|
|
// Close closed connection.
|
|
|
|
assert.NoError(client.Close())
|
|
|
|
assert.Empty(client.conn)
|
|
|
|
assert.Equal(connectivity.Shutdown, conn.GetState())
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:32:54 -04:00
|
|
|
func TestGetState(t *testing.T) {
|
|
|
|
someErr := errors.New("some error")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
pubAPIClient pubproto.APIClient
|
|
|
|
wantErr bool
|
|
|
|
wantState state.State
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
pubAPIClient: &stubPubAPIClient{getStateState: state.IsNode},
|
|
|
|
wantState: state.IsNode,
|
|
|
|
},
|
|
|
|
"getState error": {
|
|
|
|
pubAPIClient: &stubPubAPIClient{getStateErr: someErr},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"uninitialized": {
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
client := Client{}
|
|
|
|
if tc.pubAPIClient != nil {
|
|
|
|
client.pubapi = tc.pubAPIClient
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := client.GetState(context.Background())
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.wantState, state)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func TestActivate(t *testing.T) {
|
|
|
|
testKey := base64.StdEncoding.EncodeToString([]byte("32bytesWireGuardKeyForTheTesting"))
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-20 09:17:33 -04:00
|
|
|
pubAPIClient *stubPubAPIClient
|
2022-03-22 11:03:15 -04:00
|
|
|
userPublicKey string
|
2022-04-13 06:39:55 -04:00
|
|
|
ips []string
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"normal activation": {
|
2022-04-20 09:17:33 -04:00
|
|
|
pubAPIClient: &stubPubAPIClient{},
|
2022-03-22 11:03:15 -04:00
|
|
|
userPublicKey: testKey,
|
2022-04-13 06:39:55 -04:00
|
|
|
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-20 09:17:33 -04:00
|
|
|
"client without pubAPIClient": {
|
2022-03-22 11:03:15 -04:00
|
|
|
userPublicKey: testKey,
|
2022-04-13 06:39:55 -04:00
|
|
|
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"empty public key parameter": {
|
2022-04-20 09:17:33 -04:00
|
|
|
pubAPIClient: &stubPubAPIClient{},
|
2022-03-22 11:03:15 -04:00
|
|
|
userPublicKey: "",
|
2022-04-13 06:39:55 -04:00
|
|
|
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"invalid public key parameter": {
|
2022-04-20 09:17:33 -04:00
|
|
|
pubAPIClient: &stubPubAPIClient{},
|
2022-03-22 11:03:15 -04:00
|
|
|
userPublicKey: "invalid Key",
|
2022-04-13 06:39:55 -04:00
|
|
|
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"empty ips parameter": {
|
2022-04-20 09:17:33 -04:00
|
|
|
pubAPIClient: &stubPubAPIClient{},
|
2022-03-22 11:03:15 -04:00
|
|
|
userPublicKey: testKey,
|
2022-04-13 06:39:55 -04:00
|
|
|
ips: []string{},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"fail ActivateAsCoordinator": {
|
2022-04-20 09:17:33 -04:00
|
|
|
pubAPIClient: &stubPubAPIClient{activateAsCoordinatorErr: someErr},
|
2022-03-22 11:03:15 -04:00
|
|
|
userPublicKey: testKey,
|
2022-04-13 06:39:55 -04:00
|
|
|
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
client := Client{}
|
2022-04-20 09:17:33 -04:00
|
|
|
if tc.pubAPIClient != nil {
|
|
|
|
client.pubapi = tc.pubAPIClient
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-04-25 11:21:58 -04:00
|
|
|
_, err := client.Activate(context.Background(), []byte(tc.userPublicKey), []byte("Constellation"), tc.ips, nil, nil, "serviceaccount://test")
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
2022-04-20 09:17:33 -04:00
|
|
|
assert.Equal("32bytesWireGuardKeyForTheTesting", string(tc.pubAPIClient.activateAsCoordinatorReqKey))
|
|
|
|
assert.Equal(tc.ips, tc.pubAPIClient.activateAsCoordinatorReqIPs)
|
|
|
|
assert.Equal("Constellation", string(tc.pubAPIClient.activateAsCoordinatorMasterSecret))
|
|
|
|
assert.Equal("serviceaccount://test", tc.pubAPIClient.activateCloudServiceAccountURI)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:17:33 -04:00
|
|
|
type stubPubAPIClient struct {
|
2022-04-20 09:32:54 -04:00
|
|
|
getStateState state.State
|
|
|
|
getStateErr error
|
2022-04-13 06:39:55 -04:00
|
|
|
activateAsCoordinatorErr error
|
|
|
|
activateAdditionalNodesErr error
|
|
|
|
activateAsCoordinatorReqKey []byte
|
|
|
|
activateAsCoordinatorReqIPs []string
|
|
|
|
activateAsCoordinatorMasterSecret []byte
|
|
|
|
activateAdditionalNodesReqIPs []string
|
|
|
|
activateCloudServiceAccountURI string
|
2022-03-22 11:03:15 -04:00
|
|
|
pubproto.APIClient
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:32:54 -04:00
|
|
|
func (s *stubPubAPIClient) GetState(ctx context.Context, in *pubproto.GetStateRequest, opts ...grpc.CallOption) (*pubproto.GetStateResponse, error) {
|
|
|
|
return &pubproto.GetStateResponse{State: uint32(s.getStateState)}, s.getStateErr
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:17:33 -04:00
|
|
|
func (s *stubPubAPIClient) ActivateAsCoordinator(ctx context.Context, in *pubproto.ActivateAsCoordinatorRequest,
|
2022-03-22 11:03:15 -04:00
|
|
|
opts ...grpc.CallOption,
|
|
|
|
) (pubproto.API_ActivateAsCoordinatorClient, error) {
|
|
|
|
s.activateAsCoordinatorReqKey = in.AdminVpnPubKey
|
2022-04-13 06:39:55 -04:00
|
|
|
s.activateAsCoordinatorReqIPs = in.NodePublicIps
|
2022-03-22 11:03:15 -04:00
|
|
|
s.activateAsCoordinatorMasterSecret = in.MasterSecret
|
|
|
|
s.activateCloudServiceAccountURI = in.CloudServiceAccountUri
|
2022-04-20 09:17:33 -04:00
|
|
|
return dummyActivateAsCoordinatorClient{}, s.activateAsCoordinatorErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-20 09:17:33 -04:00
|
|
|
func (s *stubPubAPIClient) ActivateAdditionalNodes(ctx context.Context, in *pubproto.ActivateAdditionalNodesRequest,
|
2022-03-22 11:03:15 -04:00
|
|
|
opts ...grpc.CallOption,
|
|
|
|
) (pubproto.API_ActivateAdditionalNodesClient, error) {
|
2022-04-13 06:39:55 -04:00
|
|
|
s.activateAdditionalNodesReqIPs = in.NodePublicIps
|
2022-04-20 09:17:33 -04:00
|
|
|
return dummyActivateAdditionalNodesClient{}, s.activateAdditionalNodesErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|