Class: ChefVault::ItemKeys

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vault, name) ⇒ ItemKeys

Returns a new instance of ItemKeys.



19
20
21
22
23
24
25
26
# File 'lib/chef-vault/item_keys.rb', line 19

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



32
33
34
35
36
37
38
39
# File 'lib/chef-vault/item_keys.rb', line 32

def add(chef_client, data_bag_shared_secret, type)
  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



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

def admins
  @raw_data["admins"]
end

#clientsObject



54
55
56
# File 'lib/chef-vault/item_keys.rb', line 54

def clients
  @raw_data["clients"]
end

#delete(chef_client, type) ⇒ Object



41
42
43
44
# File 'lib/chef-vault/item_keys.rb', line 41

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)


28
29
30
# File 'lib/chef-vault/item_keys.rb', line 28

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

#save(item_id = ) ⇒ Object



62
63
64
65
66
67
68
69
70
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 62

def save(item_id=@raw_data['id'])
  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, item_id)

    FileUtils.mkdir(data_bag_path) unless File.exist?(data_bag_path)
    File.open("#{data_bag_item_path}.json", 'w') do |file|
      file.write(JSON.pretty_generate(raw_data))
    end

    raw_data
  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



46
47
48
49
50
51
52
# File 'lib/chef-vault/item_keys.rb', line 46

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