cloudpassage 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eaf01ba7692899c65548276989de4dc78c0f41a4
4
- data.tar.gz: 7d3c5caafdcbbfaccb6d773cbb237a96d70d73ee
3
+ metadata.gz: 2f885d2347066b179712e43dae23e79c82f8a2f5
4
+ data.tar.gz: b0259a907cc18cdcfce0233ee452fb1465a2ad4e
5
5
  SHA512:
6
- metadata.gz: 99122f68fa2bcefd3765a5d650d4b719c27759bbb3081c2fa7e6e2fdaeca6c668e283b278e30ba80e9ec2bf38dea3392b64a4687cb0279222993ac26328cbee6
7
- data.tar.gz: f664529308ac909bcab4c4d14b72c46a3a91295aa43b6d508a10d708e4d43384daea2d7ffa7c0285b6eecbd5c43642e47401cf60697814dd384dff24cdf1df62
6
+ metadata.gz: abd4ddf4ef561a50d17ca25fb4cf62e84edff52f95066313585c9c92a62460edcd20ba46a7dfd6d478a52e1c2286c031cce42b6a1f5a906d2a014ca4bbde7319
7
+ data.tar.gz: 167df14e02f8736cd0d58023c0372ec3d5d0a579c9f46dbcd3a8950de805ee476f0c1ab1017dd10c107c98262bc6add2289cc7081c309c15ca5c68831bcc7a56
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ .vendor
1
2
  vendor
2
3
  .idea
3
4
  *.gem
data/.pryrc CHANGED
@@ -1,2 +1,2 @@
1
1
  require 'cloudpassage/pry'
2
- include Cloudpassage::Pry
2
+ extend Cloudpassage::Pry
data/README.md CHANGED
@@ -49,6 +49,18 @@ Or install it yourself as:
49
49
  puts api.servers.all[0].accounts.all
50
50
  puts api.servers.all[0].issues.all
51
51
  puts api.server_groups.all[0].groups.all
52
+
53
+ # Add/move a server to a group (groups are case-sensitive)
54
+ api.servers.get('some_id').group='some_group_id'
55
+
56
+ # List servers by passing in state (missing, deactivated, active)
57
+ api.servers.filter(:state=>'missing')
58
+
59
+ # Delete a server permanently
60
+ api.servers.get('id').destroy
61
+
62
+ # Retire a 'inactive' server
63
+ api.servers.get('id').retire
52
64
 
53
65
  # Get specific things.
54
66
  puts api.file_integrity_policies.get('file_integrity_policy_id').data.to_json
@@ -1,11 +1,13 @@
1
1
  module Cloudpassage
2
2
 
3
- BASE_URL='https://api.cloudpassage.com/v1/'
3
+ BASE_URL = 'https://api.cloudpassage.com/v1/'
4
4
 
5
+ # Obtains a new API object by generating a token using id + secret.
5
6
  def self.api(id, secret)
6
7
  Api.new(self.token(id, secret))
7
8
  end
8
9
 
10
+ # Interface to all cloudpassage stuff
9
11
  class Api
10
12
  attr_reader :token, :base_resource
11
13
 
@@ -14,42 +16,52 @@ module Cloudpassage
14
16
  @base_resource = RestClient::Resource.new(BASE_URL)
15
17
  end
16
18
 
19
+ # Configuration policies
17
20
  def configuration_policies
18
21
  Policies.new(token, base_resource['policies'])
19
22
  end
20
23
 
24
+ # Events
21
25
  def events
22
26
  Events.new(token, base_resource['events'])
23
27
  end
24
28
 
29
+ # File integrity policies
25
30
  def file_integrity_policies
26
31
  FimPolicies.new(token, base_resource['fim_policies'])
27
32
  end
28
33
 
34
+ # Firewall interfaces
29
35
  def firewall_interfaces
30
36
  FirewallInterfaces.new(token, base_resource['firewall_interfaces'])
31
37
  end
32
38
 
39
+ # Firewall policies
33
40
  def firewall_policies
34
41
  FirewallPolicies.new(token, base_resource['firewall_policies'])
35
42
  end
36
43
 
44
+ # Firewall services
37
45
  def firewall_services
38
46
  FirewallServices.new(token, base_resource['firewall_services'])
39
47
  end
40
48
 
49
+ # Firewall zones
41
50
  def firewall_zones
42
51
  FirewallZones.new(token, base_resource['firewall_zones'])
43
52
  end
44
53
 
54
+ # Servers
45
55
  def servers
46
56
  Servers.new(token, base_resource['servers'])
47
57
  end
48
58
 
59
+ # Groups
49
60
  def server_groups
50
61
  Groups.new(token, base_resource['groups'])
51
62
  end
52
63
 
64
+ # Users
53
65
  def users
54
66
  Users.new(token, base_resource['users'])
55
67
  end
@@ -1,6 +1,8 @@
1
1
  require 'wait'
2
2
 
3
3
  module Cloudpassage
