Class: ChefVault::ItemKeys

Inherits:
Chef::DataBagItem
  • Object
show all
Includes:
Mixins
Defined in:
lib/chef-vault/item_keys.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins

#delete_solo, #find_solo_path, #load_solo, #save_solo

Constructor Details

#initialize(vault, name) ⇒ ItemKeys

Returns a new instance of ItemKeys.



28
29
30
31
32
33
34
35
36
37
# File 'lib/chef-vault/item_keys.rb', line 28

def initialize(vault, name)
  super() # parentheses required to strip off parameters
  @data_bag = vault
  @raw_data["id"] = name
  @raw_data["admins"] = []
  @raw_data["clients"] = []
  @raw_data["search_query"] = []
  @raw_data["mode"] = "default"
  @cache = {} # write-back cache for keys
end

Instance Attribute Details

#skip_reencryptionTrueClass, FalseClass

Returns whether symetrical key is reencrypted all the time or re-used from previous computations.

Returns:

  • (TrueClass, FalseClass)

    whether symetrical key is reencrypted all the time or re-used from previous computations



26
27
28
# File 'lib/chef-vault/item_keys.rb', line 26

def skip_reencryption
  @skip_reencryption
end

Class Method Details

.encode_key(key_string, data_bag_shared_secret) ⇒ Object



274
275
276
277
# File 'lib/chef-vault/item_keys.rb', line 274

def self.encode_key(key_string, data_bag_shared_secret)
  public_key = OpenSSL::PKey::RSA.new(key_string)
  Base64.encode64(public_key.public_encrypt(data_bag_shared_secret))
end

.from_data_bag_item(data_bag_item) ⇒ Object



228
229
230
231
232
# File 'lib/chef-vault/item_keys.rb', line 228

def self.from_data_bag_item(data_bag_item)
  item = new(data_bag_item.data_bag, data_bag_item.name)
  item.raw_data = data_bag_item.raw_data
  item
end

.load(vault, name) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/chef-vault/item_keys.rb', line 234

def self.load(vault, name)
  begin
    data_bag_item = Chef::DataBagItem.load(vault, name)
  rescue Net::HTTPClientException => http_error
    if http_error.response.code == "404"
      raise ChefVault::Exceptions::KeysNotFound,
        "#{vault}/#{name} could not be found"
    else
      raise http_error
    end
  rescue Chef::Exceptions::ValidationFailed
    raise ChefVault::Exceptions::KeysNotFound,
      "#{vault}/#{name} could not be found"
  end

  from_data_bag_item(data_bag_item)
end

Instance Method Details

#[](key) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chef-vault/item_keys.rb', line 39

def [](key)
  # return options immediately
  return @raw_data[key] if %w{id admins clients search_query mode}.include?(key)

  # check if the key is in the write-back cache
  ckey = @cache[key]
  return ckey unless ckey.nil?

  # check if the key is saved in sparse mode
  skey = sparse_key(sparse_id(key)) if sparse?
  if skey
    skey[key]
  else
    # fallback to raw data
    @raw_data[key]
  end
end

#add(chef_key, data_bag_shared_secret) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef-vault/item_keys.rb', line 68

def add(chef_key, data_bag_shared_secret)
  type = chef_key.type
  unless @raw_data.key?(type)
    raise ChefVault::Exceptions::V1Format,
      "cannot manage a v1 vault.  See UPGRADE.md for help"
  end
  @cache[chef_key.name] = skip_reencryption ? self[chef_key.name] : nil
  begin
    @cache[chef_key.name] ||= ChefVault::ItemKeys.encode_key(chef_key.key, data_bag_shared_secret)
  rescue OpenSSL::PKey::RSAError
    raise OpenSSL::PKey::RSAError, "While adding #{chef_key.type} an invalid or old (pre chef-server 12) format public key was found for #{chef_key.name}"
  end
  @raw_data[type] << chef_key.name unless @raw_data[type].include?(chef_key.name)
  @raw_data[type]
end

#adminsObject



116
117
118
# File 'lib/chef-vault/item_keys.rb', line 116

def admins
  @raw_data["admins"]
end

#clear_encryptedObject



84
85
86
87
88
# File 'lib/chef-vault/item_keys.rb', line 84

def clear_encrypted
  @cache.clear
  self["clients"].each { |client| @raw_data.delete(client) }
  self["admins"].each { |admin| @raw_data.delete(admin) }
end

#clientsObject



112
113
114
# File 'lib/chef-vault/item_keys.rb', line 112

def clients
  @raw_data["clients"]
end

#delete(chef_key) ⇒ Object



90
91
92
93
94
# File 'lib/chef-vault/item_keys.rb', line 90

def delete(chef_key)
  @cache[chef_key.name] = false
  raw_data[chef_key.type].delete(chef_key.name)
  raw_data.delete(chef_key.name)
end

#destroyObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/chef-vault/item_keys.rb', line 200

