chef-handler-sns 0.2.6 → 1.0.0.beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -12
- data/README.md +53 -5
- data/lib/chef/handler/sns/config/ohai.rb +67 -0
- data/lib/chef/handler/sns/config.rb +30 -16
- data/lib/chef/handler/sns/templates/body.erb +1 -1
- data/lib/chef/handler/sns/version.rb +1 -1
- data/lib/chef/handler/sns.rb +19 -18
- data/test/test_chef_handler_sns.rb +39 -12
- data/test/test_chef_handler_sns_config.rb +49 -0
- data/test/test_chef_handler_sns_config_ohai.rb +75 -0
- metadata +12 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d829c08cd526a8fa89e47bcc6c277807e9c85644
|
4
|
+
data.tar.gz: 6590d80bc3673b6b3a353c28451e431b1517d0f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a203949fa961def921c58890f09e254615a4eb0c80cf5e82d120a0be02a63fdd3fa364cb844febccfb58daf41f9c7159c4d7af332fae8bdc5359a3c4083e6cdf
|
7
|
+
data.tar.gz: aa93a6fb844649dea3377851eea94b4cf436607ddfd683a1ac6aaf7e6d531c70f3b25d3ad0c225ee91f15c0dbaaef552ce5dcd8ae4f60aeb449146f6e33c8c9b
|
data/LICENSE
CHANGED
@@ -175,18 +175,7 @@ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
175
175
|
|
176
176
|
END OF TERMS AND CONDITIONS
|
177
177
|
|
178
|
-
|
179
|
-
|
180
|
-
To apply the Apache License to your work, attach the following
|
181
|
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
-
replaced with your own identifying information. (Don't include
|
183
|
-
the brackets!) The text should be enclosed in the appropriate
|
184
|
-
comment syntax for the file format. We also recommend that a
|
185
|
-
file or class name and description of purpose be included on the
|
186
|
-
same "printed page" as the copyright notice for easier
|
187
|
-
identification within third-party archives.
|
188
|
-
|
189
|
-
Copyright [yyyy] [name of copyright owner]
|
178
|
+
Copyright 2014 Onddo Labs, SL. (www.onddo.com)
|
190
179
|
|
191
180
|
Licensed under the Apache License, Version 2.0 (the "License");
|
192
181
|
you may not use this file except in compliance with the License.
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Chef Handler SNS
|
2
2
|
|
3
|
-
A simple Chef report handler that reports status of a Chef run through Amazon SNS.
|
3
|
+
A simple Chef report handler that reports status of a Chef run through [Amazon SNS](http://aws.amazon.com/sns/), [including IAM roles support](#usage-with-amazon-iam-roles).
|
4
|
+
|
5
|
+
[Amazon SNS](http://aws.amazon.com/sns/) can send notifications by SMS, email, [Amazon SQS](http://aws.amazon.com/sqs/) queues or to any HTTP endpoint.
|
4
6
|
|
5
7
|
This Chef Handler is heavily based on [Joshua Timberman](https://github.com/jtimberman) examples.
|
6
8
|
|
@@ -14,7 +16,13 @@ This Chef Handler is heavily based on [Joshua Timberman](https://github.com/jtim
|
|
14
16
|
## Requirements
|
15
17
|
|
16
18
|
* Amazon AWS: uses Amazon SNS service.
|
17
|
-
* Uses the `
|
19
|
+
* Uses the `aws-sdk` library.
|
20
|
+
* `aws-sdk` requires `nokogiri`, which also has the following requires:
|
21
|
+
* `libxml2-dev` and `libxslt-dev` installed (optional).
|
22
|
+
* `gcc` and `make` installed (this will compile and install libxml2 and libxslt internally if not found).
|
23
|
+
* For `Ruby 1.8`, you need to install old versions of the following dependencies:
|
24
|
+
* `mime-types < 2.0`.
|
25
|
+
* `nokogiri < 1.6`.
|
18
26
|
|
19
27
|
## Usage
|
20
28
|
|
@@ -103,17 +111,57 @@ chef_handler "Chef::Handler::Sns" do
|
|
103
111
|
end
|
104
112
|
```
|
105
113
|
|
114
|
+
### Usage with Amazon IAM roles
|
115
|
+
|
116
|
+
If you are using AWS [IAM roles](http://docs.aws.amazon.com/IAM/latest/UserGuide/WorkingWithRoles.html) with your server, probably you only need to specify the `topic_arn` parameter. A few simple examples:
|
117
|
+
|
118
|
+
#### Method 1: In the Chef config file
|
119
|
+
|
120
|
+
You can install the RubyGem and configure Chef to use it:
|
121
|
+
|
122
|
+
gem install chef-handler-sns
|
123
|
+
|
124
|
+
Then add to the configuration (`/etc/chef/solo.rb` for chef-solo or `/etc/chef/client.rb` for chef-client):
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
require "chef/handler/sns"
|
128
|
+
|
129
|
+
exception_handlers << Chef::Handler::Sns.new({
|
130
|
+
:topic_arn => "arn:aws:sns:us-east-1:12341234:MyTopicName"
|
131
|
+
})
|
132
|
+
```
|
133
|
+
|
134
|
+
#### Method 2: In a recipe with the chef_handler LWRP
|
135
|
+
|
136
|
+
Use the [chef_handler LWRP](http://community.opscode.com/cookbooks/chef_handler), creating a recipe with the following:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
# Install the `chef-handler-sns` RubyGem during the compile phase
|
140
|
+
chef_gem "chef-handler-sns"
|
141
|
+
|
142
|
+
# Then activate the handler with the `chef_handler` LWRP
|
143
|
+
chef_handler "Chef::Handler::Sns" do
|
144
|
+
source "#{Gem::Specification.find_by_name("chef-handler-sns").lib_dirs_glob}/chef/handler/sns"
|
145
|
+
arguments { :topic_arn => "arn:aws:sns:us-east-1:12341234:MyTopicName" }
|
146
|
+
supports :exception => true
|
147
|
+
action :enable
|
148
|
+
end
|
149
|
+
```
|
150
|
+
|
106
151
|
## Handler Configuration Options
|
107
152
|
|
108
153
|
The following options are available to configure the handler:
|
109
154
|
|
110
|
-
* `access_key` - AWS access key (required).
|
111
|
-
* `secret_key` - AWS secret key (required).
|
155
|
+
* `access_key` - AWS access key (required, but will try to read it from ohai with IAM roles).
|
156
|
+
* `secret_key` - AWS secret key (required, but will try to read it from ohai with IAM roles).
|
157
|
+
* `token` - AWS security token (optional, read from ohai with IAM roles).
|
112
158
|
* `topic_arn` - AWS topic ARN name (required).
|
113
159
|
* `region` - AWS region (optional).
|
114
160
|
* `subject` - Message subject string in erubis format (optional).
|
115
161
|
* `body_template` - Full path of an erubis template file to use for the message body (optional).
|
116
162
|
|
163
|
+
**Note:** When the machine has an IAM role, will try to read the credentials from ohai. So in the best case, you only need to specify the `topic_arn`.
|
164
|
+
|
117
165
|
### subject
|
118
166
|
|
119
167
|
Here is an example of the `subject` configuration option using the ruby configuration file (`solo.rb` or `client.rb`):
|
@@ -224,7 +272,7 @@ Minitest tests can be run as usual:
|
|
224
272
|
| | |
|
225
273
|
|:---------------------|:-----------------------------------------|
|
226
274
|
| **Author:** | Xabier de Zuazo (<xabier@onddo.com>)
|
227
|
-
| **Copyright:** | Copyright (c)
|
275
|
+
| **Copyright:** | Copyright (c) 2014 Onddo Labs, SL. (www.onddo.com)
|
228
276
|
| **License:** | Apache License, Version 2.0
|
229
277
|
|
230
278
|
Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -0,0 +1,67 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Xabier de Zuazo (<xabier@onddo.com>)
|
3
|
+
# Copyright:: Copyright (c) 2014 Onddo Labs, SL.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
class Chef
|
20
|
+
class Handler
|
21
|
+
class Sns < ::Chef::Handler
|
22
|
+
module Config
|
23
|
+
class Ohai
|
24
|
+
|
25
|
+
def initialize(node)
|
26
|
+
read_config(node)
|
27
|
+
end
|
28
|
+
|
29
|
+
def region
|
30
|
+
@region
|
31
|
+
end
|
32
|
+
|
33
|
+
def access_key
|
34
|
+
@access_key
|
35
|
+
end
|
36
|
+
|
37
|
+
def secret_key
|
38
|
+
@secret_key
|
39
|
+
end
|
40
|
+
|
41
|
+
def token
|
42
|
+
@token
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
|
47
|
+
def read_config(node)
|
48
|
+
return unless node.attribute?('ec2')
|
49
|
+
if node.ec2.attribute?('placement_availability_zone') and
|
50
|
+
node.ec2.placement_availability_zone.kind_of?(String)
|
51
|
+
@region = node.ec2.placement_availability_zone.chop
|
52
|
+
end
|
53
|
+
if node.ec2.attribute?('iam') and node.ec2.iam.attribute?('security-credentials')
|
54
|
+
iam_role, credentials = node.ec2.iam['security-credentials'].first
|
55
|
+
unless credentials.nil?
|
56
|
+
@access_key = credentials['AccessKeyId']
|
57
|
+
@secret_key = credentials['SecretAccessKey']
|
58
|
+
@token = credentials['Token']
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
#
|
2
2
|
# Author:: Xabier de Zuazo (<xabier@onddo.com>)
|
3
|
-
# Copyright:: Copyright (c)
|
3
|
+
# Copyright:: Copyright (c) 2014 Onddo Labs, SL.
|
4
4
|
# License:: Apache License, Version 2.0
|
5
5
|
#
|
6
6
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -16,6 +16,7 @@
|
|
16
16
|
# limitations under the License.
|
17
17
|
#
|
18
18
|
|
19
|
+
require 'chef/handler/sns/config/ohai'
|
19
20
|
require 'chef/mixin/params_validate'
|
20
21
|
require 'chef/exceptions'
|
21
22
|
|
@@ -27,7 +28,19 @@ class Chef
|
|
27
28
|
include ::Chef::Mixin::ParamsValidate
|
28
29
|
|
29
30
|
REQUIRED = [ 'access_key', 'secret_key', 'topic_arn' ]
|
30
|
-
|
31
|
+
|
32
|
+
def config_from_ohai(node)
|
33
|
+
config_ohai = Config::Ohai.new(node)
|
34
|
+
[
|
35
|
+
:region,
|
36
|
+
:access_key,
|
37
|
+
:secret_key,
|
38
|
+
:token,
|
39
|
+
].each do |attr|
|
40
|
+
self.send(attr, config_ohai.send(attr)) if self.send(attr).nil?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
31
44
|
def config_init(config={})
|
32
45
|
config.each do |key, value|
|
33
46
|
if Config.respond_to?(key) and not /^config_/ =~ key.to_s
|
@@ -37,21 +50,22 @@ class Chef
|
|
37
50
|
end
|
38
51
|
end
|
39
52
|
end
|
40
|
-
|
41
|
-
def config_check
|
53
|
+
|
54
|
+
def config_check(node=nil)
|
55
|
+
config_from_ohai(node) if node
|
42
56
|
REQUIRED.each do |key|
|
43
57
|
if self.send(key).nil?
|
44
58
|
raise Exceptions::ValidationFailed,
|
45
59
|
"Required argument #{key.to_s} is missing!"
|
46
60
|
end
|
47
61
|
end
|
48
|
-
|
62
|
+
|
49
63
|
if body_template and not ::File.exists?(body_template)
|
50
64
|
raise Exceptions::ValidationFailed,
|
51
65
|
"Template file not found: #{body_template}."
|
52
66
|
end
|
53
67
|
end
|
54
|
-
|
68
|
+
|
55
69
|
def access_key(arg=nil)
|
56
70
|
set_or_return(
|
57
71
|
:access_key,
|
@@ -59,7 +73,7 @@ class Chef
|
|
59
73
|
:kind_of => String
|
60
74
|
)
|
61
75
|
end
|
62
|
-
|
76
|
+
|
63
77
|
def secret_key(arg=nil)
|
64
78
|
set_or_return(
|
65
79
|
:secret_key,
|
@@ -67,7 +81,7 @@ class Chef
|
|
67
81
|
:kind_of => String
|
68
82
|
)
|
69
83
|
end
|
70
|
-
|
84
|
+
|
71
85
|
def region(arg=nil)
|
72
86
|
set_or_return(
|
73
87
|
:region,
|
@@ -75,7 +89,7 @@ class Chef
|
|
75
89
|
:kind_of => String
|
76
90
|
)
|
77
91
|
end
|
78
|
-
|
92
|
+
|
79
93
|
def token(arg=nil)
|
80
94
|
set_or_return(
|
81
95
|
:token,
|
@@ -83,7 +97,7 @@ class Chef
|
|
83
97
|
:kind_of => String
|
84
98
|
)
|
85
99
|
end
|
86
|
-
|
100
|
+
|
87
101
|
def topic_arn(arg=nil)
|
88
102
|
set_or_return(
|
89
103
|
:topic_arn,
|
@@ -91,7 +105,7 @@ class Chef
|
|
91
105
|
:kind_of => String
|
92
106
|
)
|
93
107
|
end
|
94
|
-
|
108
|
+
|
95
109
|
def subject(arg=nil)
|
96
110
|
set_or_return(
|
97
111
|
:subject,
|
@@ -99,7 +113,7 @@ class Chef
|
|
99
113
|
:kind_of => String
|
100
114
|
)
|
101
115
|
end
|
102
|
-
|
116
|
+
|
103
117
|
def body_template(arg=nil)
|
104
118
|
set_or_return(
|
105
119
|
:body_template,
|
@@ -107,8 +121,8 @@ class Chef
|
|
107
121
|
:kind_of => String
|
108
122
|
)
|
109
123
|
end
|
110
|
-
|
124
|
+
|
111
125
|
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -26,7 +26,7 @@ Instance Public IPv4: <%= node.ec2.public_ipv4 %>
|
|
26
26
|
Instance Local IPv4: <%= node.ec2.local_ipv4 %>
|
27
27
|
<% end -%>
|
28
28
|
<% end -%>
|
29
|
-
|
29
|
+
|
30
30
|
Chef Client Elapsed Time: <%= elapsed_time.to_s %>
|
31
31
|
Chef Client Start Time: <%= start_time.to_s %>
|
32
32
|
Chef Client Start Time: <%= end_time.to_s %>
|
data/lib/chef/handler/sns.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
#
|
2
2
|
# Author:: Xabier de Zuazo (<xabier@onddo.com>)
|
3
|
-
# Copyright:: Copyright (c)
|
3
|
+
# Copyright:: Copyright (c) 2014 Onddo Labs, SL.
|
4
4
|
# License:: Apache License, Version 2.0
|
5
5
|
#
|
6
6
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -18,26 +18,29 @@
|
|
18
18
|
|
19
19
|
require 'chef/handler'
|
20
20
|
require 'chef/handler/sns/config'
|
21
|
-
require '
|
21
|
+
require 'aws-sdk'
|
22
22
|
require 'erubis'
|
23
23
|
|
24
24
|
class Chef
|
25
25
|
class Handler
|
26
26
|
class Sns < ::Chef::Handler
|
27
27
|
include ::Chef::Handler::Sns::Config
|
28
|
-
|
28
|
+
|
29
29
|
def initialize(config={})
|
30
30
|
Chef::Log.debug("#{self.class.to_s} initialized.")
|
31
31
|
config_init(config)
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def report
|
35
|
-
config_check
|
36
|
-
sns.publish(
|
35
|
+
config_check(node)
|
36
|
+
sns.topics[topic_arn].publish(
|
37
|
+
sns_body,
|
38
|
+
{ :subject => sns_subject }
|
39
|
+
)
|
37
40
|
end
|
38
41
|
|
39
|
-
def
|
40
|
-
|
42
|
+
def get_region
|
43
|
+
sns.config.region
|
41
44
|
end
|
42
45
|
|
43
46
|
protected
|
@@ -45,18 +48,16 @@ class Chef
|
|
45
48
|
def sns
|
46
49
|
@sns ||= begin
|
47
50
|
params = {
|
51
|
+
:access_key_id => access_key,
|
52
|
+
:secret_access_key => secret_key,
|
48
53
|
:logger => Chef::Log
|
49
54
|
}
|
50
|
-
if
|
51
|
-
|
52
|
-
|
53
|
-
params[:region] = node.ec2.placement_availability_zone.chop
|
54
|
-
end
|
55
|
-
params[:token] = token if token
|
56
|
-
RightAws::SnsInterface.new(access_key, secret_key, params)
|
55
|
+
params[:region] = region if region
|
56
|
+
params[:session_token] = token if token
|
57
|
+
AWS::SNS.new(params)
|
57
58
|
end
|
58
59
|
end
|
59
|
-
|
60
|
+
|
60
61
|
def sns_subject
|
61
62
|
if subject
|
62
63
|
context = self
|
@@ -68,14 +69,14 @@ class Chef
|
|
68
69
|
"#{chef_client} #{status} in #{node.name}"
|
69
70
|
end
|
70
71
|
end
|
71
|
-
|
72
|
+
|
72
73
|
def sns_body
|
73
74
|
template = IO.read(body_template || "#{File.dirname(__FILE__)}/sns/templates/body.erb")
|
74
75
|
context = self
|
75
76
|
eruby = Erubis::Eruby.new(template)
|
76
77
|
eruby.evaluate(context)
|
77
78
|
end
|
78
|
-
|
79
|
+
|
79
80
|
end
|
80
81
|
end
|
81
82
|
end
|
@@ -2,14 +2,20 @@ require 'helper'
|
|
2
2
|
require 'chef/node'
|
3
3
|
require 'chef/run_status'
|
4
4
|
|
5
|
-
class
|
6
|
-
attr_reader :
|
5
|
+
class AWS::FakeSNS
|
6
|
+
attr_reader :sns_new
|
7
7
|
|
8
|
-
def
|
9
|
-
@
|
8
|
+
def initialize(config)
|
9
|
+
@sns_new = true
|
10
10
|
return self
|
11
11
|
end
|
12
12
|
|
13
|
+
def topics
|
14
|
+
{
|
15
|
+
'arn:aws:sns:***' => AWS::SNS::Topic.new('arn:aws:sns:***')
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
13
19
|
end
|
14
20
|
|
15
21
|
class Chef::Handler::FakeSns < Chef::Handler::Sns
|
@@ -26,17 +32,31 @@ end
|
|
26
32
|
|
27
33
|
describe Chef::Handler::Sns do
|
28
34
|
before do
|
29
|
-
|
35
|
+
AWS::SNS::Topic.any_instance.stubs(:publish).returns(true)
|
36
|
+
# avoid File.read("endpoints.json")
|
37
|
+
AWS::Core::Endpoints.stubs(:endpoints).returns({
|
38
|
+
'regions' => {
|
39
|
+
'us-east-1' => {
|
40
|
+
'sns' => {
|
41
|
+
'http' => true,
|
42
|
+
'https' => true,
|
43
|
+
'hostname' => 'sns.us-east-1.amazonaws.com',
|
44
|
+
}
|
45
|
+
}
|
46
|
+
}
|
47
|
+
})
|
30
48
|
|
31
49
|
@node = Chef::Node.new
|
32
50
|
@node.name('test')
|
33
51
|
Chef::Handler::Sns.any_instance.stubs(:node).returns(@node)
|
52
|
+
Chef::Handler::FakeSns.any_instance.stubs(:node).returns(@node)
|
34
53
|
|
35
54
|
@run_status = if Gem.loaded_specs['chef'].version > Gem::Version.new('0.12.0')
|
36
55
|
Chef::RunStatus.new(@node, {})
|
37
56
|
else
|
38
57
|
Chef::RunStatus.new(@node)
|
39
58
|
end
|
59
|
+
|
40
60
|
@run_status.start_clock
|
41
61
|
@run_status.stop_clock
|
42
62
|
|
@@ -63,18 +83,22 @@ describe Chef::Handler::Sns do
|
|
63
83
|
|
64
84
|
it 'should try to send a SNS message when properly configured' do
|
65
85
|
@sns_handler = Chef::Handler::Sns.new(@config)
|
66
|
-
|
86
|
+
AWS::SNS::Topic.any_instance.expects(:publish).once
|
67
87
|
|
68
88
|
@sns_handler.run_report_safely(@run_status)
|
69
89
|
end
|
70
90
|
|
71
|
-
it 'should create a
|
91
|
+
it 'should create a AWS::SNS object' do
|
72
92
|
@sns_handler = Chef::Handler::Sns.new(@config)
|
73
|
-
fake_sns =
|
74
|
-
|
93
|
+
fake_sns = AWS::FakeSNS.new({
|
94
|
+
:access_key_id => @config[:access_key],
|
95
|
+
:secret_access_key => @config[:secret_key],
|
96
|
+
:logger => Chef::Log
|
97
|
+
})
|
98
|
+
AWS::SNS.stubs(:new).returns(fake_sns)
|
75
99
|
@sns_handler.run_report_safely(@run_status)
|
76
100
|
|
77
|
-
assert_equal fake_sns.
|
101
|
+
assert_equal fake_sns.sns_new, true
|
78
102
|
end
|
79
103
|
|
80
104
|
it 'should detect the AWS region automatically' do
|
@@ -82,7 +106,7 @@ describe Chef::Handler::Sns do
|
|
82
106
|
@sns_handler = Chef::Handler::Sns.new(@config)
|
83
107
|
@sns_handler.run_report_safely(@run_status)
|
84
108
|
|
85
|
-
@sns_handler.
|
109
|
+
@sns_handler.get_region.must_equal 'eu-west-1'
|
86
110
|
end
|
87
111
|
|
88
112
|
it 'should not detect AWS region automatically whan manually set' do
|
@@ -91,7 +115,7 @@ describe Chef::Handler::Sns do
|
|
91
115
|
@sns_handler = Chef::Handler::Sns.new(@config)
|
92
116
|
@sns_handler.run_report_safely(@run_status)
|
93
117
|
|
94
|
-
@sns_handler.
|
118
|
+
@sns_handler.get_region.must_equal 'us-east-1'
|
95
119
|
end
|
96
120
|
|
97
121
|
it 'should be able to generate the default subject in chef-client' do
|
@@ -141,6 +165,9 @@ describe Chef::Handler::Sns do
|
|
141
165
|
@config[:body_template] = '/tmp/existing-template.erb'
|
142
166
|
::File.stubs(:exists?).with(@config[:body_template]).returns(true)
|
143
167
|
IO.stubs(:read).with(@config[:body_template]).returns(body_msg)
|
168
|
+
|
169
|
+
fake_sns = AWS::FakeSNS.new({})
|
170
|
+
AWS::SNS.stubs(:new).returns(fake_sns)
|
144
171
|
@fake_sns_handler = Chef::Handler::FakeSns.new(@config)
|
145
172
|
Chef::Handler::FakeSns.any_instance.stubs(:node).returns(@node)
|
146
173
|
@fake_sns_handler.run_report_unsafe(@run_status)
|
@@ -5,6 +5,13 @@ class SnsConfig
|
|
5
5
|
include Chef::Handler::Sns::Config
|
6
6
|
end
|
7
7
|
|
8
|
+
module Chef::Handler::Sns::Config
|
9
|
+
class FakeOhai < Chef::Handler::Sns::Config::Ohai
|
10
|
+
def intialize
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
8
15
|
describe Chef::Handler::Sns::Config do
|
9
16
|
before do
|
10
17
|
@config_params = {
|
@@ -12,6 +19,8 @@ describe Chef::Handler::Sns::Config do
|
|
12
19
|
:secret_key => '***AMAZON-SECRET***',
|
13
20
|
:topic_arn => 'arn:aws:sns:***',
|
14
21
|
}
|
22
|
+
@node = Chef::Node.new
|
23
|
+
@node.name('test')
|
15
24
|
@sns_config = SnsConfig.new
|
16
25
|
end
|
17
26
|
|
@@ -86,4 +95,44 @@ describe Chef::Handler::Sns::Config do
|
|
86
95
|
|
87
96
|
end
|
88
97
|
|
98
|
+
describe 'config_check' do
|
99
|
+
|
100
|
+
it 'should call #config_from_ohai method' do
|
101
|
+
@sns_config.access_key(@config_params[:access_key])
|
102
|
+
@sns_config.secret_key(@config_params[:secret_key])
|
103
|
+
@sns_config.topic_arn(@config_params[:topic_arn])
|
104
|
+
@sns_config.expects(:config_from_ohai).once
|
105
|
+
|
106
|
+
@sns_config.config_check(@node)
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
describe 'config_from_ohai' do
|
112
|
+
before do
|
113
|
+
@fake_ohai = Chef::Handler::Sns::Config::FakeOhai.new(@node)
|
114
|
+
Chef::Handler::Sns::Config::Ohai.stubs(:new).returns(@fake_ohai)
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should create Config::Ohai object' do
|
118
|
+
Chef::Handler::Sns::Config::Ohai.expects(:new).once.returns(@fake_ohai)
|
119
|
+
|
120
|
+
@sns_config.config_from_ohai(@node)
|
121
|
+
end
|
122
|
+
|
123
|
+
[
|
124
|
+
:region,
|
125
|
+
:access_key,
|
126
|
+
:secret_key,
|
127
|
+
:token,
|
128
|
+
].each do |method|
|
129
|
+
it "should call Config::Ohai##{method} method" do
|
130
|
+
@fake_ohai.expects(method).once
|
131
|
+
|
132
|
+
@sns_config.config_from_ohai(@node)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
89
138
|
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Chef::Handler::Sns::Config::Ohai do
|
4
|
+
before do
|
5
|
+
@node = Chef::Node.new
|
6
|
+
@node.name('test')
|
7
|
+
@node.set['ec2'] = {
|
8
|
+
'placement_availability_zone' => 'region1a',
|
9
|
+
'iam' => {
|
10
|
+
'security-credentials' => {
|
11
|
+
'iam-role1' => {
|
12
|
+
'AccessKeyId' => 'access_key1',
|
13
|
+
'SecretAccessKey' => 'secret_key1',
|
14
|
+
'Token' => 'token1',
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'read_config' do
|
22
|
+
|
23
|
+
it 'should read the region' do
|
24
|
+
config = Chef::Handler::Sns::Config::Ohai.new(@node)
|
25
|
+
assert_equal config.region, 'region1'
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should not read the region when not set' do
|
29
|
+
@node.set['ec2']['placement_availability_zone'] = nil
|
30
|
+
config = Chef::Handler::Sns::Config::Ohai.new(@node)
|
31
|
+
assert_equal config.region, nil
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should not read the credentials when has not IAM role' do
|
35
|
+
@node.set['ec2'] = {}
|
36
|
+
config = Chef::Handler::Sns::Config::Ohai.new(@node)
|
37
|
+
assert_equal config.access_key, nil
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should read the access_key' do
|
41
|
+
config = Chef::Handler::Sns::Config::Ohai.new(@node)
|
42
|
+
assert_equal config.access_key, 'access_key1'
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should not read the access_key when not set' do
|
46
|
+
@node.set['ec2']['iam']['security-credentials']['iam-role1']['AccessKeyId'] = nil
|
47
|
+
config = Chef::Handler::Sns::Config::Ohai.new(@node)
|
48
|
+
assert_equal config.access_key, nil
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should read the secret_key' do
|
52
|
+
config = Chef::Handler::Sns::Config::Ohai.new(@node)
|
53
|
+
assert_equal config.secret_key, 'secret_key1'
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should not read the secret_key when not set' do
|
57
|
+
@node.set['ec2']['iam']['security-credentials']['iam-role1']['SecretAccessKey'] = nil
|
58
|
+
config = Chef::Handler::Sns::Config::Ohai.new(@node)
|
59
|
+
assert_equal config.secret_key, nil
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should read the security token' do
|
63
|
+
config = Chef::Handler::Sns::Config::Ohai.new(@node)
|
64
|
+
assert_equal config.token, 'token1'
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should not read the security token when not set' do
|
68
|
+
@node.set['ec2']['iam']['security-credentials']['iam-role1']['Token'] = nil
|
69
|
+
config = Chef::Handler::Sns::Config::Ohai.new(@node)
|
70
|
+
assert_equal config.token, nil
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chef-handler-sns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0.beta.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Onddo Labs, SL.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: aws-sdk
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '1.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '1.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: erubis
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: mime-types
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ~>
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '1.0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ~>
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '1.0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: chef
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,7 +122,8 @@ dependencies:
|
|
136
122
|
- - '>='
|
137
123
|
- !ruby/object:Gem::Version
|
138
124
|
version: '0'
|
139
|
-
description: Chef report handler to send SNS notifications on failures or changes
|
125
|
+
description: Chef report handler to send Amazon SNS notifications on failures or changes,
|
126
|
+
includes IAM roles support
|
140
127
|
email: team@onddo.com
|
141
128
|
executables: []
|
142
129
|
extensions: []
|
@@ -147,9 +134,11 @@ files:
|
|
147
134
|
- lib/chef/handler/sns.rb
|
148
135
|
- lib/chef/handler/sns/templates/body.erb
|
149
136
|
- lib/chef/handler/sns/config.rb
|
137
|
+
- lib/chef/handler/sns/config/ohai.rb
|
150
138
|
- lib/chef/handler/sns/version.rb
|
151
139
|
- test/helper.rb
|
152
140
|
- test/test_chef_handler_sns.rb
|
141
|
+
- test/test_chef_handler_sns_config_ohai.rb
|
153
142
|
- test/test_chef_handler_sns_config.rb
|
154
143
|
homepage: http://onddo.github.io/chef-handler-sns
|
155
144
|
licenses:
|
@@ -166,9 +155,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
166
155
|
version: '0'
|
167
156
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
168
157
|
requirements:
|
169
|
-
- - '
|
158
|
+
- - '>'
|
170
159
|
- !ruby/object:Gem::Version
|
171
|
-
version:
|
160
|
+
version: 1.3.1
|
172
161
|
requirements: []
|
173
162
|
rubyforge_project:
|
174
163
|
rubygems_version: 2.0.6
|
@@ -178,4 +167,5 @@ summary: Chef SNS reports
|
|
178
167
|
test_files:
|
179
168
|
- test/helper.rb
|
180
169
|
- test/test_chef_handler_sns.rb
|
170
|
+
- test/test_chef_handler_sns_config_ohai.rb
|
181
171
|
- test/test_chef_handler_sns_config.rb
|