module KY::Manipulation

Constants

BASE_64_DETECTION_REGEX
CONFIG_SUFFIX
DEFAULT_DATA_KEY
MAGIC_DELIMITER
SECRET_SUFFIX

Public Class Methods

code_yaml(yaml_source, direction) click to toggle source
# File lib/ky/manipulation.rb, line 34
def code_yaml(yaml_source, direction)
  YAML.load(yaml_source.read).tap { |hsh|
    data = hsh[obscured_data_key]
    hsh[obscured_data_key] = data.map { |key, value|
        [key, handle_coding(direction, value)]
      }.to_h
  }.to_plain_yaml
end
decode(output, input) click to toggle source
# File lib/ky/manipulation.rb, line 11
def decode(output, input)
  output << code_yaml(input, :decode)
end
detect_file(value) click to toggle source
# File lib/ky/manipulation.rb, line 52
def detect_file(value)
  value.match /\A#{magic_delimiter}(.+)#{magic_delimiter}\z/
end
encode(output, input) click to toggle source
# File lib/ky/manipulation.rb, line 15
def encode(output, input)
  output << code_yaml(input, :encode)
end
handle_coding(direction, value) click to toggle source
# File lib/ky/manipulation.rb, line 43
def handle_coding(direction, value)
  direction == :decode ? Base64.decode64(value) : Base64.strict_encode64(value_or_file_contents(value))
end
magic_delimiter() click to toggle source
# File lib/ky/manipulation.rb, line 56
def magic_delimiter
  ENV['MAGIC_FILE'] || MAGIC_DELIMITER
end
merge(output, input1, input2) click to toggle source
# File lib/ky/manipulation.rb, line 19
def merge(output, input1, input2)
  output << merge_yaml(input1, input2)
end
merge_hash(hsh1, hsh2) click to toggle source
# File lib/ky/manipulation.rb, line 30
def merge_hash(hsh1, hsh2)
  hsh1.deeper_merge!(hsh2, merge_hash_arrays: true, extend_existing_arrays: true, knockout_prefix: "--").compact_blank(recurse: true)
end
merge_yaml(input1, input2) click to toggle source
# File lib/ky/manipulation.rb, line 23
def merge_yaml(input1, input2)
  combined = {}
  YAML.load(input1.read).tap { |hsh|
    merge_hash(hsh, YAML.load(input2.read))
  }.to_plain_yaml
end
obscured_data_key() click to toggle source
# File lib/ky/manipulation.rb, line 60
def obscured_data_key
  ENV['DATA_KEY'] || DEFAULT_DATA_KEY
end
value_or_file_contents(value) click to toggle source
# File lib/ky/manipulation.rb, line 47
def value_or_file_contents(value)
  return value unless detect_file(value)
  value_contents = open(value.gsub(magic_delimiter, '')) { |f| f.read }
end
write_configs_encode_if_needed(config_hsh, secret_hsh, output_path, project_name) click to toggle source
# File lib/ky/manipulation.rb, line 64
def write_configs_encode_if_needed(config_hsh, secret_hsh, output_path, project_name)
  secret_yaml = if secret_hsh[obscured_data_key].values.all? {|value| BASE_64_DETECTION_REGEX =~ value }
                  secret_hsh.to_plain_yaml
                else
                  code_yaml(StringIO.new(secret_hsh.to_plain_yaml), :encode)
                end
  File.write("#{output_path}/#{project_name}#{SECRET_SUFFIX}", secret_yaml)
  File.write("#{output_path}/#{project_name}#{CONFIG_SUFFIX}", config_hsh.to_plain_yaml)
end