Skip to content

Commit 7072db2

Browse files
committed
feat: support NAS access point ram authentication
1 parent f66e17d commit 7072db2

File tree

5 files changed

+100
-24
lines changed

5 files changed

+100
-24
lines changed

pkg/mounter/interceptors/alinas_secret.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package interceptors
22

33
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
48
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter"
9+
"k8s.io/klog/v2"
510
)
611

712
type AlinasSecretInterceptor struct{}
@@ -12,6 +17,32 @@ func NewAlinasSecretInterceptor() mounter.MountInterceptor {
1217
return AlinasSecretInterceptor{}
1318
}
1419

15-
func (AlinasSecretInterceptor) BeforeMount(req *mounter.MountOperation) (*mounter.MountOperation, error) {
16-
return req, nil
20+
func (AlinasSecretInterceptor) BeforeMount(op *mounter.MountOperation) (*mounter.MountOperation, error) {
21+
if op == nil || op.Secrets == nil {
22+
return op, nil
23+
}
24+
25+
tmpDir, err := os.MkdirTemp("", "alinas-")
26+
if err != nil {
27+
return op, err
28+
}
29+
30+
credFileContent := makeCredFileContent(op.Secrets)
31+
credFilePath := path.Join(tmpDir, op.VolumeID+".credentials")
32+
if err = os.WriteFile(credFilePath, credFileContent, 0o600); err != nil {
33+
return op, err
34+
}
35+
36+
klog.V(4).InfoS("Created alinas credential file", "path", credFilePath)
37+
op.Options = append(op.Options, "ram_config_file="+credFilePath)
38+
return op, nil
39+
}
40+
41+
func makeCredFileContent(secrets map[string]string) []byte {
42+
return fmt.Appendf(
43+
nil,
44+
"[NASCredentials]\naccessKeyID=%s\naccessKeySecret=%s",
45+
secrets["akId"],
46+
secrets["akSecret"],
47+
)
1748
}

pkg/nas/mounter.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package nas
22

33
import (
4+
"context"
5+
46
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter"
57
"k8s.io/klog/v2"
68
mountutils "k8s.io/mount-utils"
@@ -11,18 +13,20 @@ type NasMounter struct {
1113
alinasMounter mountutils.Interface
1214
}
1315

14-
func (m *NasMounter) Mount(source string, target string, fstype string, options []string) (err error) {
16+
var _ mounter.Mounter = &NasMounter{}
17+
18+
func (m *NasMounter) ExtendedMount(ctx context.Context, op *mounter.MountOperation) (err error) {
1519
logger := klog.Background().WithValues(
16-
"source", source,
17-
"target", target,
18-
"options", options,
19-
"fstype", fstype,
20+
"source", op.Source,
21+
"target", op.Target,
22+
"options", op.Options,
23+
"fstype", op.FsType,
2024
)
21-
switch fstype {
25+
switch op.FsType {
2226
case "alinas", "cpfs", "cpfs-nfs":
23-
err = m.alinasMounter.Mount(source, target, fstype, options)
27+
err = m.alinasMounter.Mount(op.Source, op.Target, op.FsType, op.Options)
2428
default:
25-
err = m.Interface.Mount(source, target, fstype, options)
29+
err = m.Mount(op.Source, op.Target, op.FsType, op.Options)
2630
}
2731
if err != nil {
2832
logger.Error(err, "failed to mount")
@@ -32,7 +36,7 @@ func (m *NasMounter) Mount(source string, target string, fstype string, options
3236
return err
3337
}
3438

35-
func newNasMounter(agentMode bool, socketPath string) mountutils.Interface {
39+
func newNasMounter(agentMode bool, socketPath string) mounter.Mounter {
3640
inner := mountutils.NewWithoutSystemd("")
3741
m := &NasMounter{
3842
Interface: inner,
@@ -41,7 +45,7 @@ func newNasMounter(agentMode bool, socketPath string) mountutils.Interface {
4145
switch {
4246
case socketPath != "":
4347
m.alinasMounter = mounter.NewProxyMounter(socketPath, inner)
44-
case !agentMode: // normal case, use connector mounter to ensure backward compatability
48+
case !agentMode: // normal case, use connector mounter to ensure backward compatibility
4549
m.alinasMounter = mounter.NewConnectorMounter(inner, "")
4650
}
4751
return m

pkg/nas/mounter_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package nas
22

33
import (
4+
"context"
45
"errors"
56
"testing"
67

8+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter"
79
"github.com/stretchr/testify/assert"
810
mountutils "k8s.io/mount-utils"
911
)
@@ -34,7 +36,7 @@ func TestNasMounter_MountSuccess(t *testing.T) {
3436
Interface: &successMockMounter{},
3537
alinasMounter: &successMockMounter{},
3638
}
37-
err := nasMounter.Mount("", "", "nas", []string{})
39+
err := nasMounter.ExtendedMount(context.Background(), &mounter.MountOperation{})
3840
assert.NoError(t, err)
3941
}
4042

@@ -43,6 +45,8 @@ func TestNasMounter_FuseMountError(t *testing.T) {
4345
Interface: &errorMockMounter{},
4446
alinasMounter: &errorMockMounter{},
4547
}
46-
err := nasMounter.Mount("", "", "cpfs", []string{})
48+
err := nasMounter.ExtendedMount(context.Background(), &mounter.MountOperation{
49+
FsType: "cpfs",
50+
})
4751
assert.Error(t, err)
4852
}

pkg/nas/nodeserver.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/dadi"
3535
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/features"
3636
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/losetup"
37+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter"
3738
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/nas/internal"
3839
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/utils"
3940
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/utils/rund/directvolume"
@@ -42,12 +43,11 @@ import (
4243
v1 "k8s.io/api/core/v1"
4344
"k8s.io/client-go/tools/record"
4445
"k8s.io/klog/v2"
45-
mountutils "k8s.io/mount-utils"
4646
)
4747

4848
type nodeServer struct {
4949
config *internal.NodeConfig
50-
mounter mountutils.Interface
50+
mounter mounter.Mounter
5151
locks *utils.VolumeLocks
5252
recorder record.EventRecorder
5353
common.GenericNodeServer
@@ -91,6 +91,8 @@ type Options struct {
9191
MountProtocol string `json:"mountProtocol"`
9292
ClientType string `json:"clientType"`
9393
FSType string `json:"fsType"`
94+
AkID string
95+
AkSecret string
9496
}
9597

9698
// RunvNasOptions struct definition
@@ -246,6 +248,8 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
246248
opt.MountProtocol = strings.TrimSpace(value)
247249
}
248250
}
251+
opt.AkID = req.Secrets[akIDKey]
252+
opt.AkSecret = req.Secrets[akSecretKey]
249253

250254
if cnfsName != "" {
251255
cnfs, err := ns.getCNFS(ctx, req, cnfsName)

pkg/nas/utils.go

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

1919
import (
20+
"context"
2021
"errors"
2122
"fmt"
2223
"os"
@@ -27,7 +28,8 @@ import (
2728

2829
"github.com/alibabacloud-go/tea/tea"
2930
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/losetup"
30-
mounter "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/utils"
31+
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter"
32+
mounterutils "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/utils"
3133
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/nas/cloud"
3234
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/nas/interfaces"
3335
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/utils"
@@ -52,6 +54,8 @@ const (
5254
TcpSlotTableEntries = "/proc/sys/sunrpc/tcp_slot_table_entries"
5355
TcpSlotTableEntriesValue = "128\n"
5456

57+
akIDKey = "akId"
58+
akSecretKey = "akSecret"
5559
filesystemIDKey = "fileSystemId"
5660
filesystemTypeKey = "fileSystemType"
5761
)
@@ -66,11 +70,12 @@ type RoleAuth struct {
6670
Code string
6771
}
6872

69-
func doMount(mounter mountutils.Interface, opt *Options, targetPath, volumeId, podUid string) error {
73+
func doMount(m mounter.Mounter, opt *Options, targetPath, volumeId, podUid string) error {
7074
var (
7175
mountFstype string
7276
source string
7377
combinedOptions []string
78+
secrets map[string]string
7479
isPathNotFound func(error) bool
7580
)
7681
if opt.Accesspoint != "" {
@@ -81,6 +86,12 @@ func doMount(mounter mountutils.Interface, opt *Options, targetPath, volumeId, p
8186
if opt.Options != "" {
8287
combinedOptions = append(combinedOptions, opt.Options)
8388
}
89+
if opt.AkID != "" && opt.AkSecret != "" {
90+
secrets = map[string]string{
91+
akIDKey: opt.AkID,
92+
akSecretKey: opt.AkSecret,
93+
}
94+
}
8495

8596
switch opt.ClientType {
8697
case EFCClient:
@@ -127,7 +138,15 @@ func doMount(mounter mountutils.Interface, opt *Options, targetPath, volumeId, p
127138
return strings.Contains(err.Error(), "reason given by server: No such file or directory") || strings.Contains(err.Error(), "access denied by server while mounting")
128139
}
129140
}
130-
err := mounter.Mount(source, targetPath, mountFstype, combinedOptions)
141+
142+
err := m.ExtendedMount(context.Background(), &mounter.MountOperation{
143+
Source: source,
144+
Target: targetPath,
145+
FsType: mountFstype,
146+
Options: combinedOptions,
147+
Secrets: secrets,
148+
VolumeID: volumeId,
149+
})
131150
if err == nil {
132151
return nil
133152
}
@@ -154,16 +173,30 @@ func doMount(mounter mountutils.Interface, opt *Options, targetPath, volumeId, p
154173
return err
155174
}
156175
defer os.Remove(tmpPath)
157-
if err := mounter.Mount(rootSource, tmpPath, mountFstype, combinedOptions); err != nil {
176+
if err := m.ExtendedMount(context.Background(), &mounter.MountOperation{
177+
Source: rootSource,
178+
Target: tmpPath,
179+
FsType: mountFstype,
180+
Options: combinedOptions,
181+
Secrets: secrets,
182+
VolumeID: volumeId,
183+
}); err != nil {
158184
return err
159185
}
160186
if err := os.MkdirAll(filepath.Join(tmpPath, relPath), os.ModePerm); err != nil {
161187
return err
162188
}
163-
if err := cleanupMountpoint(mounter, tmpPath); err != nil {
189+
if err := cleanupMountpoint(m, tmpPath); err != nil {
164190
klog.Errorf("failed to cleanup tmp mountpoint %s: %v", tmpPath, err)
165191
}
166-
return mounter.Mount(source, targetPath, mountFstype, combinedOptions)
192+
return m.ExtendedMount(context.Background(), &mounter.MountOperation{
193+
Source: source,
194+
Target: targetPath,
195+
FsType: mountFstype,
196+
Options: combinedOptions,
197+
Secrets: secrets,
198+
VolumeID: volumeId,
199+
})
167200
}
168201

169202
// check system config,
@@ -181,7 +214,7 @@ func ParseMountFlags(mntOptions []string) (string, string) {
181214
var vers string
182215
var otherOptions []string
183216
for _, options := range mntOptions {
184-
for _, option := range mounter.SplitMountOptions(options) {
217+
for _, option := range mounterutils.SplitMountOptions(options) {
185218
if option == "" {
186219
continue
187220
}
@@ -201,7 +234,7 @@ func ParseMountFlags(mntOptions []string) (string, string) {
201234

202235
func addTLSMountOptions(baseOptions []string) []string {
203236
for _, options := range baseOptions {
204-
for _, option := range mounter.SplitMountOptions(options) {
237+
for _, option := range mounterutils.SplitMountOptions(options) {
205238
if option == "" {
206239
continue
207240
}

0 commit comments

Comments
 (0)