<commit_before>module Dome
  class HieraLookup
    def initialize(environment)
      @environment = environment.environment
      @account     = environment.account
      @ecosystem   = environment.ecosystem
      @settings    = Dome::Settings.new
    end

    def config
      @config ||= YAML.load_file(File.join(puppet_dir, 'hiera.yaml')).merge(
        {
          logger: 'noop',
          yaml: {
            datadir: "#{puppet_dir}/hieradata",
          },
          eyaml: {
            datadir: "#{puppet_dir}/hieradata",
            pkcs7_private_key: eyaml_private_key,
            pkcs7_public_key: eyaml_public_key
          }
        }
      )
    end

    def puppet_dir
      directory = File.join(@settings.project_root, 'puppet')
      puts "The configured Puppet directory is: #{directory.colorize(:green)}" unless @directory
      @directory ||= directory
    end

    def eyaml_private_key
      private_key = File.join(puppet_dir, 'keys/private_key.pkcs7.pem')
      raise "Cannot find eyaml private key! Make sure it exists at #{private_key}" unless File.exist?(private_key)
      puts "Found eyaml private key: #{private_key.colorize(:green)}"
      private_key
    end

    def eyaml_public_key
      public_key = File.join(puppet_dir, 'keys/public_key.pkcs7.pem')
      raise "Cannot find eyaml public key! Make sure it exists at #{public_key}" unless File.exist?(public_key)
      puts "Found eyaml public key: #{public_key.colorize(:green)}"
      public_key
    end

    def lookup(key, default = nil, order_override = nil, resolution_type = :priority)
      hiera = Hiera.new(config: config)

      hiera_scope = {}
      hiera_scope['ecosystem'] = @ecosystem
      hiera_scope['location']  = 'aeuw1'
      hiera_scope['env']       = @environment

      hiera.lookup(key.to_s, default, hiera_scope, order_override, resolution_type)
    end

    def secret_env_vars(secret_vars)
      secret_vars.each_pair do |key, val|
        hiera_lookup = lookup(val)
        terraform_env_var = "TF_VAR_#{key}"
        ENV[terraform_env_var] = hiera_lookup
        if hiera_lookup
          puts "Setting #{terraform_env_var.colorize(:green)}."
        else
          puts "Hiera lookup failed for '#{val}', so #{terraform_env_var} was not set.".colorize(:red)
        end
      end
    end

    def extract_certs(certs)
      create_certificate_directory

      certs.each_pair do |key, val|
        directory = "#{certificate_directory}/#{key}"
        puts "Extracting cert #{key.colorize(:green)} into: #{directory.colorize(:green)}"
        File.open(directory, 'w') { |f| f.write(lookup(val)) }
      end
    end

    def create_certificate_directory
      puts "Creating certificate directory at #{certificate_directory.colorize(:green)}."
      FileUtils.mkdir_p certificate_directory
    end
<commit_msg>improved certificate debug output<commit_after>module Dome
  class HieraLookup
    def initialize(environment)
      @environment = environment.environment
      @account     = environment.account
      @ecosystem   = environment.ecosystem
      @settings    = Dome::Settings.new
    end

    def config
      @config ||= YAML.load_file(File.join(puppet_dir, 'hiera.yaml')).merge(
        {
          logger: 'noop',
          yaml: {
            datadir: "#{puppet_dir}/hieradata",
          },
          eyaml: {
            datadir: "#{puppet_dir}/hieradata",
            pkcs7_private_key: eyaml_private_key,
            pkcs7_public_key: eyaml_public_key
          }
        }
      )
    end

    def puppet_dir
      directory = File.join(@settings.project_root, 'puppet')
      puts "The configured Puppet directory is: #{directory.colorize(:green)}" unless @directory
      @directory ||= directory
    end

    def eyaml_private_key
      private_key = File.join(puppet_dir, 'keys/private_key.pkcs7.pem')
      raise "Cannot find eyaml private key! Make sure it exists at #{private_key}" unless File.exist?(private_key)
      puts "Found eyaml private key: #{private_key.colorize(:green)}"
      private_key
    end

    def eyaml_public_key
      public_key = File.join(puppet_dir, 'keys/public_key.pkcs7.pem')
      raise "Cannot find eyaml public key! Make sure it exists at #{public_key}" unless File.exist?(public_key)
      puts "Found eyaml public key: #{public_key.colorize(:green)}"
      public_key
    end

    def lookup(key, default = nil, order_override = nil, resolution_type = :priority)
      hiera = Hiera.new(config: config)

      hiera_scope = {}
      hiera_scope['ecosystem'] = @ecosystem
      hiera_scope['location']  = 'aeuw1'
      hiera_scope['env']       = @environment

      hiera.lookup(key.to_s, default, hiera_scope, order_override, resolution_type)
    end

    def secret_env_vars(secret_vars)
      secret_vars.each_pair do |key, val|
        hiera_lookup = lookup(val)
        terraform_env_var = "TF_VAR_#{key}"
        ENV[terraform_env_var] = hiera_lookup
        if hiera_lookup
          puts "Setting #{terraform_env_var.colorize(:green)}."
        else
          puts "Hiera lookup failed for '#{val}', so #{terraform_env_var} was not set.".colorize(:red)
        end
      end
    end

    def extract_certs(certs)
      create_certificate_directory

      certs.each_pair do |key, val|
        directory = "#{certificate_directory}/#{key}"
        puts "Extracting certificate #{key.colorize(:green)} into #{directory.colorize(:green)}"
        File.open(directory, 'w') { |f| f.write(lookup(val)) }
      end
    end

    def create_certificate_directory
      puts "Creating certificate directory at #{certificate_directory.colorize(:green)}"
      FileUtils.mkdir_p certificate_directory
    end

{'fraction_non_alphanumeric': 0.10480042201512221, 'fraction_numerical': 0.0017583963425356075, 'mean_word_length': 2.774386197743862, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3273840', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1787, 'n_words': 391}