2022-03-22 11:03:15 -04:00
|
|
|
package pubapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-05-17 06:24:22 -04:00
|
|
|
"fmt"
|
2022-04-20 11:06:47 -04:00
|
|
|
"io"
|
2022-03-22 11:03:15 -04:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2022-04-20 11:06:47 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/atls"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/core"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/peer"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/pubapi/pubproto"
|
2022-04-13 03:18:32 -04:00
|
|
|
"github.com/edgelesssys/constellation/coordinator/role"
|
2022-03-22 11:03:15 -04:00
|
|
|
"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"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/vpnapi/vpnproto"
|
2022-05-17 06:24:22 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/deploy/ssh"
|
|
|
|
"github.com/edgelesssys/constellation/internal/deploy/user"
|
|
|
|
"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"
|
2022-04-20 11:06:47 -04:00
|
|
|
"google.golang.org/grpc/credentials"
|
2022-03-22 11:03:15 -04:00
|
|
|
kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestActivateAsNode(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
2022-04-13 06:39:55 -04:00
|
|
|
peer1 := peer.Peer{PublicIP: "192.0.2.11:2000", VPNIP: "192.0.2.21", VPNPubKey: []byte{1, 2, 3}}
|
|
|
|
peer2 := peer.Peer{PublicIP: "192.0.2.12:2000", VPNIP: "192.0.2.22", VPNPubKey: []byte{2, 3, 4}}
|
2022-05-17 06:24:22 -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-20 11:06:47 -04:00
|
|
|
initialPeers []peer.Peer
|
|
|
|
updatedPeers []peer.Peer
|
|
|
|
state state.State
|
|
|
|
getUpdateErr error
|
|
|
|
setVPNIPErr error
|
|
|
|
messageSequenceOverride []string
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
|
|
|
wantState state.State
|
2022-05-17 06:24:22 -04:00
|
|
|
sshKeys []*ssh.UserKey
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"basic": {
|
2022-04-26 10:54:05 -04:00
|
|
|
initialPeers: []peer.Peer{peer1},
|
|
|
|
updatedPeers: []peer.Peer{peer2},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
wantState: state.NodeWaitingForClusterJoin,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-05-17 06:24:22 -04:00
|
|
|
"basic with SSH users": {
|
|
|
|
initialPeers: []peer.Peer{peer1},
|
|
|
|
updatedPeers: []peer.Peer{peer2},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
wantState: state.NodeWaitingForClusterJoin,
|
|
|
|
sshKeys: []*ssh.UserKey{sshUser1, sshUser2},
|
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
"already activated": {
|
2022-04-26 10:54:05 -04:00
|
|
|
initialPeers: []peer.Peer{peer1},
|
|
|
|
updatedPeers: []peer.Peer{peer2},
|
|
|
|
state: state.IsNode,
|
|
|
|
wantErr: true,
|
|
|
|
wantState: state.IsNode,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"wrong peer kind": {
|
2022-04-26 10:54:05 -04:00
|
|
|
initialPeers: []peer.Peer{peer1},
|
|
|
|
updatedPeers: []peer.Peer{peer2},
|
|
|
|
state: state.ActivatingNodes,
|
|
|
|
wantErr: true,
|
|
|
|
wantState: state.ActivatingNodes,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"GetUpdate error": {
|
2022-04-26 10:54:05 -04:00
|
|
|
initialPeers: []peer.Peer{peer1},
|
|
|
|
updatedPeers: []peer.Peer{peer2},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
getUpdateErr: someErr,
|
|
|
|
wantState: state.NodeWaitingForClusterJoin,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"SetVPNIP error": {
|
2022-04-26 10:54:05 -04:00
|
|
|
initialPeers: []peer.Peer{peer1},
|
|
|
|
updatedPeers: []peer.Peer{peer2},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
setVPNIPErr: someErr,
|
|
|
|
wantErr: true,
|
|
|
|
wantState: state.Failed,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-20 11:06:47 -04:00
|
|
|
"no messages sent to node": {
|
|
|
|
initialPeers: []peer.Peer{peer1},
|
|
|
|
updatedPeers: []peer.Peer{peer2},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
messageSequenceOverride: []string{},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
|
|
|
wantState: state.AcceptingInit,
|
2022-04-20 11:06:47 -04:00
|
|
|
},
|
|
|
|
"only initial message sent to node": {
|
|
|
|
initialPeers: []peer.Peer{peer1},
|
|
|
|
updatedPeers: []peer.Peer{peer2},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
messageSequenceOverride: []string{"initialRequest"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
|
|
|
wantState: state.Failed,
|
2022-04-20 11:06:47 -04:00
|
|
|
},
|
|
|
|
"wrong initial message sent to node": {
|
|
|
|
initialPeers: []peer.Peer{peer1},
|
|
|
|
updatedPeers: []peer.Peer{peer2},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
messageSequenceOverride: []string{"stateDiskKey"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
|
|
|
wantState: state.AcceptingInit,
|
2022-04-20 11:06:47 -04:00
|
|
|
},
|
|
|
|
"initial message sent twice to node": {
|
|
|
|
initialPeers: []peer.Peer{peer1},
|
|
|
|
updatedPeers: []peer.Peer{peer2},
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
messageSequenceOverride: []string{"initialRequest", "initialRequest"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
|
|
|
wantState: state.Failed,
|
2022-04-20 11:06:47 -04:00
|
|
|
},
|
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)
|
|
|
|
|
2022-04-20 11:06:47 -04:00
|
|
|
const (
|
|
|
|
nodeIP = "192.0.2.2"
|
|
|
|
nodeVPNIP = "10.118.0.2"
|
|
|
|
)
|
2022-03-22 11:03:15 -04:00
|
|
|
vpnPubKey := []byte{7, 8, 9}
|
|
|
|
ownerID := []byte("ownerID")
|
|
|
|
clusterID := []byte("clusterID")
|
2022-04-20 11:06:47 -04:00
|
|
|
stateDiskKey := []byte("stateDiskKey")
|
|
|
|
messageSequence := []string{"initialRequest", "stateDiskKey"}
|
|
|
|
if tc.messageSequenceOverride != nil {
|
|
|
|
messageSequence = tc.messageSequenceOverride
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
logger := zaptest.NewLogger(t)
|
2022-05-17 06:24:22 -04:00
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
linuxUserManager := user.NewLinuxUserManagerFake(fs)
|
|
|
|
cor := &fakeCore{state: tc.state, vpnPubKey: vpnPubKey, setVPNIPErr: tc.setVPNIPErr, linuxUserManager: linuxUserManager}
|
2022-04-28 03:49:15 -04:00
|
|
|
netDialer := testdialer.NewBufconnDialer()
|
|
|
|
dialer := grpcutil.NewDialer(fakeValidator{}, netDialer)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
api := New(logger, cor, dialer, nil, nil, nil)
|
2022-03-22 11:03:15 -04:00
|
|
|
defer api.Close()
|
|
|
|
|
|
|
|
vserver := grpc.NewServer()
|
|
|
|
vapi := &stubVPNAPI{peers: tc.updatedPeers, getUpdateErr: tc.getUpdateErr}
|
|
|
|
vpnproto.RegisterAPIServer(vserver, vapi)
|
2022-04-28 03:49:15 -04:00
|
|
|
go vserver.Serve(netDialer.GetListener(net.JoinHostPort("10.118.0.1", vpnAPIPort)))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer vserver.GracefulStop()
|
|
|
|
|
2022-04-20 11:06:47 -04:00
|
|
|
tlsConfig, err := atls.CreateAttestationServerTLSConfig(&core.MockIssuer{})
|
|
|
|
require.NoError(err)
|
|
|
|
pubserver := grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsConfig)))
|
|
|
|
pubproto.RegisterAPIServer(pubserver, api)
|
2022-04-28 03:49:15 -04:00
|
|
|
go pubserver.Serve(netDialer.GetListener(net.JoinHostPort(nodeIP, endpointAVPNPort)))
|
2022-04-20 11:06:47 -04:00
|
|
|
defer pubserver.GracefulStop()
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-05-17 06:24:22 -04:00
|
|
|
_, nodeVPNPubKey, err := activateNode(require, netDialer, messageSequence, nodeIP, "9000", nodeVPNIP, peer.ToPubProto(tc.initialPeers), ownerID, clusterID, stateDiskKey, ssh.ToProtoSlice(tc.sshKeys))
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantState, cor.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)
|
|
|
|
|
2022-04-20 11:06:47 -04:00
|
|
|
assert.Equal(vpnPubKey, nodeVPNPubKey)
|
|
|
|
assert.Equal(nodeVPNIP, cor.vpnIP)
|
|
|
|
assert.Equal(ownerID, cor.ownerID)
|
|
|
|
assert.Equal(clusterID, cor.clusterID)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
api.Close() // blocks until update loop finished
|
|
|
|
|
|
|
|
if tc.getUpdateErr == nil {
|
2022-04-20 11:06:47 -04:00
|
|
|
require.Len(cor.updatedPeers, 2)
|
|
|
|
assert.Equal(tc.updatedPeers, cor.updatedPeers[1])
|
2022-03-22 11:03:15 -04:00
|
|
|
} else {
|
2022-04-20 11:06:47 -04:00
|
|
|
require.Len(cor.updatedPeers, 1)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-04-20 11:06:47 -04:00
|
|
|
assert.Equal(tc.initialPeers, cor.updatedPeers[0])
|
|
|
|
assert.Equal([]role.Role{role.Node}, cor.persistNodeStateRoles)
|
2022-05-17 06:24:22 -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 TestTriggerNodeUpdate(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
peers := []peer.Peer{
|
2022-04-13 06:39:55 -04:00
|
|
|
{PublicIP: "192.0.2.11:2000", VPNIP: "192.0.2.21", VPNPubKey: []byte{1, 2, 3}},
|
|
|
|
{PublicIP: "192.0.2.12:2000", VPNIP: "192.0.2.22", VPNPubKey: []byte{2, 3, 4}},
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
peers []peer.Peer
|
|
|
|
state state.State
|
|
|
|
getUpdateErr error
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"basic": {
|
|
|
|
peers: peers,
|
|
|
|
state: state.IsNode,
|
|
|
|
},
|
|
|
|
"not activated": {
|
2022-04-26 10:54:05 -04:00
|
|
|
peers: peers,
|
|
|
|
state: state.AcceptingInit,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"wrong peer kind": {
|
2022-04-26 10:54:05 -04:00
|
|
|
peers: peers,
|
|
|
|
state: state.ActivatingNodes,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"GetUpdate error": {
|
|
|
|
peers: peers,
|
|
|
|
state: state.IsNode,
|
|
|
|
getUpdateErr: someErr,
|
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)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
logger := zaptest.NewLogger(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
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
api := New(logger, core, dialer, nil, nil, nil)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
vserver := grpc.NewServer()
|
|
|
|
vapi := &stubVPNAPI{
|
|
|
|
peers: tc.peers,
|
|
|
|
getUpdateErr: tc.getUpdateErr,
|
|
|
|
}
|
|
|
|
vpnproto.RegisterAPIServer(vserver, vapi)
|
2022-04-28 03:49:15 -04:00
|
|
|
go vserver.Serve(netDialer.GetListener(net.JoinHostPort("10.118.0.1", vpnAPIPort)))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer vserver.GracefulStop()
|
|
|
|
|
|
|
|
_, err := api.TriggerNodeUpdate(context.Background(), &pubproto.TriggerNodeUpdateRequest{})
|
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)
|
|
|
|
|
|
|
|
// second update should be a noop
|
|
|
|
_, err = api.TriggerNodeUpdate(context.Background(), &pubproto.TriggerNodeUpdateRequest{})
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
require.Len(core.updatedPeers, 1)
|
|
|
|
assert.Equal(tc.peers, core.updatedPeers[0])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJoinCluster(t *testing.T) {
|
|
|
|
someErr := errors.New("failed")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
state state.State
|
|
|
|
getJoinArgsErr error
|
|
|
|
joinClusterErr error
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
|
|
|
wantState state.State
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"basic": {
|
2022-04-26 10:54:05 -04:00
|
|
|
state: state.NodeWaitingForClusterJoin,
|
|
|
|
wantState: state.IsNode,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"not activated": {
|
2022-04-26 10:54:05 -04:00
|
|
|
state: state.AcceptingInit,
|
|
|
|
wantErr: true,
|
|
|
|
wantState: state.AcceptingInit,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"wrong peer kind": {
|
2022-04-26 10:54:05 -04:00
|
|
|
state: state.ActivatingNodes,
|
|
|
|
wantErr: true,
|
|
|
|
wantState: state.ActivatingNodes,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"GetK8sJoinArgs error": {
|
|
|
|
state: state.NodeWaitingForClusterJoin,
|
|
|
|
getJoinArgsErr: someErr,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
|
|
|
wantState: state.NodeWaitingForClusterJoin,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"JoinCluster error": {
|
|
|
|
state: state.NodeWaitingForClusterJoin,
|
|
|
|
joinClusterErr: 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)
|
|
|
|
|
|
|
|
logger := zaptest.NewLogger(t)
|
|
|
|
core := &fakeCore{state: tc.state, joinClusterErr: tc.joinClusterErr}
|
2022-04-28 03:49:15 -04:00
|
|
|
netDialer := testdialer.NewBufconnDialer()
|
|
|
|
dialer := grpcutil.NewDialer(fakeValidator{}, netDialer)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
api := New(logger, core, dialer, nil, nil, nil)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
vserver := grpc.NewServer()
|
|
|
|
vapi := &stubVPNAPI{
|
|
|
|
joinArgs: kubeadm.BootstrapTokenDiscovery{
|
|
|
|
APIServerEndpoint: "endp",
|
|
|
|
Token: "token",
|
|
|
|
CACertHashes: []string{"dis"},
|
|
|
|
},
|
|
|
|
getJoinArgsErr: tc.getJoinArgsErr,
|
|
|
|
}
|
|
|
|
vpnproto.RegisterAPIServer(vserver, vapi)
|
2022-04-28 03:49:15 -04:00
|
|
|
go vserver.Serve(netDialer.GetListener(net.JoinHostPort("192.0.2.1", vpnAPIPort)))
|
2022-03-22 11:03:15 -04:00
|
|
|
defer vserver.GracefulStop()
|
|
|
|
|
2022-04-13 06:39:55 -04:00
|
|
|
_, err := api.JoinCluster(context.Background(), &pubproto.JoinClusterRequest{CoordinatorVpnIp: "192.0.2.1"})
|
2022-03-22 11:03:15 -04:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
assert.Equal([]kubeadm.BootstrapTokenDiscovery{vapi.joinArgs}, core.joinArgs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 06:24:22 -04:00
|
|
|
func activateNode(require *require.Assertions, dialer netDialer, messageSequence []string, nodeIP, bindPort, nodeVPNIP string, peers []*pubproto.Peer, ownerID, clusterID, stateDiskKey []byte, sshUserKeys []*pubproto.SSHUserKey) (string, []byte, error) {
|
2022-04-20 11:06:47 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
conn, err := dialGRPC(ctx, dialer, net.JoinHostPort(nodeIP, bindPort))
|
|
|
|
require.NoError(err)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
client := pubproto.NewAPIClient(conn)
|
|
|
|
stream, err := client.ActivateAsNode(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, message := range messageSequence {
|
|
|
|
switch message {
|
|
|
|
case "initialRequest":
|
|
|
|
err = stream.Send(&pubproto.ActivateAsNodeRequest{
|
|
|
|
Request: &pubproto.ActivateAsNodeRequest_InitialRequest{
|
|
|
|
InitialRequest: &pubproto.ActivateAsNodeInitialRequest{
|
2022-05-17 06:24:22 -04:00
|
|
|
NodeVpnIp: nodeVPNIP,
|
|
|
|
Peers: peers,
|
|
|
|
OwnerId: ownerID,
|
|
|
|
ClusterId: clusterID,
|
|
|
|
SshUserKeys: sshUserKeys,
|
2022-04-20 11:06:47 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
case "stateDiskKey":
|
|
|
|
err = stream.Send(&pubproto.ActivateAsNodeRequest{
|
|
|
|
Request: &pubproto.ActivateAsNodeRequest_StateDiskKey{
|
|
|
|
StateDiskKey: stateDiskKey,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("unknown message in activation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.NoError(stream.CloseSend())
|
|
|
|
|
|
|
|
diskUUIDReq, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
diskUUID := diskUUIDReq.GetStateDiskUuid()
|
|
|
|
|
|
|
|
vpnPubKeyReq, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
nodeVPNPubKey := vpnPubKeyReq.GetNodeVpnPubKey()
|
|
|
|
|
|
|
|
_, err = stream.Recv()
|
|
|
|
if err != io.EOF {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return diskUUID, nodeVPNPubKey, nil
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:49:15 -04:00
|
|
|
func dialGRPC(ctx context.Context, dialer netDialer, target string) (*grpc.ClientConn, error) {
|
2022-04-20 11:06:47 -04:00
|
|
|
tlsConfig, err := atls.CreateAttestationClientTLSConfig([]atls.Validator{&core.MockValidator{}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return grpc.DialContext(ctx, target,
|
|
|
|
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
|
|
|
return dialer.DialContext(ctx, "tcp", addr)
|
|
|
|
}),
|
|
|
|
grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
type stubVPNAPI struct {
|
2022-04-25 11:26:17 -04:00
|
|
|
peers []peer.Peer
|
|
|
|
joinArgs kubeadm.BootstrapTokenDiscovery
|
|
|
|
getUpdateErr error
|
|
|
|
getJoinArgsErr error
|
|
|
|
getK8SCertKeyErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
vpnproto.UnimplementedAPIServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *stubVPNAPI) GetUpdate(ctx context.Context, in *vpnproto.GetUpdateRequest) (*vpnproto.GetUpdateResponse, error) {
|
|
|
|
return &vpnproto.GetUpdateResponse{ResourceVersion: 1, Peers: peer.ToVPNProto(a.peers)}, a.getUpdateErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *stubVPNAPI) GetK8SJoinArgs(ctx context.Context, in *vpnproto.GetK8SJoinArgsRequest) (*vpnproto.GetK8SJoinArgsResponse, error) {
|
|
|
|
return &vpnproto.GetK8SJoinArgsResponse{
|
|
|
|
ApiServerEndpoint: a.joinArgs.APIServerEndpoint,
|
|
|
|
Token: a.joinArgs.Token,
|
|
|
|
DiscoveryTokenCaCertHash: a.joinArgs.CACertHashes[0],
|
|
|
|
}, a.getJoinArgsErr
|
|
|
|
}
|
2022-04-25 11:26:17 -04:00
|
|
|
|
|
|
|
func (a *stubVPNAPI) GetK8SCertificateKey(ctx context.Context, in *vpnproto.GetK8SCertificateKeyRequest) (*vpnproto.GetK8SCertificateKeyResponse, error) {
|
|
|
|
return &vpnproto.GetK8SCertificateKeyResponse{CertificateKey: "dummyCertKey"}, a.getK8SCertKeyErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *stubVPNAPI) newServer() *grpc.Server {
|
|
|
|
server := grpc.NewServer()
|
|
|
|
vpnproto.RegisterAPIServer(server, a)
|
|
|
|
return server
|
|
|
|
}
|
2022-04-28 03:49:15 -04:00
|
|
|
|
|
|
|
type netDialer interface {
|
|
|
|
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
|
|
|
}
|