Permalink
Browse files

Add some logging. Pull in scrolls. ✌️

  • Loading branch information...
1 parent f487976 commit ef3720f165f86a5bbb630babfbbb8fffbe00060a @dylanegan committed Apr 1, 2012
View
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
gem.add_dependency "clamp", "~> 0.3.0"
gem.add_dependency "fog", "~> 1.2.0"
gem.add_dependency "rake"
+ gem.add_dependency "scrolls", "~> 0.0.5"
gem.add_development_dependency "minitest"
end
View
@@ -4,6 +4,10 @@
require "fog/bouncer/sources"
require "fog/bouncer/version"
+require "scrolls"
+
+Scrolls::Log.start
+
module Fog
module Bouncer
def self.doorlists
@@ -19,14 +23,22 @@ def self.fog
)
end
+ def self.log(data, &block)
+ Scrolls.log({ 'fog-bouncer' => true }.merge(data), &block)
+ end
+
def self.load(file)
if file && File.exists?(file)
- instance_eval(File.read(file))
+ Fog::Bouncer.log(load: true, file: file) do
+ instance_eval(File.read(file))
+ end
end
end
def self.security(name, &block)
- doorlists[name] = Fog::Bouncer::Security.new(name, &block)
+ Fog::Bouncer.log(security: true, name: name) do
+ doorlists[name] = Fog::Bouncer::Security.new(name, &block)
+ end
end
class Security
View
@@ -3,6 +3,14 @@ module Bouncer
class Group
attr_reader :name, :description, :security
+ def self.log(data, &block)
+ Fog::Bouncer.log({ group: true }.merge(data), &block)
+ end
+
+ def log(data, &block)
+ self.class.log({ name: name }.merge(data), &block)
+ end
+
def initialize(name, description, security, &block)
@name = name
@description = description
@@ -11,9 +19,11 @@ def initialize(name, description, security, &block)
end
def clone(sources)
- clone = self.class.new(name, description, security)
- clone.sources = sources
- clone
+ log(clone: true) do
+ clone = self.class.new(name, description, security)
+ clone.sources = sources
+ clone
+ end
end
def sources
@@ -100,23 +110,32 @@ def remote
end
def sync
- create_missing_remote
- synchronize_sources
+ log(sync: true) do
+ create_missing_remote
+ synchronize_sources
+ end
end
def destroy_extras
if extras?
- remote.fog.connection.revoke_security_group_ingress(name, "IpPermissions" => extras.to_ip_permissions)
- remote.reload
+ log(destroy_extras: true) do
+ extra.log(removing: true)
+ remote.fog.connection.revoke_security_group_ingress(name, "IpPermissions" => extras.to_ip_permissions)
+ remote.reload
+ end
end
@extras = nil
end
def create_missing
if missing?
create_missing_remote
- remote.fog.connection.authorize_security_group_ingress(name, "IpPermissions" => missing.to_ip_permissions)
- remote.reload
+
+ log(create_missing: true) do
+ missing.log(creating: true)
+ remote.fog.connection.authorize_security_group_ingress(name, "IpPermissions" => missing.to_ip_permissions)
+ remote.reload
+ end
end
@missing = nil
end
@@ -125,14 +144,18 @@ def create_missing_remote
if remote
remote.reload
else
- Fog::Bouncer.fog.security_groups.create(:name => name, :description => description)
- remote = RemoteGroup.for(name, security)
+ log(create_missing_remote: true) do
+ Fog::Bouncer.fog.security_groups.create(:name => name, :description => description)
+ remote = RemoteGroup.for(name, security)
+ end
end
end
def synchronize_sources
- destroy_extras
- create_missing
+ log(synchronize_sources: true) do
+ destroy_extras
+ create_missing
+ end
end
end
@@ -179,14 +202,21 @@ def from(group)
end
def revoke
- fog.connection.revoke_security_group_ingress(name, "IpPermissions" => sources.to_ip_permissions) if sources.any?
+ if sources.any?
+ log(revoke: true) do
+ sources.log(revoking: true)
+ fog.connection.revoke_security_group_ingress(name, "IpPermissions" => sources.to_ip_permissions)
+ end
+ end
reload
end
def destroy
revoke
unless name == "default"
- fog.destroy
+ log(destroy: true) do
+ fog.destroy
+ end
end
end
@@ -1,7 +1,7 @@
module Fog
module Bouncer
class Protocol
- attr_reader :from, :to
+ attr_reader :from, :source, :to
def initialize(port, source)
if port.is_a?(Range)
@@ -32,6 +32,10 @@ def <=>(other)
def inspect
"<#{self.class.name} @from=#{from.inspect} @to=#{to.inspect}>"
end
+
+ def to_log
+ { source: source.source, protocol: type, from: from, to: to }
+ end
end
module Protocols
View
@@ -3,16 +3,26 @@ module Bouncer
class Source
attr_reader :group, :source
+ def self.log(data, &block)
+ Fog::Bouncer.log({ source: true }.merge(data), &block)
+ end
+
+ def log(data, &block)
+ self.class.log({ source: source }.merge(data), &block)
+ end
+
def initialize(source, group, &block)
@source = source
@group = group
instance_eval(&block) if block_given?
end
def clone(protocols)
- clone = self.class.new(source, group)
- clone.protocols = protocols
- clone
+ log(clone: true) do
+ clone = self.class.new(source, group)
+ clone.protocols = protocols
+ clone
+ end
end
def extras
View
@@ -3,6 +3,14 @@
module Fog
module Bouncer
class SourcesProxy < Array
+ def log(data)
+ each do |source|
+ source.protocols.each do |protocol|
+ Fog::Bouncer.log(data.merge(protocol.to_log))
+ end
+ end
+ end
+
def to_ip_permissions
permissions = []
@@ -38,14 +46,6 @@ class CIDR < Fog::Bouncer::Source
def range
@source
end
-
- def sync
- protocols.each do |type, rules|
- rule.each do |rule|
-
- end
- end
- end
end
class Group < Fog::Bouncer::Source
View
@@ -0,0 +1 @@
+*
@@ -60,8 +60,10 @@
it "detects groups with missing sources" do
source = Fog::Bouncer::Sources.for("2.2.2.2/2", @douchebag)
source.protocols << Fog::Bouncer::Protocols::TCP.new(90, source)
+ sources = Fog::Bouncer::SourcesProxy.new
+ sources << source
@douchebag.sources << source
- cloned_douchebag = @douchebag.clone([source])
+ cloned_douchebag = @douchebag.clone(sources)
@doorlist.missing.must_equal [cloned_douchebag, @guido]
end
end
View
@@ -6,6 +6,8 @@
require "fog/bouncer"
+Scrolls::Log.start(File.open(File.dirname(__FILE__) + '/../logs/test.log', 'w'))
+
def load_security(security)
Fog::Bouncer.load File.dirname(__FILE__) + "/support/security/#{security}.rb"
end
@@ -21,6 +23,8 @@ def load_security(security)
doorlist.remote_groups.each do |group|
group.destroy
end
+
+ doorlist.reset!
end
end

0 comments on commit ef3720f

Please sign in to comment.