kafo 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kafo might be problematic. Click here for more details.

@@ -1,28 +1,47 @@
1
1
  require 'yaml'
2
2
  require 'kafo/puppet_module'
3
+ require 'kafo/password_manager'
3
4
 
4
5
  class Configuration
5
6
  attr_reader :config_file
6
7
 
8
+ def self.application_config_file
9
+ File.join(Dir.pwd, 'config/kafo.yaml')
10
+ end
7
11
 
8
- begin
9
- default_hash = YAML.load_file(File.join(Dir.pwd, 'config/kafo.yaml'))
10
- rescue => e
11
- default_hash = {}
12
+ def self.save_configuration(configuration)
13
+ File.write(application_config_file, YAML.dump(configuration))
12
14
  end
13
- KAFO = {
15
+
16
+ def self.configure_application
17
+ begin
18
+ configuration = YAML.load_file(application_config_file)
19
+ rescue => e
20
+ configuration = {}
21
+ end
22
+
23
+ default = {
14
24
  :log_dir => '/var/log/kafo',
15
25
  :log_level => :info,
16
26
  :no_prefix => false,
17
27
  :mapping => {}
18
- }.merge(default_hash || {})
28
+ }
29
+
30
+ result = default.merge(configuration || {})
31
+ result[:password] ||= PasswordManager.new.password
32
+ save_configuration(result)
33
+
34
+ result
35
+ end
36
+
37
+ KAFO = configure_application
19
38
 
20
39
  def initialize(file)
21
40
  @logger = Logging.logger.root
22
41
  @logger.info "Loading config file #{file}"
23
42
 
24
43
  begin
25
- @data = YAML.load_file file
44
+ @data = YAML.load_file(file)
26
45
  rescue Errno::ENOENT => e
27
46
  puts "No answers file at #{file} found, can not continue"
28
47
  exit(23)
@@ -138,7 +138,7 @@ class KafoConfigure < Clamp::Command
138
138
  ]
139
139
  options.push '--noop' if noop?
140
140
  begin
141
- PTY.spawn("echo include kafo_configure | puppet apply #{options.join(' ')}") do |stdin, stdout, pid|
141
+ PTY.spawn("echo 'include kafo_configure' | puppet apply #{options.join(' ')}") do |stdin, stdout, pid|
142
142
  begin
143
143
  stdin.each { |line| puppet_log(line) }
144
144
  rescue Errno::EIO
data/lib/kafo/param.rb CHANGED
@@ -61,5 +61,6 @@ end
61
61
 
62
62
  require 'kafo/params/boolean'
63
63
  require 'kafo/params/string'
64
+ require 'kafo/params/password'
64
65
  require 'kafo/params/array'
65
66
  require 'kafo/params/integer'
