Module: KY::Manipulation

Defined in:
lib/ky/manipulation.rb

Constant Summary collapse

DEFAULT_DATA_KEY =
'data'
MAGIC_DELIMITER =
'@'
BASE_64_DETECTION_REGEX =
/^([A-Za-z0-9+]{4})*([A-Za-z0-9+]{4}|[A-Za-z0-9+]{3}=|[A-Za-z0-9+]{2}==)$/

Class Method Summary collapse

Class Method Details

.code_yaml(yaml_source, direction) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/ky/manipulation.rb', line 32

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) ⇒ Object



9
10
11
# File 'lib/ky/manipulation.rb', line 9

def decode(output, input)
  output << code_yaml(input, :decode)
end

.detect_file(value) ⇒ Object



50
51
52
# File 'lib/ky/manipulation.rb', line 50

def detect_file(value)
  value.match /\A#{magic_delimiter}(.+)#{magic_delimiter}\z/
end

.encode(output, input) ⇒ Object



13
14
15
# File 'lib/ky/manipulation.rb', line 13

def encode(output, input)
  output << code_yaml(input, :encode)
end

.handle_coding(direction, value) ⇒ Object



41
42
43
# File 'lib/ky/manipulation.rb', line 41

def handle_coding(direction, value)
  direction == :decode ? Base64.decode64(value) : Base64.strict_encode64(value_or_file_contents(value))
end

.magic_delimiterObject



54
55
56
# File 'lib/ky/manipulation.rb', line 54

def magic_delimiter
  ENV['MAGIC_FILE'] || MAGIC_DELIMITER
end

.merge(output, input1, input2) ⇒ Object



17
18
19
# File 'lib/ky/manipulation.rb', line 17

def merge(output, input1, input2)
  output << merge_yaml(input1, input2)
end

.merge_hash(hsh1, hsh2) ⇒ Object



28
29
30
# File 'lib/ky/manipulation.rb', line 28

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) ⇒ Object



21
22
23
24
25
26
# File 'lib/ky/manipulation.rb', line 21

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_keyObject



58
59
60
# File 'lib/ky/manipulation.rb', line 58

def obscured_data_key
  ENV['DATA_KEY'] || DEFAULT_DATA_KEY
end

.value_or_file_contents(value) ⇒ Object



45
46
47
48
# File 'lib/ky/manipulation.rb', line 45

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) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/ky/manipulation.rb', line 62

def write_configs_encode_if_needed(config_hsh, secret_hsh, output_path, project_name)
  if secret_hsh[obscured_data_key].values.all? {|value| BASE_64_DETECTION_REGEX =~ value }
    File.write("#{output_path}/#{project_name}.secret.yml", secret_hsh.to_plain_yaml)
  else
    File.write("#{output_path}/#{project_name}.secret.yml", code_yaml(StringIO.new(secret_hsh.to_plain_yaml), :encode))
  end
  File.write("#{output_path}/#{project_name}.configmap.yml", config_hsh.to_plain_yaml)
end