Skip to content

Commit

Permalink
Fix a missing nil check and add a test for the mTLS Client builder
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm committed Nov 18, 2024
1 parent e5d69df commit 753c58d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/httphelper/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ func (provider *ManualManagementApiSecurityProvider) ValidateConfig(ctx context.

func (provider *ManualManagementApiSecurityProvider) BuildHttpClient(ctx context.Context, client client.Client, transport *http.Transport) (HttpClient, error) {
httpClient := &http.Client{Transport: transport}
if transport.TLSClientConfig != nil {
if transport != nil && transport.TLSClientConfig != nil {
return httpClient, nil
}

Expand Down
59 changes: 59 additions & 0 deletions pkg/httphelper/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@
package httphelper

import (
"context"
"crypto/x509"
"encoding/pem"
"net/http"
"os"
"path/filepath"
"testing"

api "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/client-go/kubernetes/scheme"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func helperLoadBytes(t *testing.T, name string) []byte {
Expand Down Expand Up @@ -98,3 +107,53 @@ func Test_validatePrivateKey(t *testing.T) {
t, 1, len(errs),
"Should consider an empty key as an invalid key")
}

// Create Datacenter with managementAuth set to manual and TLS enabled, test that the client is created with the correct TLS configuration using
// BuildManagementApiHttpClient method
func TestBuildMTLSClient(t *testing.T) {
require := require.New(t)
api.AddToScheme(scheme.Scheme)
decode := serializer.NewCodecFactory(scheme.Scheme).UniversalDeserializer().Decode

loadYaml := func(path string) (runtime.Object, error) {
bytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}
obj, _, err := decode(bytes, nil, nil)
return obj, err
}

clientSecret, err := loadYaml(filepath.Join("..", "..", "tests", "testdata", "mtls-certs-client.yaml"))
require.NoError(err)

serverSecret, err := loadYaml(filepath.Join("..", "..", "tests", "testdata", "mtls-certs-server.yaml"))
require.NoError(err)

dc := &api.CassandraDatacenter{
Spec: api.CassandraDatacenterSpec{
ClusterName: "test-cluster",
ManagementApiAuth: api.ManagementApiAuthConfig{
Manual: &api.ManagementApiAuthManualConfig{
ClientSecretName: "mgmt-api-client-credentials",
ServerSecretName: "mgmt-api-server-credentials",
},
},
},
}

trackObjects := []runtime.Object{
clientSecret,
serverSecret,
dc,
}

client := fake.NewClientBuilder().WithRuntimeObjects(trackObjects...).Build()
ctx := context.TODO()

httpClient, err := BuildManagementApiHttpClient(ctx, client, dc, nil)
require.NoError(err)

tlsConfig := httpClient.(*http.Client).Transport.(*http.Transport).TLSClientConfig
require.NotNil(tlsConfig)
}

0 comments on commit 753c58d

Please sign in to comment.