iron_hide-storage-couchdb_adapter 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +90 -0
- data/Rakefile +12 -0
- data/iron_hide-storage-couchdb_adapter.gemspec +27 -0
- data/lib/iron_hide/couchdb_tasks.rb +72 -0
- data/lib/iron_hide/storage/couchdb_adapter.rb +64 -0
- data/lib/iron_hide/storage/couchdb_adapter/version.rb +7 -0
- metadata +151 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ac941912f46eb6ad9672808497c3d92dd2e2d0ba
|
4
|
+
data.tar.gz: c3ec87e541bd3a7ecf7a7340726c93d41919c762
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c6fe271465762e93e01a837745726a29b4b3263d1941c0ec53ffc49a48719e7a1e57bd9095f875f299d32a608d9d3c0a55125592b792a4dd4ce37a8959413363
|
7
|
+
data.tar.gz: 2716bd33b4a6ade2a311287bb399731d0e7e046f0ddc336ab07c245fed0910c95adb99d83aefd91c91ccbf81c077a8fae0a2d096483ded06f7bde8affdd771f5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 The Climate Corporation
|
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,90 @@
|
|
1
|
+
# IronHide::Storage::CouchdbAdapter
|
2
|
+
|
3
|
+
A CouchDB adapter for the IronHide authorization library.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'iron_hide-storage-couchdb_adapter'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install iron_hide-storage-couchdb_adapter
|
18
|
+
|
19
|
+
## Setting up a CouchDB server
|
20
|
+
|
21
|
+
Assuming that you don't have a CouchDB server, this project comes with a few helper Rake tasks.
|
22
|
+
|
23
|
+
### Setup a local Couchdb server
|
24
|
+
The best resource for getting started with CouchDB is http://guide.couchdb.org/
|
25
|
+
|
26
|
+
Using brew:
|
27
|
+
|
28
|
+
```
|
29
|
+
brew install couchdb
|
30
|
+
couchdb # Starts the server
|
31
|
+
```
|
32
|
+
|
33
|
+
|
34
|
+
### Setup the server for use with IronHide
|
35
|
+
To install the helper Rake tasks:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
# Rakefile
|
39
|
+
require 'rake'
|
40
|
+
require 'iron_hide/couchdb_tasks'
|
41
|
+
```
|
42
|
+
|
43
|
+
To create a new database, either use the HTTP API, Futon, or this simple Rake task:
|
44
|
+
|
45
|
+
```
|
46
|
+
bundle exec rake iron_hide:create_db\['http://127.0.0.1:5984/rules'\]
|
47
|
+
```
|
48
|
+
|
49
|
+
To upload some rules to this database:
|
50
|
+
|
51
|
+
```
|
52
|
+
bundle exec rake iron_hide:load_rules\['/absolute/path/file.json','http://127.0.0.1:5984/rules'\]
|
53
|
+
```
|
54
|
+
|
55
|
+
Note: Required step
|
56
|
+
Setup the required views
|
57
|
+
|
58
|
+
```
|
59
|
+
bundle exec iron_hide:rake create_views\['http://127.0.0.1:5984/rules'\]
|
60
|
+
```
|
61
|
+
|
62
|
+
## Usage
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
require 'iron_hide'
|
66
|
+
require 'iron_hide/storage/couchdb_adapter'
|
67
|
+
|
68
|
+
IronHide.config do |c|
|
69
|
+
c.adapter = :couchdb
|
70
|
+
c.namespace = 'TestApp'
|
71
|
+
c.couchdb_server = 'http://127.0.0.1:5984' # Default
|
72
|
+
c.couchdb_database = 'rules' # Default
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
1. Fork it ( http://github.com/<my-github-username>/iron_hide-storage-couchdb_adapter/fork )
|
79
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
80
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
81
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
82
|
+
5. Create new Pull Request
|
83
|
+
|
84
|
+
## Further Reading
|
85
|
+
* [Tim Anglade](https://twitter.com/timanglade)
|
86
|
+
http://www.slideshare.net/timanglade/couchdb-ruby-youre-doing-it-wrong
|
87
|
+
|
88
|
+
## TODO
|
89
|
+
- Caching
|
90
|
+
- Tests
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'iron_hide/storage/couchdb_adapter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "iron_hide-storage-couchdb_adapter"
|
8
|
+
spec.version = IronHide::Storage::CouchdbAdapter::VERSION
|
9
|
+
spec.authors = ["Alan Cohen"]
|
10
|
+
spec.email = ["acohen@climate.com"]
|
11
|
+
spec.summary = %q{A CouchDB adapter for IronHide Authorization}
|
12
|
+
spec.description = %q{Requires IronHide}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1"
|
21
|
+
spec.add_development_dependency "iron_hide"
|
22
|
+
spec.add_development_dependency "rake", "~> 10"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.14"
|
24
|
+
spec.add_runtime_dependency "typhoeus", "~> 0.6.8"
|
25
|
+
spec.add_runtime_dependency "multi_json"
|
26
|
+
spec.add_runtime_dependency "couchrest", "~> 1.2"
|
27
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'couchrest'
|
2
|
+
require 'typhoeus'
|
3
|
+
require 'multi_json'
|
4
|
+
|
5
|
+
module IronHide
|
6
|
+
class CouchDbTasks
|
7
|
+
include Rake::DSL if defined? Rake::DSL
|
8
|
+
|
9
|
+
def install_tasks
|
10
|
+
namespace :iron_hide do
|
11
|
+
# Interface to load some local JSON files to a remote CouchDB server
|
12
|
+
#
|
13
|
+
# Usage:
|
14
|
+
# bundle exec rake load_rules\['/absolute/path/file.json','http://127.0.0.1:5984/rules'\]
|
15
|
+
#
|
16
|
+
desc 'Load rules from local JSON file to remote CouchDB server'
|
17
|
+
task :load_rules, :file, :db_server do |t, args|
|
18
|
+
db = args[:db_server]
|
19
|
+
rules = MultiJson.load(File.open(args[:file]).read)
|
20
|
+
|
21
|
+
# Use the bulk document API
|
22
|
+
# See: https://wiki.apache.org/couchdb/HTTP_Bulk_Document_API
|
23
|
+
response = Typhoeus.post("#{db}/_bulk_docs",
|
24
|
+
body: MultiJson.dump(docs: rules), headers: { "Content-Type" => "application/json" })
|
25
|
+
|
26
|
+
puts "Status: #{response.response_code}\nBody: #{response.body}"
|
27
|
+
end
|
28
|
+
|
29
|
+
# Interface to create a CouchDB database if one doesn't exist
|
30
|
+
#
|
31
|
+
# Usage:
|
32
|
+
# bundle exec rake create_db\['http://127.0.0.1:5984/rules'\]
|
33
|
+
#
|
34
|
+
desc 'Create a database'
|
35
|
+
task :create_db, :db do |t, args|
|
36
|
+
CouchRest.database!(args[:db])
|
37
|
+
end
|
38
|
+
|
39
|
+
# Interface to create required views on CouchDB server.
|
40
|
+
#
|
41
|
+
# Usage:
|
42
|
+
# bundle exec rake create_views\['http://127.0.0.1:5984/rules'\]
|
43
|
+
#
|
44
|
+
desc 'Create a map-reduce view on a CouchDB server to find rules by namespace and resource'
|
45
|
+
task :create_views, :db_server do |t, args|
|
46
|
+
db = args[:db_server]
|
47
|
+
function = "function(doc) {" \
|
48
|
+
" doc.action.forEach(function(e) {" \
|
49
|
+
" if(e && doc.resource){" \
|
50
|
+
" emit(doc.resource + '::' + e, doc);" \
|
51
|
+
" }" \
|
52
|
+
" })" \
|
53
|
+
"};"
|
54
|
+
|
55
|
+
document = {
|
56
|
+
"_id" => "_design/rules",
|
57
|
+
"views" => {
|
58
|
+
"resource_rules" => {
|
59
|
+
"map" => function
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
response = Typhoeus.post(db,
|
64
|
+
body: MultiJson.dump(document), headers: { "Content-Type" => "application/json"} )
|
65
|
+
puts "Status: #{response.response_code}\nBody: #{response.body}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
IronHide::CouchDbTasks.new.install_tasks
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
require 'iron_hide'
|
3
|
+
require 'iron_hide/storage'
|
4
|
+
require 'typhoeus'
|
5
|
+
|
6
|
+
module IronHide
|
7
|
+
class Storage
|
8
|
+
class CouchdbAdapter
|
9
|
+
|
10
|
+
# @option opts [String] :resource *required*
|
11
|
+
# @option opts [String] :action *required*
|
12
|
+
# @return [Array<Hash>] array of canonical JSON representation of rules
|
13
|
+
def where(opts = {})
|
14
|
+
self["#{opts.fetch(:resource)}::#{opts.fetch(:action)}"]
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
# Implements an interface that makes selecting rules look like a Hash:
|
19
|
+
# @example
|
20
|
+
# {
|
21
|
+
# 'com::test::TestResource::read' => {
|
22
|
+
# ...
|
23
|
+
# }
|
24
|
+
# }
|
25
|
+
# adapter['com::test::TestResource::read']
|
26
|
+
# #=> [Array<Hash>]
|
27
|
+
#
|
28
|
+
# @param [Symbol] val
|
29
|
+
# @return [Array<Hash>] array of canonical JSON representation of rules
|
30
|
+
def [](val)
|
31
|
+
response = couchd_db_rules(val)
|
32
|
+
if response.response_code == 200
|
33
|
+
MultiJson.load(response.response_body)["rows"].reduce([]) do |rval, row|
|
34
|
+
rval << row["value"]
|
35
|
+
end
|
36
|
+
else
|
37
|
+
# Do Something
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def couchd_db_rules(val)
|
42
|
+
Typhoeus.get(
|
43
|
+
"#{server}/#{database}/_design/rules/_view/resource_rules?key=\"#{val}\""
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
def server
|
48
|
+
IronHide.configuration.couchdb_server
|
49
|
+
end
|
50
|
+
|
51
|
+
def database
|
52
|
+
IronHide.configuration.couchdb_database
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Add adapter class to IronHide::Storage
|
60
|
+
IronHide::Storage::ADAPTERS.merge!(couchdb: :CouchdbAdapter)
|
61
|
+
|
62
|
+
# Add default configuration variables
|
63
|
+
IronHide.configuration.add_configuration(couchdb_server: 'http://127.0.0.1:5984',
|
64
|
+
couchdb_database: 'rules')
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: iron_hide-storage-couchdb_adapter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alan Cohen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: iron_hide
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.14'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.14'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: typhoeus
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.6.8
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.6.8
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: multi_json
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: couchrest
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.2'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.2'
|
111
|
+
description: Requires IronHide
|
112
|
+
email:
|
113
|
+
- acohen@climate.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- Gemfile
|
120
|
+
- LICENSE.txt
|
121
|
+
- README.md
|
122
|
+
- Rakefile
|
123
|
+
- iron_hide-storage-couchdb_adapter.gemspec
|
124
|
+
- lib/iron_hide/couchdb_tasks.rb
|
125
|
+
- lib/iron_hide/storage/couchdb_adapter.rb
|
126
|
+
- lib/iron_hide/storage/couchdb_adapter/version.rb
|
127
|
+
homepage: ''
|
128
|
+
licenses:
|
129
|
+
- MIT
|
130
|
+
metadata: {}
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 2.2.1
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: A CouchDB adapter for IronHide Authorization
|
151
|
+
test_files: []
|