2022-05-23 05:36:54 -04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-06-15 10:00:48 -04:00
|
|
|
"path/filepath"
|
2022-05-23 05:36:54 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-06-15 10:00:48 -04:00
|
|
|
attestationtypes "github.com/edgelesssys/constellation/internal/attestation/types"
|
2022-05-23 05:36:54 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-06-28 10:51:30 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/logger"
|
2022-07-05 05:41:31 -04:00
|
|
|
"github.com/edgelesssys/constellation/joinservice/joinproto"
|
2022-05-23 05:36:54 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"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-06-15 10:00:48 -04:00
|
|
|
testID := attestationtypes.ID{
|
2022-05-23 05:36:54 -04:00
|
|
|
Owner: []byte{0x4, 0x5, 0x6},
|
|
|
|
Cluster: []byte{0x7, 0x8, 0x9},
|
|
|
|
}
|
|
|
|
testJoinToken := &kubeadmv1.BootstrapTokenDiscovery{
|
|
|
|
APIServerEndpoint: "192.0.2.1",
|
|
|
|
CACertHashes: []string{"hash"},
|
|
|
|
Token: "token",
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-07-05 05:41:31 -04:00
|
|
|
isControlPlane bool
|
|
|
|
kubeadm stubTokenGetter
|
|
|
|
kms stubKeyGetter
|
|
|
|
ca stubCA
|
|
|
|
id []byte
|
|
|
|
wantErr bool
|
2022-05-23 05:36:54 -04:00
|
|
|
}{
|
2022-07-05 05:41:31 -04:00
|
|
|
"worker node": {
|
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken},
|
|
|
|
kms: stubKeyGetter{dataKey: testKey},
|
2022-07-15 03:33:11 -04:00
|
|
|
ca: stubCA{cert: testCert},
|
2022-07-05 05:41:31 -04:00
|
|
|
id: mustMarshalID(testID),
|
2022-05-23 05:36:54 -04:00
|
|
|
},
|
|
|
|
"GetDataKey fails": {
|
2022-07-05 05:41:31 -04:00
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken},
|
|
|
|
kms: stubKeyGetter{getDataKeyErr: someErr},
|
2022-07-15 03:33:11 -04:00
|
|
|
ca: stubCA{cert: testCert},
|
2022-05-23 05:36:54 -04:00
|
|
|
id: mustMarshalID(testID),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"loading IDs fails": {
|
2022-07-05 05:41:31 -04:00
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken},
|
|
|
|
kms: stubKeyGetter{dataKey: testKey},
|
2022-07-15 03:33:11 -04:00
|
|
|
ca: stubCA{cert: testCert},
|
2022-05-23 05:36:54 -04:00
|
|
|
id: []byte{0x1, 0x2, 0x3},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"no ID file": {
|
2022-07-05 05:41:31 -04:00
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken},
|
|
|
|
kms: stubKeyGetter{dataKey: testKey},
|
2022-07-15 03:33:11 -04:00
|
|
|
ca: stubCA{cert: testCert},
|
2022-05-23 05:36:54 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"GetJoinToken fails": {
|
2022-07-05 05:41:31 -04:00
|
|
|
kubeadm: stubTokenGetter{getJoinTokenErr: someErr},
|
|
|
|
kms: stubKeyGetter{dataKey: testKey},
|
2022-07-15 03:33:11 -04:00
|
|
|
ca: stubCA{cert: testCert},
|
2022-06-02 09:58:19 -04:00
|
|
|
id: mustMarshalID(testID),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"GetCertificate fails": {
|
2022-07-05 05:41:31 -04:00
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken},
|
|
|
|
kms: stubKeyGetter{dataKey: testKey},
|
|
|
|
ca: stubCA{getCertErr: someErr},
|
2022-05-23 05:36:54 -04:00
|
|
|
id: mustMarshalID(testID),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
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}},
|
|
|
|
},
|
|
|
|
kms: stubKeyGetter{dataKey: testKey},
|
2022-07-15 03:33:11 -04:00
|
|
|
ca: stubCA{cert: testCert},
|
2022-07-11 07:29:22 -04:00
|
|
|
id: mustMarshalID(testID),
|
2022-07-05 05:41:31 -04:00
|
|
|
},
|
|
|
|
"GetControlPlaneCertificateKey fails": {
|
|
|
|
isControlPlane: true,
|
|
|
|
kubeadm: stubTokenGetter{token: testJoinToken, certificateKeyErr: someErr},
|
|
|
|
kms: stubKeyGetter{dataKey: testKey},
|
2022-07-15 03:33:11 -04:00
|
|
|
ca: stubCA{cert: testCert},
|
2022-07-05 05:41:31 -04:00
|
|
|
id: mustMarshalID(testID),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
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)
|
|
|
|
|
|
|
|
file := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
if len(tc.id) > 0 {
|
2022-06-29 10:13:01 -04:00
|
|
|
require.NoError(file.Write(filepath.Join(constants.ServiceBasePath, constants.IDFilename), tc.id, 0o644))
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
2022-06-28 10:51:30 -04:00
|
|
|
api := New(
|
|
|
|
logger.NewTest(t),
|
|
|
|
file,
|
|
|
|
tc.ca,
|
|
|
|
tc.kubeadm,
|
|
|
|
tc.kms,
|
|
|
|
)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
var expectedIDs attestationtypes.ID
|
|
|
|
require.NoError(json.Unmarshal(tc.id, &expectedIDs))
|
|
|
|
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.kms.dataKey, resp.StateDiskKey)
|
|
|
|
assert.Equal(expectedIDs.Cluster, resp.ClusterId)
|
|
|
|
assert.Equal(expectedIDs.Owner, resp.OwnerId)
|
|
|
|
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)
|
|
|
|
|
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-06-15 10:00:48 -04:00
|
|
|
func mustMarshalID(id attestationtypes.ID) []byte {
|
2022-05-23 05:36:54 -04:00
|
|
|
b, err := json.Marshal(id)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
dataKey []byte
|
|
|
|
getDataKeyErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f stubKeyGetter) GetDataKey(context.Context, string, int) ([]byte, error) {
|
|
|
|
return f.dataKey, f.getDataKeyErr
|
|
|
|
}
|
2022-06-02 09:58:19 -04:00
|
|
|
|
|
|
|
type stubCA struct {
|
|
|
|
cert []byte
|
|
|
|
getCertErr error
|
|
|
|
}
|
|
|
|
|
2022-07-15 03:33:11 -04:00
|
|
|
func (f stubCA) GetCertificate(csr []byte) ([]byte, error) {
|
|
|
|
return f.cert, f.getCertErr
|
2022-06-02 09:58:19 -04:00
|
|
|
}
|