homebrew-core/Formula/gator.rb

107 lines
4.1 KiB
Ruby

class Gator < Formula
desc "CLI Utility for Open Policy Agent Gatekeeper"
homepage "https://open-policy-agent.github.io/gatekeeper/website/docs/gator"
url "https://github.com/open-policy-agent/gatekeeper/archive/refs/tags/v3.9.2.tar.gz"
sha256 "8573ec1dc12999f49a608a982e464d9323d4f82ca446f6f5d88b8f2ecae32030"
license "Apache-2.0"
head "https://github.com/open-policy-agent/gatekeeper.git", branch: "master"
bottle do
sha256 cellar: :any_skip_relocation, arm64_monterey: "b797d677073a9f4a7c9b2919f74d6594af766006c637f90933788d0a9a22e72b"
sha256 cellar: :any_skip_relocation, arm64_big_sur: "a2d2c39766b2fd3b88e7e7db33a119b07fde8a33790219a016f749653467ded2"
sha256 cellar: :any_skip_relocation, monterey: "8ab57af3eea2d9bc3b0cc4d7c4aeb2632cf108d976642c5b607526878a222cbe"
sha256 cellar: :any_skip_relocation, big_sur: "dfe468b7556bd2d66d5de6ecd4de2700b903da7e9d1bf583d90ac9283920bc96"
sha256 cellar: :any_skip_relocation, catalina: "6591cd22bbc4e0f93f7110921ea88cc4553e7da143dea21bce16a0bd2f33eb13"
sha256 cellar: :any_skip_relocation, x86_64_linux: "6c1a5ea28d15af4c06ff32e1f362af677810d3de04bc5ff427ca69fa0e9117cb"
end
depends_on "go" => :build
def install
ldflags = %W[
-s -w
-X github.com/open-policy-agent/gatekeeper/pkg/version.Version=#{version}
]
system "go", "build", *std_go_args(ldflags: ldflags), "./cmd/gator"
generate_completions_from_executable(bin/"gator", "completion")
end
test do
assert_match "gator is a suite of authorship tools for Gatekeeper", shell_output("#{bin}/gator -h")
# Create a test manifest file
(testpath/"gator-manifest.yaml").write <<~EOS
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-demo-disallowed
annotations:
kubernetes.io/ingress.allow-http: "false"
spec:
tls: [{}]
rules:
- host: example-host.example.com
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: nginx
port:
number: 80
EOS
# Create a test constraint tempalte
(testpath/"template-and-constraints/gator-constraint-template.yaml").write <<~EOS
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: k8shttpsonly
annotations:
description: >-
Requires Ingress resources to be HTTPS only.
Ingress resources must:
- include a valid TLS configuration
- include the `kubernetes.io/ingress.allow-http` annotation, set to
`false`.
https://kubernetes.io/docs/concepts/services-networking/ingress/#tls
spec:
crd:
spec:
names:
kind: K8sHttpsOnly
targets:
- target: admission.k8s.gatekeeper.sh
rego: |
package k8shttpsonly
violation[{"msg": msg}] {
input.review.object.kind == "Ingress"
re_match("^(extensions|networking.k8s.io)/", input.review.object.apiVersion)
ingress := input.review.object
not https_complete(ingress)
msg := sprintf("Ingress should be https. tls configuration and allow-http=false annotation are required for %v", [ingress.metadata.name])
}
https_complete(ingress) = true {
ingress.spec["tls"]
count(ingress.spec.tls) > 0
ingress.metadata.annotations["kubernetes.io/ingress.allow-http"] == "false"
}
EOS
# Create a test constraint file
(testpath/"template-and-constraints/gator-constraint.yaml").write <<~EOS
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: K8sHttpsOnly
metadata:
name: ingress-https-only
spec:
match:
kinds:
- apiGroups: ["extensions", "networking.k8s.io"]
kinds: ["Ingress"]
EOS
assert_empty shell_output("#{bin}/gator test -f gator-manifest.yaml -f template-and-constraints/")
end
end