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

support new automate compliance backend #1819

Merged
merged 1 commit into from
May 17, 2017

Conversation

chris-rock
Copy link
Contributor

No description provided.

@chris-rock chris-rock requested a review from alexpop May 16, 2017 22:45
@chris-rock chris-rock force-pushed the chris-rock/compliance-plugin branch from 42bd079 to 035281c Compare May 16, 2017 22:45
Copy link
Contributor

@adamleff adamleff left a comment

Choose a reason for hiding this comment

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

I don't think any of my comments are blocking, but my opinion is that they'd make the code cleaner and easier to test if implemented.

mapped_profiles = profiles.values.flatten
else
owner_id = config['user']
mapped_profiles = profiles.map { |e|
Copy link
Contributor

Choose a reason for hiding this comment

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

Stylistically, we should ensure multiline blocks be of the do...end variety to remain consistent.

if config['server_type'] == 'automate'
mapped_profiles = profiles.values.to_a.flatten
else
if config['server_type'] == 'compliance'
Copy link
Contributor

Choose a reason for hiding this comment

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

We're doing this if... elsif...else expressions a few times throughout this change. Can I propose some helper methods?

def is_compliance_server?(config)
  config['server_type'] == 'compliance'
end

def is_automate_server_pre_080?(config)
  config['server_type'] == 'automate' && config['version'].empty?
end

def is_automate_server_080_and_later?(config)
  config['server_type'] == 'automate' && !config['version'].empty?
end

def is_automate_server?(config)
  config['server_type'] == 'automate'
end

That way we're only defining the logic in one place and it's easier to add unit tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can definitely do that. Even after my fix, this module needs massive refactoring. It started with 1 server version, and we have essentially 2 versions Chef Compliance and 2 versions Chef Automate now. That needs to be abstracted.

def self.version(config)
url = config['server']
insecure = config['insecure']

if url.nil?
puts "
Server configuration information is missing.
Please login using `inspec compliance login https://compliance.test --user admin --insecure --token 'PASTE TOKEN HERE' `
"
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this line isn't changed in your PR, but shouldn't we be exiting here if we don't have the config? We seem to continue on, and it looks like data will never be an assigned variable and we'll likely throw an exception.

if url.nil?
  puts 'warning message'
  exit 1
end
# reach out to compliance API as needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

API implementation may throw an error but will never exit, this can only be done by CLI

# upload the tar to Chef Compliance
config['server_type'] == 'automate' ? url = "#{config['server']}/#{config['user']}" : url = "#{config['server']}/owners/#{owner}/compliance/#{profile_name}/tar"
# Chef Compliance
if config['server_type'] == 'compliance'
Copy link
Contributor

Choose a reason for hiding this comment

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

See earlier comment about using helper methods here instead of repeating the config[] logic.

@@ -151,12 +178,23 @@ def self.get_token(config)

def self.target_url(config, profile)
if config['server_type'] == 'automate'
target = "#{config['server']}/#{profile}/tar"
owner, id = profile.split('/')
Copy link
Contributor

Choose a reason for hiding this comment

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

The splitting of the profile into owner and ID is now redundantly defined. We could eliminate the redundancy:

owner, id = profile.split('/')
if is_automate_server?(config)
   # etc...

else
owner, id = profile.split('/')
target = "#{config['server']}/owners/#{owner}/compliance/#{id}/tar"
end
target
end

# returns a parsed url for `admin/profile` or `compliance://admin/profile`
def self.sanitize_profile_name(profile)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sure I'm missing something with this method... The end state is to return 'admin/profile' regardless of whether it was supplied with a URI scheme or not, correct? It seems like we don't need to prepend a non-schemed URI with a scheme just to remove the scheme from the string. In fact, that's kinda what was being done in an earlier version of this method is removed later in this PR.

Could this method be simplified to:

uri = URI(profile)
"#{uri.host}#{uri.path}"
irb(main):021:0* a = URI('compliance://admin/profile')
=> #<URI::Generic:0x007fc51b0b1980 URL:compliance://admin/profile>
irb(main):022:0> b = URI('admin/profile')
=> #<URI::Generic:0x007fc51b0a8420 URL:admin/profile>
irb(main):023:0> "#{a.host}#{a.path}"
=> "admin/profile"
irb(main):024:0> "#{b.host}#{b.path}"
=> "admin/profile"

Like I said, I'm sure I'm missing something here so feel free to educate me!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just learned yesterday that Automate is using fancy usernames like adam@leff.com, which breaks the current parsing since adam@ is not part of host. I agree that we need way more testing around this

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I understand. I'd love for us to use URI smarter than just deleting the scheme from the string, but we can tackle that another time.

if config['server_type'] == 'automate'
puts 'Version not available when logged in with Automate.'
info = Compliance::API.version(config)
if !info.nil? && info['version']
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be nice to have Compliance::API.version return an empty hash instead of nil to avoid having to do this extra check.

Copy link
Contributor Author

@chris-rock chris-rock May 17, 2017

Choose a reason for hiding this comment

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

I like that

@chris-rock chris-rock force-pushed the chris-rock/compliance-plugin branch from 569a3eb to 88349d9 Compare May 17, 2017 08:53
# Chef Automate
elsif is_automate_server?(config)
url = "#{config['server']}/profiles/#{config['user']}"
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's catch the third case i.e. else raise ... becuse there is no url instead of getting the nil error further down

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

Copy link
Contributor

@arlimus arlimus left a comment

Choose a reason for hiding this comment

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

Thank you Chris for adding all of this!
Ditto Adam for the review, I agree this needs more work.
My comment isn't critical for the merge, but needs to land at some point (afaics).

@chris-rock
Copy link
Contributor Author

@arlimus will add that quickly...

elsif is_automate_server?(config)
url = "#{config['server']}/profiles/#{config['user']}"
else
raise ServerConfigurationMissing
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd just add a string message to this to make it complete and give the troubleshooter an additional clue.

raise ServerConfigurationMissing, 'No `server_type` entry in the config, unable to determine server type.'

url = config['server']
insecure = config['insecure']

raise ServerConfigurationMissing if url.nil?
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here...

raise ServerConfigurationMissing, 'No `server` entry in config, unable to determine server URL.' if url.nil?

Copy link
Contributor

@adamleff adamleff left a comment

Choose a reason for hiding this comment

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

I'd obviously love to see some unit tests for this bundle but understand that time is not on our side. At least factoring out some of the logic into helper methods will make it easier to do so in the future.

Thanks for fixing this all up, @chris-rock.

@adamleff
Copy link
Contributor

@chris-rock don't forget to fix up the DCO issue with your third commit before we merge.

@chris-rock
Copy link
Contributor Author

Once @alexpop approves, I squash the commits

Signed-off-by: Christoph Hartmann <chris@lollyrock.com>
@chris-rock chris-rock force-pushed the chris-rock/compliance-plugin branch from 20a65b3 to 5cc288d Compare May 17, 2017 13:02
Copy link
Contributor

@alexpop alexpop left a comment

Choose a reason for hiding this comment

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

Works great, thanks Chris!

@chris-rock chris-rock merged commit 3140eea into master May 17, 2017
@chris-rock chris-rock deleted the chris-rock/compliance-plugin branch May 17, 2017 13:15
@adamleff adamleff added the Type: Enhancement Improves an existing feature label May 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Improves an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants