Skip to content
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

Proposed alternative solution for sanitizing any proto field tagged with csi_secure #3

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.

[[override]]
name = "github.com/container-storage-interface/spec"
version = "v1.0.0"

[[constraint]]
name = "github.com/golang/protobuf"
version = "1.2.0"

[[constraint]]
branch = "master"
name = "golang.org/x/net"

[[constraint]]
name = "google.golang.org/grpc"
version = "1.17.0"

[prune]
go-tests = true
unused-packages = true
2 changes: 1 addition & 1 deletion protosanitizer/protosanitizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"fmt"
"testing"

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/protobuf/proto"
csi03 "github.com/kubernetes-csi/csi-lib-utils/protosanitizer/test/csi03"
csi "github.com/kubernetes-csi/csi-lib-utils/protosanitizer/test/csi10"
"github.com/kubernetes-csi/csi-lib-utils/protosanitizer/test/csitest"
"github.com/stretchr/testify/assert"
)
Expand Down
113 changes: 113 additions & 0 deletions protosanitizer/protosanitizer_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package protosanitizer

import (
"strconv"
"strings"
"testing"

csipb "github.com/container-storage-interface/spec/lib/go/csi"
)

func TestSanitizaMsg(t *testing.T) {

tests := []struct {
name string
msg *csipb.CreateVolumeRequest
additionalFields []string
expected string
}{
{
name: "test_with_csi_secret",
msg: createVolReq(),
expected: `name:"test-volume" capacity_range:<required_bytes:1024 limit_bytes:1024 > volume_capabilities:<mount:<fs_type:"ext4" mount_flags:"flag1" mount_flags:"flag2" mount_flags:"flag3" > access_mode:<mode:MULTI_NODE_MULTI_WRITER > > parameters:<key:"param1" value:"param1" > parameters:<key:"param2" value:"param2" > secrets:<key:"secret1" value:"***Sanitized***" > secrets:<key:"secret2" value:"***Sanitized***" > volume_content_source:<volume:<volume_id:"Source_Volume_ID" > > accessibility_requirements:<requisite:<segments:<key:"segment" value:"segment0" > > requisite:<segments:<key:"segment" value:"segment1" > > preferred:<segments:<key:"segment" value:"segment10" > > preferred:<segments:<key:"segment" value:"segment11" > > > `,
},
{
name: "test_with_csi_secret_and_additional_field",
msg: createVolReq(),
additionalFields: []string{"parameters"},
expected: `name:"test-volume" capacity_range:<required_bytes:1024 limit_bytes:1024 > volume_capabilities:<mount:<fs_type:"ext4" mount_flags:"flag1" mount_flags:"flag2" mount_flags:"flag3" > access_mode:<mode:MULTI_NODE_MULTI_WRITER > > parameters:<key:"param1" value:"***Sanitized***" > parameters:<key:"param2" value:"***Sanitized***" > secrets:<key:"secret1" value:"***Sanitized***" > secrets:<key:"secret2" value:"***Sanitized***" > volume_content_source:<volume:<volume_id:"Source_Volume_ID" > > accessibility_requirements:<requisite:<segments:<key:"segment" value:"segment0" > > requisite:<segments:<key:"segment" value:"segment1" > > preferred:<segments:<key:"segment" value:"segment10" > > preferred:<segments:<key:"segment" value:"segment11" > > > `,
},
}
for _, test := range tests {
result := SanitizeMsg(test.msg, test.additionalFields...)
if c := strings.Compare(test.expected, result); c != 0 {
t.Errorf("Test %s failed, expected: \"%s\" got: \"%s\"", test.name, test.expected, result)
}
}
}

func rTopo() []*csipb.Topology {
t := make([]*csipb.Topology, 0)
for i := 0; i < 2; i++ {
t = append(t, topo("segment"+strconv.Itoa(i)))
}
return t
}

func pTopo() []*csipb.Topology {
t := make([]*csipb.Topology, 0)
for i := 10; i < 12; i++ {
t = append(t, topo("segment"+strconv.Itoa(i)))
}
return t
}

func topo(segname string) *csipb.Topology {
return &csipb.Topology{
Segments: map[string]string{"segment": segname},
}
}

func volCap() *csipb.VolumeCapability {
return &csipb.VolumeCapability{
AccessType: &csipb.VolumeCapability_Mount{
Mount: &csipb.VolumeCapability_MountVolume{
FsType: "ext4",
MountFlags: []string{"flag1", "flag2", "flag3"},
},
},
AccessMode: &csipb.VolumeCapability_AccessMode{
Mode: csipb.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
},
}
}

func createVolReq() *csipb.CreateVolumeRequest {
return &csipb.CreateVolumeRequest{
Name: "test-volume",
CapacityRange: &csipb.CapacityRange{
RequiredBytes: int64(1024),
LimitBytes: int64(1024),
},
VolumeCapabilities: []*csipb.VolumeCapability{volCap()},
Secrets: map[string]string{"secret1": "secret1", "secret2": "secret2"},
Parameters: map[string]string{"param1": "param1", "param2": "param2"},
VolumeContentSource: &csipb.VolumeContentSource{
Type: &csipb.VolumeContentSource_Volume{
Volume: &csipb.VolumeContentSource_VolumeSource{
VolumeId: "Source_Volume_ID",
},
},
},
AccessibilityRequirements: &csipb.TopologyRequirement{
Requisite: rTopo(),
Preferred: pTopo(),
},
}
}
Loading