-
Notifications
You must be signed in to change notification settings - Fork 251
Support NAS access point RAM authentication with AK/SK #1562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iltyty
wants to merge
5
commits into
kubernetes-sigs:master
Choose a base branch
from
iltyty:nas-ap-authentication
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f66e17d
refactor: split non-mounting actions into interceptors
iltyty 7072db2
feat: support NAS access point ram authentication
iltyty 2cfc19d
refactor(mounter): use ossfs secret and monitor interceptors in ossfs…
iltyty 7202992
refactor(mounter): reimplement interceptors following gRPC pattern
iltyty eb1997d
fix(nas): atomically update alinas credential file via temp file and …
iltyty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package interceptors | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "path" | ||
|
|
||
| "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| var _ mounter.MountInterceptor = AlinasSecretInterceptor | ||
|
|
||
| func AlinasSecretInterceptor(ctx context.Context, op *mounter.MountOperation, handler mounter.MountHandler) error { | ||
| if op == nil || op.Secrets == nil { | ||
| return handler(ctx, op) | ||
| } | ||
|
|
||
| tmpCredFile, err := os.CreateTemp("", op.VolumeID+"-*.credentials") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer func() { | ||
| if err = os.Remove(tmpCredFile.Name()); err != nil && !os.IsNotExist(err) { | ||
| klog.ErrorS(err, "Failed to remove temporary alinas credential file", "path", tmpCredFile.Name()) | ||
| } | ||
| }() | ||
|
|
||
| credFileContent := makeCredFileContent(op.Secrets) | ||
| if _, err = tmpCredFile.Write(credFileContent); err != nil { | ||
| return err | ||
| } | ||
| if err = tmpCredFile.Close(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| credFilePath := path.Join(os.TempDir(), op.VolumeID+".credentials") | ||
| if err = os.Rename(tmpCredFile.Name(), credFilePath); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| klog.V(4).InfoS("Created alinas credential file", "path", credFilePath) | ||
| op.Options = append(op.Options, "ram_config_file="+credFilePath) | ||
|
|
||
| return handler(ctx, op) | ||
| } | ||
|
|
||
| func makeCredFileContent(secrets map[string]string) []byte { | ||
| return fmt.Appendf( | ||
| nil, | ||
| "[NASCredentials]\naccessKeyID=%s\naccessKeySecret=%s", | ||
| secrets["akId"], | ||
| secrets["akSecret"], | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package interceptors | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter" | ||
| "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/proxy/server" | ||
| "k8s.io/klog/v2" | ||
| "k8s.io/mount-utils" | ||
| ) | ||
|
|
||
| var _ mounter.MountInterceptor = OssfsMonitorInterceptor | ||
|
|
||
| var ( | ||
| raw = mount.NewWithoutSystemd("") | ||
| monitorManager = server.NewMountMonitorManager() | ||
| ) | ||
|
|
||
| func OssfsMonitorInterceptor(ctx context.Context, op *mounter.MountOperation, handler mounter.MountHandler) error { | ||
| if op == nil || op.MetricsPath == "" { | ||
| return handler(ctx, op) | ||
| } | ||
|
|
||
| // Get or create monitor for this target | ||
| monitor, found := monitorManager.GetMountMonitor(op.Target, op.MetricsPath, raw, true) | ||
| if monitor == nil { | ||
| klog.ErrorS(errors.New("failed to get mount monitor"), "stop monitoring mountpoint status", "mountpoint", op.Target) | ||
| return handler(ctx, op) | ||
| } | ||
| if found { | ||
| monitor.IncreaseMountRetryCount() | ||
| } | ||
|
|
||
| err := handler(ctx, op) | ||
|
|
||
| // Immediate process-exit handling during mount attempt | ||
| // Assume the process exits with no error upon receiving SIGTERM, | ||
| // and exits with an error in case of unexpected failures. | ||
| monitor.HandleMountFailureOrExit(err) | ||
|
|
||
| if op.MountResult == nil { | ||
| return err | ||
| } | ||
|
|
||
| res, ok := op.MountResult.(server.OssfsMountResult) | ||
| if !ok { | ||
| klog.ErrorS(errors.New("failed to assert ossfs mount result type"), "skipping monitoring of mountpoint", "mountpoint", op.Target) | ||
| return err | ||
| } | ||
|
|
||
| go func() { | ||
| err := <-res.ExitChan | ||
| monitor.HandleMountFailureOrExit(err) | ||
| }() | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| monitor.HandleMountSuccess(res.PID) | ||
| // Start monitoring goroutine (ticker based only) | ||
| monitorManager.StartMonitoring(op.Target) | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package interceptors | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "os" | ||
|
|
||
| "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter" | ||
| "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/proxy/server" | ||
| "github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/mounter/utils" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| var _ mounter.MountInterceptor = OssfsSecretInterceptor | ||
|
|
||
| func OssfsSecretInterceptor(ctx context.Context, op *mounter.MountOperation, handler mounter.MountHandler) error { | ||
| return ossfsSecretInterceptor(ctx, op, handler, "ossfs") | ||
| } | ||
|
|
||
| func Ossfs2SecretInterceptor(ctx context.Context, op *mounter.MountOperation, handler mounter.MountHandler) error { | ||
| return ossfsSecretInterceptor(ctx, op, handler, "ossfs2") | ||
| } | ||
|
|
||
| func ossfsSecretInterceptor(ctx context.Context, op *mounter.MountOperation, handler mounter.MountHandler, fuseType string) error { | ||
| passwdFile, err := utils.SaveOssSecretsToFile(op.Secrets, op.FsType) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if passwdFile != "" { | ||
| if fuseType == "ossfs" { | ||
| op.Args = append(op.Args, "passwd_file="+passwdFile) | ||
| } else { | ||
| op.Args = append(op.Args, []string{"-c", passwdFile}...) | ||
| } | ||
| } | ||
|
|
||
| if err = handler(ctx, op); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if passwdFile == "" || op.MountResult == nil { | ||
| return nil | ||
| } | ||
| result, ok := op.MountResult.(*server.OssfsMountResult) | ||
| if !ok { | ||
| klog.ErrorS( | ||
| errors.New("failed to assert ossfs mount result"), | ||
| "skipping cleanup of passwd file", "mountpoint", op.Target, "path", passwdFile, | ||
| ) | ||
| return nil | ||
| } | ||
|
|
||
| go func() { | ||
| <-result.ExitChan | ||
| if err := os.Remove(passwdFile); err != nil { | ||
| klog.ErrorS(err, "failed to cleanup ossfs passwd file", "mountpoint", op.Target, "path", passwdFile) | ||
| } | ||
| }() | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,69 @@ | ||
| package mounter | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| mountutils "k8s.io/mount-utils" | ||
| ) | ||
|
|
||
| type ExtendedMountParams struct { | ||
| type Mounter interface { | ||
| mountutils.Interface | ||
| ExtendedMount(ctx context.Context, op *MountOperation) error | ||
| } | ||
|
|
||
| type MountOperation struct { | ||
| Source string | ||
| Target string | ||
| FsType string | ||
| Options []string | ||
| Args []string | ||
| Secrets map[string]string | ||
| MetricsPath string | ||
| VolumeID string | ||
|
|
||
| MountResult any | ||
| } | ||
|
|
||
| type Mounter interface { | ||
| mountutils.Interface | ||
| ExtendedMount(source, target, fstype string, options []string, params *ExtendedMountParams) error | ||
| type MountHandler func(ctx context.Context, op *MountOperation) error | ||
|
|
||
| type MountInterceptor func(ctx context.Context, op *MountOperation, handler MountHandler) error | ||
|
|
||
| type MountWorkflow struct { | ||
| Mounter | ||
| chainedHandler MountHandler | ||
| } | ||
|
|
||
| var _ Mounter = &MountWorkflow{} | ||
|
|
||
| func (w *MountWorkflow) ExtendedMount(ctx context.Context, op *MountOperation) error { | ||
| return w.chainedHandler(ctx, op) | ||
| } | ||
|
|
||
| // chainInterceptors creates a chain of interceptors similar to gRPC | ||
| func chainInterceptors(interceptors []MountInterceptor, finalHandler MountHandler) MountHandler { | ||
| if len(interceptors) == 0 { | ||
| return finalHandler | ||
| } | ||
|
|
||
| return func(ctx context.Context, op *MountOperation) error { | ||
| return interceptors[0](ctx, op, getChainHandler(interceptors, 0, finalHandler)) | ||
| } | ||
| } | ||
|
|
||
| // getChainHandler creates a handler that chains interceptors recursively | ||
| func getChainHandler(interceptors []MountInterceptor, curr int, finalHandler MountHandler) MountHandler { | ||
| if curr == len(interceptors)-1 { | ||
| return finalHandler | ||
| } | ||
|
|
||
| return func(ctx context.Context, op *MountOperation) error { | ||
| return interceptors[curr+1](ctx, op, getChainHandler(interceptors, curr+1, finalHandler)) | ||
| } | ||
| } | ||
|
|
||
| func NewForMounter(m Mounter, interceptors ...MountInterceptor) Mounter { | ||
| return &MountWorkflow{ | ||
| Mounter: m, | ||
| chainedHandler: chainInterceptors(interceptors, m.ExtendedMount), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test mounting two APs on the same node with different AK/SKs.