-
Notifications
You must be signed in to change notification settings - Fork 681
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
Require a key attribute for the key_rsa resource #2891
Require a key attribute for the key_rsa resource #2891
Conversation
Defining an attribute without a default value generates a stacktrace Signed-off-by: Omar J Irizarry <[email protected]>
Signed-off-by: Omar J. Irizarry <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @omar-irizarry ! This is a really nice addition. Do you mind adding a unit test for this case?
lib/resources/key_rsa.rb
Outdated
if @passphrase.is_a? Inspec::Attribute::DEFAULT_ATTRIBUTE | ||
return fail_resource 'Please provide default value for attribute' | ||
end | ||
begin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also consider moving the logic out of the initialize method, so that we make sure it is a control error and its not happening during initialization.
Moved logic out of the initilize method. Signed-off-by: Omar Irizarry <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! Have some small changes I'd like to see before merging. =^)
lib/utils/pkey_reader.rb
Outdated
raise Inspec::Exceptions::ResourceFailed, 'passphrase Error' | ||
end | ||
key | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could restructure this to clarify the flow like this:
def read_pkey(filecontent, passphrase)
default_attribute?(passphrase)
OpenSSL::PKey.read(filecontent, passphrase)
rescue OpenSSL::PKey::PKeyError
raise Inspec::Exceptions::ResourceFailed, 'passphrase error'
end
lib/utils/pkey_reader.rb
Outdated
if passphrase.is_a? Inspec::Attribute::DEFAULT_ATTRIBUTE | ||
raise Inspec::Exceptions::ResourceFailed, 'Please provide default value for attribute' | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Predicate methods (foo?
) are expected to return true
/false
. I think it's right to pull out the test and exception raising into another method that can be used as a guard statement in read_pkey()
. So maybe this method could be renamed ensure_not_default
or raise_if_default
or similar instead?
refactoring for better clarity. Signed-off-by: Omar J Irizarry <[email protected]>
fixing trailing white spaces Signed-off-by: Omar J Irizarry <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Checking that passphrase is not of type Attribute::DEFAULT_ATTRIBUTE before calling OpenSSL::PKey.read.
Also added a catch to exception OpenSSL::PKey::PKeyError to prevent a stack trace error if the passphrase was incorrect.