consul/agent/grpc-middleware/handshake_test.go

88 lines
2.2 KiB
Go
Raw Permalink Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
package middleware
import (
"net"
"testing"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/credentials"
)
type fakeTransportCredentials struct {
credentials.TransportCredentials
callback func(conn net.Conn) (net.Conn, credentials.AuthInfo, error)
}
func (f fakeTransportCredentials) ServerHandshake(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
return f.callback(conn)
}
func TestGRPCMiddleware_optionalTransportCredentials_ServerHandshake(t *testing.T) {
plainConn := LabelledConn{protocol: ProtocolPlaintext}
tlsConn := LabelledConn{protocol: ProtocolTLS}
tests := []struct {
name string
conn net.Conn
callback func(conn net.Conn) (net.Conn, credentials.AuthInfo, error)
expectConn net.Conn
expectAuthInfo credentials.AuthInfo
expectErr string
}{
{
name: "plaintext_noop",
conn: plainConn,
expectConn: plainConn,
expectAuthInfo: nil,
},
{
name: "tls_with_missing_auth",
conn: tlsConn,
callback: func(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
return conn, nil, nil
},
expectConn: nil,
expectAuthInfo: nil,
expectErr: "missing auth info after handshake",
},
{
name: "tls_success",
conn: tlsConn,
callback: func(conn net.Conn) (net.Conn, credentials.AuthInfo, error) {
return conn, credentials.TLSInfo{}, nil
},
expectConn: tlsConn,
expectAuthInfo: credentials.TLSInfo{},
expectErr: "",
},
{
name: "invalid_protocol",
conn: LabelledConn{protocol: -1},
expectConn: nil,
expectAuthInfo: nil,
expectErr: "invalid protocol for grpc connection",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
creds := optionalTransportCredentials{
TransportCredentials: fakeTransportCredentials{
callback: tc.callback,
},
}
conn, authInfo, err := creds.ServerHandshake(tc.conn)
require.Equal(t, tc.expectConn, conn)
require.Equal(t, tc.expectAuthInfo, authInfo)
if tc.expectErr == "" {
require.NoError(t, err)
} else {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectErr)
}
})
}
}