2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2023-01-06 06:04:36 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/joinservice/joinproto"
|
2022-05-23 05:36:54 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-05-23 05:36:54 -04:00
|
|
|
kubeadmv1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
|
|
|
)
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2022-07-05 05:41:31 -04:00
|
|
|
func TestIssueJoinTicket(t *testing.T) {
|
2022-05-23 05:36:54 -04:00
|
|
|
someErr := errors.New("error")
|
|
|
|
testKey := []byte{0x1, 0x2, 0x3}
|
2022-06-02 09:58:19 -04:00
|
|
|
testCert := []byte{0x4, 0x5, 0x6}
|
2022-07-26 04:58:39 -04:00
|
|
|
measurementSecret := []byte{0x7, 0x8, 0x9}
|
|
|
|
uuid := "uuid"
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
testJoinToken := &kubeadmv1.BootstrapTokenDiscovery{
|
|
|
|
APIServerEndpoint: "192.0.2.1",
|
|
|
|
CACertHashes: []string{"hash"},
|
|
|
|
Token: "token",
|
|
|
|
}
|
|
|
|
|
2023-01-06 06:04:36 -05:00
|
|
|
clusterComponents := components.Components{
|
2022-11-23 04:29:36 -05:00
|
|
|
{
|
2023-12-11 02:08:55 -05:00
|
|
|
Url: "URL",
|
2022-11-23 04:29:36 -05:00
|
|
|
Hash: "hash",
|
|
|
|
InstallPath: "install-path",
|
|
|
|
Extract: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
testCases := map[string]struct {
|
2022-11-23 04:29:36 -05:00
|
|
|
isControlPlane bool
|
|
|
|
kubeadm stubTokenGetter
|
|
|
|
kms stubKeyGetter
|
|
|
|
ca stubCA
|
|
|
|
kubeClient stubKubeClient
|
|
|
|
missingComponentsReferenceFile bool
|
|
|
|
wantErr bool
|
2022-05-23 05:36:54 -04:00
|
|
|
}{
|
2022-07-05 05:41:31 -04:00
|
|
|
"worker node": {
|
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken},
|
2022-07-26 04:58:39 -04:00
|
|
|
kms: stubKeyGetter{dataKeys: map[string][]byte{
|
|
|
|
uuid: testKey,
|
|
|
|
attestation.MeasurementSecretContext: measurementSecret,
|
|
|
|
}},
|
2022-12-06 12:48:01 -05:00
|
|
|
ca: stubCA{cert: testCert, nodeName: "node"},
|
2023-01-06 06:04:36 -05:00
|
|
|
kubeClient: stubKubeClient{getComponentsVal: clusterComponents, getK8sComponentsRefFromNodeVersionCRDVal: "k8s-components-ref"},
|
2022-11-23 04:29:36 -05:00
|
|
|
},
|
|
|
|
"kubeclient fails": {
|
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken},
|
|
|
|
kms: stubKeyGetter{dataKeys: map[string][]byte{
|
|
|
|
uuid: testKey,
|
|
|
|
attestation.MeasurementSecretContext: measurementSecret,
|
|
|
|
}},
|
2022-12-06 12:48:01 -05:00
|
|
|
ca: stubCA{cert: testCert, nodeName: "node"},
|
2022-11-23 04:29:36 -05:00
|
|
|
kubeClient: stubKubeClient{getComponentsErr: someErr},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-12-06 12:48:01 -05:00
|
|
|
"Getting Node Name from CSR fails": {
|
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken},
|
|
|
|
kms: stubKeyGetter{dataKeys: map[string][]byte{
|
|
|
|
uuid: testKey,
|
|
|
|
attestation.MeasurementSecretContext: measurementSecret,
|
|
|
|
}},
|
|
|
|
ca: stubCA{cert: testCert, nodeName: "node", getNameErr: someErr},
|
2023-01-06 06:04:36 -05:00
|
|
|
kubeClient: stubKubeClient{getComponentsVal: clusterComponents, getK8sComponentsRefFromNodeVersionCRDVal: "k8s-components-ref"},
|
2022-12-06 12:48:01 -05:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"Cannot add node to JoiningNode CRD": {
|
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken},
|
|
|
|
kms: stubKeyGetter{dataKeys: map[string][]byte{
|
|
|
|
uuid: testKey,
|
|
|
|
attestation.MeasurementSecretContext: measurementSecret,
|
|
|
|
}},
|
|
|
|
ca: stubCA{cert: testCert, nodeName: "node"},
|
2023-01-06 06:04:36 -05:00
|
|
|
kubeClient: stubKubeClient{getComponentsVal: clusterComponents, addNodeToJoiningNodesErr: someErr, getK8sComponentsRefFromNodeVersionCRDVal: "k8s-components-ref"},
|
2022-12-06 12:48:01 -05:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-11-23 04:29:36 -05:00
|
|
|
"GetDataKey fails": {
|
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken},
|
|
|
|
kms: stubKeyGetter{dataKeys: make(map[string][]byte), getDataKeyErr: someErr},
|
2022-12-06 12:48:01 -05:00
|
|
|
ca: stubCA{cert: testCert, nodeName: "node"},
|
2023-01-06 06:04:36 -05:00
|
|
|
kubeClient: stubKubeClient{getComponentsVal: clusterComponents, getK8sComponentsRefFromNodeVersionCRDVal: "k8s-components-ref"},
|
2022-11-23 04:29:36 -05:00
|
|
|
wantErr: true,
|
2022-05-23 05:36:54 -04:00
|
|
|
},
|
|
|
|
"GetJoinToken fails": {
|
2022-07-05 05:41:31 -04:00
|
|
|
kubeadm: stubTokenGetter{getJoinTokenErr: someErr},
|
2022-07-26 04:58:39 -04:00
|
|
|
kms: stubKeyGetter{dataKeys: map[string][]byte{
|
|
|
|
uuid: testKey,
|
|
|
|
attestation.MeasurementSecretContext: measurementSecret,
|
|
|
|
}},
|
2022-12-06 12:48:01 -05:00
|
|
|
ca: stubCA{cert: testCert, nodeName: "node"},
|
2023-01-06 06:04:36 -05:00
|
|
|
kubeClient: stubKubeClient{getComponentsVal: clusterComponents, getK8sComponentsRefFromNodeVersionCRDVal: "k8s-components-ref"},
|
2022-11-23 04:29:36 -05:00
|
|
|
wantErr: true,
|
2022-06-02 09:58:19 -04:00
|
|
|
},
|
|
|
|
"GetCertificate fails": {
|
2022-07-05 05:41:31 -04:00
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken},
|
2022-07-26 04:58:39 -04:00
|
|
|
kms: stubKeyGetter{dataKeys: map[string][]byte{
|
|
|
|
uuid: testKey,
|
|
|
|
attestation.MeasurementSecretContext: measurementSecret,
|
|
|
|
}},
|
2022-12-06 12:48:01 -05:00
|
|
|
ca: stubCA{getCertErr: someErr, nodeName: "node"},
|
2023-01-06 06:04:36 -05:00
|
|
|
kubeClient: stubKubeClient{getComponentsVal: clusterComponents, getK8sComponentsRefFromNodeVersionCRDVal: "k8s-components-ref"},
|
2022-11-23 04:29:36 -05:00
|
|
|
wantErr: true,
|
2022-05-23 05:36:54 -04:00
|
|
|
},
|
2022-07-05 05:41:31 -04:00
|
|
|
"control plane": {
|
|
|
|
isControlPlane: true,
|
2022-07-11 07:29:22 -04:00
|
|
|
kubeadm: stubTokenGetter{
|
|
|
|
token: testJoinToken,
|
|
|
|
files: map[string][]byte{"test": {0x1, 0x2, 0x3}},
|
|
|
|
},
|
2022-07-26 04:58:39 -04:00
|
|
|
kms: stubKeyGetter{dataKeys: map[string][]byte{
|
|
|
|
uuid: testKey,
|
|
|
|
attestation.MeasurementSecretContext: measurementSecret,
|
|
|
|
}},
|
2022-12-06 12:48:01 -05:00
|
|
|
ca: stubCA{cert: testCert, nodeName: "node"},
|
2023-01-06 06:04:36 -05:00
|
|
|
kubeClient: stubKubeClient{getComponentsVal: clusterComponents, getK8sComponentsRefFromNodeVersionCRDVal: "k8s-components-ref"},
|
2022-07-05 05:41:31 -04:00
|
|
|
},
|
|
|
|
"GetControlPlaneCertificateKey fails": {
|
|
|
|
isControlPlane: true,
|
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken, certificateKeyErr: someErr},
|
2022-07-26 04:58:39 -04:00
|
|
|
kms: stubKeyGetter{dataKeys: map[string][]byte{
|
|
|
|
uuid: testKey,
|
|
|
|
attestation.MeasurementSecretContext: measurementSecret,
|
|
|
|
}},
|
2022-12-06 12:48:01 -05:00
|
|
|
ca: stubCA{cert: testCert, nodeName: "node"},
|
2023-01-06 06:04:36 -05:00
|
|
|
kubeClient: stubKubeClient{getComponentsVal: clusterComponents, getK8sComponentsRefFromNodeVersionCRDVal: "k8s-components-ref"},
|
2022-11-23 04:29:36 -05:00
|
|
|
wantErr: true,
|
2022-07-05 05:41:31 -04:00
|
|
|
},
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
salt := []byte{0xA, 0xB, 0xC}
|
2022-07-18 06:28:02 -04:00
|
|
|
|
2022-11-23 04:29:36 -05:00
|
|
|
api := Server{
|
|
|
|
measurementSalt: salt,
|
|
|
|
ca: tc.ca,
|
|
|
|
joinTokenGetter: tc.kubeadm,
|
|
|
|
dataKeyGetter: tc.kms,
|
2022-12-06 12:48:01 -05:00
|
|
|
kubeClient: &tc.kubeClient,
|
2022-11-23 04:29:36 -05:00
|
|
|
log: logger.NewTest(t),
|
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
|
2022-07-05 05:41:31 -04:00
|
|
|
req := &joinproto.IssueJoinTicketRequest{
|
|
|
|
DiskUuid: "uuid",
|
|
|
|
IsControlPlane: tc.isControlPlane,
|
2022-06-21 05:10:32 -04:00
|
|
|
}
|
2022-07-05 05:41:31 -04:00
|
|
|
resp, err := api.IssueJoinTicket(context.Background(), req)
|
2022-06-21 05:10:32 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(err)
|
2022-07-26 04:58:39 -04:00
|
|
|
assert.Equal(tc.kms.dataKeys[uuid], resp.StateDiskKey)
|
|
|
|
assert.Equal(salt, resp.MeasurementSalt)
|
|
|
|
assert.Equal(tc.kms.dataKeys[attestation.MeasurementSecretContext], resp.MeasurementSecret)
|
2022-06-21 05:10:32 -04:00
|
|
|
assert.Equal(tc.kubeadm.token.APIServerEndpoint, resp.ApiServerEndpoint)
|
|
|
|
assert.Equal(tc.kubeadm.token.CACertHashes[0], resp.DiscoveryTokenCaCertHash)
|
|
|
|
assert.Equal(tc.kubeadm.token.Token, resp.Token)
|
|
|
|
assert.Equal(tc.ca.cert, resp.KubeletCert)
|
2023-12-11 02:08:55 -05:00
|
|
|
assert.Equal(tc.kubeClient.getComponentsVal, resp.KubernetesComponents)
|
2022-12-06 12:48:01 -05:00
|
|
|
assert.Equal(tc.ca.nodeName, tc.kubeClient.joiningNodeName)
|
2023-01-03 06:09:53 -05:00
|
|
|
assert.Equal(tc.kubeClient.getK8sComponentsRefFromNodeVersionCRDVal, tc.kubeClient.componentsRef)
|
2022-06-21 05:10:32 -04:00
|
|
|
|
2022-07-05 05:41:31 -04:00
|
|
|
if tc.isControlPlane {
|
2022-07-11 07:29:22 -04:00
|
|
|
assert.Len(resp.ControlPlaneFiles, len(tc.kubeadm.files))
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
func TestIssueRejoinTicker(t *testing.T) {
|
|
|
|
uuid := "uuid"
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
keyGetter stubKeyGetter
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
keyGetter: stubKeyGetter{
|
|
|
|
dataKeys: map[string][]byte{
|
|
|
|
uuid: {0x1, 0x2, 0x3},
|
|
|
|
attestation.MeasurementSecretContext: {0x4, 0x5, 0x6},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"failure": {
|
|
|
|
keyGetter: stubKeyGetter{
|
|
|
|
dataKeys: make(map[string][]byte),
|
|
|
|
getDataKeyErr: errors.New("error"),
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-11-23 04:29:36 -05:00
|
|
|
api := Server{
|
|
|
|
ca: stubCA{},
|
|
|
|
joinTokenGetter: stubTokenGetter{},
|
|
|
|
dataKeyGetter: tc.keyGetter,
|
|
|
|
log: logger.NewTest(t),
|
|
|
|
}
|
2022-07-26 04:58:39 -04:00
|
|
|
|
|
|
|
req := &joinproto.IssueRejoinTicketRequest{
|
|
|
|
DiskUuid: uuid,
|
|
|
|
}
|
|
|
|
resp, err := api.IssueRejoinTicket(context.Background(), req)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.keyGetter.dataKeys[attestation.MeasurementSecretContext], resp.MeasurementSecret)
|
|
|
|
assert.Equal(tc.keyGetter.dataKeys[uuid], resp.StateDiskKey)
|
|
|
|
})
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubTokenGetter struct {
|
2022-06-21 05:10:32 -04:00
|
|
|
token *kubeadmv1.BootstrapTokenDiscovery
|
|
|
|
getJoinTokenErr error
|
2022-07-11 07:29:22 -04:00
|
|
|
files map[string][]byte
|
2022-06-21 05:10:32 -04:00
|
|
|
certificateKeyErr error
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f stubTokenGetter) GetJoinToken(time.Duration) (*kubeadmv1.BootstrapTokenDiscovery, error) {
|
|
|
|
return f.token, f.getJoinTokenErr
|
|
|
|
}
|
|
|
|
|
2022-07-11 07:29:22 -04:00
|
|
|
func (f stubTokenGetter) GetControlPlaneCertificatesAndKeys() (map[string][]byte, error) {
|
|
|
|
return f.files, f.certificateKeyErr
|
2022-06-21 05:10:32 -04:00
|
|
|
}
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
type stubKeyGetter struct {
|
2022-07-26 04:58:39 -04:00
|
|
|
dataKeys map[string][]byte
|
2022-05-23 05:36:54 -04:00
|
|
|
getDataKeyErr error
|
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
func (f stubKeyGetter) GetDataKey(_ context.Context, name string, _ int) ([]byte, error) {
|
|
|
|
return f.dataKeys[name], f.getDataKeyErr
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
2022-06-02 09:58:19 -04:00
|
|
|
|
|
|
|
type stubCA struct {
|
|
|
|
cert []byte
|
|
|
|
getCertErr error
|
2022-12-06 12:48:01 -05:00
|
|
|
nodeName string
|
|
|
|
getNameErr error
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (f stubCA) GetCertificate(_ []byte) ([]byte, error) {
|
2022-07-15 03:33:11 -04:00
|
|
|
return f.cert, f.getCertErr
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|
2022-11-23 04:29:36 -05:00
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (f stubCA) GetNodeNameFromCSR(_ []byte) (string, error) {
|
2022-12-06 12:48:01 -05:00
|
|
|
return f.nodeName, f.getNameErr
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:29:36 -05:00
|
|
|
type stubKubeClient struct {
|
2023-12-11 02:08:55 -05:00
|
|
|
getComponentsVal []*components.Component
|
2022-11-23 04:29:36 -05:00
|
|
|
getComponentsErr error
|
|
|
|
|
2023-01-03 06:09:53 -05:00
|
|
|
getK8sComponentsRefFromNodeVersionCRDErr error
|
|
|
|
getK8sComponentsRefFromNodeVersionCRDVal string
|
|
|
|
|
2022-12-29 08:51:26 -05:00
|
|
|
addNodeToJoiningNodesErr error
|
|
|
|
joiningNodeName string
|
2023-01-03 06:09:53 -05:00
|
|
|
componentsRef string
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubKubeClient) GetK8sComponentsRefFromNodeVersionCRD(_ context.Context, _ string) (string, error) {
|
2023-01-03 06:09:53 -05:00
|
|
|
return s.getK8sComponentsRefFromNodeVersionCRDVal, s.getK8sComponentsRefFromNodeVersionCRDErr
|
2022-11-23 04:29:36 -05:00
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubKubeClient) GetComponents(_ context.Context, _ string) (components.Components, error) {
|
2022-11-23 04:29:36 -05:00
|
|
|
return s.getComponentsVal, s.getComponentsErr
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func (s *stubKubeClient) AddNodeToJoiningNodes(_ context.Context, nodeName string, componentsRef string, _ bool) error {
|
2022-12-06 12:48:01 -05:00
|
|
|
s.joiningNodeName = nodeName
|
2023-01-03 06:09:53 -05:00
|
|
|
s.componentsRef = componentsRef
|
2022-12-06 12:48:01 -05:00
|
|
|
return s.addNodeToJoiningNodesErr
|
2022-11-23 04:29:36 -05:00
|
|
|
}
|