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}==)$/- SECRET_SUFFIX =
".secret.yml"- CONFIG_SUFFIX =
".configmap.yml"
Class Method Summary collapse
- .code_yaml(yaml_source, direction) ⇒ Object
- .decode(output, input) ⇒ Object
- .detect_file(value) ⇒ Object
- .encode(output, input) ⇒ Object
- .handle_coding(direction, value) ⇒ Object
- .magic_delimiter ⇒ Object
- .merge(output, input1, input2) ⇒ Object
- .merge_hash(hsh1, hsh2) ⇒ Object
- .merge_yaml(input1, input2) ⇒ Object
- .obscured_data_key ⇒ Object
- .value_or_file_contents(value) ⇒ Object
- .write_configs_encode_if_needed(config_hsh, secret_hsh, output_path, project_name) ⇒ Object
Class Method Details
.code_yaml(yaml_source, direction) ⇒ Object
34 35 36 37 38 39 40 41 |
# 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) ⇒ Object
11 12 13 |
# File 'lib/ky/manipulation.rb', line 11 def decode(output, input) output << code_yaml(input, :decode) end |
.detect_file(value) ⇒ Object
52 53 54 |
# File 'lib/ky/manipulation.rb', line 52 def detect_file(value) value.match /\A#{magic_delimiter}(.+)#{magic_delimiter}\z/ end |
.encode(output, input) ⇒ Object
15 16 17 |
# File 'lib/ky/manipulation.rb', line 15 def encode(output, input) output << code_yaml(input, :encode) end |
.handle_coding(direction, value) ⇒ Object
43 44 45 |
# 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 ⇒ Object
56 57 58 |
# File 'lib/ky/manipulation.rb', line 56 def magic_delimiter ENV['MAGIC_FILE'] || MAGIC_DELIMITER end |
.merge(output, input1, input2) ⇒ Object
19 20 21 |
# File 'lib/ky/manipulation.rb', line 19 def merge(output, input1, input2) output << merge_yaml(input1, input2) end |
.merge_hash(hsh1, hsh2) ⇒ Object
30 31 32 |
# 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) ⇒ Object
23 24 25 26 27 28 |
# 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 ⇒ Object
60 61 62 |
# File 'lib/ky/manipulation.rb', line 60 def obscured_data_key ENV['DATA_KEY'] || DEFAULT_DATA_KEY end |
.value_or_file_contents(value) ⇒ Object
47 48 49 50 |
# 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) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# 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 |