cloud66-oss / copper Goto Github PK
View Code? Open in Web Editor NEWA configuration file validator for Kubernetes.
Home Page: https://help.cloud66.com/copper/
License: Apache License 2.0
A configuration file validator for Kubernetes.
Home Page: https://help.cloud66.com/copper/
License: Apache License 2.0
c66-copper's executable "console" conflicts with countries
Overwrite the executable? [yN] y
c66-copper's executable "setup" conflicts with paper_trail-background
Overwrite the executable? [yN] y
Hi There!
I just installed ruby and copper on my mac to give this a try. I am using the basic example as described in https://copper.sh/ and I get the error shown below. Am I doing something wrong? Yes I am a ruby newbie...
Carltons-MacBook-Pro:rendered-armada.d ckmason$ cat cop-deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
namespace: foobar
name: foo
spec:
template:
spec:
containers:
- name: mysql
image: index.docker.io/library/mysql:6.5.0
Carltons-MacBook-Pro:rendered-armada.d ckmason$
Carltons-MacBook-Pro:rendered-armada.d ckmason$
Carltons-MacBook-Pro:rendered-armada.d ckmason$ cat my_rules.cop
rule NoLatest ensure { // use of latest as image tag is not allowed
fetch("$.spec.template.spec.containers..image")
.as(:image)
.pick(:tag)
.contains("latest") == false
}
Carltons-MacBook-Pro:rendered-armada.d ckmason$
Carltons-MacBook-Pro:rendered-armada.d ckmason$
Carltons-MacBook-Pro:rendered-armada.d ckmason$ copper check --rules my_rules.cop --file cop-deploy.yaml
Validating part 0
Traceback (most recent call last):
15: from /usr/local/lib/ruby/gems/2.5.0/bin/copper:23:in `<main>'
14: from /usr/local/lib/ruby/gems/2.5.0/bin/copper:23:in `load'
13: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:7:in `<top (required)>'
12: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:121:in `<module:Copper>'
11: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/base.rb:466:in `start'
10: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
9: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
8: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
7: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:73:in `check'
6: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:73:in `each_with_index'
5: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:73:in `each'
4: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:75:in `block in check'
3: from /usr/local/Cellar/ruby/2.5.3_1/lib/ruby/2.5.0/psych.rb:263:in `load'
2: from /usr/local/Cellar/ruby/2.5.3_1/lib/ruby/2.5.0/psych.rb:350:in `parse'
1: from /usr/local/Cellar/ruby/2.5.3_1/lib/ruby/2.5.0/psych.rb:402:in `parse_stream'
/usr/local/Cellar/ruby/2.5.3_1/lib/ruby/2.5.0/psych.rb:402:in `parse': (<unknown>): mapping values are not allowed in this context at line 11 column 22 (Psych::SyntaxError)
I don't see a way to write Copper rules on the filename. We have a policy that the .yaml
manifest file must be named in a certain way. For example, NAME-TYPE.yaml
where NAME
must match .spec.metadata.name
. and TYPE
must match .spec.kind
.
Similarly, I might want to enforce rules on the parent directories of the manifest. For example, if the directory structure has some correspondence on the .spec.medata.labels
values.
Would that be possible to implement, perhaps by exposing the raw filepath to a rule?
The current version of getting started contains
$ copper check –rules my_rule.cop –file service.yml
This looks like the command uses one dash (-rules
) rather than two (--rules
), which is probably a quirk of the formatter.
This is exacerbated by there being some problems in CLI syntax handling, meaning the output is an unhelpful
$ copper check -rules foo -file bar ERROR: "copper check" was called with arguments ["-rules", "foo", "-file", "bar"] Usage: "copper check"
However, copper check
also doesn't suggest what the problem is:
$ copper check ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/c66-copper-0.0.4/bin/copper:39:in ``exists?': no implicit conversion of nil into String (TypeError)
...`
To work around, someone can check the source and see what it's expecting, but this could be a bit of a speedbump for people ramping up on copper. The website doesn't appear to be part of the repo or I'd make a PR.
YAML files can optionally being with 3 dashes, e.g ---
. Copper will parse that as a resource. For example, given the manifest:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
spec:
template:
spec:
containers:
- image: busybox
name: busybox
and rule:
rule DeploymentApiVersion ensure {
fetch("$[?(@['kind'] == 'Deployment')].apiVersion").first == "apps/v1"
}
copper will fail for the opening dashes:
$ copper check --rules rule.cop --file deployment.yaml
Validating part 0
DeploymentApiVersion - FAIL
Validating part 1
DeploymentApiVersion - PASS
Hello Again,
I am trying to validate that a simple Ingress .yaml contains a certain ingress class in its annotations yet, I get:
/usr/local/lib/ruby/gems/2.5.0/gems/jsonpath-0.9.8/lib/jsonpath.rb:39:in `initialize': can't modify frozen String (FrozenError)
I think it has something to do with the .
and /
in my annotation values.
Here is my input yaml:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: eurekap
spec:
backend:
serviceName: public-gdpr-data-deletion
servicePort: 443
Here is my Copper rule file:
rule IngressAnnotation ensure {
fetch("$.metadata.annotations.kubernetes.io/ingress.class").first == "eurekap"
}
And when I run Copper, I get the following error:
Carltons-MacBook-Pro:rendered-armada.d ckmason$ copper check --rules ingress.cop --file ingress-good.yaml
Validating part 0
Traceback (most recent call last):
23: from /usr/local/lib/ruby/gems/2.5.0/bin/copper:23:in `<main>'
22: from /usr/local/lib/ruby/gems/2.5.0/bin/copper:23:in `load'
21: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:7:in `<top (required)>'
20: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:121:in `<module:Copper>'
19: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/base.rb:466:in `start'
18: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
17: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
16: from /usr/local/lib/ruby/gems/2.5.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
15: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:73:in `check'
14: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:73:in `each_with_index'
13: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:73:in `each'
12: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:76:in `block in check'
11: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/bin/copper:85:in `validate'
10: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/lib/copper/copper.rb:12:in `execute'
9: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/lib/copper/root.rb:5:in `value'
8: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/lib/copper/root.rb:5:in `each'
7: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/lib/copper/root.rb:6:in `block in value'
6: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/lib/copper/rule_definition.rb:11:in `value'
5: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/lib/copper/logic.rb:6:in `value'
4: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/lib/copper/comparison.rb:5:in `value'
3: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/lib/copper/expression.rb:5:in `value'
2: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/lib/copper/functions/fetch.rb:14:in `value'
1: from /usr/local/lib/ruby/gems/2.5.0/gems/c66-copper-0.0.7/lib/copper/functions/fetch.rb:14:in `new'
/usr/local/lib/ruby/gems/2.5.0/gems/jsonpath-0.9.8/lib/jsonpath.rb:39:in `initialize': can't modify frozen String (FrozenError)
If I modify both my .yaml and the rule to get rid of the slashes and dots in the annotation element, it works. I can't use that as a solution though. So, its got something to do with the format of my annotation and not sure how to get it to read them properly.
For example, if I modify my .yaml and rule to change my annotation from kubernetes.io/ingress.class
to ingress-class
it works.
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
ingress-class: eurekap
spec:
backend:
serviceName: public-gdpr-data-deletion
servicePort: 443
The Copper rule to be:
rule IngressAnnotation ensure {
fetch("$.metadata.annotations.ingress-class").first == "eurekap"
}
Then it works:
Carltons-MacBook-Pro:rendered-armada.d ckmason$ copper check --rules ingress.cop --file ingress-good.yaml
Validating part 0
IngressAnnotation - PASS
Thanks for your help.
I think it would be a nice addition to add lodash to the libjs library.
Filtering, mapping, etc. is quite a lot of work in ES5, lodash can make this a lot easier for people.
When running on a multi-template file, it seems the return code only reflects the last segment. For example, this output corresponds to a return code 1
:
Validating part 0
ApiV1Only - FAIL
Validating part 1
ApiV1Only - PASS
Validating part 2
ApiV1Only - FAIL
...while this output corresponds to a return code 0
:
Validating part 0
ApiV1Only - FAIL
Validating part 1
ApiV1Only - PASS
Validating part 2
ApiV1Only - PASS
I would expect a return code 0
only when all validations pass, and a 1
otherwise.
I will look at making this change and opening a PR - just wanted to document it here first!
Under the jsonpath part, it says:
fetch("$.spec.spec.containers[?(@.name == 'mysql')]") // will return ["index.docker.io/library/ubuntu:latest"]
But I think it should be something like (not sure if it's missing a dot):
fetch("$.spec.spec.containers[?(@.name == 'mysql')].image") // will return ["quay.io/mysql:2.3.0"]
I tried to install Copper with the following steps, but actually another copper (https://rubygems.org/gems/copper) was installed. Looks like it is not yours.
Copper is available as a Ruby gem. We recommend installing Copper using Rubygems.
To install Copper, use the following command:
$ gem install copper
You should now be able to run $ copper version successfully.
Hi all, it looks like https://copper.sh still refers and documents the v1 of copper instead of the current V2 version?
Kind of a fundamental flaw really
If I have a fake concatenated manifest (in this case the output of `helm template), the I find it tricky to filter on object types.
---
apiVersion: v1
kind: ConfigMap
---
apiVersion: v1
kind: ConfigMap
---
apiVersion: v1
kind: ConfigMap
---
apiVersion: v1
kind: Service
---
apiVersion: apps/v1beta1
kind: Deployment
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
Aside, the tool recognizes the first element as an empty doc, I think.
How do I enforce things for kind: Service
only?
Is there a way to read bunch of yaml files from directory instead just a file to apply the rule. I currently have yml files stored in "files" directory and --file option isn't reading all the files.
copper check --rules myrule.cop --file files/*.yml
tried
copper check --rules myrule.cop --dir files/*.yml , but no worky.
ERROR: "copper check" was called with arguments ["--dir", "files/elastic_scc.yml", "files/test-admin.yml"]
Usage: "copper check"
I came across copper via Kubernetes SIG Apps. What is the differentiator to kubetest?
I think right now the ES5 javascript engine is being used for the validators. And by using the ES6 engine it would be much easier to write some validation rules due to the additional features ES6 comes with.
If a validator doesn't find at least one violation, it'll result in an error:
interface {} is []interface {}, not []map[string]interface {}
This can be reproduced easily by modifying the (sample)[https://github.com/cloud66-oss/copper/blob/2.0.0/samples/sample.yml] by either of the following:
Deployment
object entirely from the yamllatest
into any other stringA declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.