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

Pulls file credentials parsing out of Azure class #324

Merged
merged 6 commits into from
Aug 10, 2018

Conversation

dmccown
Copy link
Contributor

@dmccown dmccown commented Jul 18, 2018

This change pulls file credentials parsing out of Azure.rb. In
extracting the file credentials parsing I refactored the code to
introduce a strategy pattern around the different ways one may access
credentials from the file.

I introduced more unit testing for the extracted classes based on their
public interfaces.

This change also fixes an implementation error where asking for the
credential at index 0, would return the last item in your credentials
file. It will now raise an error that you should give a value greater
than 0. Credit to @Padgett for catching this bug!

Signed-off-by: David McCown [email protected]

Copy link

@TrevorBramble TrevorBramble left a comment

Choose a reason for hiding this comment

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

👏 Looks great! Just had some suggestions and I think one actual typo in a comment.

@@ -38,7 +38,8 @@ def initialize(options)
@cache[:api_call] = {}

if @options[:client_secret].nil? && @options[:client_id].nil?
parse_credentials_file
values = AzureHelpers::FileCredentials.parse(@options)
@options.merge!(values)

Choose a reason for hiding this comment

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

I'd skip the local variable and inline this if there's no conditional usage of it.

subscription_id: subscription_id,
tenant_id: credentials[subscription_id]['tenant_id'],
client_id: credentials[subscription_id]['client_id'],
client_secret: credentials[subscription_id]['client_secret']

Choose a reason for hiding this comment

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

Either these should be fetch operations or there should be a rescue NoMethodError statement for trying to call [] on nil. Unless you're really sure all those values exist before getting here. =^)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

credentials[subscription_id] should be safe. If we use either the 0 index method or the index based lookup the subscription id is read from the credentials file. If find the credentials with the subscription_id then we have a guard to ensure that section exists in the file.

There may be an edge case if the file has no entries. I can test that out.

def validate!
if @credentials.sections.count > 1
raise 'Multiple credentials detected, please set the AZURE_SUBSCRIPTION_ID environment variable.'
end

Choose a reason for hiding this comment

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

Personally I find using guard statements easier to scan.

def validate!
  return if @credentials.section.count < 2

  raise 'Multiple credentials detected, please set the AZURE_SUBSCRIPTION_ID environment variable.'
end

assert_equal('my_subscription_id', result[:subscription_id])
end

it 'raises and error when no subscription id given and multiple entries' do

Choose a reason for hiding this comment

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

"raises and error"

David McCown added 2 commits July 19, 2018 09:41
This change pulls file credentials parsing out of Azure.rb. In
extracting the file credentials parsing I refactored the code to
introduce a strategy pattern around the different ways one may access
credentials from the file.

I introduced more unit testing for the extracted classes based on their
public interfaces.

This change also fixes an implementation error where asking for the
credential at index 0, would return the last item in your credentials
file. It will now raise an error that you should give a value greater
than 0.

Signed-off-by: David McCown <[email protected]>
@dmccown dmccown force-pushed the dmccown/refactor-azure-connector branch from a4d4f47 to 7e2f80a Compare July 19, 2018 14:51
@dmccown dmccown force-pushed the dmccown/refactor-azure-connector branch from be91498 to b2273ea Compare July 19, 2018 15:05
David McCown added 3 commits July 19, 2018 10:50
Copy link
Contributor

@jquick jquick left a comment

Choose a reason for hiding this comment

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

This is really nice! Great job on this @dmccown

Copy link
Contributor

@jquick jquick left a comment

Choose a reason for hiding this comment

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

Thanks @dmccown ! Looks good

@jquick jquick merged commit d3214ef into inspec:master Aug 10, 2018
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