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

work with token and direct compliance server API #20

Merged
merged 7 commits into from
Apr 21, 2016

Conversation

srenatus
Copy link
Contributor

@srenatus srenatus commented Apr 6, 2016

This adds the ability to directly talk to a compliance server, without
passing through a chef-server. To ensure proper authentication, this
needs a user (access) token:

token = '....'

# iterate over all selected profiles
node['audit']['profiles'].each do |owner_profile, enabled|
  next unless enabled
  o, p = owner_profile.split('/')

  compliance_profile p do
    owner o
    server URI.parse('http://192.168.33.1:8080/api/')
    token token
    action [:fetch, :execute]
  end
end

compliance_report 'chef-server' do
  server URI.parse('http://192.168.33.1:8080/api/')
  token token
  organization 'brewinc'
end if node['audit']['profiles'].values.any?

There're two big buts here, though:

  1. It's expecting an access token; so to be of real use in automation setups, something surrounding this chef-run needs to exchange a refresh token for an access token and use ENV['ACCESS_TOKEN'] (or similar) to pass it into the recipe.
  2. Compliance will only accept reports for users (identified via the token) that are authenticated using an oc_id connector (i.e., coming "from a chef server") -- and thus this doesn't make testing the audit-cookbook <--> compliance interaction that much easier: it still needs a chef server in the mix.

@srenatus
Copy link
Contributor Author

srenatus commented Apr 7, 2016

example recipe

token = 'eyJhbGciOiJSUzI1NiIsImtpZCI6InVQUExHVjFiVDg4YTNnZ0MtQ1hxUDdfTlgzTGZiblZSeHhYUTQwV2l0cXI0Z0pRTGw3T2........'

# iterate over all selected profiles
node['audit']['profiles'].each do |owner_profile, enabled|
  next unless enabled
  o, p = owner_profile.split('/')

  compliance_profile p do
    owner o
    server URI.parse('http://192.168.33.1:8080/api/') # <---
    token token # <---
    action [:fetch, :execute]
  end
end

# report the results
compliance_report 'chef-server' do
  server URI.parse('http://192.168.33.1:8080/api/')
  token token # <---
  direct true # <---
  owner 'admin' # <---
end if node['audit']['profiles'].values.any?

@srenatus srenatus force-pushed the sr/direct-connection branch 2 times, most recently from 0e79b9a to 015c65c Compare April 11, 2016 13:08
tf.binmode
Net::HTTP.start(url.host, url.port) do |http|
http.use_ssl = url.scheme == 'https'
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # FIXME
Copy link
Contributor

Choose a reason for hiding this comment

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

could we make it an attribute and set the default to false?

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'm afraid I don't get it. It's not boolean but a string property that is nil if unset and some string if it was provided.
So currently, it's a property of a resource. You can use attributes to set it in your recipe (see here) -- I haven't had a need to touch the default recipe here.

Having a resource break its interface and reach out into node attributes doesn't seem right to me...

Copy link
Contributor

Choose a reason for hiding this comment

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

@srenatus of course we should not use node attributes directly in a library. I was thinking about making it an option to compliance profile:

compliance_profile p do
    owner o
    server URI.parse(ENV['COMPLIANCE_API'])
    insecure true
    token token
    action [:fetch, :execute]
end

Then we could use an attribute as input. I would like to make insecure to false by default. In insecure is true, we could set the proper openssl string as required.

@chris-rock
Copy link
Contributor

@srenatus very good, I like the improvement. Just some minor questions

@srenatus
Copy link
Contributor Author

Thanks @alexpop for finding the nasty bug resolved in bfeb004 👍

@chris-rock is this what you had in mind?

@srenatus srenatus force-pushed the sr/direct-connection branch from bfeb004 to fef1848 Compare April 13, 2016 12:03
@srenatus srenatus force-pushed the sr/direct-connection branch from fef1848 to 7106b99 Compare April 18, 2016 07:46
@srenatus
Copy link
Contributor Author

Rebased & refactored.

@@ -17,16 +17,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

token = node['audit']['api_token']
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be token = node['audit']['token'] ?

@chris-rock
Copy link
Contributor

Awesome @srenatus, I just found some minor nitpicking things :-)

@chris-rock
Copy link
Contributor

I merge it, once its green

@srenatus srenatus force-pushed the sr/direct-connection branch from b321200 to 8703b8f Compare April 20, 2016 15:53
@chris-rock
Copy link
Contributor

Awesome @srenatus

@chris-rock chris-rock merged commit 54e178b into master Apr 21, 2016
@chris-rock chris-rock deleted the sr/direct-connection branch April 21, 2016 06:59
@cjohannsen81
Copy link
Contributor

Hey, just learned that there has to be a slash at the end (URI object). Otherwise it will fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants