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

don't raise error in get if certificate is empty #84

Conversation

rishichawda
Copy link
Member

@rishichawda rishichawda commented Sep 7, 2021

Signed-off-by: rishichawda [email protected]

Description

Currently cert_get call is checking if the value returned by get_cert_pem for powershell_exec! output is empty, and raises an error. However, we do have a cert_validate that achieves the same thing by calling verify_certificate which will return "Certificate not found" as string. It doesn't throw any errors, and only returns the string with the error message. This should also be eliminated since the valid? call is expected to return either true or false.

Complete details on the idea behind these changes are in #83

Issues Resolved

#83
chef/chef#11994

[List any existing issues this PR resolves, or any Discourse or
StackOverflow discussions that are relevant]

Check List

@rishichawda rishichawda force-pushed the shouldnot-raise-error-for-empty-certificate-in-get-call branch from ed5ec4b to 6a721c9 Compare September 7, 2021 12:43
@rishichawda rishichawda changed the title [wip] don't raise error in get if certificate is empty don't raise error in get if certificate is empty Sep 7, 2021
@rishichawda rishichawda force-pushed the shouldnot-raise-error-for-empty-certificate-in-get-call branch 3 times, most recently from 0008172 to 80cbd7b Compare September 7, 2021 13:37
@rishichawda rishichawda force-pushed the shouldnot-raise-error-for-empty-certificate-in-get-call branch from 80cbd7b to d94d5b6 Compare September 7, 2021 13:39
Signed-off-by: rishichawda <[email protected]>
@rishichawda rishichawda force-pushed the shouldnot-raise-error-for-empty-certificate-in-get-call branch from d94d5b6 to c52c0fd Compare September 7, 2021 13:43
@rishichawda rishichawda force-pushed the shouldnot-raise-error-for-empty-certificate-in-get-call branch 2 times, most recently from f5cb4e1 to 48363eb Compare September 16, 2021 17:20
@rishichawda rishichawda force-pushed the shouldnot-raise-error-for-empty-certificate-in-get-call branch from 48363eb to 0049fca Compare September 16, 2021 17:30
@johnmccrae
Copy link
Contributor

Rishi, I think I found a couple of the problems. I am also working out some issues with the unit tests failing.

@rishichawda
Copy link
Member Author

@johnmccrae Please let me know if there's something I can help with.

Comment on lines 116 to 124
def valid?(certificate_thumbprint, store_location: "", store_name: "")
cert_validate(certificate_thumbprint, store_location: store_location, store_name: store_name)
cert_validate(certificate_thumbprint, store_location: store_location, store_name: store_name).yield_self do |x|
if x.is_a?(TrueClass) || x.is_a?(FalseClass)
x
else
false
end
end
end

Choose a reason for hiding this comment

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

I think the implementations of valid? and cert_validate should have been other way round or somewhat swapped, i.e. valid? should contain the validation logic that cert_validate has, returning a bool, while cert_validate should use valid? to check and raise error or formatted error string. Something like below -

def valid?
  # validation logic
  # return boolean
end

def cert_validate
  # use valid? to check
  # raise error or return formatted error string (based on use case)
end

def get!(certificate_thumbprint, store_name: @store_name, store_location: @store_location)
cert_pem = cert_get(certificate_thumbprint, store_name: store_name, store_location: store_location)

raise ArgumentError, "Unable to retrieve the certificate" if cert_pem.empty?

Choose a reason for hiding this comment

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

ArgumentError would be fitting if we were validating arguments here but since we are calling an external entity/store, we should either relay/wrap that entity's error or define a custom exception.

unless cert_pem.empty?
build_openssl_obj(cert_pem)
end
cert_pem.empty? ? cert_pem : build_openssl_obj(cert_pem)

Choose a reason for hiding this comment

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

I think this should return nil or cert_pem/object. This would also clean .empty? checks at other places.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we get on Teams and talk about this? I feel like we have competing agendas and I want to make sure I am doing this correctly.

@johnmccrae johnmccrae merged commit 9d563c5 into chef:main Sep 28, 2021
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.

3 participants