soar_smaak 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 37ab95cb119f68e830f45587aa09c336ac5f11b2
4
+ data.tar.gz: 731e14a0d878053d7b71ddee74dc69be3213469a
5
+ SHA512:
6
+ metadata.gz: dd6abe2f923047b0ebe092c26cea8c01e364629b36fc91a5f24adbe0949865f3f34c6521f0a6f0af9bbd9450eaeb6e12b43d80a2fb22f23c44be0a71c12aab11
7
+ data.tar.gz: 18f28528d0518a32fe936868f23091705bf76047522fed6b3b02327770001bbb6dbdec65a7f874bcf59060bfb0f69fa8f340ea87721b73633363af3170e07086
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-gemset
11
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in soar_smaak.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ernst Van Graan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # SoarSmaak
2
+
3
+ This library offers Rack middle-ware for SMAAK communication. SoarSmaak::Router will refuse request in case of any failure with a 500. Requests are examined to determine whether the route the request is destined for requires message signing, or whether the request itself is a SMAAK request. If either are true, the SMAAK message is interpreted and verified. The message is decrypted if it was encrypted, and passed to the rack application stack unencrypted, only if the signature is verified. After processing by the application stack, responses are signed into SMAAK responses, and encrypted if the authorization message indicated it should be. Non-SMAAK messages are passed through to the application stack without interpretation or verification.
4
+
5
+ ## Dependencies
6
+
7
+ ### configuration
8
+
9
+ A dictionary including a SMAAK public and private key identifying the service component in question, as well as an optional associations dictionary with the identity and public key and pre-shared key of associations that the SMAAK middleware should verify. An example follows:
10
+
11
+ ```
12
+ public_key: |
13
+ -----BEGIN RSA PRIVATE KEY-----
14
+ MIICIdfsAKCAgEAsfB2D/ZZKyDB7YuZBzD1JghlkjglfdgloskzseFs1qiVLXks3
15
+ ...
16
+ -----END RSA PUBLIC KEY-----
17
+ private_key: |
18
+ -----BEGIN RSA PRIVATE KEY-----
19
+ MIIJKgIBAAKCAgEAsfB2D/ZZKyDB7YuZBzD1Jgh5kiWqOLjtpBkzseFs1qiVLX/S
20
+ ...
21
+ -----END RSA PRIVATE KEY-----
22
+ associations:
23
+ i-can.identify-this.server:
24
+ public_key: |
25
+ -----BEGIN RSA PUBLIC KEY-----
26
+ MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAyt2nPgdfE/Osc2HQ8ZT0
27
+ -----END RSA PUBLIC KEY-----
28
+ psk: 655U0Rw6Rk12
29
+ ```
30
+
31
+ ### signed_routes
32
+
33
+ A dictionary of paths and an indication whether the route is signed, e.g.
34
+
35
+ ```
36
+ { "/secure-service" => true,
37
+ "/another-service" => false }
38
+ ```
39
+
40
+ ### auditing
41
+
42
+ An auditing provider that adheres to the API specified here: https://rubygems.org/gems/soar_auditor_api. If an auditing provider is not present, $stderr will be used.
43
+
44
+ ## Installation
45
+
46
+ Add this line to your application's Gemfile:
47
+
48
+ ```ruby
49
+ gem 'soar_smaak'
50
+ ```
51
+
52
+ And then execute:
53
+
54
+ $ bundle
55
+
56
+ Or install it yourself as:
57
+
58
+ $ gem install soar_smaak
59
+
60
+ ## Usage
61
+
62
+ In your application builder, set SOAR aspects that SoarSmaak depend on:
63
+
64
+ ```
65
+ SoarAspects::config = @config
66
+ SoarAspects::signed_routes = @signed_routes
67
+ SoarAspects::auditing = @auditing
68
+ use SoarAspects
69
+ ```
70
+
71
+ Then add the SMAAK middleware router:
72
+
73
+ ```
74
+ use SoarSmaak::Router
75
+ ```
76
+
77
+ ## Contributing
78
+
79
+ Please send feedback and comments to the author at:
80
+
81
+ Ernst van Graan <ernst.van.graan@hetzner.co.za>
82
+
83
+ This gem is sponsored by Hetzner (Pty) Ltd - http://hetzner.co.za
84
+
85
+ ## License
86
+
87
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
88
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "soar_smaak"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ require 'smaak'
2
+
3
+ module SoarSmaak
4
+ class Interpreter
5
+ def self.smaak_request?(request)
6
+ auth_header = ::Smaak::RackAdaptor.new(request).header("authorization")
7
+ (not (auth_header.nil?)) and (auth_header.include?("x-smaak"))
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,59 @@
1
+ module SoarSmaak
2
+ class Router
3
+ attr_reader :configuration
4
+
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def smaak(request)
10
+ @auditing.debug("Smaak: verifying request")
11
+ auth_message, body = @server.verify_signed_request(request)
12
+ @auditing.debug("Smaak: auth_message: #{auth_message}")
13
+ return auth_message, body
14
+ end
15
+
16
+ def call(env)
17
+ @configuration = env['config']
18
+ @auditing = env['auditing']
19
+ @signed_routes = env['signed_routes']
20
+ SoarSmaak::SecureService::auditing = @auditing
21
+ secure_service = SoarSmaak::SecureService.get_instance(@configuration)
22
+ @server = secure_service.smaak_server
23
+ @auditing.debug("Smaak: signed routes are: #{@signed_routes}")
24
+ request = Rack::Request.new(env)
25
+ if SoarSmaak::Interpreter::smaak_request?(request) or @signed_routes[request.path]
26
+ @auditing.debug("Smaak: routing smaak request")
27
+ auth_message, body = smaak(request)
28
+ return [403, {}, [" 403 - Not authorized"]] if not auth_message.identifier
29
+ @auditing.debug("Smaak: request authorized for #{auth_message.identifier}")
30
+ env["rack.input"] = body
31
+ else
32
+ @auditing.debug("routing non-smaak request")
33
+ end
34
+
35
+ http_code, content_type, body = @app.call(env)
36
+
37
+ if SoarSmaak::Interpreter::smaak_request?(request) or @signed_routes[request.path]
38
+ @auditing.debug("Smaak: request authorized for #{auth_message.identifier}")
39
+ return [http_code, content_type, [@server.compile_response(auth_message, body[0])]] if auth_message.encrypt
40
+ return [http_code, content_type, @server.compile_response(auth_message, body)]
41
+ else
42
+ return http_code, content_type, body
43
+ end
44
+ rescue => ex
45
+ puts ex
46
+ [500, {"Content-Type" => "text/html"}, ['Unable to route request on SMAAK route. Was your request a SMAAK request?']]
47
+ end
48
+
49
+ private
50
+
51
+ def debug(message)
52
+ if @auditing
53
+ @auditing.debug(message)
54
+ else
55
+ $stderr.puts(message)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,62 @@
1
+ module SoarSmaak
2
+ class SecureService < ::Smaak::SmaakService
3
+ attr_reader :dynamic
4
+ attr_reader :trust_store
5
+ @@auditing
6
+
7
+ def self.auditing=(auditing)
8
+ @@auditing = auditing
9
+ end
10
+
11
+ def self.smaak_configured?(configuration)
12
+ (not configuration['public_key'].nil?) and
13
+ (not configuration['private_key'].nil?) and
14
+ (not configuration['associations'].nil?)
15
+ end
16
+
17
+ def self.smaak_dynamic?(configuration)
18
+ configuration['smaak'] and (configuration['smaak'].downcase.strip == 'dynamic')
19
+ end
20
+
21
+ def configure_services(configuration)
22
+ if (not SoarSmaak::SecureService::smaak_configured?(configuration) and
23
+ SoarSmaak::SecureService::smaak_dynamic?(configuration))
24
+ seed_dynamic_smaak
25
+ elsif (SoarSmaak::SecureService::smaak_configured?(configuration))
26
+ configure_smaak(configuration)
27
+ else
28
+ @@auditing.debug "SMAAK is neither dynamic nor configured. SMAAK support disabled"
29
+ end
30
+ end
31
+
32
+ def seed_dynamic_smaak
33
+ @@auditing.debug "SMAAK credentials not provided."
34
+ load_smaak_trust_store
35
+ randomize_smaak
36
+ end
37
+
38
+ def randomize_smaak
39
+ @dynamic = @trust_store.associations.keys[rand(@trust_store.associations.keys.size) + 1]
40
+ @@auditing.debug "Seeding random key-pair and identifier"
41
+ @smaak_server.set_public_key(@trust_store.associations[@dynamic]['public_key'])
42
+ @smaak_server.set_private_key(@trust_store.associations[@dynamic]['private_key'])
43
+ @smaak_server.verify_recipient = false
44
+ end
45
+
46
+ def load_smaak_trust_store
47
+ @trust_store = SoarSmaak::SmaakTrustStore.new
48
+ @trust_store.associations.each do |identifier, config|
49
+ @smaak_server.add_association(identifier, config['public_key'], config['psk'], config['encrypt'])
50
+ end
51
+ end
52
+
53
+ def configure_smaak(configuration)
54
+ @smaak_server.set_public_key(configuration['public_key'])
55
+ @smaak_server.set_private_key(configuration['private_key'])
56
+ configuration['associations'].each do |identifier, config|
57
+ @smaak_server.add_association(identifier, config['public_key'], config['psk'], config['encrypt'])
58
+ end
59
+ @@auditing.debug "SMAAK credentials provided. SMAAK configured"
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,18 @@
1
+ require 'yaml'
2
+
3
+ module SoarSmaak
4
+ class SmaakTrustStore
5
+ attr_reader :associations
6
+
7
+ def initialize
8
+ @errors = []
9
+ @associations = YAML.load_file('config/smaak_trust_store')['associations']
10
+
11
+ rescue => e
12
+ message = "Could not load SMAAK trust store. Is it YAML?"
13
+ @errors << message
14
+ @errors << e.message
15
+ raise ArgumentError.new(message)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module SoarSmaak
2
+ VERSION = "0.1.0"
3
+ end
data/lib/soar_smaak.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+ require 'digest'
4
+ require 'rack'
5
+ require 'smaak'
6
+ require 'smaak/smaak_service'
7
+ require "soar_smaak/version"
8
+ require "soar_smaak/interpreter"
9
+ require "soar_smaak/smaak_trust_store"
10
+ require "soar_smaak/router"
11
+ require "soar_smaak/secure_service"
12
+
13
+ module SoarSmaak
14
+
15
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'soar_smaak/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "soar_smaak"
8
+ spec.version = SoarSmaak::VERSION
9
+ spec.authors = ["Ernst Van Graan"]
10
+ spec.email = ["ernst.van.graan@hetzner.co.za"]
11
+
12
+ spec.summary = %q{Rack middle-ware for supporting SMAAK communication}
13
+ spec.description = %q{Rack middle-ware for supporting SMAAK communication}
14
+ spec.homepage = "https://github.com/hetznerZA/soar_smaak"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "rack", "~> 1.6.4"
31
+ spec.add_dependency "smaak", "~> 0.2.0"
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.12"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "byebug"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soar_smaak
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ernst Van Graan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: smaak
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Rack middle-ware for supporting SMAAK communication
98
+ email:
99
+ - ernst.van.graan@hetzner.co.za
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/soar_smaak.rb
114
+ - lib/soar_smaak/interpreter.rb
115
+ - lib/soar_smaak/router.rb
116
+ - lib/soar_smaak/secure_service.rb
117
+ - lib/soar_smaak/smaak_trust_store.rb
118
+ - lib/soar_smaak/version.rb
119
+ - soar_smaak.gemspec
120
+ homepage: https://github.com/hetznerZA/soar_smaak
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.5.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Rack middle-ware for supporting SMAAK communication
144
+ test_files: []