Class: InspecPlugins::Artifact::Base

Inherits:
Object
  • Object
show all
Includes:
Inspec::Dist
Defined in:
lib/plugins/inspec-artifact/lib/inspec-artifact/base.rb

Constant Summary collapse

KEY_BITS =
2048
KEY_ALG =
OpenSSL::PKey::RSA
INSPEC_PROFILE_VERSION_1 =
"INSPEC-PROFILE-1".freeze
INSPEC_REPORT_VERSION_1 =
"INSPEC-REPORT-1".freeze
ARTIFACT_DIGEST =
OpenSSL::Digest::SHA512
ARTIFACT_DIGEST_NAME =
"SHA512".freeze
VALID_PROFILE_VERSIONS =
Set.new [INSPEC_PROFILE_VERSION_1]
VALID_PROFILE_DIGESTS =
Set.new [ARTIFACT_DIGEST_NAME]
SIGNED_PROFILE_SUFFIX =
"iaf".freeze
SIGNED_REPORT_SUFFIX =
"iar".freeze

Constants included from Inspec::Dist

Inspec::Dist::AUTOMATE_PRODUCT_NAME, Inspec::Dist::COMPLIANCE_PRODUCT_NAME, Inspec::Dist::EXEC_NAME, Inspec::Dist::PRODUCT_NAME, Inspec::Dist::SERVER_PRODUCT_NAME

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.keygen(options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/plugins/inspec-artifact/lib/inspec-artifact/base.rb', line 30

def self.keygen(options)
  key = KEY_ALG.new KEY_BITS
  puts "Generating private key"
  open "#{options["keyname"]}.pem.key", "w" do |io|
    io.write key.to_pem
  end
  puts "Generating public key"
  open "#{options["keyname"]}.pem.pub", "w" do |io|
    io.write key.public_key.to_pem
  end
end

.profile_install(options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/plugins/inspec-artifact/lib/inspec-artifact/base.rb', line 85

def self.profile_install(options)
  artifact = new
  puts "Installing profile"
  file_to_verifiy = options["infile"]
  dest_dir = options["destdir"]
  artifact.verify(file_to_verifiy) do |content|
    Dir.mktmpdir do |workdir|
      tmpfile = Pathname.new(workdir).join("artifact_to_install.tar.gz")
      File.write(tmpfile, content)
      puts "Installing to #{dest_dir}"
      `tar xzf #{tmpfile} -C #{dest_dir}`
    end
  end
end

.profile_sign(options) ⇒ Object



42
43
44
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
# File 'lib/plugins/inspec-artifact/lib/inspec-artifact/base.rb', line 42

def self.profile_sign(options)
  artifact = new
  path_to_profile = options["profile"]

  # Write inspec.json file within artifact
  write_inspec_json(path_to_profile, options)

  Dir.mktmpdir do |workdir|
    puts "Signing #{options["profile"]} with key #{options["keyname"]}"
    profile_md = artifact.(path_to_profile)
    artifact_filename = "#{profile_md["name"]}-#{profile_md["version"]}.#{SIGNED_PROFILE_SUFFIX}"
    tarfile = artifact.profile_compress(path_to_profile, profile_md, workdir)
    content = IO.binread(tarfile)
    signing_key = KEY_ALG.new File.read "#{options["keyname"]}.pem.key"
    sha = ARTIFACT_DIGEST.new
    signature = signing_key.sign sha, content
    # convert the signature to Base64
    signature_base64 = Base64.encode64(signature)
    tar_content = IO.binread(tarfile)
    File.open(artifact_filename, "wb") do |f|
      f.puts(INSPEC_PROFILE_VERSION_1)
      f.puts(options["keyname"])
      f.puts(ARTIFACT_DIGEST_NAME)
      f.puts(signature_base64)
      f.puts("") # newline separates artifact header with body
      f.write(tar_content)
    end
    puts "Successfully generated #{artifact_filename}"
  end

  # Cleanup
  File.delete("#{path_to_profile}/inspec.json")
end

.profile_verify(options) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/plugins/inspec-artifact/lib/inspec-artifact/base.rb', line 76

def self.profile_verify(options)
  artifact = new
  file_to_verifiy = options["infile"]
  puts "Verifying #{file_to_verifiy}"
  artifact.verify(file_to_verifiy) do ||
    puts "Artifact is valid"
  end
end

.write_inspec_json(root_path, opts) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/plugins/inspec-artifact/lib/inspec-artifact/base.rb', line 177

def self.write_inspec_json(root_path, opts)
  profile = Inspec::Profile.for_path(root_path, opts)
  Inspec::Utils::JsonProfileSummary.produce_json(
    info: profile.info,
    write_path: "#{root_path}/inspec.json",
    suppress_output: true
  )
end

Instance Method Details

#profile_compress(path_to_profile, profile_md, workdir) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/plugins/inspec-artifact/lib/inspec-artifact/base.rb', line 126

def profile_compress(path_to_profile, profile_md, workdir)
  profile_name = profile_md["name"]
  profile_version = profile_md["version"]
  outfile_name = "#{workdir}/#{profile_name}-#{profile_version}.tar.gz"
  `tar czf #{outfile_name} -C #{path_to_profile} .`
  outfile_name
end

#read_profile_metadata(path_to_profile) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/plugins/inspec-artifact/lib/inspec-artifact/base.rb', line 100

def (path_to_profile)
  begin
    p = Pathname.new(path_to_profile)
    p = p.join("inspec.yml")
    unless p.exist?
      raise "#{path_to_profile} doesn't appear to be a valid #{PRODUCT_NAME} profile"
    end

    yaml = YAML.load_file(p.to_s)
    yaml = yaml.to_hash

    unless yaml.key? "name"
      raise "Profile is invalid, name is not defined"
    end

    unless yaml.key? "version"
      raise "Profile is invalid, version is not defined"
    end
  rescue => e
    # rewrap it and pass it up to the CLI
    raise "Error reading #{PRODUCT_NAME} profile metadata: #{e}"
  end

  yaml
end

#valid_header?(file_alg, file_version, file_keyname) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
137
138
139
140
141
142
143
# File 'lib/plugins/inspec-artifact/lib/inspec-artifact/base.rb', line 134

def valid_header?(file_alg, file_version, file_keyname)
  public_keyfile = "#{file_keyname}.pem.pub"
  puts "Looking for #{public_keyfile} to verify artifact"
  unless File.exist? public_keyfile
    raise "Can't find #{public_keyfile}"
  end

  raise "Invalid artifact digest algorithm detected" unless VALID_PROFILE_DIGESTS.member?(file_alg)
  raise "Invalid artifact version detected" unless VALID_PROFILE_VERSIONS.member?(file_version)
end

#verify(file_to_verifiy, &content_block) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/plugins/inspec-artifact/lib/inspec-artifact/base.rb', line 145

def verify(file_to_verifiy, &content_block)
  f = File.open(file_to_verifiy, "r")
  file_version = f.readline.strip!
  file_keyname = f.readline.strip!
  file_alg = f.readline.strip!

  file_sig = ""
  # the signature is multi-line
  while (line = f.readline) != "\n"
    file_sig += line
  end
  file_sig.strip!
  f.close

  valid_header?(file_alg, file_version, file_keyname)

  public_keyfile = "#{file_keyname}.pem.pub"
  verification_key = KEY_ALG.new File.read public_keyfile

  f = File.open(file_to_verifiy, "r")
  while f.readline != "\n" do end
  content = f.read

  signature = Base64.decode64(file_sig)
  digest = ARTIFACT_DIGEST.new
  if verification_key.verify digest, signature, content
    content_block.yield(content)
  else
    raise "Artifact is invalid"
  end
end