2022-03-22 11:03:15 -04:00
|
|
|
package pubapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-11 09:28:41 -04:00
|
|
|
"encoding/json"
|
2022-03-22 11:03:15 -04:00
|
|
|
"errors"
|
2022-05-16 11:32:00 -04:00
|
|
|
"fmt"
|
2022-04-20 11:06:47 -04:00
|
|
|
"io"
|
2022-03-22 11:03:15 -04:00
|
|
|
"net"
|
2022-04-13 06:39:55 -04:00
|
|
|
"sync"
|
2022-03-22 11:03:15 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/atls"
|
2022-04-11 09:28:41 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/attestation/vtpm"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/core"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/oid"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/peer"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/pubapi/pubproto"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/role"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/state"
|
2022-04-28 03:49:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/util/grpcutil"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/util/testdialer"
|
2022-05-16 11:32:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/deploy/ssh"
|
|
|
|
"github.com/edgelesssys/constellation/internal/deploy/user"
|
2022-05-10 06:35:17 -04:00
|
|
|
kms "github.com/edgelesssys/constellation/kms/server/setup"
|
2022-04-11 09:28:41 -04:00
|
|
|
"github.com/edgelesssys/constellation/state/keyservice/keyproto"
|
2022-05-16 11:32:00 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
2022-04-11 09:28:41 -04:00
|
|
|
grpcpeer "google.golang.org/grpc/peer"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestActivateAsCoordinator(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
coordinatorPubKey := []byte{6, 7, 8}
|
2022-04-20 11:06:47 -04:00
|
|
|
testNode1 := newStubPeer("192.0.2.11", []byte{1, 2, 3})
|
|
|
|
testNode2 := newStubPeer("192.0.2.12", []byte{2, 3, 4})
|
|
|
|
testNode3 := newStubPeer("192.0.2.13", []byte{3, 4, 5})
|
2022-04-26 10:54:05 -04:00
|
|
|
wantNode1 := peer.Peer{PublicIP: "192.0.2.11", VPNIP: "10.118.0.11", VPNPubKey: []byte{1, 2, 3}, Role: role.Node}
|
|
|
|
wantNode2 := peer.Peer{PublicIP: "192.0.2.12", VPNIP: "10.118.0.12", VPNPubKey: []byte{2, 3, 4}, Role: role.Node}
|
|
|
|
wantNode3 := peer.Peer{PublicIP: "192.0.2.13", VPNIP: "10.118.0.13", VPNPubKey: []byte{3, 4, 5}, Role: role.Node}
|
|
|
|
wantCoord := peer.Peer{PublicIP: "192.0.2.1", VPNIP: "10.118.0.1", VPNPubKey: coordinatorPubKey, Role: role.Coordinator}
|
2022-04-13 06:39:55 -04:00
|
|
|
adminPeer := peer.Peer{VPNPubKey: []byte{7, 8, 9}, Role: role.Admin}
|
2022-05-16 11:32:00 -04:00
|
|
|
sshUser1 := &ssh.UserKey{
|
|
|
|
Username: "test-user-1",
|
|
|
|
PublicKey: "ssh-rsa abcdefg",
|
|
|
|
}
|
|
|
|
sshUser2 := &ssh.UserKey{
|
|
|
|
Username: "test-user-2",
|
|
|
|
PublicKey: "ssh-ed25519 hijklmn",
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-13 06:39:55 -04:00
|
|
|
nodes []*stubPeer
|
2022-03-22 11:03:15 -04:00
|
|
|
state state.State
|
|
|
|
switchToPersistentStoreErr error
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
|
|
|
wantPeers []peer.Peer
|
|
|
|
wantState state.State
|
2022-04-13 06:39:55 -04:00
|
|
|
adminVPNIP string
|
2022-05-16 11:32:00 -04:00
|
|
|
sshKeys []*ssh.UserKey
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"0 nodes": {
|
2022-04-26 10:54:05 -04:00
|
|
|
state: state.AcceptingInit,
|
|
|
|
wantPeers: []peer.Peer{wantCoord},
|
|
|
|
wantState: state.ActivatingNodes,
|
|
|
|
adminVPNIP: "10.118.0.11",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"1 node": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
wantPeers: []peer.Peer{wantCoord, wantNode1},
|
|
|
|
wantState: state.ActivatingNodes,
|
|
|
|
adminVPNIP: "10.118.0.12",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"2 nodes": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1, testNode2},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
wantPeers: []peer.Peer{wantCoord, wantNode1, wantNode2},
|
|
|
|
wantState: state.ActivatingNodes,
|
|
|
|
adminVPNIP: "10.118.0.13",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"3 nodes": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1, testNode2, testNode3},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
wantPeers: []peer.Peer{wantCoord, wantNode1, wantNode2, wantNode3},
|
|
|
|
wantState: state.ActivatingNodes,
|
|
|
|
adminVPNIP: "10.118.0.14",
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-05-16 11:32:00 -04:00
|
|
|
"coordinator with SSH users": {
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
wantPeers: []peer.Peer{wantCoord},
|
|
|
|
wantState: state.ActivatingNodes,
|
|
|
|
adminVPNIP: "10.118.0.11",
|
|
|
|
sshKeys: []*ssh.UserKey{sshUser1, sshUser2},
|
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
"already activated": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1},
|
|
|
|
state: state.ActivatingNodes,
|
|
|
|
wantErr: true,
|
|
|
|
wantState: state.ActivatingNodes,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"wrong peer kind": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1},
|
|
|
|
state: state.IsNode,
|
|
|
|
wantErr: true,
|
|
|
|
wantState: state.IsNode,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"node activation error": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1, {activateErr: someErr}, testNode3},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
wantErr: true,
|
|
|
|
wantState: state.Failed,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"node join error": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1, {joinErr: someErr}, testNode3},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
wantErr: true,
|
|
|
|
wantState: state.Failed,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"SwitchToPersistentStore error": {
|
2022-04-13 06:39:55 -04:00
|
|
|
nodes: []*stubPeer{testNode1},
|
2022-03-22 11:03:15 -04:00
|
|
|
state: state.AcceptingInit,
|
|
|
|
switchToPersistentStoreErr: someErr,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
|
|
|
wantState: state.Failed,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
autoscalingNodeGroups := []string{"ang1", "ang2"}
|
|
|
|
keyEncryptionKeyID := "constellation"
|
2022-05-16 11:32:00 -04:00
|
|
|
fs := afero.NewMemMapFs()
|
2022-03-22 11:03:15 -04:00
|
|
|
core := &fakeCore{
|
|
|
|
state: tc.state,
|
|
|
|
vpnPubKey: coordinatorPubKey,
|
|
|
|
switchToPersistentStoreErr: tc.switchToPersistentStoreErr,
|
|
|
|
kubeconfig: []byte("kubeconfig"),
|
|
|
|
ownerID: []byte("ownerID"),
|
|
|
|
clusterID: []byte("clusterID"),
|
2022-05-16 11:32:00 -04:00
|
|
|
linuxUserManager: user.NewLinuxUserManagerFake(fs),
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-05-16 11:32:00 -04:00
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
netDialer := testdialer.NewBufconnDialer()
|
|
|
|
dialer := grpcutil.NewDialer(fakeValidator{}, netDialer)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
getPublicIPAddr := func() (string, error) {
|
|
|
|
return "192.0.2.1", nil
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
api := New(zaptest.NewLogger(t), core, dialer, stubVPNAPIServer{}, getPublicIPAddr, nil)
|
2022-04-13 06:39:55 -04:00
|
|
|
defer api.Close()
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
// spawn nodes
|
2022-04-13 06:39:55 -04:00
|
|
|
var nodePublicIPs []string
|
|
|
|
var wg sync.WaitGroup
|
2022-03-22 11:03:15 -04:00
|
|
|
for _, n := range tc.nodes {
|
2022-04-13 06:39:55 -04:00
|
|
|
nodePublicIPs = append(nodePublicIPs, n.peer.PublicIP)
|
2022-03-22 11:03:15 -04:00
|
|
|
server := n.newServer()
|
2022-04-13 06:39:55 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func(endpoint string) {
|
2022-04-28 03:49:15 -04:00
|
|
|
listener := netDialer.GetListener(endpoint)
|
2022-04-13 06:39:55 -04:00
|
|
|
wg.Done()
|
|
|
|
_ = server.Serve(listener)
|
|
|
|
}(net.JoinHostPort(n.peer.PublicIP, endpointAVPNPort))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer server.GracefulStop()
|
|
|
|
}
|
2022-04-13 06:39:55 -04:00
|
|
|
wg.Wait()
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
stream := &stubActivateAsCoordinatorServer{}
|
|
|
|
err := api.ActivateAsCoordinator(&pubproto.ActivateAsCoordinatorRequest{
|
2022-04-13 06:39:55 -04:00
|
|
|
AdminVpnPubKey: adminPeer.VPNPubKey,
|
|
|
|
NodePublicIps: nodePublicIPs,
|
2022-03-22 11:03:15 -04:00
|
|
|
AutoscalingNodeGroups: autoscalingNodeGroups,
|
|
|
|
MasterSecret: []byte("Constellation"),
|
|
|
|
KeyEncryptionKeyId: keyEncryptionKeyID,
|
|
|
|
UseExistingKek: false,
|
|
|
|
KmsUri: kms.ClusterKMSURI,
|
|
|
|
StorageUri: kms.NoStoreURI,
|
2022-05-16 11:32:00 -04:00
|
|
|
SshUserKeys: ssh.ToProtoSlice(tc.sshKeys),
|
2022-03-22 11:03:15 -04:00
|
|
|
}, stream)
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantState, core.state)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Coordinator streams logs and admin conf
|
2022-04-05 03:12:18 -04:00
|
|
|
require.Greater(len(stream.sent), len(tc.nodes))
|
|
|
|
for i := 0; i < len(stream.sent)-1; i++ {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NotEmpty(stream.sent[i].GetLog().Message)
|
|
|
|
}
|
2022-04-05 03:12:18 -04:00
|
|
|
adminConfig := stream.sent[len(stream.sent)-1].GetAdminConfig()
|
2022-04-13 06:39:55 -04:00
|
|
|
assert.Equal(tc.adminVPNIP, adminConfig.AdminVpnIp)
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Equal(coordinatorPubKey, adminConfig.CoordinatorVpnPubKey)
|
|
|
|
assert.Equal(core.kubeconfig, adminConfig.Kubeconfig)
|
|
|
|
assert.Equal(core.ownerID, adminConfig.OwnerId)
|
|
|
|
assert.Equal(core.clusterID, adminConfig.ClusterId)
|
|
|
|
|
|
|
|
// Core is updated
|
2022-04-13 06:39:55 -04:00
|
|
|
vpnIP, err := core.GetVPNIP()
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(vpnIP, core.vpnIP)
|
|
|
|
// construct full list of expected peers
|
|
|
|
adminPeer.VPNIP = tc.adminVPNIP
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(append(tc.wantPeers, adminPeer), core.peers)
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Equal(autoscalingNodeGroups, core.autoscalingNodeGroups)
|
|
|
|
assert.Equal(keyEncryptionKeyID, core.kekID)
|
2022-04-13 03:18:32 -04:00
|
|
|
assert.Equal([]role.Role{role.Coordinator}, core.persistNodeStateRoles)
|
2022-05-16 11:32:00 -04:00
|
|
|
|
|
|
|
// Test SSH user & key creation. Both cases: "supposed to add" and "not supposed to add"
|
|
|
|
// This slightly differs from a real environment (e.g. missing /home) but should be fine in the stub context with a virtual file system
|
|
|
|
if tc.sshKeys != nil {
|
|
|
|
passwd := user.Passwd{}
|
|
|
|
entries, err := passwd.Parse(fs)
|
|
|
|
require.NoError(err)
|
|
|
|
for _, singleEntry := range entries {
|
|
|
|
username := singleEntry.Gecos
|
|
|
|
_, err := fs.Stat(fmt.Sprintf("/home/%s/.ssh/authorized_keys.d/ssh-keys", username))
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
passwd := user.Passwd{}
|
|
|
|
_, err := passwd.Parse(fs)
|
|
|
|
assert.EqualError(err, "open /etc/passwd: file does not exist")
|
|
|
|
_, err = fs.Stat("/home")
|
|
|
|
assert.EqualError(err, "open /home: file does not exist")
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActivateAdditionalNodes(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
2022-04-20 11:06:47 -04:00
|
|
|
testNode1 := newStubPeer("192.0.2.11", []byte{1, 2, 3})
|
|
|
|
testNode2 := newStubPeer("192.0.2.12", []byte{2, 3, 4})
|
|
|
|
testNode3 := newStubPeer("192.0.2.13", []byte{3, 4, 5})
|
2022-04-26 10:54:05 -04:00
|
|
|
wantNode1 := peer.Peer{PublicIP: "192.0.2.11", VPNIP: "10.118.0.11", VPNPubKey: []byte{1, 2, 3}, Role: role.Node}
|
|
|
|
wantNode2 := peer.Peer{PublicIP: "192.0.2.12", VPNIP: "10.118.0.12", VPNPubKey: []byte{2, 3, 4}, Role: role.Node}
|
|
|
|
wantNode3 := peer.Peer{PublicIP: "192.0.2.13", VPNIP: "10.118.0.13", VPNPubKey: []byte{3, 4, 5}, Role: role.Node}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes []*stubPeer
|
|
|
|
state state.State
|
|
|
|
wantErr bool
|
|
|
|
wantPeers []peer.Peer
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"0 nodes": {
|
|
|
|
state: state.ActivatingNodes,
|
|
|
|
},
|
|
|
|
"1 node": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1},
|
|
|
|
state: state.ActivatingNodes,
|
|
|
|
wantPeers: []peer.Peer{wantNode1},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"2 nodes": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1, testNode2},
|
|
|
|
state: state.ActivatingNodes,
|
|
|
|
wantPeers: []peer.Peer{wantNode1, wantNode2},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"3 nodes": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1, testNode2, testNode3},
|
|
|
|
state: state.ActivatingNodes,
|
|
|
|
wantPeers: []peer.Peer{wantNode1, wantNode2, wantNode3},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"uninitialized": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1},
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"wrong peer kind": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1},
|
|
|
|
state: state.IsNode,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"node activation error": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1, {activateErr: someErr}, testNode3},
|
|
|
|
state: state.ActivatingNodes,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"node join error": {
|
2022-04-26 10:54:05 -04:00
|
|
|
nodes: []*stubPeer{testNode1, {joinErr: someErr}, testNode3},
|
|
|
|
state: state.ActivatingNodes,
|
|
|
|
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)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
core := &fakeCore{state: tc.state}
|
2022-04-28 03:49:15 -04:00
|
|
|
netDialer := testdialer.NewBufconnDialer()
|
|
|
|
dialer := grpcutil.NewDialer(fakeValidator{}, netDialer)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
getPublicIPAddr := func() (string, error) {
|
|
|
|
return "192.0.2.1", nil
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
api := New(zaptest.NewLogger(t), core, dialer, nil, getPublicIPAddr, nil)
|
2022-04-13 06:39:55 -04:00
|
|
|
defer api.Close()
|
2022-03-22 11:03:15 -04:00
|
|
|
// spawn nodes
|
2022-04-13 06:39:55 -04:00
|
|
|
var nodePublicIPs []string
|
|
|
|
var wg sync.WaitGroup
|
2022-03-22 11:03:15 -04:00
|
|
|
for _, n := range tc.nodes {
|
2022-04-13 06:39:55 -04:00
|
|
|
nodePublicIPs = append(nodePublicIPs, n.peer.PublicIP)
|
2022-03-22 11:03:15 -04:00
|
|
|
server := n.newServer()
|
2022-04-13 06:39:55 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func(endpoint string) {
|
2022-04-28 03:49:15 -04:00
|
|
|
listener := netDialer.GetListener(endpoint)
|
2022-04-13 06:39:55 -04:00
|
|
|
wg.Done()
|
|
|
|
_ = server.Serve(listener)
|
|
|
|
}(net.JoinHostPort(n.peer.PublicIP, endpointAVPNPort))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer server.GracefulStop()
|
|
|
|
}
|
2022-04-13 06:39:55 -04:00
|
|
|
wg.Wait()
|
|
|
|
// since we are not activating the coordinator, initialize the store with IP's
|
|
|
|
require.NoError(core.InitializeStoreIPs())
|
2022-03-22 11:03:15 -04:00
|
|
|
stream := &stubActivateAdditionalNodesServer{}
|
2022-04-13 06:39:55 -04:00
|
|
|
err := api.ActivateAdditionalNodes(&pubproto.ActivateAdditionalNodesRequest{NodePublicIps: nodePublicIPs}, stream)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Coordinator streams logs
|
|
|
|
require.Len(stream.sent, len(tc.nodes)+1)
|
|
|
|
for _, s := range stream.sent {
|
|
|
|
assert.NotEmpty(s.GetLog().Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Core is updated
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantPeers, core.peers)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 06:39:55 -04:00
|
|
|
func TestAssemblePeerStruct(t *testing.T) {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
getPublicIPAddr := func() (string, error) {
|
|
|
|
return "192.0.2.1", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
vpnPubKey := []byte{2, 3, 4}
|
|
|
|
core := &fakeCore{vpnPubKey: vpnPubKey}
|
2022-04-28 03:49:15 -04:00
|
|
|
api := New(zaptest.NewLogger(t), core, nil, nil, getPublicIPAddr, nil)
|
2022-04-13 06:39:55 -04:00
|
|
|
defer api.Close()
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-13 06:39:55 -04:00
|
|
|
vpnIP, err := core.GetVPNIP()
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
want := peer.Peer{
|
2022-04-13 06:39:55 -04:00
|
|
|
PublicIP: "192.0.2.1",
|
|
|
|
VPNIP: vpnIP,
|
|
|
|
VPNPubKey: vpnPubKey,
|
|
|
|
Role: role.Coordinator,
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-13 06:39:55 -04:00
|
|
|
actual, err := api.assemblePeerStruct(vpnIP, role.Coordinator)
|
2022-03-22 11:03:15 -04:00
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(want, actual)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-13 06:39:55 -04:00
|
|
|
type stubPeer struct {
|
2022-04-20 11:06:47 -04:00
|
|
|
peer peer.Peer
|
|
|
|
activateAsNodeMessages []*pubproto.ActivateAsNodeResponse
|
|
|
|
activateAsNodeReceive int
|
|
|
|
activateErr error
|
|
|
|
joinErr error
|
2022-04-25 11:26:17 -04:00
|
|
|
getPubKeyErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
pubproto.UnimplementedAPIServer
|
|
|
|
}
|
|
|
|
|
2022-04-20 11:06:47 -04:00
|
|
|
func newStubPeer(publicIP string, vpnPubKey []byte) *stubPeer {
|
|
|
|
return &stubPeer{
|
|
|
|
peer: peer.Peer{PublicIP: publicIP, VPNPubKey: vpnPubKey},
|
|
|
|
activateAsNodeMessages: []*pubproto.ActivateAsNodeResponse{
|
|
|
|
{Response: &pubproto.ActivateAsNodeResponse_StateDiskUuid{StateDiskUuid: "state-disk-uuid"}},
|
|
|
|
{Response: &pubproto.ActivateAsNodeResponse_NodeVpnPubKey{NodeVpnPubKey: vpnPubKey}},
|
|
|
|
},
|
|
|
|
activateAsNodeReceive: 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *stubPeer) ActivateAsNode(stream pubproto.API_ActivateAsNodeServer) error {
|
|
|
|
for _, message := range n.activateAsNodeMessages {
|
|
|
|
err := stream.Send(message)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < n.activateAsNodeReceive; i++ {
|
|
|
|
_, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := stream.Recv(); err != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.activateErr
|
2022-04-13 06:39:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *stubPeer) ActivateAsAdditionalCoordinator(ctx context.Context, in *pubproto.ActivateAsAdditionalCoordinatorRequest) (*pubproto.ActivateAsAdditionalCoordinatorResponse, error) {
|
|
|
|
return &pubproto.ActivateAsAdditionalCoordinatorResponse{}, n.activateErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-13 06:39:55 -04:00
|
|
|
func (*stubPeer) TriggerNodeUpdate(ctx context.Context, in *pubproto.TriggerNodeUpdateRequest) (*pubproto.TriggerNodeUpdateResponse, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
return &pubproto.TriggerNodeUpdateResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-13 06:39:55 -04:00
|
|
|
func (n *stubPeer) JoinCluster(ctx context.Context, in *pubproto.JoinClusterRequest) (*pubproto.JoinClusterResponse, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
return &pubproto.JoinClusterResponse{}, n.joinErr
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:26:17 -04:00
|
|
|
func (n *stubPeer) GetPeerVPNPublicKey(ctx context.Context, in *pubproto.GetPeerVPNPublicKeyRequest) (*pubproto.GetPeerVPNPublicKeyResponse, error) {
|
|
|
|
return &pubproto.GetPeerVPNPublicKeyResponse{CoordinatorPubKey: n.peer.VPNPubKey}, n.getPubKeyErr
|
|
|
|
}
|
|
|
|
|
2022-04-13 06:39:55 -04:00
|
|
|
func (n *stubPeer) newServer() *grpc.Server {
|
2022-05-24 10:33:44 -04:00
|
|
|
tlsConfig, err := atls.CreateAttestationServerTLSConfig(fakeIssuer{}, nil)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
server := grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsConfig)))
|
|
|
|
pubproto.RegisterAPIServer(server, n)
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubVPNAPIServer struct{}
|
|
|
|
|
|
|
|
func (stubVPNAPIServer) Listen(endpoint string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stubVPNAPIServer) Serve() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stubVPNAPIServer) Close() {
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeIssuer struct {
|
|
|
|
oid.Dummy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fakeIssuer) Issue(userData []byte, nonce []byte) ([]byte, error) {
|
|
|
|
return userData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeValidator struct {
|
|
|
|
oid.Dummy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fakeValidator) Validate(attdoc []byte, nonce []byte) ([]byte, error) {
|
|
|
|
return attdoc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubActivateAsCoordinatorServer struct {
|
|
|
|
grpc.ServerStream
|
|
|
|
sent []*pubproto.ActivateAsCoordinatorResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubActivateAsCoordinatorServer) Send(req *pubproto.ActivateAsCoordinatorResponse) error {
|
|
|
|
s.sent = append(s.sent, req)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubActivateAdditionalNodesServer struct {
|
|
|
|
grpc.ServerStream
|
|
|
|
sent []*pubproto.ActivateAdditionalNodesResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubActivateAdditionalNodesServer) Send(req *pubproto.ActivateAdditionalNodesResponse) error {
|
|
|
|
s.sent = append(s.sent, req)
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-11 09:28:41 -04:00
|
|
|
|
|
|
|
func TestRequestStateDiskKey(t *testing.T) {
|
|
|
|
defaultKey := []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|
|
|
|
someErr := errors.New("error")
|
|
|
|
testCases := map[string]struct {
|
|
|
|
state state.State
|
|
|
|
dataKey []byte
|
|
|
|
getDataKeyErr error
|
|
|
|
pushKeyErr error
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-04-11 09:28:41 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
state: state.ActivatingNodes,
|
|
|
|
dataKey: defaultKey,
|
|
|
|
},
|
|
|
|
"Coordinator in wrong state": {
|
2022-04-26 10:54:05 -04:00
|
|
|
state: state.IsNode,
|
|
|
|
dataKey: defaultKey,
|
|
|
|
wantErr: true,
|
2022-04-11 09:28:41 -04:00
|
|
|
},
|
|
|
|
"GetDataKey fails": {
|
|
|
|
state: state.ActivatingNodes,
|
|
|
|
dataKey: defaultKey,
|
|
|
|
getDataKeyErr: someErr,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-04-11 09:28:41 -04:00
|
|
|
},
|
|
|
|
"key pushing fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
state: state.ActivatingNodes,
|
|
|
|
dataKey: defaultKey,
|
|
|
|
pushKeyErr: someErr,
|
|
|
|
wantErr: true,
|
2022-04-11 09:28:41 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
issuer := core.NewMockIssuer()
|
|
|
|
|
|
|
|
stateDiskServer := &stubStateDiskServer{pushKeyErr: tc.pushKeyErr}
|
|
|
|
|
|
|
|
// we can not use a bufconn here, since we rely on grpcpeer.FromContext() to connect to the caller
|
|
|
|
listener, err := net.Listen("tcp", ":")
|
|
|
|
require.NoError(err)
|
|
|
|
defer listener.Close()
|
|
|
|
|
2022-05-24 10:33:44 -04:00
|
|
|
tlsConfig, err := atls.CreateAttestationServerTLSConfig(issuer, nil)
|
2022-04-11 09:28:41 -04:00
|
|
|
require.NoError(err)
|
|
|
|
s := grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsConfig)))
|
|
|
|
keyproto.RegisterAPIServer(s, stateDiskServer)
|
|
|
|
defer s.GracefulStop()
|
|
|
|
go s.Serve(listener)
|
|
|
|
|
|
|
|
ctx := grpcpeer.NewContext(context.Background(), &grpcpeer.Peer{Addr: listener.Addr()})
|
|
|
|
getPeerFromContext := func(ctx context.Context) (string, error) {
|
|
|
|
peer, ok := grpcpeer.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("unable to get peer from context")
|
|
|
|
}
|
|
|
|
return peer.Addr.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
core := &fakeCore{
|
|
|
|
state: tc.state,
|
|
|
|
dataKey: tc.dataKey,
|
|
|
|
getDataKeyErr: tc.getDataKeyErr,
|
|
|
|
}
|
2022-04-28 03:49:15 -04:00
|
|
|
|
|
|
|
api := New(zaptest.NewLogger(t), core, grpcutil.NewDialer(dummyValidator{}, &net.Dialer{}), nil, nil, getPeerFromContext)
|
2022-04-11 09:28:41 -04:00
|
|
|
|
|
|
|
_, err = api.RequestStateDiskKey(ctx, &pubproto.RequestStateDiskKeyRequest{})
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 09:28:41 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.dataKey, stateDiskServer.receivedRequest.StateDiskKey)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type dummyValidator struct {
|
|
|
|
oid.Dummy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d dummyValidator) Validate(attdoc []byte, nonce []byte) ([]byte, error) {
|
|
|
|
var attestation vtpm.AttestationDocument
|
|
|
|
if err := json.Unmarshal(attdoc, &attestation); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return attestation.UserData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubStateDiskServer struct {
|
|
|
|
receivedRequest *keyproto.PushStateDiskKeyRequest
|
|
|
|
pushKeyErr error
|
|
|
|
keyproto.UnimplementedAPIServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubStateDiskServer) PushStateDiskKey(ctx context.Context, in *keyproto.PushStateDiskKeyRequest) (*keyproto.PushStateDiskKeyResponse, error) {
|
|
|
|
s.receivedRequest = in
|
|
|
|
return &keyproto.PushStateDiskKeyResponse{}, s.pushKeyErr
|
|
|
|
}
|