2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-13 05:40:27 -04:00
|
|
|
package atlscredentials
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/asn1"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/bootstrapper/initproto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
2022-06-13 05:40:27 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/goleak"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/test/bufconn"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2024-01-16 11:45:35 -05:00
|
|
|
goleak.VerifyTestMain(m, goleak.IgnoreAnyFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"))
|
2022-06-13 05:40:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestATLSCredentials(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
oid := fakeOID{1, 3, 9900, 1}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create servers
|
|
|
|
//
|
|
|
|
|
|
|
|
serverCreds := New(fakeIssuer{fakeOID: oid}, nil)
|
|
|
|
|
|
|
|
const serverCount = 15
|
|
|
|
var listeners []*bufconn.Listener
|
|
|
|
for i := 0; i < serverCount; i++ {
|
|
|
|
api := &fakeAPI{}
|
|
|
|
server := grpc.NewServer(grpc.Creds(serverCreds))
|
2022-06-21 11:59:12 -04:00
|
|
|
initproto.RegisterAPIServer(server, api)
|
2022-06-13 05:40:27 -04:00
|
|
|
|
|
|
|
listener := bufconn.Listen(1024)
|
|
|
|
listeners = append(listeners, listener)
|
|
|
|
|
|
|
|
defer server.GracefulStop()
|
|
|
|
go server.Serve(listener)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Dial concurrently
|
|
|
|
//
|
|
|
|
|
|
|
|
clientCreds := New(nil, []atls.Validator{fakeValidator{fakeOID: oid}})
|
|
|
|
|
|
|
|
errChan := make(chan error, serverCount)
|
|
|
|
for _, listener := range listeners {
|
|
|
|
lis := listener
|
|
|
|
go func() {
|
|
|
|
var err error
|
|
|
|
defer func() { errChan <- err }()
|
2024-02-21 07:30:31 -05:00
|
|
|
conn, err := grpc.DialContext(context.Background(), "", grpc.WithContextDialer(func(_ context.Context, _ string) (net.Conn, error) {
|
2022-06-13 05:40:27 -04:00
|
|
|
return lis.Dial()
|
|
|
|
}), grpc.WithTransportCredentials(clientCreds))
|
|
|
|
require.NoError(err)
|
|
|
|
defer conn.Close()
|
|
|
|
|
2022-06-21 11:59:12 -04:00
|
|
|
client := initproto.NewAPIClient(conn)
|
|
|
|
_, err = client.Init(context.Background(), &initproto.InitRequest{})
|
2022-06-13 05:40:27 -04:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < serverCount; i++ {
|
|
|
|
assert.NoError(<-errChan)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeIssuer struct {
|
|
|
|
fakeOID
|
|
|
|
}
|
|
|
|
|
2023-03-29 03:06:10 -04:00
|
|
|
func (fakeIssuer) Issue(_ context.Context, userData []byte, nonce []byte) ([]byte, error) {
|
2022-06-13 05:40:27 -04:00
|
|
|
return json.Marshal(fakeDoc{UserData: userData, Nonce: nonce})
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeValidator struct {
|
|
|
|
fakeOID
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2023-03-29 03:06:10 -04:00
|
|
|
func (v fakeValidator) Validate(_ context.Context, attDoc []byte, nonce []byte) ([]byte, error) {
|
2022-06-13 05:40:27 -04:00
|
|
|
var doc fakeDoc
|
|
|
|
if err := json.Unmarshal(attDoc, &doc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !bytes.Equal(doc.Nonce, nonce) {
|
|
|
|
return nil, errors.New("invalid nonce")
|
|
|
|
}
|
|
|
|
return doc.UserData, v.err
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeOID asn1.ObjectIdentifier
|
|
|
|
|
|
|
|
func (o fakeOID) OID() asn1.ObjectIdentifier {
|
|
|
|
return asn1.ObjectIdentifier(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeDoc struct {
|
|
|
|
UserData []byte
|
|
|
|
Nonce []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeAPI struct {
|
2022-06-21 11:59:12 -04:00
|
|
|
initproto.UnimplementedAPIServer
|
2022-06-13 05:40:27 -04:00
|
|
|
}
|
|
|
|
|
2023-05-30 07:47:36 -04:00
|
|
|
func (f *fakeAPI) Init(_ *initproto.InitRequest, stream initproto.API_InitServer) error {
|
|
|
|
_ = stream.Send(&initproto.InitResponse{})
|
|
|
|
return nil
|
2022-06-13 05:40:27 -04:00
|
|
|
}
|