Skip to content
This repository has been archived by the owner on Jan 4, 2021. It is now read-only.

Commit

Permalink
(#637) cache private keys
Browse files Browse the repository at this point in the history
Cache private keys so that decoding password protected keys happen
only once in multi batch requests

Signed-off-by: R.I.Pienaar <rip@devco.net>
  • Loading branch information
ripienaar committed Dec 26, 2020
1 parent 2840569 commit cb24369
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 10 additions & 6 deletions lib/mcollective/security/choria.rb
Expand Up @@ -602,14 +602,18 @@ def callerid
def sign(string, id=nil)
key = client_private_key

if has_client_private_key?
Log.debug("Signing request using client private key %s" % key)
else
raise("Cannot find private key %s, cannot sign message" % key)
@_keys ||= {}
if @_keys[key].nil?
if has_client_private_key?
Log.debug("Signing request using client private key %s" % key)
else
raise("Cannot find private key %s, cannot sign message" % key)
end

@_keys[key] ||= OpenSSL::PKey::RSA.new(File.read(key))
end

key = OpenSSL::PKey::RSA.new(File.read(key))
signed = key.sign(OpenSSL::Digest.new("SHA256"), string)
signed = @_keys[key].sign(OpenSSL::Digest.new("SHA256"), string)

Base64.encode64(signed).chomp
end
Expand Down
4 changes: 3 additions & 1 deletion spec/unit/mcollective/security/choria_spec.rb
Expand Up @@ -503,7 +503,9 @@ module MCollective
it "should produce correct client signatures" do
signed = File.read("spec/fixtures/too_many_secrets.sig")
security.initiated_by = :client
choria.expects(:client_private_key).returns(File.expand_path("spec/fixtures/rip.mcollective.key")).twice
choria.expects(:client_private_key).returns(File.expand_path("spec/fixtures/rip.mcollective.key")).times(4)
expect(security.sign("too many secrets")).to eq(signed)
expect(security.sign("too many secrets")).to eq(signed)
expect(security.sign("too many secrets")).to eq(signed)
end
end
Expand Down

0 comments on commit cb24369

Please sign in to comment.