{"id":238350,"date":"2022-05-13T08:45:00","date_gmt":"2022-05-13T12:45:00","guid":{"rendered":"https:\/\/wordpress-756359-3782526.cloudwaysapps.com\/?p=238350"},"modified":"2025-05-30T07:27:18","modified_gmt":"2025-05-30T07:27:18","slug":"22-05-13-regula","status":"publish","type":"post","link":"https:\/\/www.travis-ci.com\/blog\/22-05-13-regula\/","title":{"rendered":"Travis CI and Regula"},"content":{"rendered":"\n<p>Regula checks infrastructure as code templates (Terraform, CloudFormation, k8s manifests) for AWS, Azure, Google Cloud, and Kubernetes security and compliance using Open Policy, let\u2019s dig in a bit deeper and see how we can make this integrate with Travis CI.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"regula--travis-ci\">Regula + Travis CI<\/h2>\n\n\n\n<p>We\u2019re gonna get Regula up and running, with something I thought of which is using a&nbsp;<code>mock_key.json<\/code>&nbsp;file I created, this is so you can sample it first, once you get a final&nbsp;<code>main.tf<\/code>&nbsp;(Terraform), you can edit as you please. Once Terraform calls Regula then Travis picks up the \/POST and sees if it gets a response from Regula. This is a working example, and Travis is in my opinion the easiest development tool, to set Regula up on and get up and running with max uptime.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"terraform\">Terraform<\/h2>\n\n\n\n<p>We can grab Terraform by putting this in our&nbsp;<code>script<\/code>&nbsp;hook in our&nbsp;<code>travis.yml<\/code>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -fsSL https:\/\/apt.releases.hashicorp.com\/gpg | sudo apt-key add -\nsudo apt-add-repository \"deb &#91;arch=amd64] https:\/\/apt.releases.hashicorp.com $(lsb_release -cs) main\"\nsudo apt-get update &amp;&amp; sudo apt-get install terraform<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"kubernetes\">Kubernetes<\/h2>\n\n\n\n<p>There is a compliant Kubernetes pod in this repo I\u2019ve also banged out, since we\u2019re using Travis there\u2019s not&nbsp;<em>alot<\/em>&nbsp;of need for it, but I\u2019m just trying to give you the most feasible option.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"swagger\">Swagger<\/h2>\n\n\n\n<p>There\u2019s a full&nbsp;<code>swagger.yaml<\/code>&nbsp;file within the repository.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"travis-ci\">Travis CI<\/h2>\n\n\n\n<p>All of the&nbsp;<code>.travis.yml<\/code>&nbsp;file below was made by me (Montana Mendy). Just some crucial points to go over for the&nbsp;<code>.travis.yml<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>group: edge\nbranches:\n  only:\n    - master\n    - \/^(cherry-pick-)?backport-\\d+-to-\/\naddons:\n  apt:\n    packages:\n      - moreutils\nenv:\n  global:\n    - 'PATH=\"$HOME\/.local\/bin:$PATH\"'\n    - REGULA_VERSION=1.6.0\ninstall:\n  - &gt;-\n    if &#91;&#91; \"${TRAVIS_COMMIT_MESSAGE}\" = *\"&#91;Build latest]\"* ]]; then export\n    BUILD_VERSION=\"$(cat packaging\/version | cut -d'-' -f1,2 | sed -e\n    's\/-\/.\/g').latest\"; fi;\nbefore_script:\n  - mkdir \"$HOME\/.local\/bin\"\n  - &gt;-\n    curl -L\n    \"https:\/\/github.com\/fugue\/regula\/releases\/download\/v${REGULA_VERSION}\/regula_${REGULA_VERSION}_Linux_x86_64.tar.gz\"\n    | tar -xvz -C \"$HOME\/.local\/bin\"\n  - 'RANGE1=`echo \"$TRAVIS_COMMIT_RANGE\" | awk ''{n=split($1,a,\".\");print a&#91;1]}''`'\nscript:\n  - REGULA_OUTPUT=\"$(mktemp)\"\n  - (regula run -f json || true) | tee \"$REGULA_OUTPUT\"\n  - REGULA_RULES_PASSED=\"$(jq -r '.summary.rule_results.PASS' \"$REGULA_OUTPUT\")\"\n  - REGULA_RULES_FAILED=\"$(jq -r '.summary.rule_results.FAIL' \"$REGULA_OUTPUT\")\"\n  - regula -v\n  - regula init\n  - regula run\n  - regula run --format table\n  - &gt;-\n    echo \"${REGULA_RULES_PASSED} rules passed, ${REGULA_RULES_FAILED} rules\n    failed\" &gt;&amp;2\n  - 'if &#91;&#91; \"$REGULA_RULES_FAILED\" -gt 0 ]]; then exit 1; fi'\n  - curl -fsSL https:\/\/apt.releases.hashicorp.com\/gpg | sudo apt-key add -\n  - sudo apt-add-repository \"deb &#91;arch=amd64] https:\/\/apt.releases.hashicorp.com $(lsb_release -cs) main\"\n  - sudo apt-get update &amp;&amp; sudo apt-get install terraform\n  - terraform -v\nafter_deploy:\n  - &gt;-\n    if &#91; -n \"${BUILDER_NAME}\" ]; then rm -rf \/home\/${BUILDER_NAME}\/* &amp;&amp; echo\n    \"Cleared \/home\/${BUILDER_NAME} directory\" || echo \"Failed to clean\n    \/home\/${BUILDER_NAME} directory\"; fi;\n  - 'if &#91; -d \"${PACKAGES_DIRECTORY}\" ]; then rm -rf \"${PACKAGES_DIRECTORY}\"; fi;'\n  - &gt;-\n    if: \"((branch IN (master, develop) &amp;&amp; type = push) OR branch =~ \/.*env.*\/ OR\n    commit_message =~ \/\\\\&#91;recreate env\\\\]\/) AND commit_message !~ \/\\\\&#91;delete\n    env\\\\]\/ AND type != cron AND commit_message !~ \/\\\\&#91;execute .*. test\\\\]\/ AND\n    commit_message !~ \/\\\\&#91;start recreate scheduler\\\\]\/\"<\/code><\/pre>\n\n\n\n<p>We are going to change the&nbsp;<code>PATH<\/code>&nbsp;and use&nbsp;<code>cURL<\/code>&nbsp;to fetch the latest version of Regula, make sure you have it by going to https:\/\/regula.dev\/. You can also make sure by running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>regula version<\/code><\/pre>\n\n\n\n<p>In the CLI, but it will print out something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"316\" src=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2024\/07\/167257899-26f89d71-dd03-43e5-89c2-a317a5323b04-1024x316-1.png\" alt=\"\" class=\"wp-image-241346\" srcset=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2024\/07\/167257899-26f89d71-dd03-43e5-89c2-a317a5323b04-1024x316-1.png 1024w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2024\/07\/167257899-26f89d71-dd03-43e5-89c2-a317a5323b04-1024x316-1-300x93.png 300w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2024\/07\/167257899-26f89d71-dd03-43e5-89c2-a317a5323b04-1024x316-1-768x237.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>If the&nbsp;<code>regula<\/code>&nbsp;command isn\u2019t working you need to install Regula and the binary. Make sure you run the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew tap fugue\/regula<\/code><\/pre>\n\n\n\n<p>Once brew has symlinked&nbsp;<code>fugue\/regula<\/code>, you can now start the install process:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew install regula<\/code><\/pre>\n\n\n\n<p>If you want to upgrade regula, just run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew upgrade regula<\/code><\/pre>\n\n\n\n<p>You\u2019ll also notice we used&nbsp;<code>cURL<\/code>&nbsp;to grab Terraform via:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>script:\n  - curl -fsSL https:\/\/apt.releases.hashicorp.com\/gpg | sudo apt-key add -\n  - sudo apt-add-repository \"deb &#91;arch=amd64] https:\/\/apt.releases.hashicorp.com $(lsb_release -cs) main\"\n  - sudo apt-get update &amp;&amp; sudo apt-get install terraform\n  - terraform -v<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"running-regula-with-a-config-file\">Running Regula with a config file<\/h2>\n\n\n\n<p>You\u2019ll want to pull up your CLI, make sure you have a directory named&nbsp;<code>\/infra_tf<\/code>, and run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>regula run -f json --include example_custom_rule --include config.rego infra_tf<\/code><\/pre>\n\n\n\n<p>The output should be something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> {\n      \"controls\": &#91;\n        \"CIS-Kubernetes_v1.6.1_5.1.6\"\n      ],\n      \"filepath\": \"pod-compliant.yaml\",\n      \"input_type\": \"k8s\",\n      \"provider\": \"kubernetes\",\n      \"resource_id\": \"Pod.default.hello\",\n      \"resource_type\": \"Pod\",\n      \"rule_description\": \"Service account 'automountServiceAccountToken' should be set to 'false'. Avoid automounting service account tokens. Service account tokens are used to authenticate requests from in-cluster processes to the Kubernetes API server. Many workloads do not need to communicate with the API server and hence should have automountServiceAccountToken set to false.\",\n      \"rule_id\": \"FG_R00484\",\n      \"rule_message\": \"\",\n      \"rule_name\": \"k8s_service_account_tokens\",\n      \"rule_result\": \"PASS\",\n      \"rule_severity\": \"Medium\",\n      \"rule_summary\": \"Service account 'automountServiceAccountToken' should be set to 'false'\",\n      \"source_location\": &#91;\n        {\n          \"path\": \"pod-compliant.yaml\",\n          \"line\": 1,\n          \"column\": 1\n        }\n      ]\n    }\n  \"summary\": {\n    \"filepaths\": &#91;\n      \"pod-compliant.yaml\"\n    ],\n    \"rule_results\": {\n      \"FAIL\": 0,\n      \"PASS\": 14,\n      \"WAIVED\": 0\n    },\n    \"severities\": {\n      \"Critical\": 0,\n      \"High\": 0,\n      \"Informational\": 0,\n      \"Low\": 0,\n      \"Medium\": 0,\n      \"Unknown\": 0\n    }\n  }<\/code><\/pre>\n\n\n\n<p>Depending on the policies, enforcements you\u2019ve set it will look different. Now let\u2019s make sure in Travis CI if our pods are compliant:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"980\" height=\"605\" src=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2024\/07\/168380916-509070a5-fb16-4b42-abe9-38dd920220ff-980x605-1.png\" alt=\"\" class=\"wp-image-241344\" srcset=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2024\/07\/168380916-509070a5-fb16-4b42-abe9-38dd920220ff-980x605-1.png 980w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2024\/07\/168380916-509070a5-fb16-4b42-abe9-38dd920220ff-980x605-1-300x185.png 300w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2024\/07\/168380916-509070a5-fb16-4b42-abe9-38dd920220ff-980x605-1-768x474.png 768w\" sizes=\"auto, (max-width: 980px) 100vw, 980px\" \/><\/figure>\n\n\n\n<p>They certainly seem to be, now we can move on!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"usage\">Usage<\/h2>\n\n\n\n<p>The usage does get pretty complex from the Regula side &#8211; I\u2019ve been thinking about writing a bash script to help users and make it easier.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>Here\u2019s my <a href=\"https:\/\/github.com\/Montana\/travis-regula\" target=\"_blank\" rel=\"noreferrer noopener\">repository for the integration<\/a>, as always I try and make as much of my code as open source as possible.&nbsp;As always, if you have any questions, please email me at&nbsp;<a href=\"mailto:montana@travis-ci.org\">montana@travis-ci.org<\/a>&nbsp;for help.<\/p>\n\n\n\n<p>Happy building!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Regula checks infrastructure as code templates for AWS, Azure, Google Cloud, and Kubernetes security and compliance using Open Policy. Now, you can integrate this with Travis CI.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_breakdance_hide_in_design_set":false,"_breakdance_tags":"","footnotes":""},"categories":[16],"tags":[7,19,20,5],"class_list":["post-238350","post","type-post","status-publish","format-standard","hentry","category-news","tag-community","tag-feature","tag-infrastructure","tag-news"],"_links":{"self":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/comments?post=238350"}],"version-history":[{"count":2,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238350\/revisions"}],"predecessor-version":[{"id":241347,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238350\/revisions\/241347"}],"wp:attachment":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/media?parent=238350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/categories?post=238350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/tags?post=238350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}