cfn-bridge 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +36 -0
  6. data/Rakefile +2 -0
  7. data/bin/cfn-bridge +5 -0
  8. data/cfn-bridge.gemspec +33 -0
  9. data/lib/cfn-bridge.rb +1 -0
  10. data/lib/cloud_formation/bridge/cli.rb +16 -0
  11. data/lib/cloud_formation/bridge/errors.rb +5 -0
  12. data/lib/cloud_formation/bridge/exception_notifier.rb +59 -0
  13. data/lib/cloud_formation/bridge/executor.rb +50 -0
  14. data/lib/cloud_formation/bridge/http_bridge.rb +30 -0
  15. data/lib/cloud_formation/bridge/names.rb +34 -0
  16. data/lib/cloud_formation/bridge/poller.rb +67 -0
  17. data/lib/cloud_formation/bridge/request.rb +94 -0
  18. data/lib/cloud_formation/bridge/resources/base.rb +31 -0
  19. data/lib/cloud_formation/bridge/resources/cloud_formation_outputs.rb +44 -0
  20. data/lib/cloud_formation/bridge/resources/subscribe_queue_to_topic.rb +66 -0
  21. data/lib/cloud_formation/bridge/version.rb +5 -0
  22. data/spec/files/outputs-formation.json +48 -0
  23. data/spec/files/sample-create-message.json +12 -0
  24. data/spec/files/sample-delete-message.json +12 -0
  25. data/spec/files/subscribe-to-sns-formation.json +79 -0
  26. data/spec/files/test-formation.json +75 -0
  27. data/spec/lib/cloud_formation/bridge/executor_spec.rb +66 -0
  28. data/spec/lib/cloud_formation/bridge/poller_spec.rb +88 -0
  29. data/spec/lib/cloud_formation/bridge/request_spec.rb +130 -0
  30. data/spec/lib/cloud_formation/bridge/resources/cloud_formation_outputs_spec.rb +36 -0
  31. data/spec/lib/cloud_formation/bridge/resources/subscribe_queue_to_topic_spec.rb +95 -0
  32. data/spec/spec_helper.rb +92 -0
  33. data/spec/support/cloud_formation_creator.rb +104 -0
  34. data/spec/support/file_support.rb +7 -0
  35. metadata +245 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a8cbad0195cc6723b1376f7116ee26fa15f0d8a6