4
+
5
+ # Default options to use when waiting.
4
6
  def self.wait_options
5
7
  {
6
8
  :attempts => 50000,
@@ -10,6 +12,7 @@ module Cloudpassage
10
12
  }
11
13
  end
12
14
 
15
+ # Base class representing cloudpassage objects.
13
16
  class Base
14
17
  def initialize(token, base_resource, data=nil)
15
18
  @token = token
@@ -17,6 +20,8 @@ module Cloudpassage
17
20
  @data = data
18
21
  end
19
22
 
23
+ # Return data from sending http GET to underlying resource.
24
+ # Uses cached value if resource has been retrieved already.
20
25
  def data
21
26
  if @data.nil?
22
27
  @data = JSON.parse(@base_resource.get(headers), :symbolize_names=>true)[object_symbol]
@@ -33,6 +38,7 @@ module Cloudpassage
33
38
  {'Authorization'=>"Bearer #{@token}"}
34
39
  end
35
40
 
41
+ # If method is missing, try to pass through to underlying data hash.
36
42
  def method_missing(sym, *args, &block)
37
43
  if (data && data[sym])
38
44
  data[sym]
@@ -58,9 +64,12 @@ module Cloudpassage
58
64
  def object_symbol
59
65
  class_name = self.class.name
60
66
  index = class_name.rindex(/::/)
61
- class_name[index+2..-1].underscore.to_sym
67
+ class_name[index + 2 .. -1].underscore.to_sym
62
68
  end
63
69
 
70
+ # Wait for block to evaluate to true.
71
+ # If specified, options can be used to override default options.
72
+ # Options should conform to https://rubygems.org/gems/wait
64
73
  def wait_for(options={}, &block)
65
74
  Wait.new(Cloudpassage::wait_options.merge(options)).until do
66
75
  reload
@@ -5,6 +5,7 @@ require 'json'
5
5
  module Cloudpassage
6
6
  RestClient.proxy = ENV['HTTPS_PROXY'] if ENV['HTTPS_PROXY'].to_s.length > 0
7
7
 
8
+ # Obtain an oauth token using id and secret
8
9
  def self.token(client_id, client_secret)
