Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

COOK-4715 - add upgrade recipe and complete test harness #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.vagrant
Berksfile.lock
*~
*#
.#*
\#*#
.*.sw[a-z]
*.un~
/cookbooks

# Bundler
Gemfile.lock
bin/*
.bundle/*

.kitchen/
.kitchen.local.yml
25 changes: 25 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
driver_plugin: vagrant

driver_config:
require_chef_omnibus: true

provisioner:
name: chef_zero

platforms:
- name: ubuntu-12.04
- name: ubuntu-14.04
- name: debian-7.4
- name: centos-6.5

suites:
- name: upgrade
run_list:
- recipe[test]
- recipe[postfix]
- recipe[openssl::upgrade]
attributes:
openssl:
restart_services:
- postfix
28 changes: 28 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
AllCops:
Includes:
- Berksfile
- Gemfile
- Rakefile
- Thorfile
- Guardfile
Excludes:
- vendor/**

ClassLength:
Enabled: false
Documentation:
Enabled: false
Encoding:
Enabled: false
HashSyntax:
Enabled: false
LineLength:
Enabled: false
MethodLength:
Enabled: false
SignalException:
Enabled: false
TrailingComma:
Enabled: false
WordArray:
Enabled: false
8 changes: 8 additions & 0 deletions Berksfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source 'https://api.berkshelf.com'

metadata

group :integration do
cookbook 'test', :path => 'test/fixtures/cookbooks/test'
cookbook 'postfix'
end
55 changes: 49 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,71 @@
openssl Cookbook
================
Provide a library method to generate secure random passwords in recipes.

This cookbook provides a library method to generate secure random passwords in recipes using the Ruby OpenSSL library.

It also provides an attribute-driven recipe for upgrading OpenSSL packages.

Requirements
------------
Works on any platform with OpenSSL Ruby bindings installed, which are a requirement for Chef anyway.

The `secure_password` works on any platform with OpenSSL Ruby bindings installed, which are a requirement for Chef anyway.

The upgrade recipe works on the following tested platforms:

* Ubuntu 12.04, 14.04
* Debian 7.4
* CentOS 6.5

It may work on other platforms or versions of the above platforms with or without modification.

[Chef Sugar](https://github.com/sethvargo/chef-sugar) was introduced as a dependency to provide helpers that make the default attribute settings (see Attributes) easier to reason about.

Attributes
----------

* `node['openssl']['packages']` - An array of packages of openssl. The default attributes attempt to be smart about which packages are the default, but this may need to be changed by users of the `openssl::upgrade` recipe.
* `node['openssl']['restart_services']` - An array of service resources that use the `node['openssl']['packages']`. This is empty by default as Chef has no reliably reasonable way to detect which applications or services are compiled against these packages. *Note* These each need to be "`service`" resources specified somewhere in the recipes in the node's run list.

Recipes
-------

### upgrade

The upgrade recipe iterates over the list of packages in the `node['openssl']['packages']` attribute and manages them with the `:upgrade` action. Each package will send `:restart` notification to service resources named by the `node['openssl']['restart_services']` attribute.

Usage
-----
Most often this will be used to generate a secure password for an attribute.

Most often this will be used to generate a secure password for an attribute. In a recipe:

```ruby
include Opscode::OpenSSL::Password
set_unless[:my_password] = secure_password
::Chef::Recipe.send(:include, Opscode::OpenSSL::Password)
node.set_unless[:my_password] = secure_password
```

To use the `openssl::upgrade` recipe, set the attributes as mentioned above. For example, we have a "stats_collector" service that uses openssl. It has a recipe that looks like this:

```ruby
node.default['openssl']['restart_services'] = ['stats_collector']

# other recipe code here...
service 'stats_collector' do
action [:enable, :start]
end

include_recipe 'openssl::upgrade'
```

This will ensure that openssl is upgraded to the latest version so the `stats_collector` service won't be exploited (hopefully!).

License & Authors
-----------------
- Author:: Joshua Timberman (<[email protected]>)

- Author:: Joshua Timberman (<[email protected]>)

```text
Copyright:: 2009-2011, Opscode, Inc
Copyright:: 2014, Chef Software, Inc <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
21 changes: 21 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Cookbook Name:: openssl
# Attributes:: default
#
# Copyright 2014, Chef Software, Inc. <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

default['openssl']['packages'] = []
default['openssl']['restart_services'] = []
Copy link
Contributor Author

Choose a reason for hiding this comment

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

By default, these are empty, users can set defaults in roles or however else they set attributes. The actual defaults detected-by-platform is done in the upgrade recipe (see comment below).

18 changes: 11 additions & 7 deletions metadata.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
name "openssl"
maintainer "Opscode, Inc."
maintainer_email "[email protected]"
license "Apache 2.0"
description "Provides a library with a method for generating secure random passwords."
name 'openssl'
maintainer 'Opscode, Inc.'
maintainer_email '[email protected]'
license 'Apache 2.0'
description 'Provides a library with a method for generating secure random passwords.'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version "1.1.1"
version '1.1.1'

recipe "openssl", "Empty, this cookbook provides a library, see README.md"
recipe 'openssl', 'Empty, this cookbook provides a library, see README.md'

# chef-sugar greatly reduces the amount of code required to check
# conditionals for the attributes used in the upgrader recipe.
depends 'chef-sugar'
1 change: 0 additions & 1 deletion recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

39 changes: 39 additions & 0 deletions recipes/upgrade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# Cookbook Name:: openssl
# Recipe:: upgrade
#
# Copyright 2014, Chef Software, Inc. <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
include_recipe 'chef-sugar'

node.default['openssl']['packages'] = case
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This attribute is done in the recipe because chef-sugar wouldn't be installed and available when the attributes file is parsed without a custom bootstrap.

when debian_before_or_at_squeeze?, ubuntu_before_or_at_lucid?
%w{libssl0.9.8 openssl}
when debian_after_or_at_wheezy?, ubuntu_after_or_at_precise?
%w{libssl1.0.0 openssl}
when rhel?
%w{openssl}
else
[]
end

node['openssl']['packages'].each do |ssl_pkg|
package ssl_pkg do
action :upgrade
node['openssl']['restart_services'].each do |ssl_svc|
notifies :restart, "service[#{ssl_svc}]"
end
end
end
68 changes: 68 additions & 0 deletions spec/recipes/upgrade_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require_relative '../spec_helper'

describe 'openssl::upgrade' do
context 'notify restart on upgrade' do
let(:chef_run) do
ChefSpec::Runner.new(
:platform => 'debian',
:version => '7.4'
) do |node|
node.set['openssl']['packages'] = ['openssl']
node.set['openssl']['restart_services'] = ['httpd']
end.converge('test::httpd', described_recipe)
end

let(:package) { chef_run.package('openssl') }

it 'restart httpd when upgrading openssl' do
expect(package).to notify('service[httpd]').to(:restart)
end

end
context 'ubuntu_before_or_at_lucid' do
let(:chef_run) do
ChefSpec::Runner.new(
:platform => 'ubuntu',
:version => '10.04'
).converge(described_recipe)
end

it 'will upgrade the libssl0.9.8 package' do
expect(chef_run).to upgrade_package('libssl0.9.8')
end

it 'will upgrade the openssl package' do
expect(chef_run).to upgrade_package('openssl')
end

end

context 'ubuntu_after_or_at_precise' do
let(:chef_run) do
ChefSpec::Runner.new(
:platform => 'ubuntu',
:version => '12.04'
).converge(described_recipe)
end

it 'will upgrade the libssl1.0.0 package' do
expect(chef_run).to upgrade_package('libssl1.0.0')
end

it 'will upgrade the openssl package' do
expect(chef_run).to upgrade_package('openssl')
end
end

context 'redhat_enterprise' do
let(:chef_run) do
ChefSpec::Runner.new(
:platform => 'redhat',
:version => '6.5'
).converge(described_recipe)
end
it 'will upgrade the openssl package' do
expect(chef_run).to upgrade_package('openssl')
end
end
end
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'chefspec'
require 'chefspec/berkshelf'

RSpec.configure do |config|
config.color_enabled = true
config.formatter = :documentation
end
4 changes: 4 additions & 0 deletions test/fixtures/cookbooks/test/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name 'test'
license 'Apache 2.0'
description 'Installs/Configures test'
version '0.1.0'
21 changes: 21 additions & 0 deletions test/fixtures/cookbooks/test/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# Cookbook Name:: test
# Recipe:: default
#
# Copyright:: Copyright (c) 2014, Chef Software, Inc. <[email protected]>
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

execute 'apt-get update' if platform_family?('debian')
1 change: 1 addition & 0 deletions test/fixtures/cookbooks/test/recipes/httpd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service('httpd') { action :nothing }