azure-sas 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 53b5149cc877e7373747a5803b482fba6172839a
4
+ data.tar.gz: 36508e8078da0ecc09bf016c1c2a83f2a7e8780a
5
+ SHA512:
6
+ metadata.gz: d25a316b2ab6797abe5e84b105c4ed6c8807ed510ce5262d48820e795f6a0ecb6e8fe70acc18149b9224c24b83b38fbcf616dc76d4fadcd6b122c1706d39b915
7
+ data.tar.gz: acd524a09e1d61e79ea5ece34c485845947b39f1cd7ebf541af77ebdf9e6e3fc476e986f2215da58dbf8a4c739480c564c2d7abd2abd16c3e9239337d6023db5
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ azure-sas (0.1)
5
+ addressable
6
+ azure
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.5.0)
12
+ public_suffix (~> 2.0, >= 2.0.2)
13
+ azure (0.7.7)
14
+ addressable (~> 2.3)
15
+ azure-core (~> 0.1)
16
+ faraday (~> 0.9)
17
+ faraday_middleware (~> 0.10)
18
+ mime-types (>= 1, < 4.0)
19
+ nokogiri (~> 1.6)
20
+ systemu (~> 2.6)
21
+ thor (~> 0.19)
22
+ azure-core (0.1.6)
23
+ faraday (~> 0.9)
24
+ faraday_middleware (~> 0.10)
25
+ nokogiri (~> 1.6)
26
+ faraday (0.9.2)
27
+ multipart-post (>= 1.2, < 3)
28
+ faraday_middleware (0.11.0)
29
+ faraday (>= 0.7.4, < 1.0)
30
+ mime-types (3.1)
31
+ mime-types-data (~> 3.2015)
32
+ mime-types-data (3.2016.0521)
33
+ mini_portile2 (2.1.0)
34
+ minitest (5.10.1)
35
+ multipart-post (2.0.0)
36
+ nokogiri (1.7.0.1)
37
+ mini_portile2 (~> 2.1.0)
38
+ public_suffix (2.0.5)
39
+ rake (12.0.0)
40
+ systemu (2.6.5)
41
+ thor (0.19.4)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ azure-sas!
48
+ bundler (~> 1.5)
49
+ minitest
50
+ rake
51
+
52
+ BUNDLED WITH
53
+ 1.13.6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Michael Lutsiuk
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ # azure-sas
2
+ Shared Access Signature generation for Azure
3
+
4
+ I've implemented this for generating SAS for blobs on azure storage.
5
+ https://github.com/giantmachines/azure-contrib was not suitable, because it
6
+ depends on the deprecated version of `celluloid` and is not actively mainained.
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ t.test_files = FileList['test/**/*test.rb']
6
+ end
7
+
8
+ task default: :test
@@ -0,0 +1,24 @@
1
+ $:.unshift File.expand_path('../lib', __FILE__)
2
+ require 'azure/sas'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'azure-sas'
6
+ spec.version = Azure::SAS::VERSION
7
+ spec.authors = ['Michael Lutsiuk']
8
+ spec.email = ['michael.lutsiuk@gmail.com']
9
+ spec.summary = 'Azure Shared Access Signature generation'
10
+ spec.description = 'Implements the generation of Azure Shared Access Signature (SAS)'
11
+ spec.homepage = ''
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_development_dependency 'bundler', '~> 1.5'
20
+ spec.add_development_dependency 'rake', '~> 12.0'
21
+ spec.add_development_dependency 'minitest', '~> 5.10'
22
+ spec.add_dependency 'azure', '~> 0.7'
23
+ spec.add_dependency 'addressable', '~> 2.5'
24
+ end
@@ -0,0 +1,65 @@
1
+ require 'addressable/uri'
2
+ require 'azure'
3
+ require 'uri'
4
+ require 'time'
5
+
6
+ require 'azure/sas/version'
7
+ require 'azure/sas/canonicalized_resource'
8
+ require 'azure/sas/options'
9
+ require 'azure/sas/string_to_sign'
10
+ require 'azure/sas/sign'
11
+
12
+ module Azure
13
+ # Generates an Azure Shared Access Signature
14
+ # @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/delegating-access-with-a-shared-access-signature
15
+ class SAS
16
+ class BLOB < SAS
17
+ def initialize(*)
18
+ super
19
+ @options.signedresource = BLOB_RESOURCE
20
+ end
21
+
22
+ def signature
23
+ canonicalized_resource = CanonicalizedResource.new(@storage_account, @uri, blob: true).generate
24
+ body = StringToSign::V20120212::Blob.new(canonicalized_resource, @options).generate
25
+ Sign.new(@storage_access_key, body).perform
26
+ end
27
+ end
28
+
29
+ WrongOptionsError = Class.new(StandardError)
30
+
31
+ BLOB_RESOURCE = 'b'.freeze
32
+ CONTAINER_RESOURCE = 'c'.freeze
33
+
34
+ SIGNATURE = 'sig'.freeze
35
+
36
+ def initialize(storage_access_key, storage_account, uri, options = {})
37
+ @uri = Addressable::URI.parse(uri)
38
+ @options = Options.new
39
+ @storage_access_key = storage_access_key
40
+ @storage_account = storage_account
41
+
42
+ options.each do |key, value|
43
+ @options.public_send("#{key}=", value)
44
+ end
45
+ end
46
+
47
+ def generate
48
+ uri = @uri.dup
49
+ uri.query_values = (uri.query_values || {}).merge(query_values)
50
+ uri.to_s
51
+ end
52
+
53
+ private
54
+
55
+ def query_values
56
+ @options
57
+ .to_query_values
58
+ .merge(SIGNATURE => signature)
59
+ end
60
+
61
+ def signature
62
+ raise 'Not implemented'
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,27 @@
1
+ require 'addressable/uri'
2
+ require 'uri'
3
+
4
+ module Azure
5
+ class SAS
6
+ class CanonicalizedResource
7
+ def initialize(storage_account, uri, blob: true)
8
+ @storage_account = storage_account
9
+ @uri = Addressable::URI.parse(uri)
10
+ @blob = blob
11
+ end
12
+
13
+ def generate
14
+ path = URI.unescape(@uri.path.to_s)
15
+
16
+ resource =
17
+ if @blob
18
+ [@storage_account, *path.split('/')]
19
+ else
20
+ [@storage_account, path.split('/')[0]]
21
+ end
22
+
23
+ File.join('/', *resource)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,54 @@
1
+ module Azure
2
+ class SAS
3
+ # Holds all possible options for a SAS generation
4
+ class Options
5
+ FIELDS = {
6
+ signedresource: :sr,
7
+ signedstart: :st,
8
+ signedexpiry: :se,
9
+ signedpermissions: :sp,
10
+ identifier: :si
11
+ }.freeze
12
+
13
+ attr_accessor(*FIELDS.keys)
14
+
15
+ def validate!
16
+ validate_option_value(
17
+ :signedresource,
18
+ signedresource,
19
+ BLOB_RESOURCE, CONTAINER_RESOURCE, nil
20
+ )
21
+
22
+ validate_option_type(:signedstart, signedstart, Time)
23
+ validate_option_type(:signedexpiry, signedstart, Time)
24
+ end
25
+
26
+ def to_query_values
27
+ {
28
+ signedresource: signedresource,
29
+ signedstart: signedstart && signedstart.utc.iso8601,
30
+ signedexpiry: signedexpiry && signedexpiry.utc.iso8601,
31
+ signedpermissions: signedpermissions,
32
+ identifier: identifier
33
+ }.map do |key, value|
34
+ [FIELDS.fetch(key), value] if value
35
+ end.compact.to_h
36
+ end
37
+
38
+ private
39
+
40
+ def validate_option_type(name, val, type)
41
+ unless val.is_a?(type)
42
+ raise WrongOptionsError,
43
+ "#{name.inspect} should be of type #{type}"
44
+ end
45
+ end
46
+
47
+ def validate_option_value(name, val, *allowed)
48
+ raise WrongOptionsError,
49
+ "#{val.inspect} is not allowed value for #{name.inspect}"\
50
+ " (Allowed: #{allowed.map(&:inspect).join(', ')}"
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+ module Azure
2
+ class SAS
3
+ class Sign
4
+ class << self
5
+ attr_accessor :backend
6
+ end
7
+
8
+ self.backend = Azure::Core::Auth::Signer
9
+
10
+ def initialize(key, body)
11
+ @key = key
12
+ @body = body
13
+ end
14
+
15
+ def perform
16
+ self.class.backend.new(@key).sign(@body)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ module Azure
2
+ class SAS
3
+ module StringToSign
4
+ module V20120212
5
+ # @see https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/constructing-a-service-sas
6
+ class Blob
7
+ def initialize(canonicalized_resource, options)
8
+ @canonicalized_resource = canonicalized_resource
9
+ @options = options
10
+ end
11
+
12
+ def generate
13
+ [
14
+ @options.signedpermissions.to_s,
15
+ (@options.signedstart && @options.signedstart.utc.iso8601).to_s,
16
+ (@options.signedexpiry && @options.signedexpiry.utc.iso8601).to_s,
17
+ @canonicalized_resource.to_s,
18
+ @options.identifier.to_s
19
+ ].compact.join("\n").force_encoding('UTF-8')
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module Azure
2
+ class SAS
3
+ VERSION = '0.1'.freeze
4
+ end
5
+ end
@@ -0,0 +1,70 @@
1
+ require 'test_helper'
2
+ require 'securerandom'
3
+
4
+ class Azure::SasTest < Minitest::Test
5
+ def self.key
6
+ @key ||= SecureRandom.hex(10)
7
+ end
8
+
9
+ def self.storage
10
+ @storage ||= SecureRandom.hex(10)
11
+ end
12
+
13
+ def self.canonicalize(uri, blob: true)
14
+ ::Azure::SAS::CanonicalizedResource.new(storage, uri, blob: blob).generate
15
+ end
16
+
17
+ def self.signedstart
18
+ @signedstart ||= Time.now
19
+ end
20
+
21
+ def self.signedexpiry
22
+ @signedexpiry ||= Time.now
23
+ end
24
+
25
+ BLOB_EXAMPLES = {
26
+ [
27
+ 'https://example.com/a',
28
+ {
29
+ signedresource: 'b',
30
+ signedpermissions: 'r',
31
+ signedstart: signedstart,
32
+ signedexpiry: signedexpiry
33
+ }
34
+ ] => [
35
+ 'https://example.com/a?',
36
+ [
37
+ 'se=',
38
+ signedexpiry.utc.iso8601
39
+ ].join,
40
+
41
+ [
42
+ '&sig=',
43
+ key,
44
+ 'r',
45
+ signedstart.utc.iso8601,
46
+ signedexpiry.utc.iso8601,
47
+ canonicalize('https://example.com/a'),
48
+ ''
49
+ ].join,
50
+
51
+ '&sp=r&sr=b',
52
+ [
53
+ '&st=',
54
+ signedstart.utc.iso8601
55
+ ].join
56
+ ].join
57
+ }.freeze
58
+
59
+ def test_blob
60
+ BLOB_EXAMPLES.each do |args, expected|
61
+ actual = Azure::SAS::BLOB.new(
62
+ self.class.key,
63
+ self.class.storage,
64
+ *args
65
+ ).generate
66
+
67
+ assert_equal URI.escape(expected).to_s, URI.unescape(actual)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,15 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'azure/sas'
4
+
5
+ class TestSignBackend
6
+ def initialize(key)
7
+ @key = key
8
+ end
9
+
10
+ def sign(body)
11
+ @key.to_s + body.to_s.tr("\n \t", '')
12
+ end
13
+ end
14
+
15
+ Azure::SAS::Sign.backend = TestSignBackend
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: azure-sas
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Michael Lutsiuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-23 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: azure
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.7'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: addressable
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.5'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.5'
83
+ description: Implements the generation of Azure Shared Access Signature (SAS)
84
+ email:
85
+ - michael.lutsiuk@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - azure-sas.gemspec
97
+ - lib/azure/sas.rb
98
+ - lib/azure/sas/canonicalized_resource.rb
99
+ - lib/azure/sas/options.rb
100
+ - lib/azure/sas/sign.rb
101
+ - lib/azure/sas/string_to_sign.rb
102
+ - lib/azure/sas/version.rb
103
+ - test/azure/sas_test.rb
104
+ - test/test_helper.rb
105
+ homepage: ''
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.6.8
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Azure Shared Access Signature generation
129
+ test_files:
130
+ - test/azure/sas_test.rb
131
+ - test/test_helper.rb