#!/usr/bin/env ruby # frozen_string_literal: false require 'optparse' require 'pwn' require 'yaml' opts = {} OptionParser.new do |options| options.banner = "USAGE: #{$PROGRAM_NAME} [opts] " options.on('-cCONFIG', '--config=CONFIG', '') do |c| opts[:config] = c end options.on('-CGROUP', '--create=GROUP', '') do |g| opts[:group_name] = g end options.on('-lNAME', '--list-group=NAME', '') do |l| opts[:list_group_name] = l end options.on('-pID', '--parent-group-id=ID', '') do |p| opts[:parent_group_id] = p end options.on('-bSEC', '--binary-cleanup-age=SEC', '') do |b| opts[:binary_cleanup_age] = b end options.on('-PSEC', '--product-cleanup-age=SEC', '') do |p| opts[:product_cleanup_age] = p end end.parse! if opts.empty? puts `#{$PROGRAM_NAME} --help` exit 1 end begin pwn_provider = 'ruby-gem' pwn_provider = ENV.fetch('PWN_PROVIDER') if ENV.keys.any? { |s| s == 'PWN_PROVIDER' } config = opts[:config] raise "ERROR: BDBA YAML Config File Not Found: #{config}" unless File.exist?(config) yaml_config = YAML.load_file(config, symbolize_names: true) token = yaml_config[:token] raise "ERROR: BDBA Token Not Found: #{token}" if token.nil? list_group_name = opts[:list_group_name] parent_group_id = opts[:parent_group_id] binary_cleanup_age = opts[:binary_cleanup_age] product_cleanup_age = opts[:product_cleanup_age] if list_group_name groups_resp = PWN::Plugins::BlackDuckBinaryAnalysis.get_groups( token: token ) group_arr = groups_resp[:groups].select { |g| g[:name] == list_group_name } if list_group_name && group_arr.empty? puts 'BDBA Group Not Found.' exit 1 end group_details_arr = [] # TODO: Add pagination support # i.e. group_arr[:next] group_arr.each do |group| group_id = group[:id] this_group_details = PWN::Plugins::BlackDuckBinaryAnalysis.get_group_details( token: token, group_id: group_id ) group_details_arr.push(this_group_details) end if parent_group_id filter_group_details_arr_by_parent = group_details_arr.select do |g| g[:group][:parent] == parent_group_id.to_i end group_details_arr = filter_group_details_arr_by_parent if group_details_arr.empty? puts "BDBA Group Not Found for Parent Group ID: #{parent_group_id}" exit 1 end end puts group_details_arr.to_json exit 0 end group_name = opts[:group_name] raise "ERROR: BDBA Group Name Not Provided: #{group_name}" if group_name.nil? create_group_resp = PWN::Plugins::BlackDuckBinaryAnalysis.create_group( token: token, name: group_name, parent_id: parent_group_id, binary_cleanup_age: binary_cleanup_age, product_cleanup_age: product_cleanup_age ) puts create_group_resp.to_json rescue SystemExit, Interrupt puts "\nGoodbye." rescue StandardError => e raise e end