diff --git a/src/courses/advanced/05.md b/src/courses/advanced/05.md index c19eb5a3e..7d7ba6c15 100644 --- a/src/courses/advanced/05.md +++ b/src/courses/advanced/05.md @@ -17,7 +17,7 @@ Let's start by creating a new profile: @tab Command ```bash -inspec init profile git +inspec init profile my_git ``` @tab Output @@ -25,9 +25,9 @@ inspec init profile git ```bash ─────────────────────────── InSpec Code Generator ─────────────────────────── -Creating new profile at /workspaces/saf-training-lab-environment/git +Creating new profile at /workspaces/saf-training-lab-environment/my_git • Creating file inspec.yml - • Creating directory /workspaces/saf-training-lab-environment/git/controls + • Creating directory /workspaces/saf-training-lab-environment/my_git/controls • Creating file controls/example.rb • Creating file README.md ``` @@ -39,20 +39,20 @@ Creating new profile at /workspaces/saf-training-lab-environment/git To write resources, we first need to know what we are testing! In your Codespaces environment, there is a git repository that we will test under the `resources` folder. The git repository will be the test target, similarly to how the docker containers acted as test targets in previous sections. Unzip the target git repository using the following command: ```sh -unzip ./resources/git_test.zip +tar xzvf resources/git_test_target.tar.gz ``` This will generate a `git_test` repository which we will use for these examples. -Now let's write some tests and confirm that they run. You can put these tests in the `example.rb` file generated in the `controls` folder of your `git` InSpec profile. These tests are written using the `command` resource which is provided by InSpec. We will write a `git` resource in this section to improve this test. **Note that you will need to put the full directory path of the `.git` file from your `git_test` repository as the `git_dir` value on line 4 of `example.rb`. To get the full path of your current location in the terminal, use `pwd`.** +Now let's write some tests and confirm that they run. You can put these tests in the `example.rb` file generated in the `controls` folder of your `my_git` InSpec profile. These tests are written using the `command` resource which is provided by InSpec. We will write a `git` resource in this section to improve this test. **Note that you will need to put the full directory path of the `.git` file from your `git_test_target` repository as the `git_dir` value on line 4 of `example.rb`. To get the full path of your current location in the terminal, use `pwd`.** ```ruby # encoding: utf-8 # copyright: 2018, The Authors -git_dir = "/workspaces/saf-training-lab-environment/git_test/.git" +git_dir = "/workspaces/saf-training-lab-environment/git_test_target_target/.git" -# The following banches should exist +# The following branches should exist describe command("git --git-dir #{git_dir} branch") do its('stdout') { should match /main/ } end @@ -83,7 +83,7 @@ Run the profile. @tab Command ```bash -inspec exec git +inspec exec my_git ``` @tab Output @@ -91,20 +91,20 @@ inspec exec git ```bash Redirecting to cinc-auditor... -Profile: InSpec Profile (git) +Profile: InSpec Profile (my_git) Version: 0.1.0 Target: local:// Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch` ✔ stdout is expected to match /main/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch` ✔ stdout is expected to match /testBranch/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch` ✔ stdout is expected to match /^\* main/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log -1 --pretty=format:'%h'` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log -1 --pretty=format:'%h'` ✔ stdout is expected to match /edc207f/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log --skip=1 -1 --pretty=format:'%h'` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log --skip=1 -1 --pretty=format:'%h'` ✔ stdout is expected to match /8c30bff/ Test Summary: 5 successful, 0 failures, 0 skipped @@ -128,7 +128,7 @@ Let's rewrite these tests in a way that abstracts away the complexity of working Let's rewrite the first test in our example file to make it more readable by inventing a `git` resource that can simplify our test as follows: ```ruby -# The following banches should exist +# The following branches should exist describe git(git_dir) do its('branches') { should include 'main' } end @@ -141,7 +141,7 @@ Now let's run the profile. @tab Command ```bash -inspec exec git +inspec exec my_git ``` @tab Output @@ -151,7 +151,7 @@ Redirecting to cinc-auditor... [2023-02-22T03:21:41+00:00] ERROR: Failed to load profile git: Failed to load source for controls/example.rb: undefined method `git' for # -Profile: InSpec Profile (git) +Profile: InSpec Profile (my_git) Version: 0.1.0 Failure Message: Failed to load source for controls/example.rb: undefined method `git' for # Target: local:// @@ -168,7 +168,7 @@ We should get an error because the git method and resource are not defined yet. ### Develop the git resource -Let's start by creating a new file called `git.rb` in the `libraries` directory. If you do not already have a `libraries` directory, you can make one in the `git` InSpec profile directory. The content of the file should look like this: +Let's start by creating a new file called `git.rb` in the `libraries` directory. If you do not already have a `libraries` directory, you can make one in the `my_git` InSpec profile directory. The content of the file should look like this: ```ruby # encoding: utf-8 @@ -191,7 +191,7 @@ Run the profile again. @tab Command ```bash -inspec exec git +inspec exec my_git ``` @tab Output @@ -199,7 +199,7 @@ inspec exec git ```bash [2023-02-22T03:25:57+00:00] ERROR: Failed to load profile git: Failed to load source for controls/example.rb: wrong number of arguments (given 1, expected 0) -Profile: InSpec Profile (git) +Profile: InSpec Profile (my_git) Version: 0.1.0 Failure Message: Failed to load source for controls/example.rb: wrong number of arguments (given 1, expected 0) Target: local:// @@ -241,13 +241,13 @@ Now when we run the profile: @tab Command ```bash -inspec exec git +inspec exec my_git ``` @tab Output ```bash -Profile: InSpec Profile (git) +Profile: InSpec Profile (my_git) Version: 0.1.0 Target: local:// Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce @@ -255,13 +255,13 @@ Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce git × branches undefined method `branches' for #<#:0x00000000043620c8> - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch` ✔ stdout is expected to match /testBranch/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch` ✔ stdout is expected to match /^\* main/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log -1 --pretty=format:'%h'` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log -1 --pretty=format:'%h'` ✔ stdout is expected to match /edc207f/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log --skip=1 -1 --pretty=format:'%h'` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log --skip=1 -1 --pretty=format:'%h'` ✔ stdout is expected to match /8c30bff/ Test Summary: 4 successful, 1 failure, 0 skipped @@ -298,13 +298,13 @@ We have now defined the branches method. Let's see what the test output shows us @tab Command ```bash -inspec exec git +inspec exec my_git ``` @tab Output ```bash -Profile: InSpec Profile (git) +Profile: InSpec Profile (my_git) Version: 0.1.0 Target: local:// Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce @@ -312,13 +312,13 @@ Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce git × branches is expected to include "main" expected nil to include "main", but it does not respond to `include?` - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch` ✔ stdout is expected to match /testBranch/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git branch` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git branch` ✔ stdout is expected to match /^\* main/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log -1 --pretty=format:'%h'` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log -1 --pretty=format:'%h'` ✔ stdout is expected to match /edc207f/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log --skip=1 -1 --pretty=format:'%h'` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log --skip=1 -1 --pretty=format:'%h'` ✔ stdout is expected to match /8c30bff/ Test Summary: 4 successful, 1 failure, 0 skipped @@ -355,7 +355,7 @@ Now we see that we get a passing test! Now let's adjust our test to also check for our second branch that we created earlier as well as check our current branch: ```ruby -# The following banches should exist +# The following branches should exist describe git(git_dir) do its('branches') { should include 'main' } its('branches') { should include 'testBranch' } @@ -368,13 +368,13 @@ end @tab Command ```bash -inspec exec git +inspec exec my_git ``` @tab Output ```bash -Profile: InSpec Profile (git) +Profile: InSpec Profile (my_git) Version: 0.1.0 Target: local:// Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce @@ -384,9 +384,9 @@ Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce ✔ branches is expected to include "testBranch" × current_branch undefined method `current_branch' for #<#:0x00000000053fd0b8> - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log -1 --pretty=format:'%h'` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log -1 --pretty=format:'%h'` ✔ stdout is expected to match /edc207f/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log --skip=1 -1 --pretty=format:'%h'` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log --skip=1 -1 --pretty=format:'%h'` ✔ stdout is expected to match /8c30bff/ Test Summary: 6 successful, 1 failure, 0 skipped @@ -428,13 +428,13 @@ Now we can run the profile again. @tab Command ```bash -inspec exec git +inspec exec my_git ``` @tab Output ```bash -Profile: InSpec Profile (git) +Profile: InSpec Profile (my_git) Version: 0.1.0 Target: local:// Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce @@ -443,9 +443,9 @@ Target ID: 6dcb9e6f-5ede-5474-9521-595fadf5c7ce ✔ branches is expected to include "main" ✔ branches is expected to include "testBranch" ✔ current_branch is expected to cmp == "main" - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log -1 --pretty=format:'%h'` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log -1 --pretty=format:'%h'` ✔ stdout is expected to match /edc207f/ - Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test/.git log --skip=1 -1 --pretty=format:'%h'` + Command: `git --git-dir /workspaces/saf-training-lab-environment/git_test_target/.git log --skip=1 -1 --pretty=format:'%h'` ✔ stdout is expected to match /8c30bff/ Test Summary: 7 successful, 0 failures, 0 skipped @@ -487,7 +487,7 @@ You are currently running on: Release: 20.04 Arch: x86_64 -inspec> git('/workspaces/saf-training-lab-environment/git_test/.git').current_branch +inspec> git('/workspaces/saf-training-lab-environment/git_test_target/.git').current_branch => "main" ``` @@ -496,11 +496,11 @@ inspec> git('/workspaces/saf-training-lab-environment/git_test/.git').current_br ::: warning Note that we are passing in the _profile_ directory to the `--depends` flag, and not the profile's `libraries` directory. In our example, it's ``` sh -inspec shell --depends git +inspec shell --depends my_git ``` and not ``` sh -inspec shell --depends git/libraries +inspec shell --depends my_git/libraries ``` :::