Skip to content

Commit

Permalink
fix validate empty CA issue (#412)
Browse files Browse the repository at this point in the history
Signed-off-by: Qing Hao <[email protected]>
  • Loading branch information
haoqing0110 authored Nov 15, 2024
1 parent b88f958 commit 9440b84
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
4 changes: 0 additions & 4 deletions pkg/controller/importconfig/cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ func validateKubeAPIServerAddress(ctx context.Context, kubeAPIServer string,
func validateCAData(ctx context.Context, caData []byte, kubeAPIServer string,
klusterletConfig *klusterletconfigv1alpha1.KlusterletConfig,
clientHolder *helpers.ClientHolder, clusterName string) (bool, error) {
if len(caData) == 0 {
// CA data is empty
return false, nil
}

currentCAData, err := bootstrap.GetBootstrapCAData(ctx, clientHolder, kubeAPIServer, clusterName, klusterletConfig)
if err != nil {
Expand Down
39 changes: 37 additions & 2 deletions pkg/controller/importconfig/cluster_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,33 @@ func TestValidateCAData(t *testing.T) {
klusterletConfig *klusterletconfigv1alpha1.KlusterletConfig
valid bool
}{
// validateCAData calls GetBootstrapCAData to get current CA, GetBootstrapCAData could return nil but never return empty.
// set currentCAData to nil or []byte{} are the same to simulate the GetBootstrapCAData return nil case.
{
name: "CA data is empty",
name: "bootstrap CA data is empty",
bootstrapCAData: []byte{},
currentCAData: certData1,
},
{
name: "bootstrap CA data is nil",
bootstrapCAData: nil,
currentCAData: certData1,
},
{
name: "current CA data is nil",
bootstrapCAData: certData1,
currentCAData: nil,
},
{
name: "bootstrap and current CA data is nil",
bootstrapCAData: nil,
currentCAData: nil,
valid: true,
},
{
name: "bootstrap is empty and current CA data is nil",
bootstrapCAData: []byte{},
currentCAData: nil,
},
{
name: "cert changes",
Expand Down Expand Up @@ -396,7 +421,17 @@ func TestValidateCAData(t *testing.T) {
Data: map[string][]byte{
"tls.crt": c.currentCAData,
},
})
},
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "kube-root-ca.crt",
Namespace: "cluster",
},
Data: map[string]string{
"ca.crt": string(c.currentCAData),
},
},
)

clientHolder := &helpers.ClientHolder{
RuntimeClient: fake.NewClientBuilder().WithScheme(testscheme).WithObjects(&ocinfrav1.APIServer{
Expand Down

0 comments on commit 9440b84

Please sign in to comment.