obscure_yaml 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8cb65381c4bb6f6f29047bf4b7edf7080fb1cbb
4
- data.tar.gz: f900378b076ebfcbf898bee3b7e1fb6aa7a7c3d7
3
+ metadata.gz: e586f3486eb9b5be77a5e103aafa209eed7c4e88
4
+ data.tar.gz: 82a944c7e62989133cb8b50d44cd93bf013daaf0
5
5
  SHA512:
6
- metadata.gz: 48cda1819f12cd9776993a1cc376fe9d50e63c25e6613fa2e70ff20140e9657adc1a7ba3b85c731dd2338de2c2e7898287541675ffbb3d832f7e0b67d32a823d
7
- data.tar.gz: 13036cb2318f7a05d122491bd37f93b0ef12bd51439c9d412a89e0f9495460f7bf2eccf7eb8edf298101ae8793beeec161a69d251e8509b78a17167c8e36a373
6
+ metadata.gz: f1e280e19a6758a0cc1ec1b812f95ed449f3d122fdd50132cdd3e2acbbb551b877f6d54d40911e8edc8c0430aae94add680c95b37c68e36688175624d7909e5d
7
+ data.tar.gz: cb7b03a2c017e20d960ef2a0bdfda64fbbc4ab936bed1282b23e3e12f58e2999aa9e8487f96aa1859aeb8af7cd7690b7d4cd400df49e2581aac894d6316b8208
data/README.md CHANGED
@@ -1,4 +1,29 @@
1
- # obscure-yaml
1
+ # obscure_yaml
2
2
 
3
3
  This gem contains helper classes for working with Base64 encoded
4
- Yaml files, in case the values you need to serialize are not valid YAML values without escaping etc, it will either read the value from a specified fle or use yaml value directly, and write the resulting data all to a single yaml file with 100% base64 encoded values for consistency.
4
+ Yaml files, typically for kubernetes secrets.yml files, the values of which must be base64 encoded. It will either read the value from a specified fle or use yaml value directly, and write the resulting data all to a single yaml file with base64 encoded values under a specfied key ('data' by default) for consistency. Decoding always results in a single file, with escaped values as necessary.
5
+
6
+ Those long/unescaped values can be loaded from files referenced in the source yaml by wrapping in "magic" file/url delimiters, ('@' by default), e.g:
7
+ ```yaml
8
+ apiVersion: v1
9
+ kind: ConfigMap
10
+ data:
11
+ long_crazy_indirect_value: '@local_unescaped_file.txt@'
12
+ regular_direct_value_domain: example.com
13
+ ```
14
+
15
+ The delimiter can be changed with the env var `MAGIC_FILE` from default value of '@', and the data key can be changed from it's default value of 'data' with env var `DATA_KEY`.
16
+
17
+ Gem install as usual in bundler or directly as `obscure_yaml`, though only CLI usage is intended at present.
18
+
19
+ ###Example usage
20
+ ```
21
+ $ obscure encode connect.configmap.yml # outputs encoded yaml to stdout
22
+ $ obscure decode connect.secrets.yml # outputs decode yaml to stdout
23
+ $ obscure encode connect.configmap.yml tmp.out # writes encoded yaml to tmp.out file
24
+ $ obscure decode connect.secrets.yml tmp2.out # writes encoded yaml to tmp2.out file
25
+ $ cat file.yml | obscure decode # reads non-base64 input yaml from stdin, writes decoded yamlto stdout
26
+ $ cat file.yml | obscure encode # reads base64 encoded input yaml from stdin, write encoded to stdout
27
+ ```
28
+
29
+ A valid url may also be used in place of a file path for input or output. There are no tests at present, because this was just an afternoon project, but pull requests are welcome if you find this useful!
data/lib/obscure_yaml.rb CHANGED
@@ -5,48 +5,46 @@ require 'open-uri'
5
5
  module ObscureYaml
6
6
  DEFAULT_DATA_KEY = 'data'
7
7
  MAGIC_DELIMITER = '@'
8
- module_function
8
+ class << self
9
9
 
10
- def decode(output, input)
11
- output << construct_yaml(input, :output)
12
- end
10
+ def decode(output, input)
11
+ output << construct_yaml(input, :output)
12
+ end
13
13
 
14
- def encode(output, input)
15
- output << construct_yaml(input, :input)
16
- end
14
+ def encode(output, input)
15
+ output << construct_yaml(input, :input)
16
+ end
17
17
 
18
- private
18
+ private
19
19
 
20
- module_function
20
+ def construct_yaml(yaml_source, direction)
21
+ YAML::load(yaml_source.read).tap { |hsh|
22
+ data = hsh[obscured_data_key]
23
+ hsh[obscured_data_key] = data.map { |key, value|
24
+ [key, handle_coding(direction, value)]
25
+ }.to_h
26
+ }.to_yaml
27
+ end
21
28
 
22
- def construct_yaml(yaml_source, direction)
23
- YAML::load(yaml_source.read).tap { |hsh|
24
- data = hsh[obscured_data_key]
25
- hsh[obscured_data_key] = data.map { |key, value|
26
- [key, handle_coding(direction, value)]
27
- }.to_h
28
- }.to_yaml
29
- end
29
+ def handle_coding(direction, value)
30
+ direction == :output ? Base64.decode64(value) : Base64.encode64(value_or_file_contents(value))
31
+ end
30
32
 
31
- def handle_coding(direction, value)
32
- direction == :output ? Base64.decode64(value) : Base64.encode64(value_or_file_contents(value))
33
- end
33
+ def value_or_file_contents(value)
34
+ return value unless detect_file(value)
35
+ value_contents = open(value.gsub(magic_delimiter, '')) { |f| f.read }
36
+ end
34
37
 
35
- def value_or_file_contents(value)
36
- return value unless detect_file(value)
37
- value_contents = open(value.gsub(magic_delimiter, '')) { |f| f.read }
38
- end
38
+ def detect_file(value)
39
+ value.match /\A#{magic_delimiter}(.+)#{magic_delimiter}\z/
40
+ end
39
41
 
40
- def detect_file(value)
41
- value.match /\A#{magic_delimiter}(.+)#{magic_delimiter}\z/
42
- end
43
-
44
- def magic_delimiter
45
- ENV['MAGIC_FILE'] || MAGIC_DELIMITER
46
- end
42
+ def magic_delimiter
43
+ ENV['MAGIC_FILE'] || MAGIC_DELIMITER
44
+ end
47
45
 
48
- def obscured_data_key
49
- ENV['DATA_KEY'] || DEFAULT_DATA_KEY
46
+ def obscured_data_key
47
+ ENV['DATA_KEY'] || DEFAULT_DATA_KEY
48
+ end
50
49
  end
51
-
52
50
  end
@@ -1,3 +1,3 @@
1
1
  module ObscureYaml
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obscure_yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Glusman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-22 00:00:00.000000000 Z
11
+ date: 2016-09-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: " There was no call for this really, except we needed it.\n"
14
14
  email: