Fix the --profile-content-id option not to overwrite the existing profile_content_id value in the metadata file

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
This commit is contained in:
Vasu1105 2022-05-31 22:04:42 +05:30
parent 12b73a6609
commit fdb4d5c3ca
2 changed files with 15 additions and 11 deletions

View file

@ -49,16 +49,17 @@ module InspecPlugins
artifact = new
path_to_profile = options["profile"]
# Writes the profile content id in the inspec.yml
if options[:profile_content_id] && !options[:profile_content_id].strip.empty?
artifact.write_profile_content_id(path_to_profile, options[:profile_content_id])
end
puts "Signing #{options["profile"]} with key #{options["keyname"]}"
keypath = Inspec::IafFile.find_signing_key(options["keyname"])
# Read name and version from metadata and use them to form the filename
profile_md = artifact.read_profile_metadata(path_to_profile)
# Writes the profile content id in the inspec.yml
if options[:profile_content_id] && !options[:profile_content_id].strip.empty?
artifact.write_profile_content_id(path_to_profile, options[:profile_content_id])
end
artifact_filename = "#{profile_md["name"]}-#{profile_md["version"]}.#{SIGNED_PROFILE_SUFFIX}"
# Generating tar.gz file using archive method of Inspec Cli
@ -141,15 +142,18 @@ module InspecPlugins
p = p.join("inspec.yml")
yaml = YAML.load_file(p.to_s)
existing_profile_content_id = yaml["profile_content_id"]
motdobj = IO.readlines(p)
if existing_profile_content_id.nil?
motdobj << "profile_content_id: #{profile_content_id}\n"
else
motdobj = motdobj.map {|s| s.gsub("profile_content_id: #{existing_profile_content_id}", "profile_content_id: #{profile_content_id}")}
unless existing_profile_content_id.nil?
ui = Inspec::UI.new
ui.error("Cannot use --profile-content-id when profile_content_id already exists in metadata file.")
ui.exit(:usage_error)
end
lines = IO.readlines(p)
lines << "\nprofile_content_id: #{profile_content_id}\n"
File.open("#{p}", "w" ) do | f |
f.puts motdobj
f.puts lines
end
end
end

View file

@ -95,7 +95,7 @@ module InspecPlugins
option :keyname, type: :string, required: true,
desc: "Desriptive name of key"
option :profile_content_id, type: :string,
desc: "UUID of the profile. This will overwrite an existing profile content id if it already exists in the metadata file."
desc: "UUID of the profile. This will write the profile_content_id in the metadata file if it does not already exist in the metadata file."
def profile
InspecPlugins::Sign::Base.profile_sign(options)
end