Skip to content

Commit 7f78b7a

Browse files
authored
Merge pull request #906 from andyzhangx/fix-password
feat: support base64password field in secret
2 parents 613018d + 3f39cbd commit 7f78b7a

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ endif
246246
.PHONY: install-smb-provisioner
247247
install-smb-provisioner:
248248
kubectl delete secret smbcreds --ignore-not-found -n default
249-
kubectl create secret generic smbcreds --from-literal username=USERNAME --from-literal password="PASSWORD" --from-literal mountOptions="dir_mode=0777,file_mode=0777,uid=0,gid=0,mfsymlinks" -n default
249+
kubectl create secret generic smbcreds --from-literal username=USERNAME --from-literal password="PASSWORD" --from-literal base64password="UEFTU1dPUkQ=" --from-literal mountOptions="dir_mode=0777,file_mode=0777,uid=0,gid=0,mfsymlinks" -n default
250250
ifdef TEST_WINDOWS
251251
kubectl apply -f deploy/example/smb-provisioner/smb-server-lb.yaml
252252
else

pkg/smb/nodeserver.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
183183
}
184184
defer d.volumeLocks.Release(lockKey)
185185

186-
var username, password, domain string
186+
var username, password, base64Password, domain string
187187
for k, v := range secrets {
188188
switch strings.ToLower(k) {
189189
case usernameField:
@@ -192,9 +192,20 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
192192
password = strings.TrimSpace(v)
193193
case domainField:
194194
domain = strings.TrimSpace(v)
195+
case base64PasswordField:
196+
base64Password = strings.TrimSpace(v)
195197
}
196198
}
197199

200+
if base64Password != "" {
201+
klog.V(2).Infof("NodeStageVolume: decoding password from base64 string")
202+
decodePassword, err := base64.StdEncoding.DecodeString(base64Password)
203+
if err != nil {
204+
return nil, status.Error(codes.InvalidArgument, "error base64 decoding password")
205+
}
206+
password = string(decodePassword)
207+
}
208+
198209
if ephemeralVol {
199210
mountFlags = strings.Split(ephemeralVolMountOptions, ",")
200211
}

pkg/smb/nodeserver_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func TestNodeStageVolume(t *testing.T) {
9292
passwordField: "test_password",
9393
domainField: "test_doamin",
9494
}
95+
secretsWithBase64Password := map[string]string{
96+
usernameField: "test_username",
97+
passwordField: base64.StdEncoding.EncodeToString([]byte("test_password")),
98+
domainField: "test_doamin",
99+
}
95100

96101
tests := []struct {
97102
desc string
@@ -230,6 +235,18 @@ func TestNodeStageVolume(t *testing.T) {
230235
strings.Replace(testSource, "\\", "\\\\", -1), sourceTest, testSource, sourceTest),
231236
expectedErr: testutil.TestError{},
232237
},
238+
{
239+
desc: "[Success] Valid request with base64 encoded password",
240+
req: &csi.NodeStageVolumeRequest{VolumeId: "vol_1##", StagingTargetPath: sourceTest,
241+
VolumeCapability: &stdVolCap,
242+
VolumeContext: volContext,
243+
Secrets: secretsWithBase64Password},
244+
skipOnWindows: true,
245+
flakyWindowsErrorMessage: fmt.Sprintf("rpc error: code = Internal desc = volume(vol_1##) mount \"%s\" on %#v failed with "+
246+
"NewSmbGlobalMapping(%s, %s) failed with error: rpc error: code = Unknown desc = NewSmbGlobalMapping failed.",
247+
strings.Replace(testSource, "\\", "\\\\", -1), sourceTest, testSource, sourceTest),
248+
expectedErr: testutil.TestError{},
249+
},
233250
}
234251

235252
// Setup

pkg/smb/smb.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package smb
1818

1919
import (
2020
"context"
21+
"encoding/base64"
2122
"errors"
2223
"fmt"
2324
"net"
@@ -49,6 +50,7 @@ const (
4950
sourceField = "source"
5051
subDirField = "subdir"
5152
domainField = "domain"
53+
base64PasswordField = "base64password"
5254
mountOptionsField = "mountoptions"
5355
secretNameField = "secretname"
5456
secretNamespaceField = "secretnamespace"
@@ -232,6 +234,15 @@ func (d *Driver) GetUserNamePasswordFromSecret(ctx context.Context, secretName,
232234
username := strings.TrimSpace(string(secret.Data[usernameField][:]))
233235
password := strings.TrimSpace(string(secret.Data[passwordField][:]))
234236
domain := strings.TrimSpace(string(secret.Data[domainField][:]))
237+
base64Password := strings.TrimSpace(string(secret.Data[base64PasswordField][:]))
238+
if base64Password != "" {
239+
klog.V(2).Infof("decoding password from base64 string")
240+
decodePassword, err := base64.StdEncoding.DecodeString(base64Password)
241+
if err != nil {
242+
return "", "", "", fmt.Errorf("could not decode password from base64 string: %v", err)
243+
}
244+
password = string(decodePassword)
245+
}
235246
return username, password, domain, nil
236247
}
237248

pkg/smb/smb_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package smb
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"os"
2223
"path/filepath"
@@ -520,6 +521,33 @@ users:
520521
}
521522
}
522523

524+
func TestGetUserNamePasswordFromSecret(t *testing.T) {
525+
tests := []struct {
526+
desc string
527+
secretName string
528+
secretNamespace string
529+
expectedUsername string
530+
expectedPassword string
531+
expectedDomain string
532+
expectedError error
533+
}{
534+
{
535+
desc: "kubeclient is nil",
536+
secretName: "secretName",
537+
expectedError: fmt.Errorf("could not username and password from secret(secretName): KubeClient is nil"),
538+
},
539+
}
540+
541+
d := NewFakeDriver()
542+
for _, test := range tests {
543+
username, password, domain, err := d.GetUserNamePasswordFromSecret(context.Background(), test.secretName, test.secretNamespace)
544+
assert.Equal(t, test.expectedUsername, username, "test[%s]: unexpected username", test.desc)
545+
assert.Equal(t, test.expectedPassword, password, "test[%s]: unexpected password", test.desc)
546+
assert.Equal(t, test.expectedDomain, domain, "test[%s]: unexpected domain", test.desc)
547+
assert.Equal(t, test.expectedError, err, "test[%s]: unexpected error", test.desc)
548+
}
549+
}
550+
523551
func createTestFile(path string) error {
524552
f, err := os.Create(path)
525553
if err != nil {

0 commit comments

Comments
 (0)