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

Fix postgres_conf ability to test parameters that have a dot in them #1938

Merged

Conversation

aaronlippold
Copy link
Collaborator

Fixes #1671

Signed-off-by: Aaron Lippold lippold@gmail.com

@aaronlippold aaronlippold force-pushed the al/postgres_conf_complex_params branch 2 times, most recently from 95fbac7 to 0833573 Compare June 18, 2017 21:01
@aaronlippold
Copy link
Collaborator Author

@arlimus or @chris-rock or @adamleff please review

Copy link
Contributor

@chris-rock chris-rock left a comment

Choose a reason for hiding this comment

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

@aaronlippold Thank for this addition. Let's add some unit tests, so that we can ensure this stays in our scope in future

@aaronlippold
Copy link
Collaborator Author

@chris-rock we don't need no stinking unit tests ... lol. Ok working on them now :)

@chris-rock
Copy link
Contributor

In this case i think it is very useful especially since the postgres config includes those values!

@aaronlippold
Copy link
Collaborator Author

@chris-rock yes, I was trying to make a joke, badly it seems :)

@chris-rock
Copy link
Contributor

haha, all good

@aaronlippold aaronlippold force-pushed the al/postgres_conf_complex_params branch from bd3d63f to 2c128d7 Compare June 19, 2017 13:49
@adamleff
Copy link
Contributor

Do we really need to bring in ObjectTraverser for this change? That mixin is great for config files which are multiple-levels deep, and postgres.conf is single level (which just happens to have keys that can have dots in them, which confuse things a bit). No multidimensional hashes here.

We should be able to address this need by changing the method_missing method to:

    def method_missing(*name)
      if name.is_a?(Array) && name.first == :[]
        # user passed in parameter using: its(['foo.bar'])
        # index 0 will be :[], and index 1 will be the actual key they're looking for
        name = name[1]
      end

      param = params[name.to_s]
      return nil if param.nil?
      # extract first value if we have only one value in array
      return param[0] if param.length == 1
      param
    end

method_missing just needs to take a splat instead of a single parameter, and then we need to pluck the second one off the array.

@aaronlippold
Copy link
Collaborator Author

@adamleff if there is a simpler solution totally open to it. I just used that approach because in the original issue you had suggested it.

#1671

The correct syntax here is similar to the Elasticsearch YAML inquiry you and I spoke about today:

its(['pgaudit.log_level']) { should cmp 'log' }
However, this resource isn't currently written to support that. It needs to be extended to use ObjectTraverser like the JsonConfig class and have the method_missing method updated accordingly given that there can be dots in the keys.

Flagging as a bug.

I will update the code as you requested above and push again.

@adamleff
Copy link
Contributor

Yup, and I'm not perfect and sometimes have better ideas as time goes on :) I'm fine with ObjectTraverser here, but after that discussion, I went through and checked more Postgres documentation and examples and there is no hierarchy in the config... it's flat. So we don't need ObjectTraverser here. That's the only point I'm trying to make, both in an effort to educate, but also to keep resources as simple as necessary to do the right functionality.

I'll defer to @chris-rock or @arlimus if they'd like to use ObjectTraverser here or take a simpler approach.

@aaronlippold
Copy link
Collaborator Author

@adamleff Ok. Still having a few issues with that method. I am getting a nil return on the complex params.

Can I shoot you a copy of the updates in slack and perhaps I just made a small goof but I agree, if we don't need to pull in an entire separate lib for a simple extraction then I am all for it.

@aaronlippold aaronlippold force-pushed the al/postgres_conf_complex_params branch from 9369737 to e04de83 Compare June 20, 2017 22:35
@aaronlippold
Copy link
Collaborator Author

@chris-rock requested changes added.

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.

Looks fine except for the test file. You've got my approval if we eliminate the redundant describe in the unit test.

resource = load_resource('postgres_conf', '/etc/postgresql/9.4/main/postgresql.conf')
_(resource.params('log_connections')).must_equal 'on'
end
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 fold the two describe blocks into one as they are redundant.

describe 'Inspec::Resources::Postgres' do
  it 'verify postgresql.conf config parsing of a simple key value' do
    # etc...
  end

  it 'verify postgresql.conf config parsing of a complex key value' do
    # second test here
  end
end

Fixes inspec#1671

Signed-off-by: Aaron Lippold <lippold@gmail.com>
@aaronlippold aaronlippold force-pushed the al/postgres_conf_complex_params branch from e04de83 to c2f7ed5 Compare June 21, 2017 12:00
@aaronlippold
Copy link
Collaborator Author

@adamleff changes pushed. Thanks.

Copy link
Contributor

@chris-rock chris-rock 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 @aaronlippold for this improvement

@chris-rock chris-rock merged commit 3bb98fa into inspec:master Jun 23, 2017
@adamleff adamleff deleted the al/postgres_conf_complex_params branch June 23, 2017 15:32
@aaronlippold aaronlippold restored the al/postgres_conf_complex_params branch June 24, 2017 21:24
@adamleff adamleff changed the title Fixes the postgres_conf parsing of complex paramerters Fix postgres_conf parameters that have a dot in them Jun 27, 2017
@adamleff adamleff changed the title Fix postgres_conf parameters that have a dot in them Fix postgres_conf ability to test parameters that have a dot in them Jun 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Feature not working as expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants