Skip to content

Commit 047b9c7

Browse files
authored
fix: Add nil check for getOCIClientCertFromSecret (#466)
1 parent 4be9bd7 commit 047b9c7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

cloud/util/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func GetClusterIdentityFromRef(ctx context.Context, c client.Client, ociClusterN
7878
// getOCIClientCertFromSecret returns the cert referenced by the OCICluster.
7979
func getOCIClientCertFromSecret(ctx context.Context, c client.Client, ociClusterNamespace string, overrides *infrastructurev1beta2.ClientOverrides) (*corev1.Secret, error) {
8080
secret := &corev1.Secret{}
81-
if overrides != nil {
81+
if overrides != nil && overrides.CertOverride != nil {
8282
certSecretRef := overrides.CertOverride
8383
namespace := certSecretRef.Namespace
8484
if namespace == "" {

cloud/util/util_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,3 +879,40 @@ func TestDeleteManagedMachinesIfNotExists(t *testing.T) {
879879
})
880880
}
881881
}
882+
883+
func TestGetOCIClientCertFromSecret(t *testing.T) {
884+
testCases := []struct {
885+
name string
886+
overrides *infrastructurev1beta2.ClientOverrides
887+
objects []client.Object
888+
errorExpected bool
889+
errorMessage string
890+
}{
891+
{
892+
name: "NPE case - nil CertOverride",
893+
overrides: &infrastructurev1beta2.ClientOverrides{
894+
CertOverride: nil, // This should cause NPE
895+
},
896+
objects: []client.Object{},
897+
errorExpected: true, // Should panic or return error
898+
},
899+
// Add more test cases...
900+
}
901+
902+
for _, tt := range testCases {
903+
t.Run(tt.name, func(t *testing.T) {
904+
g := NewWithT(t)
905+
client := fake.NewClientBuilder().WithObjects(tt.objects...).Build()
906+
907+
// This should either panic or return an error
908+
_, err := getOCIClientCertFromSecret(context.Background(), client, "default", tt.overrides)
909+
910+
if tt.errorExpected {
911+
// Currently this will panic, but after the fix it should return an error
912+
g.Expect(err).To(Not(BeNil()))
913+
} else {
914+
g.Expect(err).To(BeNil())
915+
}
916+
})
917+
}
918+
}

0 commit comments

Comments
 (0)