bluecoat 0.0.4

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bluecoat.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Lothar Handl
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,67 @@
1
+ # Bluecoat
2
+
3
+ This gem provides an interface from ruby to Blue Coat Boxes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'bluecoat'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install bluecoat
18
+
19
+ ## Usage
20
+
21
+ require 'bluecoat'
22
+
23
+ ### connect to ProxySG Box
24
+ sg = BlueCoat::SG.new({
25
+ :host => "bluecoatsg.local",
26
+ :user => "admin",
27
+ :pass => "password"
28
+ })
29
+
30
+ ### fetch whole policy
31
+ policy = sg.fetch_policy
32
+
33
+ ### fetch a defined subnet
34
+ subnet = sg.fetch_subnet "subnet1"
35
+
36
+
37
+ ### connect to Reporter
38
+ bc = BlueCoat::Reporter.new({
39
+ :host => "bluecatreporter.local",
40
+ :user => "admin",
41
+ :pass => "password",
42
+ :role => "admin" # choose existant role on reporter
43
+ })
44
+
45
+ ### fetch a report from reporter through WEB API
46
+
47
+ Give parameters as specified by the Blue Coat Reporter WEB API. The result
48
+ is a two dimensional array rows/columns.
49
+
50
+ result = bc.fetch_report({
51
+ :database => "databasename",
52
+ :columns => "total_bytes",
53
+ :sort => "total_bytes",
54
+ :summarizeBy => "c_ip",
55
+ :rows => 100,
56
+ :dateRelativeUnit => :month,
57
+ :dateStart => -1,
58
+ :dateEnd => 0
59
+ })
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bluecoat.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/bluecoat/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Lothar Handl"]
6
+ gem.email = ["lothar.handl@3beg.at"]
7
+ gem.description = %q{A bundle of functions for accessing Blue Coat Appliances. I.e. Reporter, ProxySG, etc.}
8
+ gem.summary = %q{A bundle of functions for accessing Blue Coat Appliances.}
9
+ gem.homepage = "https://github.com/terracor/bluecoat"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "bluecoat"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Bluecoat::VERSION
17
+ gem.add_dependency(%q{mechanize})
18
+ gem.add_dependency(%q{ipaddress})
19
+ end
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env ruby
2
+ require 'open-uri'
3
+ require 'cgi'
4
+ require 'uri'
5
+ require 'csv'
6
+ require 'mechanize'
7
+
8
+
9
+ # the class is extended by a function to_query which converts the hash to an URI-String
10
+ class Hash
11
+ # converts hash to URI parameter string
12
+ def to_query
13
+ elements = []
14
+ keys.size.times do |i|
15
+ elements << "#{keys[i]}=#{values[i]}"
16
+ end
17
+ elements.join('&')
18
+ end
19
+ end
20
+
21
+
22
+ module BlueCoat
23
+ # this class provides methods to query blue coat reporter
24
+ class Reporter
25
+ # constructor
26
+ def initialize(options = {})
27
+ # concat URL-string
28
+ @connect = "https://#{options[:host]}:8082/api/"
29
+ @credentials = "?username=#{options[:user]}&password=#{options[:pass]}&role=#{options[:role]}"
30
+ end
31
+
32
+
33
+ # triggers the Blue Coat Reporter to send a PDF report by email.
34
+ def mail_report(params)
35
+ # default Parameter fuers Hash
36
+ params[:label] ||= "unspecified report"
37
+ file = open "#{@connect}create#{@credentials}&action=email&format=pdf&#{URI.escape(params.to_query, "| ")}", :proxy => nil
38
+ report_id = file.gets.split(":")[1]
39
+ file.close
40
+
41
+ # wait for report
42
+ status = ""
43
+ begin
44
+ sleep 1
45
+ file = open "#{@connect}status#{@credentials}&reportId=#{report_id}", :proxy => nil
46
+ file.gets
47
+ file.gets
48
+ status = file.gets.split(":")[1]
49
+ file.close
50
+ end while status == "Running\n"
51
+
52
+ # clear open report request from reporter
53
+ file = open "#{@connect}cancel#{@credentials}&reportId=#{report_id}", :proxy => nil
54
+ file.close
55
+ end
56
+
57
+
58
+ # fetches a report from the Blue Coat Reporter. The report is returned in a two-dimensional array.
59
+ # params are as by Blue Coat Reporter WEB API
60
+ def fetch_report(params)
61
+ # default Parameter fuers Hash
62
+ params[:label] ||= "unspecified report"
63
+
64
+ # start report
65
+ file = open "#{@connect}create#{@credentials}&action=download&format=csv&#{URI.escape(params.to_query, "| ")}", :proxy => nil
66
+ report_id = file.gets.split(":")[1]
67
+ file.close
68
+
69
+ # wait for report
70
+ status = ""
71
+ begin
72
+ sleep 1
73
+ file = open "#{@connect}status#{@credentials}&reportId=#{report_id}", :proxy => nil
74
+ file.gets
75
+ file.gets
76
+ status = file.gets.split(":")[1]
77
+ file.close
78
+ end while status == "Running\n"
79
+
80
+ # fetch ready report
81
+ file = open "#{@connect}download#{@credentials}&reportId=#{report_id}", :proxy => nil
82
+ rc = file.readlines.join("")
83
+ file.close
84
+
85
+ # parse csv and convert to array
86
+ rc = CSV.parse(rc).to_a
87
+
88
+ # clear open report request from reporter
89
+ file = open "#{@connect}cancel#{@credentials}&reportId=#{report_id}", :proxy => nil
90
+ file.close
91
+
92
+ # remove headline from array
93
+ rc.slice!(0)
94
+
95
+ # convert all found integer-strings to intergers
96
+ rc.map do |row|
97
+ row.map do |col|
98
+ begin
99
+ oldcol = col
100
+ col = Integer(col)
101
+ rescue
102
+ col = oldcol
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+ require 'open-uri'
3
+ require 'cgi'
4
+ require 'uri'
5
+ require 'mechanize'
6
+ require 'ipaddress'
7
+
8
+
9
+ # the class is extended by a function to_query which converts the hash to an URI-String
10
+ class Hash
11
+ # converts hash to URI parameter string
12
+ def to_query
13
+ elements = []
14
+ keys.size.times do |i|
15
+ elements << "#{keys[i]}=#{values[i]}"
16
+ end
17
+ elements.join('&')
18
+ end
19
+ end
20
+
21
+
22
+ module BlueCoat
23
+ # this class provides methods to query blue coat sg
24
+ class SG
25
+ # constructor connects to bluecoat sg
26
+ # options are: :host, :port, :username and :password
27
+ def initialize(options = {})
28
+ @host = options[:host]
29
+ @port = options[:port].nil? ? "8082" : options[:port]
30
+ @user = options[:user]
31
+ @pass = options[:pass]
32
+ end
33
+
34
+ # fetches the whole policy from blue coat sg
35
+ def fetch_policy(options = {})
36
+ if @policy.nil? || options[:no_caching] then
37
+ mech = Mechanize.new
38
+ mech.auth(@user, @pass)
39
+ page = mech.get("https://#{@host}:#{@port}/Policy/Current")
40
+ @policy = page.parser.xpath('/html/body/pre').inner_text
41
+ end
42
+ @policy
43
+ end
44
+
45
+ # fetches a subnet definition from the policy, an array of ip addresses and subnets is returned
46
+ def fetch_subnet(name, options = {})
47
+ rc = []
48
+ policy = fetch_policy(options)
49
+ policy.scan(/define subnet "#{name}"(.+?)end/m)
50
+ rc += $1.delete("\r\n").split(" ").map{|s| IPAddress s} if !$1.nil?
51
+ policy.scan(/define subnet #{name}(.+?)end/m)
52
+ rc += $1.delete("\r\n").split(" ").map{|s| IPAddress s} if !$1.nil?
53
+ rc
54
+ end
55
+ end
56
+ end
57
+
@@ -0,0 +1,3 @@
1
+ module Bluecoat
2
+ VERSION = "0.0.4"
3
+ end
data/lib/bluecoat.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'bluecoat/version'
2
+ require 'bluecoat/sg'
3
+ require 'bluecoat/reporter'
4
+ require 'openssl'
5
+
6
+ # disable SSL certificate validation
7
+ module OpenSSL
8
+ # disable SSL certificate validation
9
+ module SSL
10
+ remove_const :VERIFY_PEER
11
+ VERIFY_PEER = VERIFY_NONE
12
+ end
13
+ end
14
+
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bluecoat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lothar Handl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: ipaddress
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A bundle of functions for accessing Blue Coat Appliances. I.e. Reporter,
47
+ ProxySG, etc.
48
+ email:
49
+ - lothar.handl@3beg.at
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - bluecoat.gemspec
60
+ - lib/bluecoat.rb
61
+ - lib/bluecoat/reporter.rb
62
+ - lib/bluecoat/sg.rb
63
+ - lib/bluecoat/version.rb
64
+ homepage: https://github.com/terracor/bluecoat
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.23
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: A bundle of functions for accessing Blue Coat Appliances.
88
+ test_files: []