Class: Hiera::Backend::Eyaml::Parser::EncToken

Inherits:
Token
  • Object
show all
Defined in:
lib/hiera/backend/eyaml/parser/encrypted_tokens.rb

Constant Summary collapse

@@tokens_map =
{}
@@encrypt_unchanged =
true

Instance Attribute Summary collapse

Attributes inherited from Token

#match

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Token

#to_s

Constructor Details

#initialize(format, plain_text, encryptor, cipher, match = '', indentation = '', id = nil) ⇒ EncToken

Returns a new instance of EncToken.



47
48
49
50
51
52
53
54
55
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 47

def initialize(format, plain_text, encryptor, cipher, match = '', indentation = '', id = nil)
  @format = format
  @plain_text = Utils.convert_to_utf_8(plain_text)
  @encryptor = encryptor
  @cipher = cipher
  @indentation = indentation
  @id = id
  super(match)
end

Instance Attribute Details

#cipherObject (readonly)

Returns the value of attribute cipher.



14
15
16
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 14

def cipher
  @cipher
end

#encryptorObject (readonly)

Returns the value of attribute encryptor.



14
15
16
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 14

def encryptor
  @encryptor
end

#formatObject (readonly)

Returns the value of attribute format.



14
15
16
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 14

def format
  @format
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 14

def id
  @id
end

#indentationObject (readonly)

Returns the value of attribute indentation.



14
15
16
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 14

def indentation
  @indentation
end

#plain_textObject (readonly)

Returns the value of attribute plain_text.



14
15
16
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 14

def plain_text
  @plain_text
end

Class Method Details

.decrypted_value(format, plain_text, encryption_scheme, match, id, indentation = '') ⇒ Object



22
23
24
25
26
27
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 22

def self.decrypted_value(format, plain_text, encryption_scheme, match, id, indentation = '')
  encryptor = Encryptor.find encryption_scheme
  cipher = encryptor.encode(encryptor.encrypt(plain_text))
  id_number = id.nil? ? nil : id.gsub(/\(|\)/, '').to_i
  EncToken.new(format, plain_text, encryptor, cipher, match, indentation, id_number)
end

.encrypt_unchangedObject



43
44
45
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 43

def self.encrypt_unchanged
  @@encrypt_unchanged
end

.encrypted_value(format, encryption_scheme, cipher, match, indentation = '') ⇒ Object



16
17
18
19
20
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 16

def self.encrypted_value(format, encryption_scheme, cipher, match, indentation = '')
  decryptor = Encryptor.find encryption_scheme
  plain_text = decryptor.decrypt(decryptor.decode(cipher))
  EncToken.new(format, plain_text, decryptor, cipher, match, indentation)
end

.plain_text_value(format, plain_text, encryption_scheme, match, id, indentation = '') ⇒ Object



29
30
31
32
33
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 29

def self.plain_text_value(format, plain_text, encryption_scheme, match, id, indentation = '')
  encryptor = Encryptor.find encryption_scheme
  id_number = id.gsub(/\(|\)/, '').to_i unless id.nil?
  EncToken.new(format, plain_text, encryptor, '', match, indentation, id_number)
end

.set_encrypt_unchanged(encrypt_unchanged) ⇒ Object



39
40
41
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 39

def self.set_encrypt_unchanged(encrypt_unchanged)
  @@encrypt_unchanged = encrypt_unchanged
end

.tokens_mapObject



35
36
37
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 35

def self.tokens_map
  @@tokens_map
end

Instance Method Details

#to_decrypted(args = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 79

def to_decrypted(args = {})
  label = args[:label]
  label_string = label.nil? ? '' : "#{label}: "
  format = args[:format].nil? ? @format : args[:format]
  index = args[:index].nil? ? '' : "(#{args[:index]})"
  EncToken.tokens_map[index] = @plain_text if @@encrypt_unchanged == false

  case format
  when :block
    chevron = args[:use_chevron].nil? || args[:use_chevron] ? ">\n" : ''
    "#{label_string}#{chevron}" + indentation + "DEC#{index}::#{@encryptor.tag}[" + @plain_text + ']!'
  when :string
    "#{label_string}DEC#{index}::#{@encryptor.tag}[" + @plain_text + ']!'
  else
    raise "#{@format} is not a valid format"
  end
end

#to_encrypted(args = {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 57

def to_encrypted(args = {})
  label = args[:label]
  label_string = label.nil? ? '' : "#{label}: "
  format = args[:format].nil? ? @format : args[:format]
  encryption_method = args[:change_encryption]
  unless encryption_method.nil?
    @encryptor = Encryptor.find encryption_method
    @cipher = Base64.strict_encode64(@encryptor.encrypt(@plain_text))
  end
  case format
  when :block
    @cipher = @cipher.gsub(/\s/, '')
    chevron = args[:use_chevron].nil? || args[:use_chevron] ? ">\n" : ''
    "#{label_string}#{chevron}" + @indentation + "ENC[#{@encryptor.tag},#{@cipher}]".scan(/.{1,60}/).join("\n" + @indentation)
  when :string
    ciphertext = @cipher.gsub(/[\n\r]/, '')
    "#{label_string}ENC[#{@encryptor.tag},#{ciphertext}]"
  else
    raise "#{@format} is not a valid format"
  end
end

#to_plain_textObject



97
98
99
# File 'lib/hiera/backend/eyaml/parser/encrypted_tokens.rb', line 97

def to_plain_text
  @plain_text
end