Class: SecureConfig
- Inherits:
-
Object
- Object
- SecureConfig
- Defined in:
- lib/fox/library/secure_config.rb
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
Instance Method Summary collapse
-
#check_sanity(filename, strict = true) ⇒ Boolean
True, if sane, false if soemthing is wrong.
- #decrypt_yaml(filename = @filename) ⇒ Object
- #detect_type(filename = @filename) ⇒ Function
-
#initialize(filename, mode = :standard) ⇒ SecureConfig
constructor
A new instance of SecureConfig.
Constructor Details
#initialize(filename, mode = :standard) ⇒ SecureConfig
Returns a new instance of SecureConfig.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fox/library/secure_config.rb', line 17 def initialize filename, mode = :standard sane = check_sanity( filename ) type = detect_type( filename ) if mode == :standard if( type == :yaml ) @contents = decrypt_yaml( filename ) else raise NotImplementedError, "Don't know how to handle that file type (#{type.to_s})" end end # of if mode == end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
105 106 107 |
# File 'lib/fox/library/secure_config.rb', line 105 def contents @contents end |
Instance Method Details
#check_sanity(filename, strict = true) ⇒ Boolean
Returns True, if sane, false if soemthing is wrong.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/fox/library/secure_config.rb', line 40 def check_sanity filename, strict = true result = false raise ArgumentError, "File is not a file but a directory" if( File.directory?( filename ) ) raise ArgumentError, "File is a socket" if( File.socket?( filename ) ) raise ArgumentError, "File is not readable" unless( File.readable?( filename ) ) raise ArgumentError, "File does not exist" unless( File.exists?( filename ) ) raise ArgumentError, "File is empty" if( File.zero?( filename ) ) if( strict ) raise ArgumentError, "ENV[ 'PROPERTIES_ENCRYPTION_PASSWORD' ] missing - fatal error" if( ENV[ "PROPERTIES_ENCRYPTION_PASSWORD" ].nil? ) else puts "(WW) Assuming empty PROPERTIES_ENCRYPTION_PASSWORD ! Decryption of secure yaml will fail !" ENV[ "PROPERTIES_ENCRYPTION_PASSWORD" ] = "" end result = true return result end |
#decrypt_yaml(filename = @filename) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/fox/library/secure_config.rb', line 96 def decrypt_yaml filename = @filename password = ENV[ "PROPERTIES_ENCRYPTION_PASSWORD" ] decrypted_yaml = SecureYaml::load( File.open( filename ) ) return decrypted_yaml end |
#detect_type(filename = @filename) ⇒ Function
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fox/library/secure_config.rb', line 71 def detect_type filename = @filename filename = File.basename( filename ) # sometimes files end in .yaml.encrypted filename.gsub!( ".encrypted", "" ) extension = filename.split( "." ).last response = nil raise ArgumentError, "Error in guessing file type" if( extension.empty? ) case extension when "yaml" response = :yaml else raise NotImplementedError, "Couldn't detect file type correctly" end return response end |