@@ -0,0 +1,50 @@
1
+ module Params
2
+ # A password paramater is stored encrypted in answer file using AES 256 in CBC mode
3
+ #
4
+ # we use a passphrase that is stored in kafo.yaml for encryption
5
+ # encrypted password is prefixed with $1$ (for historical reasons, no connection to
6
+ # Modular Crypt Format)
7
+ class Password < Param
8
+ def value=(value)
9
+ super
10
+ setup_password if @value.is_a?(::String)
11
+ @value
12
+ end
13
+
14
+ # if value was not specified and default is nil we generate a random password
15
+ # also we make sure that we have encrypted version that is to be outputted
16
+ def value
17
+ @value = @value_set ? @value : (default || password_manager.password)
18
+ encrypt if @value.is_a?(::String)
19
+ @encrypted
20
+ end
21
+
22
+ private
23
+
24
+ def setup_password
25
+ encrypted? ? decrypt : encrypt
26
+ end
27
+
28
+ def encrypted?
29
+ @value.length > 3 && @value[0..2] == '$1$'
30
+ end
31
+
32
+ def decrypt
33
+ @encrypted = @value
34
+ @value = password_manager.aes_decrypt(@value[3..-1], phrase)
35
+ end
36
+
37
+ def encrypt
38
+ @encrypted = '$1$' + password_manager.aes_encrypt(@value, phrase)
39
+ end
40
+
41
+ def password_manager
42
+ @password_manager ||= PasswordManager.new
43
+ end
44
+
45
+ def phrase
46
+ Configuration::KAFO[:password]
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,44 @@
1
+ require 'securerandom'
2
+ require 'digest/sha2'
3
+ require 'openssl'
4
+ require 'base64'
5
+
6
+ class PasswordManager
7
+ # generate a random password of lenght n
8
+ #
9
+ # on ruby >= 1.9 we use builtin method urlsafe_base64, on olders we use our own
10
+ # implementation (inspired by urlsafe_base64)
11
+ #
12
+ # the result may contain A-Z, a-z, 0-9, “-” and “_”. “=”
13
+ def password(n = 32)
14
+ return SecureRandom.urlsafe_base64(n) if SecureRandom.respond_to?(:urlsafe_base64)
15
+
16
+ s = [SecureRandom.random_bytes(n)].pack("m*")
17
+ s.delete!("\n")
18
+ s.tr!("+/", "-_")
19
+ s.delete!("=")
20
+ s
21
+ end
22
+
23
+ def aes_encrypt(text, passphrase)
24
+ cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
25
+ cipher.encrypt
26
+ cipher.key = Digest::SHA2.hexdigest(passphrase)
27
+ cipher.iv = Digest::SHA2.hexdigest(passphrase + passphrase)
28
+
29
+ encrypted = cipher.update(text)
30
+ encrypted << cipher.final
31
+ Base64.encode64(encrypted)
32
+ end
33
+
34
+ def aes_decrypt(text, passphrase)
35
+ cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
36
+ cipher.decrypt
37
+ cipher.key = Digest::SHA2.hexdigest(passphrase)
38
+ cipher.iv = Digest::SHA2.hexdigest(passphrase + passphrase)
39
+
40
+ decrypted = cipher.update(Base64.decode64(text))
41
+ decrypted << cipher.final
42
+ decrypted
43
+ end
44
+ end
data/lib/kafo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kafo
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,16 @@
1
+ require File.join(File.dirname(__FILE__), '../../../../../../lib/kafo/password_manager')
2
+ # Decrypts an encrypted password using $kafo_configure::password
3
+ #
4
+ # you can use this function in order to place passwords into your config files
5
+ # in form of a plain text
6
+ module Puppet::Parser::Functions
7
+ newfunction(:decrypt, :type => :rvalue) do |args|
8
+ encrypted = args[0]
9
+ if encrypted =~ /\A\$1\$/
10
+ PasswordManager.new.aes_decrypt(encrypted[3..-1], lookupvar('::kafo_configure::password'))
11
+ else
12
+ raise Puppet::ParseError, 'wrong format of encrypted string, should start with $1$'
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,8 @@
1
+ # Loads a kafo master password from kafo.yaml
2
+ #
3
+ module Puppet::Parser::Functions
4
+ newfunction(:load_kafo_password, :type => :rvalue) do |args|
5
+ YAML.load_file('config/kafo.yaml')[:password]
6
+ end
7
+ end
8
+
@@ -8,10 +8,11 @@ class kafo_configure(
8
8
  $answers = undef
9
9
  ) {
10
10
 
11
- $params = loadanyyaml($answers,
11
+ $password = load_kafo_password()
12
+ $params = loadanyyaml($answers,
12
13
  "/etc/kafo-configure/answers.yaml",
13
14
  "config/answers.yaml")
14
- $keys = hash_keys($params)
15
+ $keys = hash_keys($params)
15
16
 
16
17
  kafo_configure::yaml_to_class { $keys: }
17
18
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kafo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -152,7 +152,9 @@ files:
152
152
  - lib/kafo/params/array.rb
153
153
  - lib/kafo/params/boolean.rb
154
154
  - lib/kafo/params/integer.rb
155
+ - lib/kafo/params/password.rb
155
156
  - lib/kafo/params/string.rb
157
+ - lib/kafo/password_manager.rb
156
158
  - lib/kafo/puppet_module.rb
157
159
  - lib/kafo/puppet_module_parser.rb
158
160
  - lib/kafo/string_helper.rb
@@ -161,9 +163,11 @@ files:
161
163
  - lib/kafo/version.rb
162
164
  - lib/kafo/wizard.rb
163
165
  - modules/kafo_configure/lib/puppet/parser/functions/class_name.rb
166
+ - modules/kafo_configure/lib/puppet/parser/functions/decrypt.rb
164
167
  - modules/kafo_configure/lib/puppet/parser/functions/dump_values.rb
165
168
  - modules/kafo_configure/lib/puppet/parser/functions/hash_keys.rb
166
169
  - modules/kafo_configure/lib/puppet/parser/functions/is_hash.rb
170
+ - modules/kafo_configure/lib/puppet/parser/functions/load_kafo_password.rb
167
171
  - modules/kafo_configure/lib/puppet/parser/functions/loadanyyaml.rb
168
172
  - modules/kafo_configure/manifests/init.pp
169
173
  - modules/kafo_configure/manifests/yaml_to_class.pp