2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
2022-07-01 04:57:29 -04:00
|
|
|
"encoding/json"
|
2022-07-05 08:14:11 -04:00
|
|
|
"errors"
|
2022-06-21 11:59:12 -04:00
|
|
|
"net"
|
2022-03-22 11:03:15 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/initproto"
|
2022-08-09 08:04:40 -04:00
|
|
|
"github.com/edgelesssys/constellation/cli/internal/cloudcmd"
|
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-06-08 02:17:52 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudtypes"
|
2022-08-09 08:04:40 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/config"
|
2022-04-06 04:36:58 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-05-16 11:32:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-06-21 11:59:12 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/grpc/atlscredentials"
|
|
|
|
"github.com/edgelesssys/constellation/internal/grpc/dialer"
|
|
|
|
"github.com/edgelesssys/constellation/internal/grpc/testdialer"
|
2022-08-09 08:04:40 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/oid"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/state"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-21 11:59:12 -04:00
|
|
|
"google.golang.org/grpc"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInitArgumentValidation(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
cmd := NewInitCmd()
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NoError(cmd.ValidateArgs(nil))
|
|
|
|
assert.Error(cmd.ValidateArgs([]string{"something"}))
|
|
|
|
assert.Error(cmd.ValidateArgs([]string{"sth", "sth"}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitialize(t *testing.T) {
|
|
|
|
testGcpState := state.ConstellationState{
|
2022-07-08 04:59:59 -04:00
|
|
|
CloudProvider: "GCP",
|
|
|
|
BootstrapperHost: "192.0.2.1",
|
2022-06-29 09:26:29 -04:00
|
|
|
GCPWorkers: cloudtypes.Instances{
|
2022-04-13 09:01:02 -04:00
|
|
|
"id-0": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
"id-1": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-06-29 09:26:29 -04:00
|
|
|
GCPControlPlanes: cloudtypes.Instances{
|
2022-04-13 09:01:02 -04:00
|
|
|
"id-c": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
testAzureState := state.ConstellationState{
|
2022-07-08 04:59:59 -04:00
|
|
|
CloudProvider: "Azure",
|
|
|
|
BootstrapperHost: "192.0.2.1",
|
2022-06-29 09:26:29 -04:00
|
|
|
AzureWorkers: cloudtypes.Instances{
|
2022-04-13 09:01:02 -04:00
|
|
|
"id-0": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
"id-1": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-06-29 09:26:29 -04:00
|
|
|
AzureControlPlane: cloudtypes.Instances{
|
2022-04-13 09:01:02 -04:00
|
|
|
"id-c": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
AzureResourceGroup: "test",
|
|
|
|
}
|
2022-05-02 04:54:54 -04:00
|
|
|
testQemuState := state.ConstellationState{
|
2022-07-08 04:59:59 -04:00
|
|
|
CloudProvider: "QEMU",
|
|
|
|
BootstrapperHost: "192.0.2.1",
|
2022-06-29 09:26:29 -04:00
|
|
|
QEMUWorkers: cloudtypes.Instances{
|
2022-05-02 04:54:54 -04:00
|
|
|
"id-0": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
"id-1": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
},
|
2022-06-29 09:26:29 -04:00
|
|
|
QEMUControlPlane: cloudtypes.Instances{
|
2022-05-02 04:54:54 -04:00
|
|
|
"id-c": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
},
|
|
|
|
}
|
2022-06-21 11:59:12 -04:00
|
|
|
testInitResp := &initproto.InitResponse{
|
|
|
|
Kubeconfig: []byte("kubeconfig"),
|
|
|
|
OwnerId: []byte("ownerID"),
|
|
|
|
ClusterId: []byte("clusterID"),
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-07-05 08:14:11 -04:00
|
|
|
someErr := errors.New("failed")
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
existingState state.ConstellationState
|
|
|
|
serviceAccountCreator stubServiceAccountCreator
|
2022-06-21 11:59:12 -04:00
|
|
|
initServerAPI *stubInitServer
|
|
|
|
setAutoscaleFlag bool
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"initialize some gcp instances": {
|
|
|
|
existingState: testGcpState,
|
2022-06-21 11:59:12 -04:00
|
|
|
initServerAPI: &stubInitServer{initResp: testInitResp},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"initialize some azure instances": {
|
|
|
|
existingState: testAzureState,
|
2022-06-21 11:59:12 -04:00
|
|
|
initServerAPI: &stubInitServer{initResp: testInitResp},
|
2022-04-12 08:20:46 -04:00
|
|
|
},
|
2022-05-02 04:54:54 -04:00
|
|
|
"initialize some qemu instances": {
|
|
|
|
existingState: testQemuState,
|
2022-06-21 11:59:12 -04:00
|
|
|
initServerAPI: &stubInitServer{initResp: testInitResp},
|
|
|
|
},
|
|
|
|
"initialize gcp with autoscaling": {
|
|
|
|
existingState: testGcpState,
|
|
|
|
initServerAPI: &stubInitServer{initResp: testInitResp},
|
|
|
|
setAutoscaleFlag: true,
|
|
|
|
},
|
|
|
|
"initialize azure with autoscaling": {
|
|
|
|
existingState: testAzureState,
|
|
|
|
initServerAPI: &stubInitServer{initResp: testInitResp},
|
|
|
|
setAutoscaleFlag: true,
|
|
|
|
},
|
2022-07-05 08:14:11 -04:00
|
|
|
"empty state": {
|
|
|
|
existingState: state.ConstellationState{},
|
|
|
|
initServerAPI: &stubInitServer{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"init call fails": {
|
|
|
|
existingState: testGcpState,
|
|
|
|
initServerAPI: &stubInitServer{initErr: someErr},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"fail to create service account": {
|
|
|
|
existingState: testGcpState,
|
|
|
|
initServerAPI: &stubInitServer{},
|
|
|
|
serviceAccountCreator: stubServiceAccountCreator{createErr: someErr},
|
|
|
|
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)
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
netDialer := testdialer.NewBufconnDialer()
|
2022-08-09 08:04:40 -04:00
|
|
|
newDialer := func(*cloudcmd.Validator) *dialer.Dialer {
|
|
|
|
return dialer.New(nil, nil, netDialer)
|
|
|
|
}
|
2022-06-21 11:59:12 -04:00
|
|
|
serverCreds := atlscredentials.New(nil, nil)
|
|
|
|
initServer := grpc.NewServer(grpc.Creds(serverCreds))
|
|
|
|
initproto.RegisterAPIServer(initServer, tc.initServerAPI)
|
2022-06-29 09:26:29 -04:00
|
|
|
port := strconv.Itoa(constants.BootstrapperPort)
|
2022-06-21 11:59:12 -04:00
|
|
|
listener := netDialer.GetListener(net.JoinHostPort("192.0.2.1", port))
|
|
|
|
go initServer.Serve(listener)
|
|
|
|
defer initServer.GracefulStop()
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
cmd := NewInitCmd()
|
2022-03-22 11:03:15 -04:00
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.SetOut(&out)
|
|
|
|
var errOut bytes.Buffer
|
|
|
|
cmd.SetErr(&errOut)
|
2022-08-09 08:04:40 -04:00
|
|
|
cmd.Flags().String("config", "", "") // register persistent flag manually
|
2022-03-22 11:03:15 -04:00
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
fileHandler := file.NewHandler(fs)
|
2022-04-06 04:36:58 -04:00
|
|
|
require.NoError(fileHandler.WriteJSON(constants.StateFilename, tc.existingState, file.OptNone))
|
2022-06-21 11:59:12 -04:00
|
|
|
require.NoError(cmd.Flags().Set("autoscale", strconv.FormatBool(tc.setAutoscaleFlag)))
|
2022-04-12 08:20:46 -04:00
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
|
|
|
|
defer cancel()
|
2022-06-28 05:19:03 -04:00
|
|
|
cmd.SetContext(ctx)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
err := initialize(cmd, newDialer, &tc.serviceAccountCreator, fileHandler)
|
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)
|
2022-06-21 11:59:12 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-07-26 04:58:39 -04:00
|
|
|
// assert.Contains(out.String(), base64.StdEncoding.EncodeToString([]byte("ownerID")))
|
2022-07-05 08:14:11 -04:00
|
|
|
assert.Contains(out.String(), base64.StdEncoding.EncodeToString([]byte("clusterID")))
|
2022-06-21 11:59:12 -04:00
|
|
|
if tc.setAutoscaleFlag {
|
|
|
|
assert.Len(tc.initServerAPI.activateAutoscalingNodeGroups, 1)
|
2022-03-22 11:03:15 -04:00
|
|
|
} else {
|
2022-06-21 11:59:12 -04:00
|
|
|
assert.Len(tc.initServerAPI.activateAutoscalingNodeGroups, 0)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteOutput(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
resp := &initproto.InitResponse{
|
|
|
|
OwnerId: []byte("ownerID"),
|
|
|
|
ClusterId: []byte("clusterID"),
|
|
|
|
Kubeconfig: []byte("kubeconfig"),
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-07-01 04:57:29 -04:00
|
|
|
|
2022-07-05 08:14:11 -04:00
|
|
|
ownerID := base64.StdEncoding.EncodeToString(resp.OwnerId)
|
|
|
|
clusterID := base64.StdEncoding.EncodeToString(resp.ClusterId)
|
|
|
|
|
2022-07-08 04:59:59 -04:00
|
|
|
expectedIDFile := clusterIDsFile{
|
2022-07-05 08:14:11 -04:00
|
|
|
ClusterID: clusterID,
|
|
|
|
OwnerID: ownerID,
|
|
|
|
Endpoint: net.JoinHostPort("ip", strconv.Itoa(constants.VerifyServiceNodePortGRPC)),
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
var out bytes.Buffer
|
|
|
|
testFs := afero.NewMemMapFs()
|
|
|
|
fileHandler := file.NewHandler(testFs)
|
|
|
|
|
2022-07-05 08:14:11 -04:00
|
|
|
err := writeOutput(resp, "ip", &out, fileHandler)
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NoError(err)
|
2022-07-26 04:58:39 -04:00
|
|
|
// assert.Contains(out.String(), ownerID)
|
2022-07-05 08:14:11 -04:00
|
|
|
assert.Contains(out.String(), clusterID)
|
2022-06-21 11:59:12 -04:00
|
|
|
assert.Contains(out.String(), constants.AdminConfFilename)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
afs := afero.Afero{Fs: testFs}
|
2022-04-06 04:36:58 -04:00
|
|
|
adminConf, err := afs.ReadFile(constants.AdminConfFilename)
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.NoError(err)
|
2022-07-05 08:14:11 -04:00
|
|
|
assert.Equal(string(resp.Kubeconfig), string(adminConf))
|
2022-07-01 04:57:29 -04:00
|
|
|
|
2022-07-05 07:52:36 -04:00
|
|
|
idsFile, err := afs.ReadFile(constants.ClusterIDsFileName)
|
2022-07-01 04:57:29 -04:00
|
|
|
assert.NoError(err)
|
2022-07-08 04:59:59 -04:00
|
|
|
var testIDFile clusterIDsFile
|
|
|
|
err = json.Unmarshal(idsFile, &testIDFile)
|
2022-07-01 04:57:29 -04:00
|
|
|
assert.NoError(err)
|
2022-07-08 04:59:59 -04:00
|
|
|
assert.Equal(expectedIDFile, testIDFile)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitCompletion(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
args []string
|
|
|
|
toComplete string
|
|
|
|
wantResult []string
|
|
|
|
wantShellCD cobra.ShellCompDirective
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"first arg": {
|
2022-04-26 10:54:05 -04:00
|
|
|
args: []string{},
|
|
|
|
toComplete: "hello",
|
|
|
|
wantResult: []string{},
|
|
|
|
wantShellCD: cobra.ShellCompDirectiveDefault,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"secnod arg": {
|
2022-04-26 10:54:05 -04:00
|
|
|
args: []string{"23"},
|
|
|
|
toComplete: "/test/h",
|
|
|
|
wantResult: []string{},
|
|
|
|
wantShellCD: cobra.ShellCompDirectiveError,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"third arg": {
|
2022-04-26 10:54:05 -04:00
|
|
|
args: []string{"./file", "sth"},
|
|
|
|
toComplete: "./file",
|
|
|
|
wantResult: []string{},
|
|
|
|
wantShellCD: cobra.ShellCompDirectiveError,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
cmd := &cobra.Command{}
|
|
|
|
result, shellCD := initCompletion(cmd, tc.args, tc.toComplete)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantResult, result)
|
|
|
|
assert.Equal(tc.wantShellCD, shellCD)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
func TestReadOrGeneratedMasterSecret(t *testing.T) {
|
2022-03-22 11:03:15 -04:00
|
|
|
testCases := map[string]struct {
|
2022-07-29 03:52:47 -04:00
|
|
|
filename string
|
|
|
|
createFileFunc func(handler file.Handler) error
|
|
|
|
fs func() afero.Fs
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"file with secret exists": {
|
2022-07-29 03:52:47 -04:00
|
|
|
filename: "someSecret",
|
|
|
|
fs: afero.NewMemMapFs,
|
|
|
|
createFileFunc: func(handler file.Handler) error {
|
|
|
|
return handler.WriteJSON(
|
|
|
|
"someSecret",
|
|
|
|
masterSecret{Key: []byte("constellation-master-secret"), Salt: []byte("constellation-32Byte-length-salt")},
|
|
|
|
file.OptNone,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"no file given": {
|
2022-07-29 03:52:47 -04:00
|
|
|
filename: "",
|
|
|
|
createFileFunc: func(handler file.Handler) error { return nil },
|
|
|
|
fs: afero.NewMemMapFs,
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"file does not exist": {
|
2022-07-29 03:52:47 -04:00
|
|
|
filename: "nonExistingSecret",
|
|
|
|
createFileFunc: func(handler file.Handler) error { return nil },
|
|
|
|
fs: afero.NewMemMapFs,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"file is empty": {
|
2022-07-29 03:52:47 -04:00
|
|
|
filename: "emptySecret",
|
|
|
|
createFileFunc: func(handler file.Handler) error {
|
|
|
|
return handler.Write("emptySecret", []byte{}, file.OptNone)
|
|
|
|
},
|
|
|
|
fs: afero.NewMemMapFs,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-07-29 03:52:47 -04:00
|
|
|
"salt too short": {
|
|
|
|
filename: "shortSecret",
|
|
|
|
createFileFunc: func(handler file.Handler) error {
|
|
|
|
return handler.WriteJSON(
|
|
|
|
"shortSecret",
|
|
|
|
masterSecret{Key: []byte("constellation-master-secret"), Salt: []byte("short")},
|
|
|
|
file.OptNone,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
fs: afero.NewMemMapFs,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-07-29 03:52:47 -04:00
|
|
|
"key too short": {
|
|
|
|
filename: "shortSecret",
|
|
|
|
createFileFunc: func(handler file.Handler) error {
|
|
|
|
return handler.WriteJSON(
|
|
|
|
"shortSecret",
|
|
|
|
masterSecret{Key: []byte("short"), Salt: []byte("constellation-32Byte-length-salt")},
|
|
|
|
file.OptNone,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
fs: afero.NewMemMapFs,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid file content": {
|
|
|
|
filename: "unencodedSecret",
|
|
|
|
createFileFunc: func(handler file.Handler) error {
|
|
|
|
return handler.Write("unencodedSecret", []byte("invalid-constellation-master-secret"), file.OptNone)
|
|
|
|
},
|
|
|
|
fs: afero.NewMemMapFs,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"file not writeable": {
|
2022-07-29 03:52:47 -04:00
|
|
|
filename: "",
|
|
|
|
createFileFunc: func(handler file.Handler) error { return nil },
|
|
|
|
fs: func() afero.Fs { return afero.NewReadOnlyFs(afero.NewMemMapFs()) },
|
|
|
|
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)
|
|
|
|
|
|
|
|
fileHandler := file.NewHandler(tc.fs())
|
2022-07-29 03:52:47 -04:00
|
|
|
require.NoError(tc.createFileFunc(fileHandler))
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
var out bytes.Buffer
|
2022-06-09 10:10:42 -04:00
|
|
|
secret, err := readOrGenerateMasterSecret(&out, fileHandler, tc.filename)
|
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)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
if tc.filename == "" {
|
2022-04-06 04:36:58 -04:00
|
|
|
require.Contains(out.String(), constants.MasterSecretFilename)
|
2022-03-22 11:03:15 -04:00
|
|
|
filename := strings.Split(out.String(), "./")
|
|
|
|
tc.filename = strings.Trim(filename[1], "\n")
|
|
|
|
}
|
|
|
|
|
2022-07-29 03:52:47 -04:00
|
|
|
var masterSecret masterSecret
|
|
|
|
require.NoError(fileHandler.ReadJSON(tc.filename, &masterSecret))
|
|
|
|
assert.Equal(masterSecret.Key, secret.Key)
|
|
|
|
assert.Equal(masterSecret.Salt, secret.Salt)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
func TestAttestation(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
initServerAPI := &stubInitServer{initResp: &initproto.InitResponse{
|
|
|
|
Kubeconfig: []byte("kubeconfig"),
|
|
|
|
OwnerId: []byte("ownerID"),
|
|
|
|
ClusterId: []byte("clusterID"),
|
|
|
|
}}
|
|
|
|
existingState := state.ConstellationState{
|
|
|
|
CloudProvider: "QEMU",
|
|
|
|
BootstrapperHost: "192.0.2.1",
|
|
|
|
QEMUWorkers: cloudtypes.Instances{
|
|
|
|
"id-0": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
"id-1": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
},
|
|
|
|
QEMUControlPlane: cloudtypes.Instances{
|
|
|
|
"id-c": {PrivateIP: "192.0.2.1", PublicIP: "192.0.2.1"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
netDialer := testdialer.NewBufconnDialer()
|
|
|
|
newDialer := func(v *cloudcmd.Validator) *dialer.Dialer {
|
|
|
|
validator := &testValidator{
|
|
|
|
Getter: oid.QEMU{},
|
|
|
|
pcrs: v.PCRS(),
|
|
|
|
}
|
|
|
|
return dialer.New(nil, validator, netDialer)
|
|
|
|
}
|
|
|
|
|
|
|
|
issuer := &testIssuer{
|
|
|
|
Getter: oid.QEMU{},
|
|
|
|
pcrs: map[uint32][]byte{
|
|
|
|
0: []byte("ffffffffffffffffffffffffffffffff"),
|
|
|
|
1: []byte("ffffffffffffffffffffffffffffffff"),
|
|
|
|
2: []byte("ffffffffffffffffffffffffffffffff"),
|
|
|
|
3: []byte("ffffffffffffffffffffffffffffffff"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
serverCreds := atlscredentials.New(issuer, nil)
|
|
|
|
initServer := grpc.NewServer(grpc.Creds(serverCreds))
|
|
|
|
initproto.RegisterAPIServer(initServer, initServerAPI)
|
|
|
|
port := strconv.Itoa(constants.BootstrapperPort)
|
|
|
|
listener := netDialer.GetListener(net.JoinHostPort("192.0.2.1", port))
|
|
|
|
go initServer.Serve(listener)
|
|
|
|
defer initServer.GracefulStop()
|
|
|
|
|
|
|
|
cmd := NewInitCmd()
|
|
|
|
cmd.Flags().String("config", constants.ConfigFilename, "") // register persistent flag manually
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.SetOut(&out)
|
|
|
|
var errOut bytes.Buffer
|
|
|
|
cmd.SetErr(&errOut)
|
|
|
|
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
fileHandler := file.NewHandler(fs)
|
|
|
|
require.NoError(fileHandler.WriteJSON(constants.StateFilename, existingState, file.OptNone))
|
|
|
|
|
|
|
|
cfg := config.Default()
|
|
|
|
cfg.RemoveProviderExcept(cloudprovider.QEMU)
|
|
|
|
cfg.Provider.QEMU.Measurements[0] = []byte("00000000000000000000000000000000")
|
|
|
|
cfg.Provider.QEMU.Measurements[1] = []byte("11111111111111111111111111111111")
|
|
|
|
cfg.Provider.QEMU.Measurements[2] = []byte("22222222222222222222222222222222")
|
|
|
|
cfg.Provider.QEMU.Measurements[3] = []byte("33333333333333333333333333333333")
|
|
|
|
require.NoError(fileHandler.WriteYAML(constants.ConfigFilename, cfg, file.OptNone))
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
cmd.SetContext(ctx)
|
|
|
|
|
|
|
|
err := initialize(cmd, newDialer, &stubServiceAccountCreator{}, fileHandler)
|
|
|
|
assert.Error(err)
|
|
|
|
// make sure the error is actually a TLS handshake error
|
|
|
|
assert.Contains(err.Error(), "transport: authentication handshake failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
type testValidator struct {
|
|
|
|
oid.Getter
|
|
|
|
pcrs map[uint32][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *testValidator) Validate(attDoc []byte, nonce []byte) ([]byte, error) {
|
|
|
|
var attestation struct {
|
|
|
|
UserData []byte
|
|
|
|
PCRs map[uint32][]byte
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(attDoc, &attestation); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, pcr := range v.pcrs {
|
|
|
|
if !bytes.Equal(attestation.PCRs[k], pcr) {
|
|
|
|
return nil, errors.New("invalid PCR value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attestation.UserData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type testIssuer struct {
|
|
|
|
oid.Getter
|
|
|
|
pcrs map[uint32][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *testIssuer) Issue(userData []byte, nonce []byte) ([]byte, error) {
|
|
|
|
return json.Marshal(
|
|
|
|
struct {
|
|
|
|
UserData []byte
|
|
|
|
PCRs map[uint32][]byte
|
|
|
|
}{
|
|
|
|
UserData: userData,
|
|
|
|
PCRs: i.pcrs,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
type stubInitServer struct {
|
|
|
|
initResp *initproto.InitResponse
|
|
|
|
initErr error
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
activateAutoscalingNodeGroups []string
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
initproto.UnimplementedAPIServer
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-03-29 05:38:14 -04:00
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
func (s *stubInitServer) Init(ctx context.Context, req *initproto.InitRequest) (*initproto.InitResponse, error) {
|
|
|
|
s.activateAutoscalingNodeGroups = req.AutoscalingNodeGroups
|
|
|
|
return s.initResp, s.initErr
|
2022-03-29 05:38:14 -04:00
|
|
|
}
|