Sha256: 97310d395ef15ed4caab6672159442eadb69b5bb884be50c826c9ce622362827

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'json'
require 'yaml'
require 'tmpdir'
require 'shellwords'

module DCCSCR
  # Class to download the dccscr_whitelist repo and store greylist entries.
  #
  class Whitelist
    UPSTREAM_REPO = 'https://repo1.dso.mil/dsop/dccscr-whitelists.git'

    attr_reader :path, :repo, :entries

    def initialize(path: nil, repo: nil, clone: true, clone_options: '--depth 1')
      if path
        @path = path
      else
        @path = Dir.mktmpdir
        at_exit { FileUtils.remove_entry @path }
      end

      if repo
        @repo = repo
      else
        @repo = UPSTREAM_REPO
      end

      if clone
        clone_options = Shellwords.join(Shellwords.split(clone_options).map { |w| Shellwords.escape(w) })
        system "git clone #{clone_options} -- #{@repo.inspect} #{@path.inspect}"
        $?.success? || fail('error cloning repo')
      end

      @entries = {}
    end

    def [](subpath)
      entries[subpath] ||= Entry.new(whitelist: self, subpath: subpath)
    end

    # Internal class to hold a single greylist.
    #
    class Entry
      attr_reader :value, :parent

      def initialize(whitelist:, subpath:, greylist: "#{File.basename(subpath)}.greylist")
        @value = JSON.parse(File.read(File.join(whitelist.path, subpath, greylist)))

        whitelist[@parent] unless (@parent = @value['image_parent_name'])&.empty?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dccscr-0.2.4 lib/dccscr/whitelist.rb