New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Telemetry - determine run context from stack introspection #4907
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e96ddbf
Add stack probe for detecting when run as CLI by Thor
clintoncwolfe 5be4ee9
stack probe for test-kitchen
clintoncwolfe cd9ca19
stack probe for audit cookbook
clintoncwolfe 4122157
rename to match class name
clintoncwolfe 45e3421
Remove too-brittle frame ranges on stack locations
clintoncwolfe 439c93b
Add a functional test to check Thor run context detection
clintoncwolfe 09cfced
Refactor os_prepare cookbook to split out inspec installation
clintoncwolfe 43a5fd7
Test kitchen setup to verify Telemetry RunContext detection under Tes…
clintoncwolfe d5be4c2
Test kitchen setup to test detection of audit-cookbook
clintoncwolfe File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
driver: | ||
name: vagrant | ||
|
||
provisioner: | ||
product_name: chef | ||
product_version: 15 | ||
|
||
verifier: | ||
name: inspec | ||
sudo: true | ||
|
||
lifecycle: | ||
# This pre_create hook, along with the install_inspec cookbook, ensures that | ||
# the VM has a version of InSpec that matches that built from source. | ||
# Note that audit cookbook will ignore this. | ||
pre_create: | ||
- local: cd inspec-bin && gem build inspec-core-bin.gemspec --output ../test/kitchen/cookbooks/install_inspec/files/inspec-core-bin.gem | ||
- local: gem build inspec-core.gemspec --output test/kitchen/cookbooks/install_inspec/files/inspec-core.gem | ||
|
||
platforms: | ||
- name: ubuntu-18.04 | ||
|
||
suites: | ||
# This test suite uses test/integration/run-context-tk to check the | ||
# Telemetry Run Context detection system. This test should detect | ||
# running under Test Kitchen. | ||
- name: run-context-tk | ||
run_list: | ||
- recipe[install_inspec] | ||
|
||
# This test suite uses a pair of profiles to check the Telemetry Run Context | ||
# detection system under audit cookbook. | ||
- name: run-context-after-audit | ||
run_list: | ||
- recipe[install_inspec] | ||
- recipe[audit] | ||
attributes: | ||
audit: | ||
profiles: | ||
# This actually runs during converge time, and performs the stack | ||
# probe that we care about. It writes a JSON copy of the stack that | ||
# it saw to /tmp/audit_stack.json . run-context-after-audit examines | ||
# that JSON file and runs it through the context probe to see what it gets. | ||
run-context-during-audit: | ||
# This should work but doesn't :-( | ||
# path: <%= Dir.pwd %>/test/integration/run-context-during-audit/ | ||
url: https://github.com/inspec/inspec-test-profile-run-context-audit/archive/v0.3.2.zip | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Inspec | ||
module Telemetry | ||
# Guesses the run context of InSpec - how were we invoked? | ||
# All stack values here are determined experimentally | ||
|
||
class RunContextProbe | ||
def self.guess_run_context(stack = nil) | ||
stack ||= caller_locations | ||
return "test-kitchen" if kitchen?(stack) | ||
return "cli" if run_by_thor?(stack) | ||
return "audit-cookbook" if audit_cookbook?(stack) | ||
|
||
"unknown" | ||
end | ||
|
||
def self.run_by_thor?(stack) | ||
stack_match(stack: stack, path: "thor/command", label: "run") && | ||
stack_match(stack: stack, path: "thor/invocation", label: "invoke_command") | ||
end | ||
|
||
def self.kitchen?(stack) | ||
stack_match(stack: stack, path: "kitchen/instance", label: "verify_action") && | ||
stack_match(stack: stack, path: "kitchen/instance", label: "verify") | ||
end | ||
|
||
def self.audit_cookbook?(stack) | ||
stack_match(stack: stack, path: "chef/handler", label: "run_report_handlers") && | ||
stack_match(stack: stack, path: "handler/audit_report", label: "report") | ||
end | ||
|
||
def self.stack_match(stack: [], label: nil, path: nil) | ||
return false if stack.nil? | ||
|
||
stack.any? do |frame| | ||
if label && path | ||
frame.label == label && frame.absolute_path.include?(path) | ||
elsif label | ||
frame.label == label | ||
elsif path | ||
frame.absolute_path.include?(path) | ||
else | ||
false | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require "functional/helper" | ||
|
||
describe "telemetry" do | ||
include FunctionalHelper | ||
parallelize_me! | ||
|
||
describe "detecting CLI runtime context" do | ||
let(:run_result) { run_inspec_process("run_context") } | ||
it "should detect cli context" do | ||
_(run_result.stderr).must_equal "" | ||
_(run_result.stdout).must_include "cli" | ||
end | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
test/integration/run-context-after-audit/controls/after-audit.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
# run-context-during-audit should have left a file with a JSON | ||
# representation of the stack as experienced by the audit cookbook. | ||
|
||
raw_data = JSON.parse(file("/tmp/audit_stack.json").content) | ||
# These aren't really stack frames, so we do some duck typing | ||
Frame = Struct.new(:absolute_path, :label) | ||
reconstructed_stack = raw_data.map{ |f| Frame.new(f["absolute_path"], f["label"]) } | ||
|
||
require "inspec/utils/telemetry/run_context_probe" | ||
|
||
control "run-context" do | ||
describe Inspec::Telemetry::RunContextProbe.guess_run_context(reconstructed_stack) do | ||
it { should eq "audit-cookbook" } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: run-context-after-audit | ||
title: InSpec Profile | ||
maintainer: InSpec Engineering | ||
copyright: Chef Software, Inc. | ||
copyright_email: inspec@chef.io | ||
license: Apache-2.0 | ||
summary: An InSpec Compliance Profile to test inspec run context detection under audit-cookbook | ||
version: 0.1.0 | ||
supports: | ||
platform: os |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# This seemingly pointless test is here so that a dump of | ||
# the stack will appear in the test output for debugging | ||
stack = caller_locations(4) | ||
control "json-stack" do | ||
describe stack.to_json do | ||
it { should be_kind_of String } | ||
end | ||
end | ||
|
||
require "inspec/utils/telemetry/run_context_probe" | ||
|
||
control "run-context" do | ||
describe Inspec::Telemetry::RunContextProbe.guess_run_context do | ||
it { should cmp "test-kitchen" } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: run-context-tk | ||
title: InSpec Profile | ||
maintainer: InSpec Engineering | ||
copyright: Chef Software, Inc. | ||
copyright_email: inspec@chef.io | ||
license: Apache-2.0 | ||
summary: An InSpec Compliance Profile to test inspec run context detection during test-kitchen verify | ||
version: 0.1.0 | ||
supports: | ||
platform: os |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name "install_inspec" | ||
maintainer "Chef Software, Inc." | ||
maintainer_email "support@chef.io" | ||
description "This cookbook installs the InSpec gem." | ||
license "Apache-2.0" | ||
version "1.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
cookbook_file "/root/inspec-core.gem" do | ||
source "inspec-core.gem" | ||
action :create | ||
end | ||
|
||
cookbook_file "/root/inspec-core-bin.gem" do | ||
source "inspec-core-bin.gem" | ||
action :create | ||
end | ||
|
||
|
||
chef_gem "inspec-core" do | ||
source "/root/inspec-core.gem" | ||
action :upgrade | ||
end | ||
|
||
chef_gem "inspec-core-bin" do | ||
source "/root/inspec-core-bin.gem" | ||
action :upgrade | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be best to delete these lines in a separate commit.