Skip to content

Commit

Permalink
Merge pull request #94 from chef/jfm/win32_cert_update
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmccrae authored May 15, 2022
2 parents c86c16b + df82147 commit ed8ebb1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
10 changes: 2 additions & 8 deletions lib/win32/certstore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get(certificate_thumbprint)
def get!(certificate_thumbprint)
cert_pem = cert_get(certificate_thumbprint)

raise ArgumentError, "Unable to retrieve the certificate" if cert_pem.empty?
raise ArgumentError, "Unable to retrieve the certificate" if cert_pem.empty? || cert_pem == "Certificate Not Found"

cert_pem
end
Expand Down Expand Up @@ -118,13 +118,7 @@ def get_thumbprint(search_token)
# @param request[thumbprint<string>] of certificate
# @return [true, false] only true or false
def valid?(certificate_thumbprint)
cert_validate(certificate_thumbprint).yield_self do |x|
if x.is_a?(TrueClass) || x.is_a?(FalseClass)
x
else
false
end
end
cert_validate(certificate_thumbprint)
end

# To close and destroy pointer of open certificate store handler
Expand Down
10 changes: 7 additions & 3 deletions lib/win32/certstore/store_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ def cert_delete(store_handler, certificate_thumbprint)
def cert_validate(certificate_thumbprint)
validate_thumbprint(certificate_thumbprint)
thumbprint = update_thumbprint(certificate_thumbprint)

cert_pem = get_cert_pem(thumbprint)
return cert_pem if cert_pem == "Certificate Not Found"
cert_pem = format_pem(cert_pem)
verify_certificate(cert_pem)
result = verify_certificate(cert_pem)
result == false ? "Certificate Has Expired" : result
end

# Search certificate from open certificate store and return list
Expand Down Expand Up @@ -184,13 +185,16 @@ def cert_lookup_by_token(search_token, store_name: @store_name, store_location:
end
powershell_cmd = <<~EOH
$result = Get-ChildItem -Path Cert:\\#{converted_store}\\#{store_name} | Where-Object { $_.Subject -match "#{search_token.strip}" } | Select-Object Thumbprint
if ([string]::IsNullOrEmpty($result)){
return "Certificate Not Found"
}
return $result[0].Thumbprint
EOH

powershell_exec!(powershell_cmd, :powershell, timeout: timeout).result

rescue ChefPowerShell::PowerShellExceptions::PowerShellCommandFailed
return "Certificate not found"
raise ArgumentError, "PowerShell threw an error retreiving the certificate. You asked for a cert with this Search Token : #{search_token}, located in this store : #{store_name}, at this location : #{store_location}"
end

# To close and destroy pointer of open certificate store handler
Expand Down
Binary file added spec/win32/assets/billg.pfx
Binary file not shown.
15 changes: 13 additions & 2 deletions spec/win32/unit/certstore_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,20 @@
describe "#cert_lookup_by_token" do
context "when searching for a certificate that does not exist" do
let(:store_name) { "root" }
it "returns a message of Certificate not found" do
it "returns a message of Certificate Not Found" do
store = certstore.open(store_name, store_location: store_location)
expect(store.cert_lookup_by_token("nunya")).to eql("Certificate not found")
expect(store.cert_lookup_by_token("nunya")).to eql("Certificate Not Found")
end
end

context "when searching for a certificate that does exist" do
before(:each) do
allow_any_instance_of(certbase).to receive(:cert_lookup_by_token).and_return("506285bbf4f30446d93e3120e2bffa71b7b9acf2")
end
let(:store_name) { "root" }
it "returns a message of Certificate Not Found" do
store = certstore.open(store_name, store_location: store_location)
expect(store.cert_lookup_by_token("BillG")).to eql("506285bbf4f30446d93e3120e2bffa71b7b9acf2")
end
end
end
Expand Down

0 comments on commit ed8ebb1

Please sign in to comment.