def destroy
  if Chef::Config[:solo_legacy_mode]
    data_bag_path = File.join(Chef::Config[:data_bag_path], data_bag)
    data_bag_item_path = File.join(data_bag_path, @raw_data["id"])
    data_bag_sparse_keys_path = File.join(data_bag_path, sparse_id("*"))
    # destroy all sparse keys
    FileUtils.rm(Dir.glob("#{data_bag_sparse_keys_path}.json"))
    # destroy this metadata
    FileUtils.rm("#{data_bag_item_path}.json")
    nil
  else
    # destroy all sparse keys
    rgx = Regexp.new("^#{sparse_id(".*")}")
    items = Chef::DataBag.load(data_bag).keys.select { |item| item =~ rgx }
    items.each do |id|
      Chef::DataBagItem.from_hash("data_bag" => data_bag, "id" => id)
        .destroy(data_bag, id)
    end
    # destroy this metadata
    super(data_bag, id)
  end
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
# File 'lib/chef-vault/item_keys.rb', line 57

def include?(key)
  # check if the key is in the write-back cache
  ckey = @cache[key]
  return (ckey ? true : false) unless ckey.nil?
  # check if the key is saved in sparse mode
  return true if sparse? && sparse_key(sparse_id(key))

  # fallback to non-sparse mode if sparse key is not found
  @raw_data.keys.include?(key)
end

#mode(mode = nil) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/chef-vault/item_keys.rb', line 96

def mode(mode = nil)
  if mode
    @raw_data["mode"] = mode
  else
    @raw_data["mode"]
  end
end

#save(item_id = ) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/chef-vault/item_keys.rb', line 120

def save(item_id = @raw_data["id"])
  # create data bag if not running in solo mode
  unless Chef::Config[:solo_legacy_mode]
    begin
      Chef::DataBag.load(data_bag)
    rescue Net::HTTPClientException => http_error
      if http_error.response.code == "404"
        chef_data_bag = Chef::DataBag.new
        chef_data_bag.name data_bag
        chef_data_bag.create
      end
    end
  end

  # write cached keys to data
  @cache.each do |key, val|
    # delete across all modes on key deletion
    if val == false
      # sparse mode key deletion
      if Chef::Config[:solo_legacy_mode]
        delete_solo(sparse_id(key))
      else
        begin
          Chef::DataBagItem.from_hash("data_bag" => data_bag,
                                      "id" => sparse_id(key))
            .destroy(data_bag, sparse_id(key))
        rescue Net::HTTPClientException => http_error
          raise http_error unless http_error.response.code == "404"
        end
      end
      # default mode key deletion
      @raw_data.delete(key)
    else
      if @raw_data["mode"] == "sparse"
        # sparse mode key creation
        skey = Chef::DataBagItem.from_hash(
          "data_bag" => data_bag,
          "id" => sparse_id(key),
          key => val
        )
        if Chef::Config[:solo_legacy_mode]
          save_solo(skey.id, skey.raw_data)
        else
          skey.save
        end
      else
        # default mode key creation
        @raw_data[key] = val
      end
    end
  end

  if @raw_data["mode"] == "sparse"
    @raw_data.each do |key, val|
      next if %w{ id clients admins search_query mode }.include?(key)

      skey = Chef::DataBagItem.from_hash(
        "data_bag" => data_bag,
        "id" => sparse_id(key),
        key => val
      )
      @raw_data.delete(key)
      if Chef::Config[:solo_legacy_mode]
        save_solo(skey.id, skey.raw_data)
      else
        skey.save
      end
    end
  end

  # save raw data
  if Chef::Config[:solo_legacy_mode]
    save_solo(item_id)
  else
    super
  end
  # clear write-back cache
  @cache = {}
end

#search_query(search_query = nil) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/chef-vault/item_keys.rb', line 104

def search_query(search_query = nil)
  if search_query
    @raw_data["search_query"] = search_query
  else
    @raw_data["search_query"]
  end
end

#sparse?Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/chef-vault/item_keys.rb', line 254

def sparse?
  @raw_data["mode"] == "sparse"
end

#sparse_id(key, item_id = ) ⇒ Object



258
259
260
# File 'lib/chef-vault/item_keys.rb', line 258

def sparse_id(key, item_id = @raw_data["id"])
  "#{item_id.chomp("_keys")}_key_#{key}"
end

#sparse_key(sid) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/chef-vault/item_keys.rb', line 262

def sparse_key(sid)
  if Chef::Config[:solo_legacy_mode]
    load_solo(sid)
  else
    begin
      Chef::DataBagItem.load(@data_bag, sid)
    rescue Net::HTTPClientException => http_error
      nil if http_error.response.code == "404"
    end
  end
end

#to_json(*a) ⇒ Object



223
224
225
226
# File 'lib/chef-vault/item_keys.rb', line 223

def to_json(*a)
  json = super
  json.gsub(self.class.name, self.class.superclass.name)
end