Class: Inspec::Telemetry::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/utils/telemetry/base.rb

Direct Known Subclasses

HTTP, Null

Constant Summary collapse

VERSION =
2.0
TYPE =
"job"
JOB_TYPE =
"InSpec"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#scratchObject

Returns the value of attribute scratch.



13
14
15
# File 'lib/inspec/utils/telemetry/base.rb', line 13

def scratch
  @scratch
end

Instance Method Details

#create_wrapperObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/inspec/utils/telemetry/base.rb', line 20

def create_wrapper
  Inspec::Log.debug "Initialising wrapper for telemetry"
  {
    version: VERSION,
    createdTimeUTC: Time.now.getutc.iso8601,
    environment: Inspec::Telemetry::RunContextProbe.guess_run_context,
    licenseIds: fetch_license_ids,
    source: "#{Inspec::Dist::EXEC_NAME}:#{Inspec::VERSION}",
    type: TYPE,
  }
end

#fetch_license_idsObject



15
16
17
18
# File 'lib/inspec/utils/telemetry/base.rb', line 15

def fetch_license_ids
  Inspec::Log.debug "Fetching license IDs for telemetry"
  @license_keys ||= ChefLicensing.license_keys
end

#format_control_tags(tags) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/inspec/utils/telemetry/base.rb', line 112

def format_control_tags(tags)
  tags_list = []
  tags.each do |key, value|
    tags_list << { name: key.to_s, value: (value || "").to_s }
  end
  tags_list
end

#note_all_invoked_featuresObject



133
134
135
136
137
# File 'lib/inspec/utils/telemetry/base.rb', line 133

def note_all_invoked_features
  Inspec::Feature.list_all_invoked_features.each do |feature|
    Inspec::Telemetry.note_feature_usage(feature.to_s)
  end
end

#note_feature_usage(feature_name) ⇒ Object



32
33
34
35
36
# File 'lib/inspec/utils/telemetry/base.rb', line 32

def note_feature_usage(feature_name)
  @scratch ||= {}
  @scratch[:features] ||= []
  @scratch[:features] << feature_name
end

#note_gem_dependency_usage(opts) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/inspec/utils/telemetry/base.rb', line 139

def note_gem_dependency_usage(opts)
  unless opts[:runner].target_profiles.map do |tp|
    tp..gem_dependencies + \
        tp.locked_dependencies.list.map { |_k, v| v.profile..gem_dependencies }.flatten
  end.flatten.empty?
    Inspec::Telemetry.note_feature_usage("inspec-gem-deps-in-profiles")
  end
end

#note_per_run_features(opts) ⇒ Object



128
129
130
131
# File 'lib/inspec/utils/telemetry/base.rb', line 128

def note_per_run_features(opts)
  note_all_invoked_features
  note_gem_dependency_usage(opts)
end

#obscure(cleartext) ⇒ Object

Hash text if non-nil



121
122
123
124
125
126
# File 'lib/inspec/utils/telemetry/base.rb', line 121

def obscure(cleartext)
  return nil if cleartext.nil?
  return nil if cleartext.empty?

  Digest::SHA2.new(256).hexdigest(cleartext)
end

#run_ending(opts) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/inspec/utils/telemetry/base.rb', line 45

def run_ending(opts)
  note_per_run_features(opts)

  payload = create_wrapper

  train_platform = opts[:runner].backend.backend.platform
  payload[:platform] = train_platform.name

  payload[:jobs] = [{
                      type: JOB_TYPE,

                      # Target platform info
                      environment: {
                        host: obscure(URI(opts[:runner].backend.backend.uri).host) || "unknown",
                        os: train_platform.name,
                        version: train_platform.release,
                        architecture: train_platform.arch || "",
                        id: train_platform.uuid,
                      },

                      runtime: Inspec::VERSION,
                      content: [],  # one content == one profile
                      steps: [],    # one step == one control
                    }]

  opts[:run_data][:profiles].each do |profile|
    payload[:jobs][0][:content] << {
      name: obscure(profile[:name]),
      version: profile[:version],
      sha256: profile[:sha256],
      maintainer: profile[:maintainer] || "",
      type: "profile",
    }

    profile[:controls].each do |control|
      payload[:jobs][0][:steps] << {
        id: obscure(control[:id]),
        name: "inspec-control",
        description: control[:desc] || "",
        target: {
          mode: opts[:runner].backend.backend.backend_type,
          id: opts[:runner].backend.backend.platform.uuid,
        },
        resources: [],
        features: [],
        tags: format_control_tags(control[:tags]),
      }

      control[:results]&.each do |resource_block|
        payload[:jobs][0][:steps].last[:resources] << {
          type: "inspec-resource",
          name: resource_block[:resource_class],
          id: obscure(resource_block[:resource_title].respond_to?(:resource_id) ? resource_block[:resource_title].resource_id : nil) || "unknown",
        }
      end

      # Per-control features.
      payload[:jobs][0][:steps].last[:features] = scratch[:features].dup
    end
  end

  Inspec::Log.debug "Final data for telemetry upload -> #{payload}"
  Inspec::Log.debug "Finishing telemetry for InSpec"
  # Return payload object for testing
  payload
end

#run_starting(_opts = {}) ⇒ Object



38
39
40
41
42
43
# File 'lib/inspec/utils/telemetry/base.rb', line 38

def run_starting(_opts = {})
  Inspec::Log.debug "Initiating telemetry for InSpec"
  @scratch ||= {}
  @scratch[:features] ||= []
  @scratch[:run_start_time] = Time.now.getutc.iso8601
end