diff --git a/lib/win32/certstore.rb b/lib/win32/certstore.rb index cacc42a..d4f48c6 100644 --- a/lib/win32/certstore.rb +++ b/lib/win32/certstore.rb @@ -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 @@ -118,13 +118,7 @@ def get_thumbprint(search_token) # @param request[thumbprint] 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 diff --git a/lib/win32/certstore/store_base.rb b/lib/win32/certstore/store_base.rb index bf0b52c..0f37db2 100644 --- a/lib/win32/certstore/store_base.rb +++ b/lib/win32/certstore/store_base.rb @@ -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 @@ -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 diff --git a/spec/win32/assets/billg.pfx b/spec/win32/assets/billg.pfx new file mode 100644 index 0000000..3efdf25 Binary files /dev/null and b/spec/win32/assets/billg.pfx differ diff --git a/spec/win32/unit/certstore_spec.rb b/spec/win32/unit/certstore_spec.rb index 51acfcc..1c03229 100644 --- a/spec/win32/unit/certstore_spec.rb +++ b/spec/win32/unit/certstore_spec.rb @@ -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