From 92965c07a2107b87fe73aeab58c4102319d8df87 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Wed, 5 Jul 2017 09:01:57 +0100 Subject: [PATCH] r/pod+replication_controller: Add validation for file mode bits --- kubernetes/schema_pod_spec.go | 7 ++++--- kubernetes/validators.go | 8 ++++++++ kubernetes/validators_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 kubernetes/validators_test.go diff --git a/kubernetes/schema_pod_spec.go b/kubernetes/schema_pod_spec.go index 5bec115d02..674e0e8a19 100644 --- a/kubernetes/schema_pod_spec.go +++ b/kubernetes/schema_pod_spec.go @@ -188,9 +188,10 @@ func volumeSchema() *schema.Resource { }, }, "default_mode": { - Type: schema.TypeInt, - Description: "Optional: mode bits to use on created files by default. Must be a value between 0 and 0777. Defaults to 0644. Directories within the path are not affected by this setting. This might be in conflict with other options that affect the file mode, like fsGroup, and the result can be other mode bits set.", - Optional: true, + Type: schema.TypeInt, + Description: "Optional: mode bits to use on created files by default. Must be a value between 0 and 0777. Defaults to 0644. Directories within the path are not affected by this setting. This might be in conflict with other options that affect the file mode, like fsGroup, and the result can be other mode bits set.", + Optional: true, + ValidateFunc: validateModeBits, }, "name": { Type: schema.TypeString, diff --git a/kubernetes/validators.go b/kubernetes/validators.go index 03dd2cf555..9959d154de 100644 --- a/kubernetes/validators.go +++ b/kubernetes/validators.go @@ -165,6 +165,14 @@ func validateTerminationGracePeriodSeconds(value interface{}, key string) (ws [] return } +func validateModeBits(value interface{}, key string) (ws []string, es []error) { + v := value.(int) + if v < 0 || v > 0777 { + es = append(es, fmt.Errorf("%s (%#o) expects octal notation (a value between 0 and 0777)", key, v)) + } + return +} + func validateAttributeValueDoesNotContain(searchString string) schema.SchemaValidateFunc { return func(v interface{}, k string) (ws []string, errors []error) { input := v.(string) diff --git a/kubernetes/validators_test.go b/kubernetes/validators_test.go new file mode 100644 index 0000000000..78b5699065 --- /dev/null +++ b/kubernetes/validators_test.go @@ -0,0 +1,27 @@ +package kubernetes + +import ( + "testing" +) + +func TestValidateModeBits(t *testing.T) { + validCases := []int{ + 0, 0001, 0644, 0777, + } + for _, mode := range validCases { + _, es := validateModeBits(mode, "mode") + if len(es) > 0 { + t.Fatalf("Expected %#o to be valid: %#v", mode, es) + } + } + + invalidCases := []int{ + -5, -1, 512, 777, + } + for _, mode := range invalidCases { + _, es := validateModeBits(mode, "mode") + if len(es) == 0 { + t.Fatalf("Expected %#o to be invalid", mode) + } + } +}