4
+ data.tar.gz: 0d6a331241ae011910c7dc6878b4541f6e4fb15d
5
+ SHA512:
6
+ metadata.gz: abc78980d3551045d36fe688daacbb02a9447dd9346a4ddc2c776c44dcebc30614631b53e1b7346a999d00a4789c48d8e61d5e86e5d520f8118348568fc50db8
7
+ data.tar.gz: 04fd75107f7b315ab4ff511f56aa5de85204d56ce07509fdf4c8820e6c54803e981ec54d11f3768b1f5ba112c44c0e95ea66363b627b2569ebb3816fefb03e69
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .env
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cloud-formation-custom-resources.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 The Neat Company
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # cfn-bridge
2
+
3
+ A bridge to allow you to build custom AWS cloud formation resources.
4
+
5
+ Check Amazon's page [on custom cloud formation resources](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-walkthrough.html)
6
+ to get more info on how and why you would like to have them.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'cfn-bridge'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install cfn-bridge
21
+
22
+ ## Usage
23
+
24
+ Run:
25
+
26
+ $ cfn-bridge start QUEUE_NAME
27
+
28
+ This will start a worker consuming from `QUEUE_NAME` until the caller calls `CTRL-C` to stop it. `QUEUE_NAME` should be the SQS queue to where the SNS topic is publishing the custom resource messages. An example template that setups the topic and the queue [is available at the repo](spec/files/test-formation.json) and can be used to provide the base topic and queue to use this application.
29
+
30
+ ## Contributing
31
+
32
+ 1. [Fork it](https://github.com/TheNeatCompany/cfn-bridge/fork)
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/cfn-bridge ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cloud_formation/bridge/cli'
4
+
5
+ CloudFormation::Bridge::Cli.start(ARGV)
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cloud_formation/bridge/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cfn-bridge"
8
+ spec.version = CloudFormation::Bridge::VERSION
9
+ spec.authors = ["Maurício Linhares"]
10
+ spec.email = ["mlinhares@neat.com"]
11
+ spec.summary = %q{Implements custom operations for CF calls}
12
+ spec.description = %q{Implements custom operations for CF calls}
13
+ spec.homepage = "https://github.com/TheNeatCompany/cfn-bridge"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.6.5'
22
+ spec.add_development_dependency 'rake', '~> 10.3.2'
23
+ spec.add_development_dependency 'rspec', '~> 3.0.0'
24
+ spec.add_development_dependency 'dotenv', '~> 0.11.1'
25
+ spec.add_development_dependency 'pry', '~> 0.10.0'
26
+
27
+ spec.add_dependency 'aws-sdk', '~> 1.50.0'
28
+ spec.add_dependency 'faraday', '~> 0.9.0'
29
+ spec.add_dependency 'faraday_middleware', '~> 0.9.1'
30
+ spec.add_dependency 'thor', '~> 0.19.1'
31
+ spec.add_dependency 'typhoeus', '~> 0.6.9'
32
+ spec.add_dependency 'rollbar', '~> 1.0.0'
33
+ end
data/lib/cfn-bridge.rb ADDED
@@ -0,0 +1 @@
1
+ require 'cloud_formation/bridge/poller'
@@ -0,0 +1,16 @@
1
+ require 'thor'
2
+ require 'cloud_formation/bridge/poller'
3
+
4
+ module CloudFormation
5
+ module Bridge
6
+ class Cli < Thor
7
+
8
+ desc "start QUEUE_NAME", "Starts watching this specific SQS queue"
9
+ def start(queue_name)
10
+ poller = CloudFormation::Bridge::Poller.new(queue_name)
11
+ poller.start
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module CloudFormation
2
+ module Bridge
3
+ OperationNotImplementedError = Class.new(StandardError)
4
+ end
5
+ end
@@ -0,0 +1,59 @@
1
+ require 'singleton'
2
+ require 'rollbar'
3
+
4
+ Rollbar.configure do |config|
5
+ config.access_token = ENV['ROLLBAR_TOKEN']
6
+ config.environment = ENV['CFN_ENVIRONMENT'] || 'development'
7
+ config.enabled = config.environment != 'test'
8
+ end
9
+
10
+ module CloudFormation
11
+ module Bridge
12
+
13
+ class StdoutExceptionNotifier
14
+
15
+ include Singleton
16
+
17
+ def report_exception(exception, custom_data = {}, user_data = {})
18
+ puts "#{exception.message} - #{custom_data.inspect} - #{user_data.inspect}\n#{exception.backtrace.join("\n")}"
19
+ end
20
+
21
+ end
22
+
23
+ class RollbarExceptionNotifier
24
+
25
+ include Singleton
26
+
27
+ def report_exception(exception, custom_data = {}, user_data = {})
28
+ Rollbar.report_exception(exception, custom_data, user_data)
29
+ end
30
+
31
+ end
32
+
33
+ class ExceptionNotifier
34
+
35
+ class << self
36
+
37
+ def notifier
38
+ @notifier ||= if ENV['ROLLBAR_TOKEN']
39
+ RollbarExceptionNotifier.instance
40
+ else
41
+ StdoutExceptionNotifier.instance
42
+ end
43
+ end
44
+
45
+ def notifier=(notifier)
46
+ @notifier = notifier
47
+ end
48
+
49
+ def report_exception(exception, custom_data = {}, user_data = {})
50
+ Rollbar.report_exception(exception, custom_data, user_data)
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+
58
+ end
59
+ end
@@ -0,0 +1,50 @@
1
+ require 'cloud_formation/bridge/exception_notifier'
2
+ require 'cloud_formation/bridge/names'
3
+ require 'cloud_formation/bridge/resources/subscribe_queue_to_topic'
4
+ require 'cloud_formation/bridge/resources/cloud_formation_outputs'
5
+
6
+ module CloudFormation
7
+ module Bridge
8
+ class Executor
9
+
10
+ include CloudFormation::Bridge::Names
11
+
12
+ DEFAULT_REGISTRY = {
13
+ "Custom::SubscribeSQSQueueToSNSTopic" =>
14
+ CloudFormation::Bridge::Resources::SubscribeQueueToTopic.new,
15
+ "Custom::CloudFormationOutputs" =>
16
+ CloudFormation::Bridge::Resources::CloudFormationOutputs.new,
17
+ }
18
+
19
+ attr_reader :registry
20
+
21
+ def initialize(registry = DEFAULT_REGISTRY)
22
+ @registry = registry
23
+ end
24
+
25
+ def execute(request)
26
+
27
+ begin
28
+ if resource = registry[request.resource_type]
29
+ response = if request.create?
30
+ resource.create(request)
31
+ elsif request.update?
32
+ resource.update(request)
33
+ else
34
+ resource.delete(request)
35
+ end
36
+
37
+ request.succeed!(response)
38
+ else
39
+ request.fail!("Don't know what to do with resource #{request.resource_type}")
40
+ end
41
+ rescue => ex
42
+ ExceptionNotifier.report_exception(ex, request.request)
43
+ request.fail!(ex.message)
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,30 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'typhoeus'
4
+ require 'typhoeus/adapters/faraday'
5
+
6
+ module CloudFormation
7
+ module Bridge
8
+ class HttpBridge
9
+
10
+ class << self
11
+
12
+ def put(url, data)
13
+ connection = Faraday.new do |f|
14
+ f.request :json
15
+ f.request :retry, max: 2, interval: 0.05, interval_randomness: 0.5, backoff_factor: 2
16
+
17
+ f.response :raise_error
18
+ f.response :json, content_type: /javascript|json/
19
+
20
+ f.adapter :typhoeus
21
+ end
22
+
23
+ connection.put(url, data, 'Content-Type' => '')
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ module CloudFormation
2
+ module Bridge
3
+ module Names
4
+
5
+ module TYPES
6
+ UPDATE = 'Update'
7
+ CREATE = 'Create'
8
+ DELETE = 'Delete'
9
+ ALL = [UPDATE, CREATE, DELETE]
10
+ end
11
+
12
+ module RESULTS
13
+ SUCCESS = 'SUCCESS'
14
+ FAILED = 'FAILED'
15
+ end
16
+
17
+ module FIELDS
18
+ REQUEST_TYPE = 'RequestType'
19
+ RESPONSE_URL = 'ResponseURL'
20
+ STACK_ID = 'StackId'
21
+ REQUEST_ID = 'RequestId'
22
+ RESOURCE_TYPE = 'ResourceType'
23
+ LOGICAL_RESOURCE_ID = 'LogicalResourceId'
24
+ PHYSICAL_RESOURCE_ID = 'PhysicalResourceId'
25
+ RESOURCE_PROPERTIES = 'ResourceProperties'
26
+ OLD_RESOURCE_PROPERTIES = 'OldResourceProperties'
27
+ STATUS = 'Status'
28
+ REASON = 'Reason'
29
+ DATA = 'Data'
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,67 @@
1
+ require 'cloud_formation/bridge/executor'
2
+ require 'cloud_formation/bridge/exception_notifier'
3
+ require 'cloud_formation/bridge/request'
4
+ require 'logger'
5
+
6
+ module CloudFormation
7
+ module Bridge
8
+ class Poller
9
+
10
+ attr_reader :logger, :running
11
+
12
+ def initialize(queue_name, executor = CloudFormation::Bridge::Executor.new, logger = Logger.new(STDOUT))
13
+ @queue_name = queue_name
14
+ @executor = executor
15
+ @logger = logger
16
+ end
17
+
18
+ def start
19
+ @running = true
20
+ while @running
21
+ poll
22
+ end
23
+ end
24
+
25
+ def stop
26
+ @running = false
27
+ end
28
+
29
+ def poll
30
+ message = queue.receive_message
31
+
32
+ return unless message
33
+
34
+ begin
35
+ logger.info("Received message #{message.id} - #{message.body}")
36
+ body = JSON.parse(message.body)
37
+ request = CloudFormation::Bridge::Request.new(JSON.parse(body["Message"]))
38
+ @executor.execute(request)
39
+ message.delete
40
+ logger.info("Processed message #{message.id}")
41
+ message
42
+ rescue => ex
43
+ logger.info("Failed to process message #{message.id} - #{ex.message}")
44
+ ExceptionNotifier.report_exception(ex,
45
+ message: message.body,
46
+ handle: message.handle,
47
+ id: message.id,
48
+ queue: @queue_name,
49
+ )
50
+ end
51
+ end
52
+
53
+ def visible_messages
54
+ queue.visible_messages
55
+ end
56
+
57
+ def queue
58
+ @queue ||= sqs.queues.named(@queue_name)
59
+ end
60
+
61
+ def sqs
62
+ @sqs ||= AWS::SQS.new
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,94 @@
1
+ require 'securerandom'
2
+ require 'cloud_formation/bridge/http_bridge'
3
+ require 'cloud_formation/bridge/names'
4
+
5
+ module CloudFormation
6
+ module Bridge
7
+ class Request
8
+
9
+ include CloudFormation::Bridge::Names
10
+
11
+ attr_reader :request
12
+
13
+ def initialize(request)
14
+ @request = request
15
+ end
16
+
17
+ def update?
18
+ request_type == TYPES::UPDATE
19
+ end
20
+
21
+ def create?
22
+ request_type == TYPES::CREATE
23
+ end
24
+
25
+ def delete?
26
+ request_type == TYPES::DELETE
27
+ end
28
+
29
+ def request_type
30
+ request[FIELDS::REQUEST_TYPE]
31
+ end
32
+
33
+ def request_url
34
+ request[FIELDS::RESPONSE_URL]
35
+ end
36
+
37
+ def stack_id
38
+ request[FIELDS::STACK_ID]
39
+ end
40
+
41
+ def request_id
42
+ request[FIELDS::REQUEST_ID]
43
+ end
44
+
45
+ def resource_type
46
+ request[FIELDS::RESOURCE_TYPE]
47
+ end
48
+
49
+ def logical_resource_id
50
+ request[FIELDS::LOGICAL_RESOURCE_ID]
51
+ end
52
+
53
+ def physical_resource_id
54
+ request[FIELDS::PHYSICAL_RESOURCE_ID]
55
+ end
56
+
57
+ def resource_properties
58
+ request[FIELDS::RESOURCE_PROPERTIES]
59
+ end
60
+
61
+ def old_resource_properties
62
+ request[FIELDS::OLD_RESOURCE_PROPERTIES]
63
+ end
64
+
65
+ def fail!(message)
66
+ response = build_response(
67
+ FIELDS::REASON => message,
68
+ FIELDS::STATUS => RESULTS::FAILED
69
+ )
70
+
71
+ HttpBridge.put(request_url, response)
72
+ end
73
+
74
+ def succeed!(response)
75
+ HttpBridge.put(request_url, build_response(response || {}))
76
+ end
77
+
78
+ def build_response(response = {})
79
+ {
80
+ FIELDS::STATUS => RESULTS::SUCCESS,
81
+ FIELDS::PHYSICAL_RESOURCE_ID => response[FIELDS::PHYSICAL_RESOURCE_ID] || physical_resource_id || generate_physical_id,
82
+ FIELDS::STACK_ID => stack_id,
83
+ FIELDS::REQUEST_ID => request_id,
84
+ FIELDS::LOGICAL_RESOURCE_ID => logical_resource_id,
85
+ }.merge(response)
86
+ end
87
+
88
+ def generate_physical_id
89
+ "#{logical_resource_id}-#{SecureRandom.uuid}"
90
+ end
91
+
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,31 @@
1
+ require 'cloud_formation/bridge/names'
2
+
3
+ module CloudFormation
4
+ module Bridge
5
+ module Resources
6
+
7
+ class Base
8
+ include CloudFormation::Bridge::Names
9
+
10
+ def require_fields(request, fields)
11
+ empty_fields = fields.select do |field|
12
+ request.resource_properties[field].nil? ||
13
+ request.resource_properties[field].strip.empty?
14
+ end
15
+
16
+ unless empty_fields.empty?
17
+ raise ArgumentError.new("The fields #{empty_fields.inspect} are required for this resource")
18
+ end
19
+
20
+ end
21
+
22
+ def update(request)
23
+ raise CloudFormation::Bridge::OperationNotImplementedError.new(
24
+ "The resource #{self.class.name} does not implement the update operation - #{request.inspect}")
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ require 'aws/cloud_formation'
2
+ require 'cloud_formation/bridge/resources/base'
3
+
4
+ module CloudFormation
5
+ module Bridge
6
+ module Resources
7
+
8
+ class CloudFormationOutputs < Base
9
+
10
+ NAME = 'Name'
11
+
12
+ def create(request)
13
+ require_fields(request, [NAME])
14
+
15
+ stack_name = request.resource_properties[NAME]
16
+
17
+ stack = stacks[stack_name]
18
+
19
+ outputs = stack.outputs.inject({}) do |acc,output|
20
+ acc[output.key] = output.value
21
+ acc
22
+ end
23
+
24
+ {
25
+ FIELDS::DATA => outputs,
26
+ FIELDS::PHYSICAL_RESOURCE_ID => stack.stack_id,
27
+ }
28
+ end
29
+
30
+ alias_method :update, :create
31
+
32
+ def delete(request)
33
+ # no need to do anything here
34
+ end
35
+
36
+ def stacks
37
+ @stacks ||= AWS::CloudFormation.new.stacks
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,66 @@
1
+ require 'cloud_formation/bridge/resources/base'
2
+ require 'aws/sns'
3
+ require 'aws/sqs'
4
+
5
+ module CloudFormation
6
+ module Bridge
7
+ module Resources
8
+
9
+ class SubscribeQueueToTopic < Base
10
+
11
+ ARN = 'Arn'
12
+ ENDPOINT = 'Endpoint'
13
+ PROTOCOL = 'Protocol'
14
+
15
+ TOPIC_ARN = 'TopicArn'
16
+ QUEUE_NAME = 'QueueName'
17
+
18
+ REQUIRED_FIELDS = [
19
+ TOPIC_ARN,
20
+ QUEUE_NAME,
21
+ ]
22
+
23
+ def create(request)
24
+ require_fields(request, REQUIRED_FIELDS)
25
+
26
+ queue = queues.named(request.resource_properties[QUEUE_NAME])
27
+ topic = topics[request.resource_properties[TOPIC_ARN]]
28
+
29
+ subscription = topic.subscribe(queue)
30
+
31
+ {
32
+ FIELDS::PHYSICAL_RESOURCE_ID => subscription.arn,
33
+ FIELDS::DATA => {
34
+ ARN => subscription.arn,
35
+ ENDPOINT => subscription.endpoint,
36
+ PROTOCOL => subscription.protocol,
37
+ },
38
+ }
39
+ end
40
+
41
+ def delete(request)
42
+ subscription = subscriptions[request.physical_resource_id]
43
+ subscription.unsubscribe if subscription && subscription.exists?
44
+ end
45
+
46
+ def topics
47
+ @topics ||= sns.topics
48
+ end
49
+
50
+ def subscriptions
51
+ @subscriptions ||= sns.subscriptions
52
+ end
53
+
54
+ def sns
55
+ @sns ||= AWS::SNS.new
56
+ end
57
+
58
+ def queues
59
+ @queues ||= AWS::SQS.new.queues
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ module CloudFormation
2
+ module Bridge
3
+ VERSION = "0.0.1"
4
+ end
5
+ end