Skip to content

Commit

Permalink
r/pod+replication_controller: Add validation for file mode bits
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jul 5, 2017
1 parent 380e750 commit 92965c0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
7 changes: 4 additions & 3 deletions kubernetes/schema_pod_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions kubernetes/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions kubernetes/validators_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 92965c0

Please sign in to comment.