-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add config file support and allow configurable filters
- Loading branch information
Showing
19 changed files
with
355 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,3 +120,5 @@ bin | |
# idea | ||
/.idea | ||
/dist | ||
|
||
gomock* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package config_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestHandler(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Config Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package config | ||
|
||
import "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
|
||
type FieldFilter struct { | ||
Skip [][]string `yaml:"skip"` | ||
SkipIfNil [][]string `yaml:"skipIfNil"` | ||
} | ||
|
||
func (ff *FieldFilter) Apply(sec map[string]interface{}) { | ||
for _, fieldPath := range ff.Skip { | ||
unstructured.RemoveNestedField(sec, fieldPath...) | ||
} | ||
|
||
for _, fieldPath := range ff.SkipIfNil { | ||
removeFieldIfNull(sec, fieldPath...) | ||
} | ||
} | ||
|
||
func removeFieldIfNull(sec map[string]interface{}, fields ...string) { | ||
path := fields[:len(fields)-1] | ||
name := fields[len(fields)-1] | ||
if m, ok, _ := unstructured.NestedMap(sec, path...); ok { | ||
f := m[name] | ||
if f == nil { | ||
delete(m, name) | ||
_ = unstructured.SetNestedMap(sec, m, path...) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package config | ||
|
||
import ( | ||
. "github.com/bakito/sealed-secrets-web/pkg/test" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var ( | ||
testConfigFile = "../../testdata/config.yaml" | ||
) | ||
|
||
var _ = Describe("Filter", func() { | ||
Context("removeNullFields", func() { | ||
var ff *FieldFilter | ||
BeforeEach(func() { | ||
cfg, err := Parse() | ||
Ω(err).ShouldNot(HaveOccurred()) | ||
ff = cfg.FieldFilter | ||
}) | ||
It("should remove nil fields", func() { | ||
secretData := map[string]interface{}{ | ||
"spec": map[string]interface{}{ | ||
"template": map[string]interface{}{ | ||
"data": nil, | ||
"metadata": map[string]interface{}{ | ||
"creationTimestamp": nil, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
ff.Apply(secretData) | ||
|
||
Ω(SubMap(secretData, "spec", "template")).ShouldNot(HaveKey("data")) | ||
Ω(SubMap(secretData, "spec", "template", "metadata")).ShouldNot(HaveKey("creationTimestamp")) | ||
}) | ||
It("should keep non nil fields", func() { | ||
secretData := map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"creationTimestamp": "00:00", | ||
}, | ||
"spec": map[string]interface{}{ | ||
"template": map[string]interface{}{ | ||
"data": map[string]interface{}{}, | ||
"metadata": map[string]interface{}{ | ||
"creationTimestamp": "00:00", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
ff.Apply(secretData) | ||
|
||
Ω(secretData["metadata"]).Should(HaveKey("creationTimestamp")) | ||
Ω(SubMap(secretData, "spec", "template")).Should(HaveKey("data")) | ||
}) | ||
}) | ||
Context("removeRuntimeFields", func() { | ||
var ff *FieldFilter | ||
BeforeEach(func() { | ||
config = &testConfigFile | ||
cfg, err := Parse() | ||
Ω(err).ShouldNot(HaveOccurred()) | ||
ff = cfg.FieldFilter | ||
}) | ||
It("should remove the fields", func() { | ||
secretData := map[string]interface{}{ | ||
"metadata": map[string]interface{}{ | ||
"creationTimestamp": "foo", | ||
"managedFields": "foo", | ||
"resourceVersion": "foo", | ||
"selfLink": "foo", | ||
"uid": "foo", | ||
"annotations": map[string]interface{}{ | ||
"kubectl.kubernetes.io/last-applied-configuration": "foo", | ||
"foo": "bar", | ||
}, | ||
}, | ||
} | ||
|
||
ff.Apply(secretData) | ||
Ω(secretData["metadata"]).ShouldNot(HaveKey("creationTimestamp")) | ||
Ω(secretData["metadata"]).ShouldNot(HaveKey("managedFields")) | ||
Ω(secretData["metadata"]).ShouldNot(HaveKey("resourceVersion")) | ||
Ω(secretData["metadata"]).ShouldNot(HaveKey("selfLink")) | ||
Ω(secretData["metadata"]).ShouldNot(HaveKey("uid")) | ||
Ω(SubMap(secretData, "metadata", "annotations")).ShouldNot(HaveKey("kubectl.kubernetes.io/last-applied-configuration")) | ||
Ω(SubMap(secretData, "metadata", "annotations")).Should(HaveKey("foo")) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.