Skip to content
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

[MSYS-545] Add code to delete certificate from cert_store. #10

Merged

Conversation

NAshwini
Copy link
Member

@NAshwini NAshwini commented Nov 21, 2017

Added code to delete single certificate from cert_store.
We can delete the certificate & check as:

store = Win32::Certstore.open("root")
cert = store.delete('Frank4DD Web CA')
puts "output : #{cert}"

@NAshwini NAshwini requested a review from NimishaS November 21, 2017 08:05
@NAshwini NAshwini force-pushed the ash/delete_single_file_certificate branch from bde9dd4 to c10384e Compare November 21, 2017 09:19
def delete(certificate_name)
delete_cert = cert_delete(@certstore_handler, certificate_name)
close
return delete_cert
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize this is the existing pattern, but we don't need an explicit return here when this is the last line of the method, i.e. this could just be delete_cert.

Similarly, I don't know that we want to close as part of every action. We could update this pattern so this entire method would just be cert_delete(@certstore_handler, certificate_name).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @btm, @NimishaS and @mwrock - Please correct me if I'm wrong, as per our discussion in the previous PR: #4 (comment), we discided to make certstore_handler secure and not accessible outside the class. That's why we created close method as private, having only read permission. For each operation of windows certificate store, we have to create a certificate_handler pointer which needs to be destroyed at the end of request. close method destroys the pointer.
So if we want to list certificates and add certificate, we have to perform the operations separately in two different blocks and with 2 different certstore_handlers. We can't perform two operations on certificate store with one certstore_handler.

@@ -54,6 +54,12 @@ def add(cert_file_path)
return add
end

def delete(certificate_name)
delete_cert = cert_delete(@certstore_handler, certificate_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we have an attr_reader we can just call certstore_handler instead of @certstore_handler. This allows a future refactor where we could replace the reader with a method if we wanted to do something else.

while (pCertContext = CertEnumCertificatesInStore(store_handler, pCertContext) and not pCertContext.null? ) do
if (CertGetNameStringW(pCertContext, CERT_NAME_FRIENDLY_DISPLAY_TYPE, CERT_NAME_ISSUER_FLAG, nil,cert_name, 1024) &&
cert_name.read_wstring.downcase == certificate_name.downcase)
if CertDeleteCertificateFromStore(CertDuplicateCertificateContext(pCertContext))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using CertEnumCertificatesInStore which lists all the certificates and looping over it, we should use some function to find the particular certificate. e.g. CertFindCertificateInStore function gives the pointer to the cert_context structure, which can be passed to CertDeleteCertificateFromStore.
This is a slightly big change.

it "return message of successful deletion" do
store = certstore.open(store_name)
delete_cert = store.delete(certificate_name)
expect(delete_cert).to eq("Deleted certificate GeoTrust Global CA successfully")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CertEnumCertificatesInStore isn't stubbed so this spec may fail on a system which doesn't have a GeoTrust Global CA certificate ca store. Since appveyor isn't configured, this isn't very clear right now.
Anyway, if we decide to stop using CertEnumCertificatesInStore as per my previous comment, then the specs will change.

allow(certbase).to receive(:CertDeleteCertificateFromStore).and_return(false)
allow(FFI::LastError).to receive(:error).and_return(-2147024891)
store = certstore.open(store_name)
expect { store.delete(certificate_name) }.to raise_error(Chef::Exceptions::Win32APIError)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If GlobalSign certificate isn't present on a system, this may fail.

@NAshwini NAshwini force-pushed the ash/delete_single_file_certificate branch from 46d615d to c42d86d Compare November 28, 2017 07:27
@NimishaS NimishaS merged commit bb9165b into chef:master Nov 28, 2017
@NimishaS NimishaS deleted the ash/delete_single_file_certificate branch November 28, 2017 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants