-
Notifications
You must be signed in to change notification settings - Fork 160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add feature to save each key in different data bag item #246
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,9 +28,33 @@ def initialize(vault, name) | |
@raw_data["admins"] = [] | ||
@raw_data["clients"] = [] | ||
@raw_data["search_query"] = [] | ||
@raw_data["mode"] = "default" | ||
@cache = {} # write-back cache for keys | ||
end | ||
|
||
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 skey | ||
skey[key] | ||
else | ||
# fallback to raw data | ||
@raw_data[key] | ||
end | ||
end | ||
|
||
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 unless sparse_key(sparse_id(key)).nil? | ||
# fallback to non-sparse mode if sparse key is not found | ||
@raw_data.keys.include?(key) | ||
end | ||
|
||
|
@@ -40,16 +64,24 @@ def add(chef_key, data_bag_shared_secret) | |
raise ChefVault::Exceptions::V1Format, | ||
"cannot manage a v1 vault. See UPGRADE.md for help" | ||
end | ||
self[chef_key.name] = ChefVault::ItemKeys.encode_key(chef_key.key, data_bag_shared_secret) | ||
@cache[chef_key.name] = ChefVault::ItemKeys.encode_key(chef_key.key, data_bag_shared_secret) | ||
@raw_data[type] << chef_key.name unless @raw_data[type].include?(chef_key.name) | ||
@raw_data[type] | ||
end | ||
|
||
def delete(chef_key) | ||
raw_data.delete(chef_key.name) | ||
@cache[chef_key.name] = false | ||
raw_data[chef_key.type].delete(chef_key.name) | ||
end | ||
|
||
def mode(mode = nil) | ||
if mode | ||
@raw_data["mode"] = mode | ||
else | ||
@raw_data["mode"] | ||
end | ||
end | ||
|
||
def search_query(search_query = nil) | ||
if search_query | ||
@raw_data["search_query"] = search_query | ||
|
@@ -67,9 +99,8 @@ def admins | |
end | ||
|
||
def save(item_id = @raw_data["id"]) | ||
if Chef::Config[:solo_legacy_mode] | ||
save_solo(item_id) | ||
else | ||
# create data bag if not running in solo mode | ||
unless Chef::Config[:solo_legacy_mode] | ||
begin | ||
Chef::DataBag.load(data_bag) | ||
rescue Net::HTTPServerException => http_error | ||
|
@@ -79,9 +110,53 @@ def save(item_id = @raw_data["id"]) | |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nil and false do not have the same meaning in the write-back cache |
||
# 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::HTTPServerException => 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 | ||
# save raw data | ||
if Chef::Config[:solo_legacy_mode] | ||
save_solo(item_id) | ||
else | ||
super | ||
end | ||
# clear write-back cache | ||
@cache = {} | ||
end | ||
|
||
def destroy | ||
|
@@ -129,6 +204,22 @@ def self.load(vault, name) | |
|
||
# @private | ||
|
||
def sparse_id(key, item_id = @raw_data["id"]) | ||
"#{item_id}_key_#{key}" | ||
end | ||
|
||
def sparse_key(sid) | ||
if Chef::Config[:solo_legacy_mode] | ||
load_solo(sid) | ||
else | ||
begin | ||
Chef::DataBagItem.load(@data_bag, sid) | ||
rescue Net::HTTPServerException => http_error | ||
nil if http_error.response.code == "404" | ||
end | ||
end | ||
end | ||
|
||
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)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OPT:
(if you really want to return true value)
or if you just want to return true-ish value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to write
!!ckey unless ckey.nil?
?Rubocop doesn't like that :(