Class: ChefVault::ItemKeys

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins

#find_solo_path, #save_solo

Constructor Details

#initialize(vault, name) ⇒ ItemKeys

Returns a new instance of ItemKeys.



24
25
26
27
28
29
30
31
# File 'lib/chef-vault/item_keys.rb', line 24

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"] = []
end

Class Method Details

.from_data_bag_item(data_bag_item) ⇒ Object



108
109
110
111
112
# File 'lib/chef-vault/item_keys.rb', line 108

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/chef-vault/item_keys.rb', line 114

def self.load(vault, name)
  begin
    data_bag_item = Chef::DataBagItem.load(vault, name)
  rescue Net::HTTPServerException => 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

#add(chef_client, data_bag_shared_secret, type) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef-vault/item_keys.rb', line 37

def add(chef_client, data_bag_shared_secret, type)
  unless @raw_data.key?(type)
    raise ChefVault::Exceptions::V1Format,
          "cannot manage a v1 vault.  See UPGRADE.md for help"
  end
  public_key = OpenSSL::PKey::RSA.new chef_client.public_key
  self[chef_client.name] =
    Base64.encode64(public_key.public_encrypt(data_bag_shared_secret))

  @raw_data[type] << chef_client.name unless @raw_data[type].include?(chef_client.name)
  @raw_data[type]
end

#adminsObject



67
68
69
# File 'lib/chef-vault/item_keys.rb', line 67

def admins
  @raw_data["admins"]
end

#clientsObject



63
64
65
# File 'lib/chef-vault/item_keys.rb', line 63

def clients
  @raw_data["clients"]
end

#delete(chef_client, type) ⇒ Object



50
51
52
53
# File 'lib/chef-vault/item_keys.rb', line 50

def delete(chef_client, type)
  raw_data.delete(chef_client)
  raw_data[type].delete(chef_client)
end

#destroyObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chef-vault/item_keys.rb', line 89

def destroy
  if Chef::Config[:solo]
    data_bag_path = File.join(Chef::Config[:data_bag_path],
                              data_bag)
    data_bag_item_path = File.join(data_bag_path, @raw_data["id"])

    FileUtils.rm("#{data_bag_item_path}.json")

    nil
  else
    super(data_bag, id)
  end
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/chef-vault/item_keys.rb', line 33

def include?(key)
  @raw_data.keys.include?(key)
end

#save(item_id = ) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef-vault/item_keys.rb', line 71

def save(item_id = @raw_data["id"])
  if Chef::Config[:solo]
    save_solo(item_id)
  else
    begin
      Chef::DataBag.load(data_bag)
    rescue Net::HTTPServerException => 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

    super
  end
end

#search_query(search_query = nil) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/chef-vault/item_keys.rb', line 55

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

#to_json(*a) ⇒ Object



103
104
105
106
# File 'lib/chef-vault/item_keys.rb', line 103

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