9
10
  client = OAuth2::Client.new(client_id, client_secret,
10
11
  :connection_opts => { :proxy => ENV['HTTPS_PROXY'] },
@@ -16,11 +17,12 @@ end
16
17
 
17
18
  # Add camel-to-snake-case conversion
18
19
  class String
20
+
21
+ # returns camel-case representation of snake-case string.
19
22
  def underscore
20
23
  self.gsub(/::/, '/').
21
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
22
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
23
- tr("-", "_").
24
- downcase
24
+ gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
25
+ gsub(/([a-z\d])([A-Z])/, '\1_\2').
26
+ tr('-', '_').downcase
25
27
  end
26
28
  end
@@ -1,11 +1,11 @@
1
1
  require 'json'
2
2
 
3
3
  module Cloudpassage
4
+
5
+ # Collections of objects.
4
6
  module Collection
5
7
  def all
6
- data.map{|i|
7
- get(i[:id], i)
8
- }
8
+ data.map { |i| get(i[:id], i) }
9
9
  end
10
10
 
11
11
  def get(id, data=nil)
@@ -1,4 +1,6 @@
1
1
  module Cloudpassage
2
+
3
+ # Events
2
4
  class Events < Base
3
5
  include Collection
4
6
 
@@ -1,4 +1,5 @@
1
1
  module Cloudpassage
2
+ # File Interity Baselines
2
3
  class FileIntegrityBaselines < Base
3
4
  include Collection
4
5
 
@@ -1,4 +1,5 @@
1
1
  module Cloudpassage
2
+ # FimPolicies
2
3
  class FimPolicies < Base
3
4
  include Collection
4
5
 
@@ -1,4 +1,6 @@
1
1
  module Cloudpassage
2
+
3
+ # Firewall Interfaces
2
4
  class FirewallInterfaces < Base
3
5
  include Collection
4
6
  def singleton_class
@@ -1,4 +1,6 @@
1
1
  module Cloudpassage
2
+
3
+ # Firewall Policies
2
4
  class FirewallPolicies < Base
3
5
  include Collection
4
6
 
@@ -1,4 +1,6 @@
1
1
  module Cloudpassage
2
+
3
+ # Firewall Rules
2
4
  class FirewallRules < Base
3
5
  include Collection
4
6
 
@@ -1,4 +1,6 @@
1
1
  module Cloudpassage
2
+
3
+ # Firewall Services
2
4
  class FirewallServices < Base
3
5
  include Collection
4
6
 
@@ -1,4 +1,6 @@
1
1
  module Cloudpassage
2
+
3
+ # Firewall Zones
2
4
  class FirewallZones < Base
3
5
  include Collection
4
6
 
@@ -1,4 +1,6 @@
1
1
  module Cloudpassage
2
+
3
+ # Groups
2
4
  class Groups < Base
3
5
  include Collection
4
6
  def singleton_class
@@ -1,4 +1,6 @@
1
1
  module Cloudpassage
2
+
3
+ # Policies
2
4
  class Policies < Base
3
5
  include Collection
4
6
 
@@ -1,21 +1,22 @@
1
1
  require 'yaml'
2
2
  require 'cloudpassage'
3
3
 
4
+ # Module to make it easy to use stuff from pry (or any sort of interactive debugger)
4
5
  module Cloudpassage::Pry
5
6
  def cloudpassage(type)
6
- type_section = section(type)
7
+ type_section = Cloudpassage::Pry::section(type)
7
8
  Cloudpassage::Api.new(Cloudpassage.token(type_section['id'], type_section['secret']))
8
9
  end
9
10
 
10
- def section(type)
11
- yaml[type.to_s]
11
+ def self.section(type)
12
+ yaml.fetch(type.to_s)
12
13
  end
13
14
 
14
- def yaml
15
- @@yaml ||= YAML.load(File.read(config_file))
15
+ def self.yaml
16
+ @yaml ||= YAML.load(File.read(config_file))
16
17
  end
17
18
 
18
- def config_file
19
+ def self.config_file
19
20
  ENV.fetch('CLOUDPASSAGE_CONFIG_FILE', "#{ENV['HOME']}/.cloudpassagerc")
20
21
  end
21
22
  end
@@ -2,9 +2,15 @@ module Cloudpassage
2
2
  class Servers < Base
3
3
  include Collection
4
4
 
5
+ def filter(options={})
6
+ servers = JSON.parse(@base_resource.get(headers.merge(:params=>options)), :symbolize_names=>true)[:servers]
7
+ servers.map { |i| get(i[:id], i) }
8
+ end
9
+
5
10
  def singleton_class
6
11
  Server
7
12
  end
13
+
8
14
  end
9
15
 
10
16
  class Server < Single
@@ -21,6 +27,17 @@ module Cloudpassage
21
27
  Accounts.new(self, @token, @base_resource['accounts'])
22
28
  end
23
29
 
30
+ def retire
31
+ # retire a 'inactive' server
32
+ payload = {"server"=>{"retire"=>true}}
33
+ @base_resource.put(payload.to_json, headers.merge(:content_type => :json))
34
+ end
35
+
36
+ def group=(group_id)
37
+ # add server to group
38
+ payload = {"server"=>{"group_id"=>group_id}}
39
+ @base_resource.put(payload.to_json, headers.merge(:content_type => :json))
40
+ end
24
41
 
25
42
  def commands
26
43
  Commands.new(@token, @base_resource['commands'])
@@ -31,6 +48,7 @@ module Cloudpassage
31
48
  end
32
49
  end
33
50
 
51
+ # Accounts on a server
34
52
  class Accounts < Base
35
53
  include Collection
36
54
 
@@ -44,9 +62,12 @@ module Cloudpassage
44
62
  end
45
63
 
46
64
  def all
47
- data.map{|i| get(i[:username], i)}
65
+ data.map { |i| get(i[:username], i) }
48
66
  end
49
67
 
68
+ # Creates username, in the given group.
69
+ # If opts[:password] is specified, that password will be used.
70
+ # Otherwise, password will be generated.
50
71
  def create(username, groups = '', opts = {})
51
72
  payload = {'account' => {
52
73
  :username => username,
@@ -1,6 +1,8 @@
1
1
  require 'json'
2
2
 
3
3
  module Cloudpassage
4
+
5
+ # Single objects.
4
6
  class Single < Base
5
7
 
6
8
  attr_reader :id
@@ -13,5 +15,9 @@ module Cloudpassage
13
15
  def get
14
16
  @base_resource.get(headers)
15
17
  end
18
+
19
+ def destroy
20
+ @base_resource.delete(headers)
21
+ end
16
22
  end
17
23
  end
@@ -1,4 +1,5 @@
1
1
  module Cloudpassage
2
+ # Users
2
3
  class Users < Base
3
4
  include Collection
4
5
 
@@ -1,3 +1,4 @@
1
1
  module Cloudpassage
2
- VERSION = '0.0.4'
2
+ # Version
3
+ VERSION = '0.0.6'
3
4
  end
data/spec/api_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require File.join(File.dirname(__FILE__), '..', 'lib', 'cloudpassage')
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'cloudpassage', 'pry')
2
3
  require 'rspec'
3
4
 
4
5
  module Cloudpassage
@@ -6,8 +7,7 @@ module Cloudpassage
6
7
  attr_reader :api
7
8
 
8
9
  before :each do
9
- token = Cloudpassage.token(ENV.fetch('CLOUDPASSAGE_ID'), ENV.fetch('CLOUDPASSAGE_SECRET'))
10
- @api = Cloudpassage::Api.new(token)
10
+ @api = Cloudpassage::Pry::cloudpassage(:test)
11
11
  end
12
12
 
13
13
  def self.collections
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudpassage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - mshea
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-11 00:00:00.000000000 Z
11
+ date: 2014-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake