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

Support file permission setting in SPC #139

Merged
merged 4 commits into from
Mar 1, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ IMPROVEMENTS:
* `-vault-tls-client-key`
* `-vault-tls-skip-verify`
* Add an optional SecretProviderClass parameter `audience` to customize the `aud` claim in the JWT [[GH-144](https://github.com/hashicorp/vault-csi-provider/pull/144)]
* New SecretProviderClass field `filePermission` can be used per-secret to set the file permissions it is written with. [[GH-139](https://github.com/hashicorp/vault-csi-provider/pull/139)]

## 1.0.0 (January 25th, 2022)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/hashicorp/vault-csi-provider

go 1.12
go 1.13

require (
github.com/hashicorp/go-hclog v1.0.0
Expand Down
11 changes: 6 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ type PodInfo struct {
}

type Secret struct {
ObjectName string `yaml:"objectName,omitempty"`
SecretPath string `yaml:"secretPath,omitempty"`
SecretKey string `yaml:"secretKey,omitempty"`
Method string `yaml:"method,omitempty"`
SecretArgs map[string]interface{} `yaml:"secretArgs,omitempty"`
ObjectName string `yaml:"objectName,omitempty"`
SecretPath string `yaml:"secretPath,omitempty"`
SecretKey string `yaml:"secretKey,omitempty"`
Method string `yaml:"method,omitempty"`
SecretArgs map[string]interface{} `yaml:"secretArgs,omitempty"`
FilePermission os.FileMode `yaml:"filePermission,omitempty"`
}

func Parse(parametersStr, targetPath, permissionStr string) (Config, error) {
Expand Down
10 changes: 5 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
objects = "-\n secretPath: \"v1/secret/foo1\"\n objectName: \"bar1\""
objects = "-\n secretPath: \"v1/secret/foo1\"\n objectName: \"bar1\"\n filePermission: 0600"
certsSPCYaml = `apiVersion: secrets-store.csi.x-k8s.io/v1
kind: SecretProviderClass
metadata:
Expand Down Expand Up @@ -92,8 +92,8 @@ func TestParseParameters(t *testing.T) {
Insecure: true,
},
Secrets: []Secret{
{"bar1", "v1/secret/foo1", "", "GET", nil},
{"bar2", "v1/secret/foo2", "", "", nil},
{"bar1", "v1/secret/foo1", "", "GET", nil, 0},
{"bar2", "v1/secret/foo2", "", "", nil, 0},
},
PodInfo: PodInfo{
Name: "nginx-secrets-store-inline",
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestParseConfig(t *testing.T) {
expected.VaultRoleName = roleName
expected.VaultTLSConfig.Insecure = true
expected.Secrets = []Secret{
{"bar1", "v1/secret/foo1", "", "", nil},
{"bar1", "v1/secret/foo1", "", "", nil, 0o600},
}
return expected
}(),
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestParseConfig(t *testing.T) {
VaultNamespace: "my-vault-namespace",
VaultKubernetesMountPath: "my-mount-path",
Secrets: []Secret{
{"bar1", "v1/secret/foo1", "", "", nil},
{"bar1", "v1/secret/foo1", "", "", nil, 0o600},
},
VaultTLSConfig: api.TLSConfig{
CACert: "my-ca-cert-path",
Expand Down
6 changes: 5 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ func (p *provider) HandleMountRequest(ctx context.Context, cfg config.Config, fl
}
versions[fmt.Sprintf("%s:%s:%s", secret.ObjectName, secret.SecretPath, secret.Method)] = "0"

files = append(files, &pb.File{Path: secret.ObjectName, Mode: int32(cfg.FilePermission), Contents: content})
filePermission := int32(cfg.FilePermission)
if secret.FilePermission != 0 {
filePermission = int32(secret.FilePermission)
}
files = append(files, &pb.File{Path: secret.ObjectName, Mode: filePermission, Contents: content})
p.logger.Info("secret added to mount response", "directory", cfg.TargetPath, "file", secret.ObjectName)
}

Expand Down
1 change: 1 addition & 0 deletions test/bats/configs/vault-kv-secretproviderclass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ spec:
- objectName: "secret-1"
secretPath: "secret/data/kv1"
secretKey: "bar1"
filePermission: 0600
- objectName: "secret-2"
secretPath: "secret/data/kv2"
secretKey: "bar2"
8 changes: 8 additions & 0 deletions test/bats/provider.bats
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,16 @@ teardown(){
result=$(kubectl --namespace=test exec nginx-kv -- cat /mnt/secrets-store/secret-1)
[[ "$result" == "hello1" ]]

# Check file permission is non-default
result=$(kubectl --namespace=test exec nginx-kv -- stat -c '%a' /mnt/secrets-store/..data/secret-1)
[[ "$result" == "600" ]]

result=$(kubectl --namespace=test exec nginx-kv -- cat /mnt/secrets-store/secret-2)
[[ "$result" == "hello2" ]]

# Check file permission is default
result=$(kubectl --namespace=test exec nginx-kv -- stat -c '%a' /mnt/secrets-store/..data/secret-2)
[[ "$result" == "644" ]]
}

@test "2 Sync with kubernetes secrets" {
Expand Down