mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
csi: configure iSCSI CHAP during node stage
This commit is contained in:
1 parent
e76964935c
commit
46c7d95387
5 files changed
+164
-3
No files matched your search
@@ -87,6 +87,35 @@ func TestControllerPublish_ReturnsISCSIPublishContextFromTargetFact(t *testing.T
|
||||
}
|
||||
}
|
||||
|
||||
func TestControllerPublish_DoesNotExposeCHAPSecretsInPublishContext(t *testing.T) {
|
||||
lookup := &stubLookup{target: PublishTarget{
|
||||
VolumeID: "v1",
|
||||
ReplicaID: "r1",
|
||||
Protocol: ProtocolISCSI,
|
||||
ISCSIAddr: "127.0.0.1:3260",
|
||||
IQN: "iqn.2026-05.example.v3:v1",
|
||||
}}
|
||||
s := NewControllerServer(lookup)
|
||||
|
||||
resp, err := s.ControllerPublishVolume(context.Background(), &csipb.ControllerPublishVolumeRequest{
|
||||
VolumeId: "v1",
|
||||
NodeId: "node-a",
|
||||
Secrets: map[string]string{
|
||||
"chapUsername": "user1",
|
||||
"chapSecret": "secret1",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("ControllerPublishVolume: %v", err)
|
||||
}
|
||||
if _, ok := resp.GetPublishContext()["chapUsername"]; ok {
|
||||
t.Fatalf("chapUsername leaked in publish_context: %+v", resp.GetPublishContext())
|
||||
}
|
||||
if _, ok := resp.GetPublishContext()["chapSecret"]; ok {
|
||||
t.Fatalf("chapSecret leaked in publish_context: %+v", resp.GetPublishContext())
|
||||
}
|
||||
}
|
||||
|
||||
func TestControllerPublish_FailsClosedWithoutVerifiedTarget(t *testing.T) {
|
||||
s := NewControllerServer(&stubLookup{err: ErrPublishTargetNotFound})
|
||||
_, err := s.ControllerPublishVolume(context.Background(), &csipb.ControllerPublishVolumeRequest{
|
||||
@@ -107,6 +136,10 @@ func TestControllerPublish_FailsClosedWhenTargetHasNoFrontendFact(t *testing.T)
|
||||
_, err := s.ControllerPublishVolume(context.Background(), &csipb.ControllerPublishVolumeRequest{
|
||||
VolumeId: "v1",
|
||||
NodeId: "node-a",
|
||||
Secrets: map[string]string{
|
||||
"chapUsername": "user1",
|
||||
"chapSecret": "secret1",
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
|
||||
@@ -29,6 +29,25 @@ func (r *realISCSIUtil) Discovery(ctx context.Context, portal string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *realISCSIUtil) ConfigureCHAP(ctx context.Context, iqn, portal string, auth ISCSIAuth) error {
|
||||
updates := []struct {
|
||||
key string
|
||||
value string
|
||||
}{
|
||||
{key: "node.session.auth.authmethod", value: "CHAP"},
|
||||
{key: "node.session.auth.username", value: auth.Username},
|
||||
{key: "node.session.auth.password", value: auth.Secret},
|
||||
}
|
||||
for _, update := range updates {
|
||||
cmd := exec.CommandContext(ctx, "iscsiadm", "-m", "node", "-T", iqn, "-p", portal, "--op=update", "-n", update.key, "-v", update.value)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("iscsiadm configure CHAP %s: %s: %w", update.key, string(out), err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *realISCSIUtil) Login(ctx context.Context, iqn, portal string) error {
|
||||
cmd := exec.CommandContext(ctx, "iscsiadm", "-m", "node", "-T", iqn, "-p", portal, "--login")
|
||||
out, err := cmd.CombinedOutput()
|
||||
|
||||
@@ -21,6 +21,7 @@ const (
|
||||
|
||||
type ISCSIUtil interface {
|
||||
Discovery(ctx context.Context, portal string) error
|
||||
ConfigureCHAP(ctx context.Context, iqn, portal string, auth ISCSIAuth) error
|
||||
Login(ctx context.Context, iqn, portal string) error
|
||||
Logout(ctx context.Context, iqn string) error
|
||||
GetDeviceByIQN(ctx context.Context, iqn string) (string, error)
|
||||
@@ -28,6 +29,11 @@ type ISCSIUtil interface {
|
||||
RescanDevice(ctx context.Context, iqn string) error
|
||||
}
|
||||
|
||||
type ISCSIAuth struct {
|
||||
Username string
|
||||
Secret string
|
||||
}
|
||||
|
||||
type MountUtil interface {
|
||||
FormatAndMount(ctx context.Context, device, target, fsType string) error
|
||||
BindMount(ctx context.Context, source, target string, readOnly bool) error
|
||||
@@ -103,12 +109,22 @@ func (s *NodeServer) NodeStageVolume(ctx context.Context, req *csipb.NodeStageVo
|
||||
}
|
||||
|
||||
portal, iqn := iscsiFromContext(req.GetPublishContext())
|
||||
auth := iscsiAuthFromContext(req.GetSecrets())
|
||||
if auth == (ISCSIAuth{}) {
|
||||
auth = iscsiAuthFromContext(req.GetPublishContext())
|
||||
}
|
||||
if portal == "" || iqn == "" {
|
||||
portal, iqn = iscsiFromContext(req.GetVolumeContext())
|
||||
if auth == (ISCSIAuth{}) {
|
||||
auth = iscsiAuthFromContext(req.GetVolumeContext())
|
||||
}
|
||||
}
|
||||
if portal == "" || iqn == "" {
|
||||
return nil, status.Error(codes.FailedPrecondition, "no iSCSI publish target")
|
||||
}
|
||||
if err := validateISCSIAuth(auth); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
loggedIn, err := s.iscsiUtil.IsLoggedIn(ctx, iqn)
|
||||
if err != nil {
|
||||
@@ -119,6 +135,11 @@ func (s *NodeServer) NodeStageVolume(ctx context.Context, req *csipb.NodeStageVo
|
||||
if err := s.iscsiUtil.Discovery(ctx, portal); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "iscsi discovery: %v", err)
|
||||
}
|
||||
if auth.Secret != "" {
|
||||
if err := s.iscsiUtil.ConfigureCHAP(ctx, iqn, portal, auth); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "iscsi chap config: %v", err)
|
||||
}
|
||||
}
|
||||
if err := s.iscsiUtil.Login(ctx, iqn, portal); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "iscsi login: %v", err)
|
||||
}
|
||||
@@ -305,6 +326,23 @@ func iscsiFromContext(ctx map[string]string) (portal, iqn string) {
|
||||
return ctx["iscsiAddr"], ctx["iqn"]
|
||||
}
|
||||
|
||||
func iscsiAuthFromContext(ctx map[string]string) ISCSIAuth {
|
||||
if ctx == nil {
|
||||
return ISCSIAuth{}
|
||||
}
|
||||
return ISCSIAuth{
|
||||
Username: ctx["chapUsername"],
|
||||
Secret: ctx["chapSecret"],
|
||||
}
|
||||
}
|
||||
|
||||
func validateISCSIAuth(auth ISCSIAuth) error {
|
||||
if (auth.Username == "") != (auth.Secret == "") {
|
||||
return status.Error(codes.FailedPrecondition, "iSCSI CHAP username and secret must be set together")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeTransportFile(stagingPath, transport string) error {
|
||||
return os.WriteFile(filepath.Join(stagingPath, transportFile), []byte(transport), 0o600)
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
type mockISCSIUtil struct {
|
||||
discoveryErr error
|
||||
configureErr error
|
||||
loginErr error
|
||||
logoutErr error
|
||||
getDeviceResult string
|
||||
@@ -31,6 +32,11 @@ func (m *mockISCSIUtil) Discovery(_ context.Context, portal string) error {
|
||||
return m.discoveryErr
|
||||
}
|
||||
|
||||
func (m *mockISCSIUtil) ConfigureCHAP(_ context.Context, iqn, portal string, auth ISCSIAuth) error {
|
||||
m.calls = append(m.calls, "chap:"+iqn+":"+portal+":"+auth.Username+":"+auth.Secret)
|
||||
return m.configureErr
|
||||
}
|
||||
|
||||
func (m *mockISCSIUtil) Login(_ context.Context, iqn, portal string) error {
|
||||
m.calls = append(m.calls, "login:"+iqn+":"+portal)
|
||||
if m.loginErr != nil {
|
||||
@@ -154,6 +160,69 @@ func TestNodeStage_UsesPublishContextBeforeVolumeContext(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeStage_ConfiguresCHAPBeforeLogin(t *testing.T) {
|
||||
mi, mm := newMockISCSIUtil(), newMockMountUtil()
|
||||
ns := newTestNode(mi, mm)
|
||||
staging := t.TempDir()
|
||||
|
||||
_, err := ns.NodeStageVolume(context.Background(), &csipb.NodeStageVolumeRequest{
|
||||
VolumeId: "v1",
|
||||
StagingTargetPath: staging,
|
||||
VolumeCapability: testVolumeCapability(),
|
||||
PublishContext: map[string]string{
|
||||
"iscsiAddr": "127.0.0.1:3260",
|
||||
"iqn": "iqn.v1",
|
||||
},
|
||||
Secrets: map[string]string{
|
||||
"chapUsername": "user1",
|
||||
"chapSecret": "secret1",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NodeStageVolume: %v", err)
|
||||
}
|
||||
want := []string{
|
||||
"isloggedin:iqn.v1",
|
||||
"discovery:127.0.0.1:3260",
|
||||
"chap:iqn.v1:127.0.0.1:3260:user1:secret1",
|
||||
"login:iqn.v1:127.0.0.1:3260",
|
||||
"getdevice:iqn.v1",
|
||||
}
|
||||
for i, w := range want {
|
||||
if i >= len(mi.calls) || mi.calls[i] != w {
|
||||
t.Fatalf("calls=%v want prefix=%v", mi.calls, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeStage_RejectsPartialCHAPContext(t *testing.T) {
|
||||
mi, mm := newMockISCSIUtil(), newMockMountUtil()
|
||||
ns := newTestNode(mi, mm)
|
||||
|
||||
_, err := ns.NodeStageVolume(context.Background(), &csipb.NodeStageVolumeRequest{
|
||||
VolumeId: "v1",
|
||||
StagingTargetPath: t.TempDir(),
|
||||
VolumeCapability: testVolumeCapability(),
|
||||
PublishContext: map[string]string{
|
||||
"iscsiAddr": "127.0.0.1:3260",
|
||||
"iqn": "iqn.v1",
|
||||
},
|
||||
Secrets: map[string]string{
|
||||
"chapUsername": "user1",
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected partial CHAP context to fail")
|
||||
}
|
||||
st, _ := status.FromError(err)
|
||||
if st.Code() != codes.FailedPrecondition {
|
||||
t.Fatalf("code=%v want FailedPrecondition", st.Code())
|
||||
}
|
||||
if len(mi.calls) != 0 {
|
||||
t.Fatalf("expected fail before iscsi calls, got %v", mi.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNodeStage_IdempotentWhenAlreadyMounted(t *testing.T) {
|
||||
mi, mm := newMockISCSIUtil(), newMockMountUtil()
|
||||
ns := newTestNode(mi, mm)
|
||||
|
||||
@@ -175,9 +175,11 @@ References:
|
||||
- configure `iscsiadm` node auth before login,
|
||||
- prove correct secret succeeds and wrong secret fails without residue.
|
||||
- Kubernetes / CSI Secret integration:
|
||||
- status: planned after OS smoke.
|
||||
- do not expose secrets through ordinary logs or publish_context unless a
|
||||
deliberate alpha tradeoff is documented.
|
||||
- status: local NodeStage seam + unit tests.
|
||||
- CSI node consumes CHAP credentials from `NodeStageVolumeRequest.Secrets`,
|
||||
- controller publish path must not copy CHAP secrets into `publish_context`,
|
||||
- node configures `iscsiadm` CHAP settings after discovery and before login.
|
||||
- #QA K8s Secret / StorageClass end-to-end wiring is still pending.
|
||||
- replayed challenge rejected if supported by the protocol path,
|
||||
|
||||
- Close bar:
|
||||
|
||||
Reference in new issue
Block a user