grafana-rb 0.17.0 → 0.18.0

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: 97ee4b71f1093f8df1f8f780a0c18db45843fdd0
4
- data.tar.gz: ea170777a2eab5af4ccade42826218914ab2f85e
3
+ metadata.gz: 7157e2f6049795a1400c4fa71c176755b781692d
4
+ data.tar.gz: 19fa013da17499527d3dc045320aa922c01bccff
5
5
  SHA512:
6
- metadata.gz: 3af6932643551fe7019e6afe2dc4378c2bde2de33ecddb9a81d12285428ee509dd8b143a5c141939d4f60686e75546fb127e22e7795b920e0d3730520a89ebc9
7
- data.tar.gz: bb1e7ab8827c51466afcd74c2e8701e79c74d30f60109793adfe5a034571e058d2cbd0f86585bb577878d3158c3d785390b18f10914393b009dbd49b162c240d
6
+ metadata.gz: e8e9b4716ebb83a368579cd149127d8d68cf29983eb11561d0c71bbdd736c7a84750cfb21b5fad712dcd74a1b83a5129433bcfcea1b52ecbcdf7354e55c02f04
7
+ data.tar.gz: ade8147a0a4b82ee7a05e5cc75a5392487206b18b79636ef663b103799efe8999cef07ffc39ad1e8a91718f943da3855712a6c4868ec6775923089497dbb6ad0
@@ -1,3 +1,7 @@
1
+ ## Grafana-rb 0.18.0 (April 13, 2017) ##
2
+
3
+ * Support crypting of sensitive information using vault file as key.
4
+
1
5
  ## Grafana-rb 0.17.0 (April 13, 2017) ##
2
6
 
3
7
  * Support protocol in `grafana_url`.
@@ -2,6 +2,7 @@ require 'yaml'
2
2
  require 'json'
3
3
  require 'pp'
4
4
  require 'net/http'
5
+ require 'openssl'
5
6
 
6
7
  module GrafanaRb
7
8
  class Cli
@@ -12,11 +13,28 @@ module GrafanaRb
12
13
  end
13
14
 
14
15
  def run
15
- if @argv.last == "apply" || @argv.last == "a" || @argv.last == "q"
16
- if @argv.size >= 2
17
- @workdir = @argv[0]
18
- end
16
+ if File.directory?(@argv[0].to_s)
17
+ @workdir = @argv.shift
18
+ end
19
+
20
+ if @argv[0] == "--vault" && File.exists?(@argv[1])
21
+ @argv.shift
22
+ @vault_file = @argv.shift
23
+ elsif File.exists?(File.join(Dir.pwd, "vault.key"))
24
+ @vault_file = File.join(Dir.pwd, "vault.key")
25
+ else
26
+ @vault_file = nil
27
+ end
28
+
29
+ if %w[apply a q].index(@argv[0])
19
30
  apply
31
+
32
+ elsif %w[encrypt en e].index(@argv[0])
33
+ puts encrypt(@argv[1])
34
+
35
+ elsif %w[decrypt de d].index(@argv[0])
36
+ puts decrypt(@argv[1])
37
+
20
38
  else
21
39
  usage
22
40
  end
@@ -27,6 +45,35 @@ module GrafanaRb
27
45
  DEFAULT_GRAFANA_USER = "admin"
28
46
  DEFAULT_GRAFANA_PASSWORD = "admin"
29
47
  TAG = "grafana-rb"
48
+ MARKER = "__VAULT:"
49
+
50
+ def bin_to_hex(s)
51
+ s.unpack('H*').first
52
+ end
53
+
54
+ def hex_to_bin(s)
55
+ s.scan(/../).map { |x| x.hex }.pack('c*')
56
+ end
57
+
58
+ def encrypt(string)
59
+ die "missed vault file" unless @vault_file
60
+ die "string encrypted yet" if string.index(MARKER) == 0
61
+ cipher = OpenSSL::Cipher::AES256.new :CBC
62
+ cipher.encrypt
63
+ iv = cipher.random_iv
64
+ cipher.key = Digest::SHA256.digest(File.read(@vault_file).strip)
65
+ MARKER + bin_to_hex(cipher.update(string) + cipher.final) + ":" + bin_to_hex(iv)
66
+ end
67
+
68
+ def decrypt(string)
69
+ die "missed vault file" unless @vault_file
70
+ die "string not encrypted" unless string.index(MARKER) == 0
71
+ cipher = OpenSSL::Cipher::AES256.new :CBC
72
+ cipher.decrypt
73
+ cipher.iv = hex_to_bin(string.sub(MARKER, "").split(":")[1])
74
+ cipher.key = Digest::SHA256.digest(File.read(@vault_file).strip)
75
+ cipher.update(hex_to_bin(string.sub(MARKER, "").split(":")[0])) + cipher.final
76
+ end
30
77
 
31
78
  def config_file
32
79
  @config_file ||= File.join(@workdir, "grafana.yml")
@@ -90,8 +137,23 @@ module GrafanaRb
90
137
  end
91
138
  end
92
139
 
140
+ def read_yaml(file)
141
+ unvault = proc { |o|
142
+ if o.is_a?(Array)
143
+ o.map { |i| unvault.call(i) }
144
+ elsif o.is_a?(Hash)
145
+ o.map { |k, v| [k, unvault.call(v)] }.to_h
146
+ elsif o.is_a?(String) && o.index(MARKER) == 0
147
+ decrypt(o)
148
+ else
149
+ o
150
+ end
151
+ }
152
+ unvault.call(YAML.load(File.read(file)))
153
+ end
154
+
93
155
  def config
94
- @config ||= YAML.load(File.read(config_file)) || {}
156
+ @config ||= read_yaml(config_file) || {}
95
157
  end
96
158
 
97
159
  def grafana_user
@@ -110,7 +172,7 @@ module GrafanaRb
110
172
 
111
173
  def dashboards
112
174
  @dashboards ||= require_files.map { |path|
113
- yaml = YAML.load(File.read(path))
175
+ yaml = read_yaml(path)
114
176
  yaml["name"] ||= File.basename(path, File.extname(path))
115
177
  yaml
116
178
  }
@@ -368,10 +430,15 @@ module GrafanaRb
368
430
  puts "Version: #{VERSION}"
369
431
  puts ""
370
432
  puts "Usage:"
371
- puts " grafana-rb [work-dir] <cmd>"
433
+ puts " grafana-rb [work-dir] [options] <cmd>"
434
+ puts ""
435
+ puts "Options:"
436
+ puts " --vault <vault-file> - vault file (<pwd>/vault.key by default)"
372
437
  puts ""
373
438
  puts "Commands:"
374
- puts " apply - update dashboards"
439
+ puts " apply - update dashboards"
440
+ puts " encrypt <string> - encrypt script using vault-file"
441
+ puts " decrypt <string> - decrypt script using vault-file"
375
442
  puts ""
376
443
  end
377
444
  end
@@ -1,3 +1,3 @@
1
1
  module GrafanaRb
2
- VERSION = "0.17.0"
2
+ VERSION = "0.18.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grafana-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Vakhov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-12 00:00:00.000000000 Z
11
+ date: 2017-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler