alert_logic 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe AlertLogic, '.logger' do
4
+ before(:each) do
5
+ AlertLogic.logger = nil
6
+ @logger = AlertLogic.logger
7
+ end
8
+
9
+ context 'defaults' do
10
+ subject { @logger }
11
+ it { should be_instance_of(Logger) }
12
+ it { should_not be_nil }
13
+ # ensure that it returns the same instance every time
14
+ 3.times { it { should be(AlertLogic.logger) } }
15
+ end
16
+
17
+ it 'should accept a file path and return a Logger instance' do
18
+ logger = AlertLogic.logger('/tmp/file')
19
+ logger.should be_instance_of(Logger)
20
+ logger.instance_variable_get(:@logdev).filename.should eq('/tmp/file')
21
+ end
22
+
23
+ it 'should automaticall use Chefs logger if present' do
24
+ module Chef; module Log; def logger; end; end; end
25
+ chef_log = double
26
+ Chef::Log.stub(:logger).and_return(chef_log)
27
+ AlertLogic.logger = nil
28
+ AlertLogic.logger.should be(chef_log)
29
+ end
30
+
31
+ it 'should accept and set an instance as the logger' do
32
+ log = Logger.new($stdout)
33
+ AlertLogic.logger = log
34
+ AlertLogic.logger.should eq(log)
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ require 'spec_helper'
@@ -0,0 +1 @@
1
+ require 'spec_helper'
@@ -0,0 +1 @@
1
+ require 'spec_helper'
@@ -0,0 +1 @@
1
+ require 'spec_helper'
@@ -0,0 +1 @@
1
+ require 'spec_helper'
@@ -0,0 +1,80 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'alert_logic'
4
+ require 'dotenv'
5
+ require 'webmock/rspec'
6
+
7
+ require 'support/fake_alert_logic_api'
8
+
9
+ WebMock.disable_net_connect!(:allow_localhost => true)
10
+ Dotenv.load
11
+
12
+ def setup_policy
13
+ @rspec_policy = AlertLogic::Policy.find_by_id(ENV['POLICY'])
14
+ end
15
+
16
+ def setup_protected_host
17
+ @rspec_protected_host =
18
+ AlertLogic::Protected_host.find_by_id(ENV['PROTECTED_HOST'])
19
+ end
20
+
21
+ def setup_appliance
22
+ @rspec_appliance = AlertLogic::Appliance.find_by_id(ENV['APPLIANCE'])
23
+ end
24
+
25
+ module Test
26
+ class << self
27
+ attr_reader :appliance_name, :appliance_id
28
+ attr_reader :protected_host_name, :protected_host_id
29
+ attr_reader :policy_name, :policy_id
30
+ attr_reader :secret_key, :all_resources
31
+ end
32
+ @appliance_name = ENV['APPLIANCE_NAME']
33
+ @appliance_id = ENV['APPLIANCE']
34
+ @policy_name = ENV['POLICY_NAME']
35
+ @policy_id = ENV['POLICY']
36
+ @protected_host_name = ENV['PROTECTED_HOST_NAME']
37
+ @protected_host_id = ENV['PROTECTED_HOST']
38
+ @secret_key = ENV['SECRET_KEY']
39
+ @all_resources =
40
+ { 'protectedhost' => @protected_host_id,
41
+ 'appliance' => @appliance_id,
42
+ 'policy' => @policy_id
43
+ }
44
+ end
45
+
46
+ def setup_all
47
+ setup_env
48
+ setup_policy
49
+ setup_protected_host
50
+ setup_appliance
51
+ end
52
+
53
+ RSpec.configure do |rspec|
54
+ rspec.treat_symbols_as_metadata_keys_with_true_values = true
55
+ rspec.run_all_when_everything_filtered = true
56
+ rspec.filter_run :focus
57
+ rspec.order = 'random'
58
+
59
+ rspec.before(:each) do
60
+ stub_request(:any, %r{publicapi.alertlogic.net/api/tm/v1/})\
61
+ .to_rack(FakeAlertLogicApi)
62
+ end
63
+
64
+ # @example Create case hooks to setup common env in the before block
65
+ # describe "it configures a new Appliance", :env do
66
+ # it "has a matching id" do
67
+ # @rspec_appliance.id.should eq(ENV['APPLIANCE'])
68
+ # end
69
+ # end
70
+ [:protected_host, :appliance, :policy, :all].each do |hook|
71
+ rspec.before(:each, hook => true) do
72
+ eval("setup_#{hook.to_s}")
73
+ end
74
+ end
75
+
76
+ rspec.after(:all) do
77
+ AlertLogic.instance_variable_set(:@api_client, nil)
78
+ AlertLogic.instance_variable_set(:@secret_key, nil)
79
+ end
80
+ end
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ lib = File.expand_path('../', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ lib = File.expand_path('../../../lib/', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ require 'faraday'
8
+ require 'pry'
9
+ require 'alert_logic'
10
+ require 'dotenv'
11
+
12
+ Dotenv.load(File.expand_path('../../../.env', __FILE__))
13
+
14
+ options = {
15
+ :url => 'https://publicapi.alertlogic.net/api/tm/v1/',
16
+ :ssl => { :verify => false } }
17
+ headers = {
18
+ 'Accept' => 'application/json',
19
+ 'User-Agent' => "alert_logic gem v#{AlertLogic::VERSION}"
20
+ }
21
+ @faraday = Faraday.new(options) do |con|
22
+ con.adapter Faraday.default_adapter
23
+ con.headers = headers
24
+ con.basic_auth ENV['SECRET_KEY'], ''
25
+ end
26
+
27
+ AlertLogic.secret_key = ENV['SECRET_KEY']
28
+ @client = AlertLogic.api_client
29
+
30
+ ARGV.clear
31
+ puts "\e[H\e[2J"
32
+ Pry.config.prompt_name = 'alertlogic'
33
+ Pry.config.output = STDOUT
34
+ Pry.config.pager = true
35
+ Pry.config.hooks.add_hook(:before_session, :set_context) do |_, _, pry|
36
+ pry.input = StringIO.new('cd @client')
37
+ end
38
+ Pry.start
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+ lib = File.expand_path('../../../lib/', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'dotenv'
6
+ require 'faraday'
7
+ require 'json'
8
+ require 'pry-debugger'
9
+ require 'logger'
10
+
11
+ Dotenv.load(File.expand_path('../../../.env', __FILE__))
12
+
13
+ def pluralize(resource)
14
+ resource =~ /^\w+y$/ ? resource.sub(/y$/, 'ies') : "#{resource}s"
15
+ end
16
+
17
+ options = {
18
+ :url => 'https://publicapi.alertlogic.net/api/tm/v1/',
19
+ :ssl => { :verify => false } }
20
+ headers = {
21
+ 'Accept' => 'application/json',
22
+ 'User-Agent' => 'alert_logic gem TEST'
23
+ }
24
+ @client = Faraday.new(options) do |con|
25
+ con.use Faraday::Response::Logger, Logger.new($stdout)
26
+ con.use Faraday::Response::RaiseError
27
+ con.adapter Faraday.default_adapter
28
+ con.headers = headers
29
+ con.basic_auth ENV['SECRET_KEY'], ''
30
+ end
31
+
32
+ # Get requests don't change resources so we'll gather them all together
33
+ get_resources = [
34
+ ['protectedhost', nil, {}, 'get_protected_hosts.json'],
35
+ ['protectedhost', ENV['PROTECTED_HOST'], {}, 'get_protected_host.json'],
36
+ ['policy', nil, {}, 'get_policies.json'],
37
+ ['policy', ENV['POLICY'], {}, 'get_policy.json'],
38
+ ['appliance', nil, {}, 'get_appliances.json'],
39
+ ['appliance', ENV['APPLIANCE'], {}, 'get_appliance.json']
40
+ ]
41
+
42
+ get_resources.each do |r|
43
+ File.open(File.expand_path("../json/#{r[3]}", __FILE__), 'w') do |file|
44
+ res = @client.get do |req|
45
+ req.url r[1] ? "#{pluralize(r[0])}/#{r[1]}" : pluralize(r[0])
46
+ r[2].each { |k, v| req.params[k] = v }
47
+ end
48
+ file.write(res.body)
49
+ end
50
+ end
51
+
52
+ # TODO: automate post requests for testing
53
+
54
+ # Post requests change resources so we'll intentionall post existing values
55
+ # to prevent actually modifying policies
56
+
57
+ # protected_host_
58
+ # post_resource = [
59
+ # ['protectedhost', ENV['PROTECTED_HOST'],
@@ -0,0 +1,39 @@
1
+ require 'sinatra/base'
2
+
3
+ class FakeAlertLogicApi < Sinatra::Base
4
+ get '/api/tm/v1/protectedhosts' do
5
+ api_response 200, 'get_protected_hosts.json'
6
+ end
7
+
8
+ get '/api/tm/v1/protectedhosts/*' do
9
+ api_response 200, 'get_protected_host.json'
10
+ end
11
+
12
+ get '/api/tm/v1/appliances' do
13
+ api_response 200, 'get_appliances.json'
14
+ end
15
+
16
+ get '/api/tm/v1/appliances/*' do
17
+ api_response 200, 'get_appliance.json'
18
+ end
19
+
20
+ get '/api/tm/v1/policies' do
21
+ api_response 200, 'get_policies.json'
22
+ end
23
+
24
+ get '/api/tm/v1/policies/*' do
25
+ api_response 200, 'get_policy.json'
26
+ end
27
+
28
+ post '/api/tm/v1/protectedhosts/*' do
29
+ api_response 200, 'post_protected_host.json'
30
+ end
31
+
32
+ private
33
+
34
+ def api_response(code, json_file)
35
+ content_type :json
36
+ status code
37
+ File.read(File.expand_path("../json/#{json_file}", __FILE__))
38
+ end
39
+ end
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ lib = File.expand_path('../', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ lib = File.expand_path('../../../lib/', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
7
+ require 'webmock'
8
+ require 'alert_logic'
9
+ require 'fake_alert_logic_api'
10
+
11
+ include WebMock::API
12
+
13
+ WebMock.disable_net_connect!(:allow_localhost => true)
14
+
15
+ stub_request(:any, /https:\/\/\h{50}:@publicapi.alertlogic.net\/api\/tm\/v1\//)
16
+ .to_rack(FakeAlertLogicApi)
17
+
18
+ require 'api_console'
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe AlertLogic::Utils, '.pluralize' do
4
+ before do
5
+ class UtilTest; include AlertLogic::Utils; end
6
+ end
7
+ let(:utils) { UtilTest.new }
8
+
9
+ it 'pluralizes words that dont end in y' do
10
+ utils.send(:pluralize, 'appliance').should eq('appliances')
11
+ utils.send(:pluralize, 'host').should eq('hosts')
12
+ utils.send(:pluralize, 'protected_host').should eq('protected_hosts')
13
+ end
14
+
15
+ it 'pluralizes words that end in y' do
16
+ utils.send(:pluralize, 'angry').should eq('angries')
17
+ utils.send(:pluralize, 'policy').should eq('policies')
18
+ utils.send(:pluralize, 'catastrophy').should eq('catastrophies')
19
+ end
20
+
21
+ it 'appends ies to trailing y in words with multiple ys' do
22
+ utils.send(:pluralize, 'anyway').should eq('anywaies')
23
+ utils.send(:pluralize, 'byway').should eq('bywaies')
24
+ utils.send(:pluralize, 'layaway').should eq('layawaies')
25
+ utils.send(:pluralize, 'yawey').should eq('yaweies')
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alert_logic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Cragun
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A feature rich API client for AlertLogic Threat Manager
28
+ email:
29
+ - ryan@rightscale.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - Gemfile
38
+ - Guardfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - alert_logic.gemspec
43
+ - lib/alert_logic.rb
44
+ - lib/alert_logic/client.rb
45
+ - lib/alert_logic/client/base_client.rb
46
+ - lib/alert_logic/client/rest_methods.rb
47
+ - lib/alert_logic/log.rb
48
+ - lib/alert_logic/resources.rb
49
+ - lib/alert_logic/resources/appliance.rb
50
+ - lib/alert_logic/resources/base_resource.rb
51
+ - lib/alert_logic/resources/filters.rb
52
+ - lib/alert_logic/resources/policy.rb
53
+ - lib/alert_logic/resources/protected_host.rb
54
+ - lib/alert_logic/utils.rb
55
+ - lib/alert_logic/version.rb
56
+ - spec/alert_logic_spec.rb
57
+ - spec/client/base_client_spec.rb
58
+ - spec/client/rest_methods_spec.rb
59
+ - spec/log_spec.rb
60
+ - spec/resources/appliance_spec.rb
61
+ - spec/resources/base_resource_spec.rb
62
+ - spec/resources/filters_spec.rb
63
+ - spec/resources/policy_spec.rb
64
+ - spec/resources/protected_host_spec.rb
65
+ - spec/spec_helper.rb
66
+ - spec/support/api_console.rb
67
+ - spec/support/build_json_responses.rb
68
+ - spec/support/fake_alert_logic_api.rb
69
+ - spec/support/fake_api_console.rb
70
+ - spec/utils_spec.rb
71
+ homepage: https://github.com/ryancragun/alert_logic-gem
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: A feature rich API client for AlertLogic Threat Manager
95
+ test_files:
96
+ - spec/alert_logic_spec.rb
97
+ - spec/client/base_client_spec.rb
98
+ - spec/client/rest_methods_spec.rb
99
+ - spec/log_spec.rb
100
+ - spec/resources/appliance_spec.rb
101
+ - spec/resources/base_resource_spec.rb
102
+ - spec/resources/filters_spec.rb
103
+ - spec/resources/policy_spec.rb
104
+ - spec/resources/protected_host_spec.rb
105
+ - spec/spec_helper.rb
106
+ - spec/support/api_console.rb
107
+ - spec/support/build_json_responses.rb
108
+ - spec/support/fake_alert_logic_api.rb
109
+ - spec/support/fake_api_console.rb
110
+ - spec/utils_spec.rb