Answers: <p>The tools that we have include in our Terraform pipelines to check code quality and best practises are;</p>
  <ol>
  <li><a href="https://github.com/terraform-linters/tflint" rel="nofollow noreferrer">Tflint</a></li>
  <li><a href="https://github.com/tfsec/tfsec" rel="nofollow noreferrer">TFSec</a></li>
  <li><a href="https://www.checkov.io/" rel="nofollow noreferrer">Checkov</a></li>
  </ol>
  <p>This any issues get highlighted early at the MR stage.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-05-13T10:18:21.550Z
Answers: 2021-05-13T10:18:21.550Z
Answers: Alastair Montgomery
Answers: <p>It's difficult to provide a definitive answer given the scope of the question, but the  goal of testing infrastructure is both laudable and seldom done at all.</p>
  <p>To my knowledge, you are looking for something akin to <a href="https://www.openpolicyagent.org/" rel="nofollow noreferrer">Open Policy Agent</a> which provides a general policy language in which to express arbitrary policy as code. I would suggest investigating that at length.</p>
  <p>At the risk of not providing a complete answer, I will give my best shot for some concrete examples as well.</p>
  <h2>Compliance as Code</h2>
  <p>In general, you need something which is often referred to as a &quot;compliance framework&quot;, and in the DevOps world more often referred to as &quot;Policy as Code&quot;. This allows you to define policies as code, which really means &quot;machine-readable and executable&quot;. You specify what the policy is in the DSL of the framework, it is executed at some stage of your pipeline, and fails the change if it violates the policy.</p>
  <p>To illustrate the point, consider someone submitting a pull request to a terraform module that inadvertently opens a port on a security group to the world which the policy says should be closed. This may be difficult to determine when you're writing the code, but the compliance test will catch it.</p>
  <p>There are a few ways to do this, but I will mention two. A notable exception is <a href="https://www.pulumi.com/docs/guides/crossguard/" rel="nofollow noreferrer">Pulumi's Crossguard</a>, but I don't have enough experience to speak to that.</p>
  <h2>Hashicorp Sentinel</h2>
  <p>In the Hashicorp world, there is the <a href="https://www.hashicorp.com/sentinel" rel="nofollow noreferrer">Sentinel Framework</a>. This is an Enterprise product, so may not be relevant to your situation, but it is one of the best implementations I've seen. Importantly, it has a <a href="https://docs.hashicorp.com/sentinel/writing/testing" rel="nofollow noreferrer">built-in testing framework </a> which aids in writing good policy code, which is also very expressive. I have used this as inspiration for how to write policy in Chef Inspec, which is the other example I will cite.</p>
  <h2>Chef Inspec</h2>
  <p><a href="https://docs.chef.io/inspec/" rel="nofollow noreferrer">Chef Inspec</a> is a framework for writing policy as code using rspec-style assertions. It uses a DSL based on ruby and supports major clouds (AWS, GCP, Azure) as well as OS-level resources. It is open source, so you can use it anywhere. You use it to make assertions in individual <em>resources</em> which are defined in the inspec DSL, <em>e.g.</em> &quot;user on an OS&quot; or &quot;AWS EC2 instance&quot;. Writing the tests consists of assertions against the actual state of those resources, which makes it quite like a unit test. However, Inspec does not have a built-in testing framework for itself like Sentinel does - although you could use the ruby ecosystem for doing this.</p>
  <hr />
  <p>Some notes:</p>
  <ol>
  <li>Both of these tools allow you to express your policy as code, and to start extending the test coverage of that policy over your existing infrastructure.</li>
  <li>Sentinel will catch violations before they are applied, but Inspec needs to be executed against an already-provisioned state.</li>
  <li>Both will do what you required, but depend on your tech stack as to whether they will be appropriate for you</li>
  <li>There is no magic bullet
  <ol>
  <li>for both of them you will need to write your policy as code. Usually this means taking some form of human-readable documentation and writing it in the DSL of the framework.</li>
  <li>On the other hand, this provides an excellent opportunity to collaborate with the security team and do what folks are calling DevSecOps these days.</li>
  </ol>
  </li>
  </ol>
  
