Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 9748994

Browse files
committed
fixup! feat: Add user configuration for all providers
Fix typo in lockPassword logic, and add unit test
1 parent 27072c6 commit 9748994

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api v0.0.0-00010101000000-000000000000
1616
github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common v0.0.0-00010101000000-000000000000
1717
github.com/go-logr/logr v1.4.1
18+
github.com/google/go-cmp v0.6.0
1819
github.com/onsi/ginkgo/v2 v2.16.0
1920
github.com/onsi/gomega v1.31.1
2021
github.com/spf13/pflag v1.0.5
@@ -71,7 +72,6 @@ require (
7172
github.com/golang/protobuf v1.5.4 // indirect
7273
github.com/google/cel-go v0.17.7 // indirect
7374
github.com/google/gnostic-models v0.6.8 // indirect
74-
github.com/google/go-cmp v0.6.0 // indirect
7575
github.com/google/go-github/v53 v53.2.0 // indirect
7676
github.com/google/go-querystring v1.1.0 // indirect
7777
github.com/google/gofuzz v1.2.0 // indirect

pkg/handlers/generic/mutation/users/inject.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ func (h *usersPatchHandler) Mutate(
119119
func generateBootstrapUser(userFromVariable v1alpha1.User) bootstrapv1.User {
120120
bootstrapUser := bootstrapv1.User{
121121
Name: userFromVariable.Name,
122-
Passwd: ptr.To(userFromVariable.HashedPassword),
123122
SSHAuthorizedKeys: userFromVariable.SSHAuthorizedKeys,
124123
Sudo: userFromVariable.Sudo,
125124
}
@@ -133,9 +132,12 @@ func generateBootstrapUser(userFromVariable v1alpha1.User) bootstrapv1.User {
133132
//
134133
// We disable password authentication by default.
135134
bootstrapUser.LockPassword = ptr.To(true)
135+
136136
if userFromVariable.HashedPassword != "" {
137137
// We enable password authentication only if a hashed password is defined.
138-
bootstrapUser.LockPassword = ptr.To(true)
138+
bootstrapUser.LockPassword = ptr.To(false)
139+
140+
bootstrapUser.Passwd = ptr.To(userFromVariable.HashedPassword)
139141
}
140142

141143
return bootstrapUser
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package users
5+
6+
import (
7+
"testing"
8+
9+
"github.com/google/go-cmp/cmp"
10+
"k8s.io/utils/ptr"
11+
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
12+
13+
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
14+
)
15+
16+
func Test_generateBootstrapUser(t *testing.T) {
17+
type args struct {
18+
userFromVariable v1alpha1.User
19+
}
20+
tests := []struct {
21+
name string
22+
args args
23+
want bootstrapv1.User
24+
}{
25+
{
26+
name: "if user sets hashed password, enable password auth and set passwd",
27+
args: args{
28+
userFromVariable: v1alpha1.User{
29+
Name: "example",
30+
HashedPassword: "example",
31+
},
32+
},
33+
want: bootstrapv1.User{
34+
Name: "example",
35+
Passwd: ptr.To("example"),
36+
LockPassword: ptr.To(false),
37+
},
38+
},
39+
{
40+
name: "if user does not set hashed password, disable password auth and do not set passwd",
41+
args: args{
42+
userFromVariable: v1alpha1.User{
43+
Name: "example",
44+
},
45+
},
46+
want: bootstrapv1.User{
47+
Name: "example",
48+
Passwd: nil,
49+
LockPassword: ptr.To(true),
50+
},
51+
},
52+
}
53+
for _, tt := range tests {
54+
t.Run(tt.name, func(t *testing.T) {
55+
got := generateBootstrapUser(tt.args.userFromVariable)
56+
if diff := cmp.Diff(tt.want, got); diff != "" {
57+
t.Errorf("generateBootstrapUser() mismatch (-want +got):\n%s", diff)
58+
}
59+
})
60+
}
61+
}

0 commit comments

Comments
 (0)