blobs 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d22b4be98490f69f8b7d774a224298a5db34d56a
4
+ data.tar.gz: a2912962981b01b736d5933a1f6e14efa81e2442
5
+ SHA512:
6
+ metadata.gz: a1914a2aca38538454a07ddb7a1801ae0c20dbabcca08bf1f32d93b23cbcb57e64c4fb79adc87cabf7cf56b4ea021d85300df174dbd67fcf543cb7d1d62e2a74
7
+ data.tar.gz: 98155369d512490053d3ac6b14cbc9d85424c683ef96b1a55fe6d96e30f4e0319d423ca7eab1ed17630fdda637c28a63a121571c165360e854c5de2e8f0a6cf6
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in blobs.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Oliver Kiessler
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,48 @@
1
+ # Blobs
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'blobs'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install blobs
18
+
19
+ ## Usage
20
+
21
+ ENV['BLOB_STORE_API_BASE_URL'] = "http://localhost:9000"
22
+ ENV['MASTER_ACCESS_TOKEN'] = "blob store master key"
23
+ ENV['SECRET_KEY'] = 'abc123'
24
+
25
+ c = Blobs::ApiClient.new
26
+ c.login("tester@test.com", "tester")
27
+ json = { foo: 'bar' }
28
+ c.create(json)
29
+ json = c.find(blob_id)
30
+ json['foo'] = 'bar2'
31
+ c.update(blob_id, json)
32
+ c.destroy(blob_id)
33
+ c.all
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ 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).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/blobs.
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "blobs"
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(__FILE__)
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
data/blobs.gemspec 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 'blobs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "blobs"
8
+ spec.version = Blobs::VERSION
9
+ spec.authors = ["Oliver Kiessler"]
10
+ spec.email = ["kiessler@inceedo.com"]
11
+
12
+ spec.summary = "Blob API Client"
13
+ spec.description = "An HTTP API Client for the blob-store service."
14
+ spec.homepage = "https://gitlab.com/okiess/blobs"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "httparty", "~> 0.13.7"
27
+ end
@@ -0,0 +1,3 @@
1
+ module Blobs
2
+ VERSION = "0.1.0"
3
+ end
data/lib/blobs.rb ADDED
@@ -0,0 +1,156 @@
1
+ require "blobs/version"
2
+ require "httparty"
3
+ require 'openssl'
4
+ require 'Base64'
5
+ require 'securerandom'
6
+ require 'cgi'
7
+
8
+ module Blobs
9
+ DEBUG = true
10
+
11
+ class ApiClient
12
+ attr_accessor :current_user
13
+ attr_accessor :token
14
+
15
+ def base_url
16
+ ENV['BLOB_STORE_API_BASE_URL']
17
+ end
18
+
19
+ def master_token
20
+ ENV['MASTER_ACCESS_TOKEN']
21
+ end
22
+
23
+ def encryption_key
24
+ ENV['SECRET_KEY']
25
+ end
26
+
27
+ def log(msg)
28
+ puts msg if DEBUG
29
+ end
30
+
31
+ def login(email, password)
32
+ auth = { username: email, password: password }
33
+ response = HTTParty.post("#{base_url}/auth?access_token=#{master_token}",
34
+ basic_auth: auth)
35
+ if response.code == 201 and response.parsed_response
36
+ log "#{response.parsed_response.inspect}"
37
+ self.current_user = response.parsed_response['user']
38
+ self.token = response.parsed_response['token']
39
+ else
40
+ nil
41
+ end
42
+ end
43
+
44
+ def all(key = nil)
45
+ raise 'No access token!' unless self.token
46
+ url = "#{base_url}/blobs?access_token=#{CGI::escape(self.token)}"
47
+ url += "&key=#{CGI::escape(sha256(key))}" if key
48
+ response = HTTParty.get(url)
49
+ log "Blobs: #{response.parsed_response.inspect}"
50
+ response.parsed_response.map do |blob|
51
+ {
52
+ id: blob['id'],
53
+ created_at: blob['createdAt'],
54
+ json: (blob['json'] ? (JSON.parse(decrypt(blob['json'])) rescue nil) : nil),
55
+ key: blob['key']
56
+ }
57
+ end
58
+ end
59
+
60
+ def find(blob_id)
61
+ raise 'No access token!' unless self.token
62
+ raise "No blob id!" unless blob_id
63
+ response = HTTParty.get("#{base_url}/blobs/#{blob_id}?access_token=#{self.token}")
64
+ log "Blob: #{response.parsed_response.inspect}"
65
+ response.parsed_response ? JSON.parse(decrypt(response.parsed_response['json'])) : nil
66
+ end
67
+
68
+ def create(json, key = 'default')
69
+ raise 'No access token!' unless self.token
70
+ raise "No json hash!" unless json
71
+ raise "No key!" unless key
72
+ response = HTTParty.post("#{base_url}/blobs?access_token=#{self.token}",
73
+ { body: { json: encrypt(json.to_json), key: sha256(key) } })
74
+ log "Blob created: #{response.parsed_response.inspect}"
75
+ response.parsed_response
76
+ end
77
+
78
+ def update(blob_id, json, key = 'default')
79
+ raise 'No access token!' unless self.token
80
+ raise "No blob id!" unless json
81
+ raise "No json hash!" unless json
82
+ response = HTTParty.put("#{base_url}/blobs/#{blob_id}?access_token=#{self.token}",
83
+ { body: { json: encrypt(json.to_json), key: sha256(key) } })
84
+ log "Blob update: #{response.parsed_response.inspect}"
85
+ response.parsed_response
86
+ end
87
+
88
+ def destroy(blob_id)
89
+ raise 'No access token!' unless self.token
90
+ raise "No blob id!" unless blob_id
91
+ response = HTTParty.delete("#{base_url}/blobs/#{blob_id}?access_token=#{self.token}")
92
+ log "Blob destroyed: #{response.parsed_response.inspect}"
93
+ response.parsed_response
94
+ end
95
+
96
+ private
97
+ def sha256(str)
98
+ Base64.encode64(Digest::SHA256.digest(str)).strip
99
+ end
100
+
101
+ def encrypt(str)
102
+ cipher = OpenSSL::Cipher::AES256.new :CBC
103
+ cipher.encrypt
104
+ cipher.key = encryption_key
105
+ cipher.iv = get_iv
106
+ Base64.encode64(cipher.update(str) + cipher.final).strip
107
+ end
108
+
109
+ def decrypt(enc_str)
110
+ decipher = OpenSSL::Cipher::AES256.new :CBC
111
+ decipher.decrypt
112
+ decipher.key = encryption_key
113
+ decipher.iv = get_iv
114
+ decipher.update(Base64.decode64(enc_str).strip) + decipher.final
115
+ end
116
+
117
+ def encryption_key
118
+ return @encryption_key if @encryption_key
119
+
120
+ cipher = OpenSSL::Cipher::AES256.new :CBC
121
+ key_file = "#{ENV['HOME']}/.blobs.key"
122
+ if File.file?(key_file)
123
+ enc_str = File.read(key_file)
124
+ cipher.decrypt
125
+ cipher.key = ENV['MASTER_ACCESS_TOKEN']
126
+ if (json_str = cipher.update(Base64.decode64(enc_str).strip) + cipher.final)
127
+ json = JSON.parse(json_str)
128
+ if json['key'] and json['iv']
129
+ @iv = Base64.decode64(json['iv']).strip
130
+ @encryption_key = Base64.decode64(json['key']).strip
131
+ return @encryption_key
132
+ else
133
+ raise "Key file is corrupt!"
134
+ end
135
+ else
136
+ raise "Can't read key file!"
137
+ end
138
+ else
139
+ @encryption_key = Digest::SHA256.digest(rand(32**32).to_s(32))
140
+ @iv = cipher.random_iv
141
+ key_file_json = { key: Base64.encode64(@encryption_key).strip, iv: Base64.encode64(@iv).strip }
142
+ File.open(key_file,"w") do |f|
143
+ cipher.encrypt
144
+ cipher.key = ENV['MASTER_ACCESS_TOKEN']
145
+ enc_json = Base64.encode64(cipher.update(key_file_json.to_json) + cipher.final).strip
146
+ f.write(enc_json)
147
+ end
148
+ end
149
+ @encryption_key
150
+ end
151
+
152
+ def get_iv
153
+ @iv
154
+ end
155
+ end
156
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blobs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oliver Kiessler
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-16 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.13.7
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.13.7
55
+ description: An HTTP API Client for the blob-store service.
56
+ email:
57
+ - kiessler@inceedo.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - blobs.gemspec
70
+ - lib/blobs.rb
71
+ - lib/blobs/version.rb
72
+ homepage: https://gitlab.com/okiess/blobs
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.6.8
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Blob API Client
96
+ test_files: []