soar_sc_routing 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: 9d26f7e7fdd02b09bad20b2d58479c51c0ca4289
4
+ data.tar.gz: 242a83d9dbc18128a59a85cb708fa4c99b8cced6
5
+ SHA512:
6
+ metadata.gz: d0c9f3bbf5783e53cf09d75b239a2634891c94de0f4131369720df89b135b892aa33e2365040c1143dd003c2a274806c225b2566fe8c0ab2b0d3eca4a8e15136
7
+ data.tar.gz: cddba5d15e661327dcc2bf9f80522f92a09f5c28db3200dfc9d6aa5c348a333d9c021aea2cc68c7c02631fd558d9ca996aa1bf8e7ce0048575edb49bb1ce8374
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .byebug_history
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.3.1
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_sc_routing.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,41 @@
1
+ # SoarScRouting
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/soar_sc_routing`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'soar_sc_routing'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install soar_sc_routing
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/soar_sc_routing.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
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_sc_routing"
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,64 @@
1
+ require 'soar_aspects'
2
+
3
+ module SoarScRouting
4
+ class BaseRouter
5
+ attr_reader :route_meta
6
+
7
+ def initialize(route_meta)
8
+ @route_meta = route_meta
9
+ end
10
+
11
+ def route(request)
12
+ route_path(request)
13
+
14
+ rescue => ex
15
+ excepted(ex)
16
+ end
17
+
18
+ def redirect_to(url, http_code = 302)
19
+ [http_code, {'Location' => url, 'Content-Type' => 'text/html', 'Content-Length' => '0'}, []]
20
+ end
21
+
22
+ protected
23
+
24
+ def not_found
25
+ raise NotImplementedError.new "Implement not found renderer"
26
+ end
27
+
28
+ def excepted(ex)
29
+ raise NotImplementedError.new "Implement exception renderer: #{ex}"
30
+ end
31
+
32
+ private
33
+
34
+ def debug(message, flow_identifier)
35
+ auditing = SoarAspects::Aspects::auditing
36
+ if auditing
37
+ auditing.debug(message, flow_identifier)
38
+ else
39
+ $stderr.puts(message)
40
+ end
41
+ end
42
+
43
+ def route_matched_path(request, path)
44
+ debug("matched #{path}",request.params['flow_identifier'])
45
+ http_code, content_type, body = @route_meta.routing[path].call(request)
46
+ debug("controller returned #{http_code}, #{content_type}", request.params['flow_identifier'])
47
+ return [http_code, content_type, body]
48
+ end
49
+
50
+ def route_path(request)
51
+ # Only allow POST requests
52
+ #if not request.post?
53
+ # return SoarSc::Web::Views::Default.render(405, "")
54
+ #end
55
+ @route_meta.routing.each do |path, block|
56
+ if matches = Regexp.new(path).match(request.path)
57
+ request.define_singleton_method(:regex_matches) { return matches.to_a }
58
+ return route_matched_path(request, path)
59
+ end
60
+ end
61
+ not_found
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,28 @@
1
+ module SoarScRouting
2
+ class Resource
3
+ attr_accessor :description
4
+ attr_accessor :method
5
+ attr_accessor :id
6
+ attr_accessor :params
7
+ attr_accessor :input
8
+ attr_accessor :output
9
+
10
+ def initialize(description, id, method = 'GET', params = nil, input = nil, output = nil)
11
+ @description = description
12
+ @id = id
13
+ @method = method
14
+ @params = params
15
+ @input = input
16
+ @output = output
17
+ end
18
+
19
+ def content
20
+ {
21
+ 'doc' => @description,
22
+ 'method' => @method,
23
+ 'id' => @id,
24
+ 'params' => @params
25
+ }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,155 @@
1
+ require 'json'
2
+ require 'smaak'
3
+ require 'jsender'
4
+ require 'soar_authorization'
5
+ require 'soar_aspects'
6
+
7
+ module SoarScRouting
8
+ class RouterMeta
9
+ include Jsender
10
+ SIGNED = true unless defined? SIGNED; SIGNED.freeze
11
+ UNSIGNED = false unless defined? UNSIGNED; UNSIGNED.freeze
12
+ AUTHORIZED = true unless defined? AUTHORIZED; AUTHORIZED.freeze
13
+ UNAUTHORIZED = false unless defined? UNAUTHORIZED; UNAUTHORIZED.freeze
14
+
15
+ attr_accessor :configuration
16
+ attr_accessor :lexicon
17
+ attr_accessor :routing
18
+ attr_accessor :signed_routes
19
+ attr_accessor :service_names
20
+ attr_accessor :policy_am
21
+ attr_accessor :autenticate
22
+
23
+ def initialize(configuration)
24
+ @configuration = configuration
25
+ @lexicon = {}
26
+ @routing = {}
27
+ @signed_routes = {}
28
+ @service_names = {}
29
+ setup_routing_table
30
+ end
31
+
32
+ def access_manager
33
+ @policy_am
34
+ end
35
+
36
+ def register_route(detail, startup_flow_id = nil)
37
+ validate_detail(detail)
38
+ info("Registering service: #{detail}", startup_flow_id)
39
+ resource = SoarScRouting::Resource.new(detail['description'], "#{detail['service_name']}", detail['method'].upcase, detail['params'])
40
+ add_resource_route(detail['path'], resource, interpret_secured(detail), interpret_authorized(detail)) do |request|
41
+ if detail['controller']
42
+ return delegate_to_controller_and_renderer(detail, startup_flow_id)
43
+ elsif detail['action']
44
+ http_code, headers, body = detail['action'].call(request)
45
+ [http_code, headers, [body]]
46
+ end
47
+ end
48
+ end
49
+
50
+ def add_route(path, description, &block)
51
+ resource = SoarScRouting::Resource.new(description, generate_id(path))
52
+ add_resource_route(path, resource, UNSIGNED, AUTHORIZED, &block)
53
+ end
54
+
55
+ def add_unsigned_unauthorized_route(path, description, &block)
56
+ resource = SoarScRouting::Resource.new(description, generate_id(path))
57
+ add_resource_route(path, resource, UNSIGNED, UNAUTHORIZED, &block)
58
+ end
59
+
60
+ def add_signed_route(path, description, &block)
61
+ resource = SoarScRouting::Resource.new(description, generate_id(path))
62
+ add_resource_route(path, resource, SIGNED, AUTHORIZED, &block)
63
+ end
64
+
65
+ def add_signed_unauthorized_route(path, description, &block)
66
+ resource = SoarScRouting::Resource.new(description, generate_id(path))
67
+ add_resource_route(path, resource, SIGNED, UNAUTHORIZED, &block)
68
+ end
69
+
70
+ protected
71
+
72
+ # Inversion of control
73
+ def setup_routing_table
74
+ end
75
+
76
+ # IOC renderer for views
77
+ def render_view(detail, http_code, body)
78
+ raise NotImplementedError.new "No renderer"
79
+ end
80
+
81
+ private
82
+
83
+ def validate_detail(detail)
84
+ raise ArgumentError.new("detail must not be nil") if detail.nil?
85
+ raise ArgumentError.new("path must be provided") if detail['path'].nil?
86
+ raise ArgumentError.new("description must be provided") if detail['description'].nil?
87
+ raise ArgumentError.new("params must be provided") if detail['params'].nil?
88
+ raise ArgumentError.new("method must be provided") if detail['method'].nil?
89
+ raise ArgumentError.new("service_name must be provided") if detail['service_name'].nil?
90
+ raise ArgumentError.new("nfrs must be provided") if detail['nfrs'].nil?
91
+ raise ArgumentError.new("nfrs['secured'] must be provided") if detail['nfrs']['secured'].nil?
92
+ raise ArgumentError.new("nfrs['authorization'] must be provided") if detail['nfrs']['authorization'].nil?
93
+ end
94
+
95
+ def delegate_to_controller_and_renderer(detail, startup_flow_id)
96
+ controller = nil
97
+ begin
98
+ controller = Object::const_get("SoarSc::Web::Controllers::#{detail['controller']}").new(@configuration)
99
+ rescue NameError => ex
100
+ warn("Could not instantiate SoarSc::Web::Controllers::#{detail['controller']}. Trying class name as-is", startup_flow_id)
101
+ controller = Object::const_get(detail['controller']).new(@configuration)
102
+ end
103
+ http_code, body = controller.serve(request)
104
+ render_view(detail, http_code, body)
105
+ end
106
+
107
+ def info(message, startup_flow_id)
108
+ auditing = SoarAspects::Aspects::auditing
109
+ if auditing
110
+ auditing.info(message, startup_flow_id)
111
+ else
112
+ $stderr.puts(message)
113
+ end
114
+ end
115
+
116
+ def warn(message, startup_flow_id)
117
+ auditing = SoarAspects::Aspects::auditing
118
+ if auditing
119
+ auditing.warn(message, startup_flow_id)
120
+ else
121
+ $stderr.puts(message)
122
+ end
123
+ end
124
+
125
+ def generate_id(path)
126
+ path.gsub("/","_")
127
+ end
128
+
129
+ def add_resource_route(path, resource, signed, authorization_required, &block)
130
+ @routing[path] = block
131
+ @service_names[path] = resource.id
132
+ @lexicon[path] = resource.content
133
+ @signed_routes[path] = signed
134
+ SoarAuthorization::Authorize::register_access_manager(path, resource.id, access_manager) if authorization_required
135
+ end
136
+
137
+ def interpret_secured(detail)
138
+ secured = 'SIGNED' # default to strong
139
+ if (not detail['nfrs']['secured'].nil?) and detail['nfrs']['secured'].is_a?(String)
140
+ secured = 'SIGNED' if detail['nfrs']['secured'].upcase.strip == 'SIGNED'
141
+ secured = nil if detail['nfrs']['secured'].upcase.strip == 'UNSIGNED'
142
+ end
143
+ secured == 'SIGNED'
144
+ end
145
+
146
+ def interpret_authorized(detail)
147
+ authorized = 'AUTHORIZED' # default to strong
148
+ if (not detail['nfrs'].nil?) and (not detail['nfrs']['authorization'].nil?) and (detail['nfrs']['authorization'].is_a?(String))
149
+ authorized = detail['nfrs']['authorization']
150
+ authorized = 'AUTHORIZED' if not ['AUTHORIZED', 'UNAUTHORIZED'].include?(detail['nfrs']['authorization'])
151
+ end
152
+ authorized == 'AUTHORIZED'
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,3 @@
1
+ module SoarScRouting
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "soar_sc_routing/version"
2
+ require "soar_sc_routing/resource"
3
+ require "soar_sc_routing/router_meta"
4
+ require "soar_sc_routing/base_router"
5
+
6
+ module SoarScRouting
7
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'soar_sc_routing/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "soar_sc_routing"
8
+ spec.version = SoarScRouting::VERSION
9
+ spec.authors = ["Ernst Van Graan"]
10
+ spec.email = ["ernst.van.graan@hetzner.co.za"]
11
+
12
+ spec.summary = %q{base router and router meta library for soar_sc}
13
+ spec.description = %q{base router and router meta library for soar_sc}
14
+ spec.homepage = "https://github.com/hetznerZA/soar_sc_routing"
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 'jsender', "~> 0.2.0"
31
+ spec.add_dependency 'smaak', "~> 0.2.2"
32
+ spec.add_dependency "soar_authorization", "~> 0.1.4"
33
+ spec.add_dependency "soar_aspects", "~> 0.1.2"
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.12"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ spec.add_development_dependency "soar_xt", "~> 0.0.3"
39
+ spec.add_development_dependency "byebug"
40
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soar_sc_routing
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-08-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jsender
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.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.2.0
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.2
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.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: soar_authorization
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: soar_aspects
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: soar_xt
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.0.3
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.0.3
125
+ - !ruby/object:Gem::Dependency
126
+ name: byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: base router and router meta library for soar_sc
140
+ email:
141
+ - ernst.van.graan@hetzner.co.za
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
149
+ - Gemfile
150
+ - LICENSE.txt
151
+ - README.md
152
+ - Rakefile
153
+ - bin/console
154
+ - bin/setup
155
+ - lib/soar_sc_routing.rb
156
+ - lib/soar_sc_routing/base_router.rb
157
+ - lib/soar_sc_routing/resource.rb
158
+ - lib/soar_sc_routing/router_meta.rb
159
+ - lib/soar_sc_routing/version.rb
160
+ - soar_sc_routing.gemspec
161
+ homepage: https://github.com/hetznerZA/soar_sc_routing
162
+ licenses:
163
+ - MIT
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.5.1
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: base router and router meta library for soar_sc
185
+ test_files: []