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

Preserve whitespace #168

Merged
merged 5 commits into from
Jun 2, 2021
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
15 changes: 15 additions & 0 deletions examples/container-deny-latest-tag/src.rego
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
# Using the latest tag on images can cause unexpected problems in production. By specifying a pinned version
# we can have higher confidence that our applications are immutable and do not change unexpectedly.
#
# The following snippet is an example of how to satisfy this requirement:
#
# ```
# apiVersion: apps/v1
# kind: Deployment
# metadata:
# name: redis
# spec:
# template:
# spec:
# containers:
# - name: redis
# image: redis:6.2
# ```
jpreese marked this conversation as resolved.
Show resolved Hide resolved
#
# @kinds apps/DaemonSet apps/Deployment apps/StatefulSet core/Pod
package container_deny_latest_tag

Expand Down
15 changes: 15 additions & 0 deletions examples/policies-no-rego.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ _source: [psp-deny-privileged](psp-deny-privileged)_
Using the latest tag on images can cause unexpected problems in production. By specifying a pinned version
we can have higher confidence that our applications are immutable and do not change unexpectedly.

The following snippet is an example of how to satisfy this requirement:

```
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
template:
spec:
containers:
- name: redis
image: redis:6.2
```


_source: [container-deny-latest-tag](container-deny-latest-tag)_

Expand Down
15 changes: 15 additions & 0 deletions examples/policies.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,21 @@ _source: [psp-deny-privileged](psp-deny-privileged)_
Using the latest tag on images can cause unexpected problems in production. By specifying a pinned version
we can have higher confidence that our applications are immutable and do not change unexpectedly.

The following snippet is an example of how to satisfy this requirement:

```
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
template:
spec:
containers:
- name: redis
image: redis:6.2
```

### Rego

```rego
Expand Down
28 changes: 10 additions & 18 deletions internal/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,15 @@ func runCreateCommand(path string) error {
return fmt.Errorf("writing template: %w", err)
}

if viper.GetBool("skip-constraints") {
if viper.GetBool("skip-constraints") || violation.SkipConstraint() {
continue
}

// skip Constraint generation if there are parameters on the template
// Skip Constraint generation if there are parameters on the template.
if len(violation.Parameters()) > 0 {
continue
}

if violation.SkipConstraint() {
continue
}

constraint, err := getConstraint(violation)
if err != nil {
return fmt.Errorf("get constraint: %w", err)
Expand Down Expand Up @@ -179,14 +175,13 @@ func getOpenAPISchemaProperties(r rego.Rego) map[string]apiextensionsv1beta1.JSO
}

func getConstraint(violation rego.Rego) (unstructured.Unstructured, error) {
constraint := unstructured.Unstructured{}

gvk := schema.GroupVersionKind{
Group: "constraints.gatekeeper.sh",
Version: "v1beta1",
Kind: violation.Kind(),
}

constraint := unstructured.Unstructured{}
constraint.SetGroupVersionKind(gvk)
constraint.SetName(violation.Name())

Expand All @@ -200,22 +195,18 @@ func getConstraint(violation rego.Rego) (unstructured.Unstructured, error) {

matchers, err := violation.Matchers()
if err != nil {
return constraint, err
}
if len(matchers.KindMatchers) == 0 {
return constraint, nil
return unstructured.Unstructured{}, fmt.Errorf("matchers: %w", err)
jpreese marked this conversation as resolved.
Show resolved Hide resolved
}

if len(matchers.KindMatchers) != 0 {
err := setKindMatcher(&constraint, matchers.KindMatchers)
if err != nil {
return constraint, err
if err := setKindMatcher(&constraint, matchers.KindMatchers); err != nil {
return unstructured.Unstructured{}, fmt.Errorf("set kind matcher: %w", err)
}
}

if len(matchers.MatchLabelsMatcher) != 0 {
err := setMatchLabelsMatcher(&constraint, matchers.MatchLabelsMatcher)
if err != nil {
return constraint, err
if err := setMatchLabelsMatcher(&constraint, matchers.MatchLabelsMatcher); err != nil {
return unstructured.Unstructured{}, fmt.Errorf("set match labels matcher: %w", err)
}
}

Expand All @@ -234,6 +225,7 @@ func setKindMatcher(constraint *unstructured.Unstructured, kindMatchers []rego.K
if kindMatcher.APIGroup == "core" {
apiGroup = ""
}

apiGroups = appendIfNotExists(apiGroups, apiGroup)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/commands/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func getDocumentation(path string, outputDirectory string) (map[rego.Severity][]

matchers, err := policy.Matchers()
if err != nil {
return nil, err
return nil, fmt.Errorf("matchers: %w", err)
}
resources := matchers.KindMatchers.String()
if resources == "" {
Expand Down
23 changes: 14 additions & 9 deletions internal/rego/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func (k KindMatchers) String() string {
for _, kindMatcher := range k {
result += kindMatcher.APIGroup + "/" + kindMatcher.Kind + " "
}
result = strings.TrimSpace(result)

result = strings.TrimSpace(result)
return result
}

Expand All @@ -38,25 +38,27 @@ func (m MatchLabelsMatcher) String() string {
for k, v := range m {
result += fmt.Sprintf("%s=%s ", k, v)
}

return strings.TrimSpace(result)
}

// Matchers returns all of the matchers found in the rego file.
func (r Rego) Matchers() (Matchers, error) {
var matchers Matchers
for _, comment := range r.headerComments {
if strings.HasPrefix(comment, "@kinds") {
if commentStartsWith(comment, "@kinds") {
var err error
matchers.KindMatchers, err = getKindMatchers(comment)
if err != nil {
return matchers, err
return Matchers{}, fmt.Errorf("get kind matchers: %w", err)
}
}
if strings.HasPrefix(comment, "@matchlabels") {

if commentStartsWith(comment, "@matchlabels") {
var err error
matchers.MatchLabelsMatcher, err = getMatchLabelsMatcher(comment)
if err != nil {
return matchers, err
return Matchers{}, fmt.Errorf("get match labels matcher: %w", err)
}
}
}
Expand All @@ -65,11 +67,10 @@ func (r Rego) Matchers() (Matchers, error) {
}

func getKindMatchers(comment string) ([]KindMatcher, error) {
var kindMatchers []KindMatcher

kindMatcherText := strings.TrimSpace(strings.SplitAfter(comment, "@kinds")[1])
kindMatcherGroups := strings.Split(kindMatcherText, " ")

var kindMatchers []KindMatcher
for _, kindMatcherGroup := range kindMatcherGroups {
kindMatcherSegments := strings.Split(kindMatcherGroup, "/")
if len(kindMatcherSegments) != 2 {
Expand All @@ -89,15 +90,19 @@ func getKindMatchers(comment string) ([]KindMatcher, error) {

func getMatchLabelsMatcher(comment string) (MatchLabelsMatcher, error) {
matcher := make(map[string]string)

matcherText := strings.TrimSpace(strings.SplitAfter(comment, "@matchlabels")[1])

for _, token := range strings.Fields(matcherText) {
split := strings.Split(token, "=")
if len(split) != 2 {
return nil, fmt.Errorf("invalid @matchlabels annotation token: %s", token)
}

matcher[split[0]] = split[1]
}

return matcher, nil
}

func commentStartsWith(comment string, keyword string) bool {
return strings.HasPrefix(strings.TrimSpace(comment), keyword)
}
jpreese marked this conversation as resolved.
Show resolved Hide resolved
Loading