{"id":18833,"date":"2023-07-14T10:02:50","date_gmt":"2023-07-14T14:02:50","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=18833"},"modified":"2023-07-14T10:02:50","modified_gmt":"2023-07-14T14:02:50","slug":"issues-using-chef-inspec-with-github-actions","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/issues-using-chef-inspec-with-github-actions\/","title":{"rendered":"Troubleshooting Common Issues when using Chef InSpec with GitHub Actions"},"content":{"rendered":"<p>At <a href=\"https:\/\/qxf2.com\/?utm_source=troubleshooting_chef_inspec_issues&#038;utm_medium=click&#038;utm_campaign=From%20blog\">Qxf2<\/a>, we have been using Infrastructure tests at our clients for a while now. We recently came up with the idea of adding some infrastructure tests for our own servers and applications within Qxf2. We used <a href=\"https:\/\/github.com\/inspec\/inspec\" rel=\"noopener\" target=\"_blank\">Chef Inspec<\/a> to write and execute our tests. Chef InSpec is a popular open-source framework that helps you test and review your applications and infrastructure. It works by checking if your system matches the way you want it to be, which you can describe using simple and understandable Chef InSpec code. I came up with the idea for this blog when working on automating the process of running the infrastructure tests for our systems, by setting up Chef Inspec to run on Github workflow. I came  across a number of issues and some misleading errors which was a bit challenging to fix. So, I decided to write this small  post to share some of the issues that I came across and how I was was able to resolve it, so that anyone facing similar issues need not pull their hair searching for solutions.<\/p>\n<h4>Issue 1: Chef InSpec cannot execute without valid licenses<\/h4>\n<p>1. One common issue that users may encounter when running Chef InSpec with GitHub Actions, is the inability to execute tests due to missing licenses. There can be multiple reasons why you may hit this issue. But, the least expected reason is the way Chef Inspec is installed in your build container.<br \/>\n2. Many would use the Github Action &#8220;<em>actionshub-chef-install<\/em>&#8220;, to try and install Chef Inspec. However, this leads to the error, &#8220;<code><em>Chef InSpec cannot execute without valid licenses<\/em><\/code>&#8220;, despite accepting the license Chef license.<br \/>\n<figure id=\"attachment_18917\" aria-describedby=\"caption-attachment-18917\" style=\"width: 900px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/valid_license.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/valid_license-1024x349.png\" alt=\"License error in Chef InSpec when running on Github Workflow\" width=\"900\" height=\"307\" class=\"size-large wp-image-18917\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/valid_license-1024x349.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/valid_license-300x102.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/valid_license-768x262.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/valid_license.png 1413w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><figcaption id=\"caption-attachment-18917\" class=\"wp-caption-text\">GitHub Workflow failing with a license error<\/figcaption><\/figure><br \/>\n3. If you are facing a similar issue you can simply fix it by downloading and installing Chef Inspec using the <em>cURL<\/em> command rather than using any prebuilt  Github actions for it. <\/p>\n<pre lang=\"ruby\">\r\n    - name: \"Install Chef Inspec\"\r\n      run: curl https:\/\/omnitruck.chef.io\/install.sh | sudo bash -s -- -P inspec\r\n<\/pre>\n<p>4. In addition, do not forget to accept the Chef license. To do so, you can simply create an environment variable &#8220;<em>CHEF_LICENSE<\/em>&#8221; and set the value &#8220;<em>accept-silent<\/em>&#8221; to it.<\/p>\n<pre lang=\"ruby\">\r\n    env:\r\n      CHEF_LICENSE: accept-silent \r\n<\/pre>\n<h4>Issue 2: Client error, can&#8217;t connect to &#8220;ssh&#8221; backend: Your SSH Agent has no keys added, and you have not specified a password or a key file<\/h4>\n<p>1. Chef Inspec uses SSH for connecting to remote systems and running the tests. For this, we need the authentication key used to connect to the host system. We usually store this key in the Github Secrets. However, when we try passing this Secret directly in the SSH command like below we would hit an error:<\/p>\n<pre lang=\"bash\">\r\ninspec exec survey_frontend_tests --input-file ubuntu_tests\/input.yaml -t ssh:\/\/ubuntu@12.345.67.890 -i ${{ secrets.SURVEY_FRONTEND_KEY }}\r\n<\/pre>\n<p>The above command would give us  a client error:&#8221;<code><em>can't connect to 'ssh' backend: Your SSH Agent has no keys added, and you have not specified a password or a key file<\/em><\/code>&#8220;.<br \/>\n<figure id=\"attachment_18919\" aria-describedby=\"caption-attachment-18919\" style=\"width: 900px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/ssh_error.png\" data-rel=\"lightbox-image-1\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/ssh_error-1024x350.png\" alt=\"SSH error in Chef InSpec when running on Github Workflow\" width=\"900\" height=\"308\" class=\"size-large wp-image-18919\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/ssh_error-1024x350.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/ssh_error-300x102.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/ssh_error-768x262.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/ssh_error.png 1412w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><figcaption id=\"caption-attachment-18919\" class=\"wp-caption-text\">GitHub Workflow failing with SSH error<\/figcaption><\/figure><br \/>\n2. This is because the SSH command cannot directly read the key from the Secrets and the key needs to be stored in a new file with the right permissions. We can do this by simply adding a step in our workflow to save the key from the Secrets to a new file and setting the required permissions for it<\/p>\n<pre lang=\"yaml\">\r\n    - name: Set the Instance key\r\n      run: |\r\n        echo \"${{ secrets.SURVEY_FRONTEND_KEY }}\" | tr -d '\\r' > key.pem\r\n        chmod 400 key.pem\r\n<\/pre>\n<p>3. We can then use this newly created key file in our SSH command to resolve our issue.<\/p>\n<pre lang=\"bash\">\r\ninspec exec survey_frontend_tests --input-file survey_frontend_tests\/input.yaml -t ssh:\/\/ubuntu@12.345.67.890 -i key.pem \r\n<\/pre>\n<h4> Issue 3: GitHub workflow failing when there are skips in your tests<\/h4>\n<p>1. Another interesting issue that I stumbled upon is that, the  GitHub workflow fails when any of the tests in our Chef InSpec profile is skipped. You may be wondering why this happens. This is because when there are skipped tests, the exit status of your test run would be &#8216;<strong>101<\/strong>&#8216;.<br \/>\n2. GitHub workflow interprets any status code other than 0 as a failure. Therefore when there are skipped tests in your test run the overall workflow status gets marked as failed.<br \/>\n<figure id=\"attachment_18921\" aria-describedby=\"caption-attachment-18921\" style=\"width: 900px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/skip_failure.png\" data-rel=\"lightbox-image-2\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/skip_failure-1024x90.png\" alt=\"Chef InSpec workflow failure when tests are skipped\" width=\"900\" height=\"79\" class=\"size-large wp-image-18921\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/skip_failure-1024x90.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/skip_failure-300x26.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/skip_failure-768x68.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/skip_failure.png 1422w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><figcaption id=\"caption-attachment-18921\" class=\"wp-caption-text\">GitHub Workflow failing when one or more tests are skipped.<\/figcaption><\/figure><br \/>\n3. We can resolve this issue by setting Chef InSpec to return only two distinct exit codes. &#8216;0&#8217; on failure and &#8216;1&#8217; on success. We can achieve this by passing the <em>&#8211;no-distinct-exit<\/em> flag when executing our Chef InSpec tests. This ensures that the skipped tests does not cause the test run to exit with a non-zero status unless there is an actual failure.<\/p>\n<pre lang=\"bash\">\r\ninspec exec survey_frontend_tests --input-file survey_frontend_tests\/input.yaml -t ssh:\/\/ubuntu@12.345.67.890 -i key.pem --no-distinct-exit \r\n<\/pre>\n<h4>Bonus: Publishing InSpec results on Netlify<\/h4>\n<p>1. As part of your Chef InSpec workflow, you may want to publish the test results somewhere, let&#8217;s say <a href=\"https:\/\/www.netlify.com\/\" rel=\"noopener\" target=\"_blank\">Netlify<\/a>. To do this this we first need to save our test results into a <code>htm<\/code>l file. We can simply use the <em>&#8211;reporter<\/em> flag of Chef Inspec to achieve this. <\/p>\n<pre lang=\"bash\">\r\ninspec exec survey_frontend_tests --input-file survey_frontend_tests\/input.yaml -t ssh:\/\/ubuntu@12.345.67.890 -i key.pem --no-distinct-exit --reporter=cli html:.\/test_report\/index.html\r\n<\/pre>\n<p>2. We then need to add a new site in our Netlify account and set the <strong>NETLIFY_AUTH_TOKEN<\/strong> and <strong>NETLIFY_SITE_ID<\/strong>. We won&#8217;t be going into the details of the setup procedure here. You can refer the <a href=\"https:\/\/qxf2.com\/blog\/host-great-expectations-test-results-on-netlify\/\">following<\/a> blog post for more information on setting up your Netlify account<br \/>\n3. Once we have our Netlify account setup, We can then use a Github action &#8216;<em>nwtgck\/actions-netlify<\/em>&#8216; to deploy our html file on it.<\/p>\n<pre lang=\"ruby\">\r\n    - name: Deploy DataDocs to Netlify\r\n      if: always()\r\n      uses: nwtgck\/actions-netlify@v1.2\r\n      with:\r\n        publish-dir: \".\/test_report\"\r\n        production-deploy: true\r\n      env:\r\n        NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}\r\n        NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}\r\n      timeout-minutes: 1\r\n<\/pre>\n<figure id=\"attachment_18923\" aria-describedby=\"caption-attachment-18923\" style=\"width: 900px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/netlify_publish.png\" data-rel=\"lightbox-image-3\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/netlify_publish-1024x118.png\" alt=\"Output for successful publication of InSpec test results to Netlify.\" width=\"900\" height=\"104\" class=\"size-large wp-image-18923\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/netlify_publish-1024x118.png 1024w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/netlify_publish-300x35.png 300w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/netlify_publish-768x88.png 768w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2023\/07\/netlify_publish.png 1407w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/a><figcaption id=\"caption-attachment-18923\" class=\"wp-caption-text\">Successful publication of InSpec test results on Netlify<\/figcaption><\/figure>\n<p>I hope you found this post helpful in resolving some of your issues. Also, keep an eye out! We have more posts on testing Infrastructure coming out soon.<\/p>\n<hr>\n<h4>Hire technical testers from Qxf2<\/h4>\n<p>Hiring Qxf2 brings the advantage of working with technical testers who go beyond traditional test automation practices. Our diverse skill set and expertise enable us to solve critical testing challenges and help teams iterate quickly and confidently. With our focus on early-stage products and modern technical stacks, we provide valuable insights and contribute to the success of your software development projects. <a href=\"https:\/\/qxf2.com\/contact?utm_source=troubleshooting_chef_inspec_issues&#038;utm_medium=click&#038;utm_campaign=From%20blog\">Contact us<\/a>!<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>At Qxf2, we have been using Infrastructure tests at our clients for a while now. We recently came up with the idea of adding some infrastructure tests for our own servers and applications within Qxf2. We used Chef Inspec to write and execute our tests. Chef InSpec is a popular open-source framework that helps you test and review your applications [&hellip;]<\/p>\n","protected":false},"author":29,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[367,301,316,366,1],"tags":[],"class_list":["post-18833","post","type-post","status-publish","format-standard","hentry","category-chef-inspec","category-github-action","category-infrastructure","category-netlify","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/18833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=18833"}],"version-history":[{"count":39,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/18833\/revisions"}],"predecessor-version":[{"id":18963,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/18833\/revisions\/18963"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=18833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=18833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=18833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}