-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathprocess_env_var.rb
More file actions
49 lines (40 loc) · 1.24 KB
/
process_env_var.rb
File metadata and controls
49 lines (40 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true
class ProcessEnvVar < Inspec.resource(1)
name 'process_env_var'
desc 'Custom resource to lookup environment variables for a process'
example "
describe process_env_var('etcd2') do
its(:ETCD_DATA_DIR) { should match(%r{/var/lib/etcd2}) }
end
"
# As described here https://github.com/inspec/inspec/blob/main/lib/inspec/resource.rb#L111
# Inspec has a weird behaviour concerning super
# rubocop:disable Lint/MissingSuper
def initialize(process)
@process = inspec.processes(process)
end
# rubocop:enable Lint/MissingSuper
def respond_to_missing?(name)
Log.debug("Missing #{name}")
end
def method_missing(name)
read_params[name.to_s] || ''
end
def params
return @params if defined?(@params)
@file = inspec.file("/proc/#{@process.pids.first}/environ")
unless @file.file?
skip_resource "Can't find environ file for #{@process}"
return @params = {}
end
@content = @file.content
if @content.empty? && !@file.empty?
skip_resource "Can't read environ file for #{@process}"
return @params = {}
end
@params = @content.split("\0").map { |i| i.split('=', 2) }.to_h
end
def to_s
"Enviroment variables for #{@process}"
end
end