Answers: CC BY-SA 4.0
Answers: 2021-01-13T06:57:24.587Z
Answers: 2021-01-13T06:57:24.587Z
Answers: Bruce Becker
Body: <p>In the dev world, we have several tools available to us for automated quality checks.  We have unit tests that run on each build, Sonar to check for code quality, metrics being sent to Grafana and Okta with automated alarms, etc.</p>
  <p>I'm not very familiar with the ops world; are there similar automated tools available for our ops code?</p>
  <p>For Terraform scripts, Dockerfiles, Helm/K8 scripts, Cloudbees configurations and other ops code, the concerns I would like to see automation for are:</p>
  <ul>
  <li>Syntax checking</li>
  <li>Code smells / anti patterns</li>
  <li>Routing / Connection checking for accuracy</li>
  <li>Security best practice checking (don't expose these ports or include these credentials,or grant these permissions kind-of-a-thing)</li>
  <li>Appropriate provisioning (too many or too few resources allocated for the size of the task)</li>
  <li>High cost warnings</li>
  <li>Unit test-ish tools to ensure we aren't breaking the build in more subtle ways than &quot;did it deploy successfully or not&quot;</li>
  </ul>
  <p>There's likely a lot more that I don't even know to look for given my lack of experience.  Are there support tools that will analyze our ops code that could provide automated support in this fashion?</p>
  
Comments: CC BY-SA 4.0
Comments: 2021-01-12T21:29:14.903Z
Comments: Maybe it's can help you partial 
  https://www.checkov.io/
  https://github.com/gruntwork-io/terratest
  https://pypi.org/project/tftest/
  
  I don't think there is anything complete today
Comments: Vader
Tags: <terraform><automation><quality-cotrol>
Title: Are there automated quality checking tools for Ops code
UserDisplayName: Jolleyboy
ThreadText: # Are there automated quality checking tools for Ops code
  
  (Asked by: username_0 on 2021-01-12)
  
  In the dev world, we have several tools available to us for automated quality checks. We have unit tests that run on each build, Sonar to check for code quality, metrics being sent to Grafana and Okta with automated alarms, etc.
  
  I'm not very familiar with the ops world; are there similar automated tools available for our ops code?
  
  For Terraform scripts, Dockerfiles, Helm/K8 scripts, Cloudbees configurations and other ops code, the concerns I would like to see automation for are:
  
  * Syntax checking
  * Code smells / anti patterns
  * Routing / Connection checking for accuracy
  * Security best practice checking (don't expose these ports or include these credentials,or grant these permissions kind-of-a-thing)
  * Appropriate provisioning (too many or too few resources allocated for the size of the task)
  * High cost warnings
  * Unit test-ish tools to ensure we aren't breaking the build in more subtle ways than "did it deploy successfully or not"
  
  There's likely a lot more that I don't even know to look for given my lack of experience. Are there support tools that will analyze our ops code that could provide automated support in this fashion?
  
  **Tags:** terraform, automation, quality-cotrol
  
  > **Comments:**
  > **username_1**: Maybe it's can help you partial  https://www.checkov.io/ https://github.com/gruntwork-io/terratest https://pypi.org/project/tftest/  I don't think there is anything complete today  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2021-01-13. Score: 3 | Accepted answer
  
  It's difficult to provide a definitive answer given the scope of the question, but the goal of testing infrastructure is both laudable and seldom done at all.
  
  To my knowledge, you are looking for something akin to [Open Policy Agent](https://www.openpolicyagent.org/) which provides a general policy language in which to express arbitrary policy as code. I would suggest investigating that at length.
  
  At the risk of not providing a complete answer, I will give my best shot for some concrete examples as well.
  
  ## Compliance as Code
  
  In general, you need something which is often referred to as a "compliance framework", and in the DevOps world more often referred to as "Policy as Code". This allows you to define policies as code, which really means "machine-readable and executable". You specify what the policy is in the DSL of the framework, it is executed at some stage of your pipeline, and fails the change if it violates the policy.
  
  To illustrate the point, consider someone submitting a pull request to a terraform module that inadvertently opens a port on a security group to the world which the policy says should be closed. This may be difficult to determine when you're writing the code, but the compliance test will catch it.
  
  There are a few ways to do this, but I will mention two. A notable exception is [Pulumi's Crossguard](https://www.pulumi.com/docs/guides/crossguard/), but I don't have enough experience to speak to that.
  
  ## Hashicorp Sentinel
  
  In the Hashicorp world, there is the [Sentinel Framework](https://www.hashicorp.com/sentinel). This is an Enterprise product, so may not be relevant to your situation, but it is one of the best implementations I've seen. Importantly, it has a [built-in testing framework](https://docs.hashicorp.com/sentinel/writing/testing)  which aids in writing good policy code, which is also very expressive. I have used this as inspiration for how to write policy in Chef Inspec, which is the other example I will cite.
  
  ## Chef Inspec
  
  [Chef Inspec](https://docs.chef.io/inspec/) is a framework for writing policy as code using rspec-style assertions. It uses a DSL based on ruby and supports major clouds (AWS, GCP, Azure) as well as OS-level resources. It is open source, so you can use it anywhere. You use it to make assertions in individual *resources* which are defined in the inspec DSL, *e.g.* "user on an OS" or "AWS EC2 instance". Writing the tests consists of assertions against the actual state of those resources, which makes it quite like a unit test. However, Inspec does not have a built-in testing framework for itself like Sentinel does - although you could use the ruby ecosystem for doing this.
  
  ---
  
  Some notes:
  
  1. Both of these tools allow you to express your policy as code, and to start extending the test coverage of that policy over your existing infrastructure.
  2. Sentinel will catch violations before they are applied, but Inspec needs to be executed against an already-provisioned state.
  3. Both will do what you required, but depend on your tech stack as to whether they will be appropriate for you
  4. There is no magic bullet
     1. for both of them you will need to write your policy as code. Usually this means taking some form of human-readable documentation and writing it in the DSL of the framework.
     2. On the other hand, this provides an excellent opportunity to collaborate with the security team and do what folks are calling DevSecOps these days.
  
  
  ---
  
  ### Answer by username_2 on 2021-05-13. Score: 0
  
  The tools that we have include in our Terraform pipelines to check code quality and best practises are;
  
  1. [Tflint](https://github.com/terraform-linters/tflint)
  2. [TFSec](https://github.com/tfsec/tfsec)
  3. [Checkov](https://www.checkov.io/)
  
  This any issues get highlighted early at the MR stage.
  
  
  ---
  
  