azure-storage 0.10.0.preview

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/lib/azure/storage.rb +58 -0
  3. data/lib/azure/storage/autoload.rb +71 -0
  4. data/lib/azure/storage/blob/append.rb +154 -0
  5. data/lib/azure/storage/blob/blob.rb +821 -0
  6. data/lib/azure/storage/blob/blob_service.rb +510 -0
  7. data/lib/azure/storage/blob/block.rb +264 -0
  8. data/lib/azure/storage/blob/container.rb +552 -0
  9. data/lib/azure/storage/blob/page.rb +380 -0
  10. data/lib/azure/storage/blob/serialization.rb +297 -0
  11. data/lib/azure/storage/client.rb +185 -0
  12. data/lib/azure/storage/configurable.rb +137 -0
  13. data/lib/azure/storage/core.rb +33 -0
  14. data/lib/azure/storage/core/auth/shared_access_signature.rb +27 -0
  15. data/lib/azure/storage/core/auth/shared_access_signature_generator.rb +194 -0
  16. data/lib/azure/storage/core/auth/shared_access_signature_signer.rb +49 -0
  17. data/lib/azure/storage/core/auth/shared_key.rb +125 -0
  18. data/lib/azure/storage/core/auth/shared_key_lite.rb +55 -0
  19. data/lib/azure/storage/core/auth/signer.rb +60 -0
  20. data/lib/azure/storage/core/autoload.rb +35 -0
  21. data/lib/azure/storage/core/client_options.rb +334 -0
  22. data/lib/azure/storage/core/client_options_error.rb +39 -0
  23. data/lib/azure/storage/core/constants.rb +1077 -0
  24. data/lib/azure/storage/core/error.rb +47 -0
  25. data/lib/azure/storage/core/filtered_service.rb +54 -0
  26. data/lib/azure/storage/core/http/debug_filter.rb +45 -0
  27. data/lib/azure/storage/core/http/http_error.rb +95 -0
  28. data/lib/azure/storage/core/http/http_filter.rb +62 -0
  29. data/lib/azure/storage/core/http/http_request.rb +182 -0
  30. data/lib/azure/storage/core/http/http_response.rb +105 -0
  31. data/lib/azure/storage/core/http/retry_policy.rb +83 -0
  32. data/lib/azure/storage/core/http/signer_filter.rb +42 -0
  33. data/lib/azure/storage/core/http_client.rb +63 -0
  34. data/lib/azure/storage/core/service.rb +55 -0
  35. data/lib/azure/storage/core/signed_service.rb +54 -0
  36. data/lib/azure/storage/core/sr.rb +83 -0
  37. data/lib/azure/storage/core/utility.rb +254 -0
  38. data/lib/azure/storage/queue/message.rb +39 -0
  39. data/lib/azure/storage/queue/queue.rb +37 -0
  40. data/lib/azure/storage/queue/queue_service.rb +580 -0
  41. data/lib/azure/storage/queue/serialization.rb +113 -0
  42. data/lib/azure/storage/service/access_policy.rb +35 -0
  43. data/lib/azure/storage/service/cors.rb +36 -0
  44. data/lib/azure/storage/service/cors_rule.rb +46 -0
  45. data/lib/azure/storage/service/enumeration_results.rb +30 -0
  46. data/lib/azure/storage/service/logging.rb +45 -0
  47. data/lib/azure/storage/service/metrics.rb +43 -0
  48. data/lib/azure/storage/service/retention_policy.rb +35 -0
  49. data/lib/azure/storage/service/serialization.rb +308 -0
  50. data/lib/azure/storage/service/signed_identifier.rb +39 -0
  51. data/lib/azure/storage/service/storage_service.rb +131 -0
  52. data/lib/azure/storage/service/storage_service_properties.rb +46 -0
  53. data/lib/azure/storage/table/auth/shared_key.rb +68 -0
  54. data/lib/azure/storage/table/auth/shared_key_lite.rb +53 -0
  55. data/lib/azure/storage/table/batch.rb +339 -0
  56. data/lib/azure/storage/table/batch_response.rb +127 -0
  57. data/lib/azure/storage/table/edmtype.rb +136 -0
  58. data/lib/azure/storage/table/entity.rb +40 -0
  59. data/lib/azure/storage/table/guid.rb +33 -0
  60. data/lib/azure/storage/table/query.rb +121 -0
  61. data/lib/azure/storage/table/serialization.rb +117 -0
  62. data/lib/azure/storage/table/table_service.rb +571 -0
  63. data/lib/azure/storage/version.rb +46 -0
  64. metadata +329 -0
@@ -0,0 +1,185 @@
1
+ #-------------------------------------------------------------------------
2
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
3
+ #
4
+ # The MIT License(MIT)
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files(the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions :
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+ #--------------------------------------------------------------------------
24
+
25
+ require 'azure/storage/core/http_client'
26
+
27
+ require 'azure/storage/core'
28
+ require 'azure/storage/core/client_options'
29
+
30
+ require 'azure/storage/blob/blob_service'
31
+ require 'azure/storage/table/table_service'
32
+ require 'azure/storage/queue/queue_service'
33
+
34
+ module Azure::Storage
35
+ class Client
36
+ include Azure::Storage::Configurable
37
+ include Azure::Storage::ClientOptions
38
+ include Azure::Core::HttpClient
39
+
40
+ # Public: Creates an instance of [Azure::Storage::Client]
41
+ #
42
+ # ==== Attributes
43
+ #
44
+ # * +options+ - Hash. Optional parameters.
45
+ #
46
+ # ==== Options
47
+ #
48
+ # Accepted key/value pairs in options parameter are:
49
+ #
50
+ # * +:use_development_storage+ - TrueClass. Whether to use storage emulator.
51
+ # * +:development_storage_proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
52
+ # * +:storage_account_name+ - String. The name of the storage account.
53
+ # * +:storage_access_key+ - Base64 String. The access key of the storage account.
54
+ # * +:storage_sas_token+ - String. The signed access signiture for the storage account or one of its service.
55
+ # * +:storage_blob_host+ - String. Specified Blob serivce endpoint or hostname
56
+ # * +:storage_table_host+ - String. Specified Table serivce endpoint or hostname
57
+ # * +:storage_queue_host+ - String. Specified Queue serivce endpoint or hostname
58
+ # * +:storage_dns_suffix+ - String. The suffix of a regional Storage Serivce, to
59
+ # * +:default_endpoints_protocol+ - String. http or https
60
+ # * +:use_path_style_uri+ - String. Whether use path style URI for specified endpoints
61
+ # * +:ca_file+ - String. File path of the CA file if having issue with SSL
62
+ #
63
+ # The valid set of options inlcude:
64
+ # * Storage Emulator: +:use_development_storage+ required, +:development_storage_proxy_uri+ optionally
65
+ # * Storage account name and key: +:storage_account_name+ and +:storage_access_key+ required, set +:storage_dns_suffix+ necessarily
66
+ # * Storage account name and SAS token: +:storage_account_name+ and +:storage_sas_token+ required, set +:storage_dns_suffix+ necessarily
67
+ # * Specified hosts and SAS token: At least one of the service host and SAS token. It's up to user to ensure the SAS token is suitable for the serivce
68
+ # * Anonymous Blob: only +:storage_blob_host+, if it is to only access blobs within a container
69
+ #
70
+ # Additional notes:
71
+ # * Specified hosts can be set when use account name with access key or sas token
72
+ # * +:default_endpoints_protocol+ can be set if the scheme is not specified in hosts
73
+ # * Storage emulator always use path style URI
74
+ # * +:ca_file+ is independent.
75
+ #
76
+ # When empty options are given, it will try to read settings from Environment Variables. Refer to [Azure::Storage::ClientOptions.env_vars_mapping] for the mapping relationship
77
+ #
78
+ # @return [Azure::Storage::Client]
79
+ def initialize(options = {})
80
+ reset!(options)
81
+ end
82
+
83
+ # Azure Blob service client configured from this Azure Storage client instance
84
+ # @return [Azure::Storage::Blob::BlobService]
85
+ def blobClient(options = {})
86
+ @blobClient ||= Azure::Storage::Blob::BlobService.new(default_client(options))
87
+ end
88
+
89
+ # Azure Queue service client configured from this Azure Storage client instance
90
+ # @return [Azure::Storage::Queue::QueueService]
91
+ def queueClient(options = {})
92
+ @queueClient ||= Azure::Storage::Queue::QueueService.new(default_client(options))
93
+ end
94
+
95
+ # Azure Table service client configured from this Azure Storage client instance
96
+ # @return [Azure::Storage::Table::TableService]
97
+ def tableClient(options = {})
98
+ @tableClient ||= Azure::Storage::Table::TableService.new(default_client(options))
99
+ end
100
+
101
+ class << self
102
+
103
+ # Public: Creates an instance of [Azure::Storage::Client]
104
+ #
105
+ # ==== Attributes
106
+ #
107
+ # * +options+ - Hash. Optional parameters.
108
+ #
109
+ # ==== Options
110
+ #
111
+ # Accepted key/value pairs in options parameter are:
112
+ #
113
+ # * +:use_development_storage+ - TrueClass. Whether to use storage emulator.
114
+ # * +:development_storage_proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
115
+ # * +:storage_account_name+ - String. The name of the storage account.
116
+ # * +:storage_access_key+ - Base64 String. The access key of the storage account.
117
+ # * +:storage_sas_token+ - String. The signed access signiture for the storage account or one of its service.
118
+ # * +:storage_blob_host+ - String. Specified Blob serivce endpoint or hostname
119
+ # * +:storage_table_host+ - String. Specified Table serivce endpoint or hostname
120
+ # * +:storage_queue_host+ - String. Specified Queue serivce endpoint or hostname
121
+ # * +:storage_dns_suffix+ - String. The suffix of a regional Storage Serivce, to
122
+ # * +:default_endpoints_protocol+ - String. http or https
123
+ # * +:use_path_style_uri+ - String. Whether use path style URI for specified endpoints
124
+ # * +:ca_file+ - String. File path of the CA file if having issue with SSL
125
+ #
126
+ # The valid set of options inlcude:
127
+ # * Storage Emulator: +:use_development_storage+ required, +:development_storage_proxy_uri+ optionally
128
+ # * Storage account name and key: +:storage_account_name+ and +:storage_access_key+ required, set +:storage_dns_suffix+ necessarily
129
+ # * Storage account name and SAS token: +:storage_account_name+ and +:storage_sas_token+ required, set +:storage_dns_suffix+ necessarily
130
+ # * Specified hosts and SAS token: At least one of the service host and SAS token. It's up to user to ensure the SAS token is suitable for the serivce
131
+ # * Anonymous Blob: only +:storage_blob_host+, if it is to only access blobs within a container
132
+ #
133
+ # Additional notes:
134
+ # * Specified hosts can be set when use account name with access key or sas token
135
+ # * +:default_endpoints_protocol+ can be set if the scheme is not specified in hosts
136
+ # * Storage emulator always use path style URI
137
+ # * +:ca_file+ is independent.
138
+ #
139
+ # When empty options are given, it will try to read settings from Environment Variables. Refer to [Azure::Storage::ClientOptions.env_vars_mapping] for the mapping relationship
140
+ #
141
+ # @return [Azure::Storage::Client]
142
+ def create(options={})
143
+ Client.new(options)
144
+ end
145
+
146
+ # Public: Creates an instance of [Azure::Storage::Client] with Storage Emulator
147
+ #
148
+ # ==== Attributes
149
+ #
150
+ # * +proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
151
+ #
152
+ # @return [Azure::Storage::Client]
153
+ def create_development(proxy_uri=nil)
154
+ proxy_uri ||= StorageServiceClientConstants::DEV_STORE_URI
155
+ create(:use_development_storage => true, :development_storage_proxy_uri => proxy_uri)
156
+ end
157
+
158
+
159
+ # Public: Creates an instance of [Azure::Storage::Client] from Environment Variables
160
+ #
161
+ # @return [Azure::Storage::Client]
162
+ def create_from_env
163
+ create
164
+ end
165
+
166
+ # Public: Creates an instance of [Azure::Storage::Client] from Environment Variables
167
+ #
168
+ # ==== Attributes
169
+ #
170
+ # * +connection_string+ - String. Please refer to https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/.
171
+ #
172
+ # @return [Azure::Storage::Client]
173
+ def create_from_connection_string(connection_string)
174
+ Client.new(connection_string)
175
+ end
176
+ end
177
+
178
+ private
179
+
180
+ def default_client(opts)
181
+ {client: self}.merge(opts || {})
182
+ end
183
+
184
+ end
185
+ end
@@ -0,0 +1,137 @@
1
+ #-------------------------------------------------------------------------
2
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
3
+ #
4
+ # The MIT License(MIT)
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files(the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions :
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+ #--------------------------------------------------------------------------
24
+
25
+ module Azure::Storage
26
+ # The Azure::Storage::Configurable module provides basic configuration for Azure storage activities.
27
+ module Configurable
28
+
29
+ # @!attribute [w] storage_access_key
30
+ # @return [String] Azure Storage access key.
31
+ # @!attribute storage_account_name
32
+ # @return [String] Azure Storage account name.
33
+ # @!attribute storage_connection_string
34
+ # @return [String] Azure Storage connection string.
35
+ # @!attribute storage_blob_host
36
+ # @return [String] Set the host for the Blob service. Only set this if you want
37
+ # something custom (like, for example, to point this to a LocalStorage
38
+ # emulator). This should be the complete host, including http:// at the
39
+ # start. When using the emulator, make sure to include your account name at
40
+ # the end.
41
+ # @!attribute storage_table_host
42
+ # @return [String] Set the host for the Table service. Only set this if you want
43
+ # something custom (like, for example, to point this to a LocalStorage
44
+ # emulator). This should be the complete host, including http:// at the
45
+ # start. When using the emulator, make sure to include your account name at
46
+ # the end.
47
+ # @!attribute storage_queue_host
48
+ # @return [String] Set the host for the Queue service. Only set this if you want
49
+ # something custom (like, for example, to point this to a LocalStorage
50
+ # emulator). This should be the complete host, including http:// at the
51
+ # start. When using the emulator, make sure to include your account name at
52
+ # the end.
53
+
54
+ attr_accessor :storage_access_key,
55
+ :storage_account_name,
56
+ :storage_connection_string
57
+
58
+ attr_writer :storage_table_host,
59
+ :storage_blob_host,
60
+ :storage_queue_host
61
+
62
+ class << self
63
+ # List of configurable keys for {Azure::Client}
64
+ # @return [Array] of option keys
65
+ def keys
66
+ @keys ||= [
67
+ :storage_access_key,
68
+ :storage_account_name,
69
+ :storage_connection_string,
70
+ :storage_table_host,
71
+ :storage_blob_host,
72
+ :storage_queue_host
73
+ ]
74
+ end
75
+ end
76
+
77
+ # Set configuration options using a block
78
+ def configure
79
+ yield self
80
+ end
81
+
82
+ # Reset configuration options to default values
83
+ def reset_config!(options = {})
84
+ Azure::Storage::Configurable.keys.each do |key|
85
+ value = if self == Azure::Storage
86
+ Azure::Storage::Default.options[key]
87
+ else
88
+ Azure::Storage.send(key)
89
+ end
90
+ instance_variable_set(:"@#{key}", options.fetch(key, value))
91
+ end
92
+ self.send(:reset_agents!) if self.respond_to?(:reset_agents!)
93
+ self
94
+ end
95
+
96
+ alias setup reset_config!
97
+
98
+ # Storage queue host
99
+ # @return [String]
100
+ def storage_queue_host
101
+ @storage_queue_host || default_host(:queue)
102
+ end
103
+
104
+ # Storage blob host
105
+ # @return [String]
106
+ def storage_blob_host
107
+ @storage_blob_host || default_host(:blob)
108
+ end
109
+
110
+ # Storage table host
111
+ # @return [String]
112
+ def storage_table_host
113
+ @storage_table_host || default_host(:table)
114
+ end
115
+
116
+ # Storage file host
117
+ # @return [String]
118
+ def storage_file_host
119
+ @storage_file_host || default_host(:file)
120
+ end
121
+
122
+ def config
123
+ self
124
+ end
125
+
126
+ private
127
+
128
+ def default_host(service)
129
+ "https://#{storage_account_name}.#{service}.core.windows.net"
130
+ end
131
+
132
+ def options
133
+ Hash[Azure::Storage::Configurable.keys.map { |key| [key, instance_variable_get(:"@#{key}")] }]
134
+ end
135
+
136
+ end
137
+ end
@@ -0,0 +1,33 @@
1
+ #-------------------------------------------------------------------------
2
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
3
+ #
4
+ # The MIT License(MIT)
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files(the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions :
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+ #--------------------------------------------------------------------------
24
+
25
+ module Azure
26
+ module Storage
27
+ end
28
+ end
29
+
30
+ require 'azure/storage/core/error'
31
+ require 'azure/storage/core/constants'
32
+ require 'azure/storage/core/sr'
33
+ require 'azure/storage/core/utility'
@@ -0,0 +1,27 @@
1
+ #-------------------------------------------------------------------------
2
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
3
+ #
4
+ # The MIT License(MIT)
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files(the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions :
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+ #--------------------------------------------------------------------------
24
+
25
+
26
+ require 'azure/storage/core/auth/shared_access_signature_generator'
27
+ require 'azure/storage/core/auth/shared_access_signature_signer'
@@ -0,0 +1,194 @@
1
+ #-------------------------------------------------------------------------
2
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
3
+ #
4
+ # The MIT License(MIT)
5
+
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files(the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions :
12
+
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+ #--------------------------------------------------------------------------
24
+
25
+ require 'azure/storage/core'
26
+ require 'azure/storage/core/client_options_error'
27
+ require 'azure/storage/core/auth/signer'
28
+ require 'time'
29
+ require 'uri'
30
+
31
+ # @see https://msdn.microsoft.com/library/azure/dn140255.aspx for more information on construction
32
+ module Azure::Storage
33
+ module Auth
34
+ class SharedAccessSignature
35
+
36
+ DEFAULTS = {
37
+ permissions: 'r',
38
+ version: Azure::Storage::Default::STG_VERSION
39
+ }
40
+
41
+ KEY_MAPPINGS = {
42
+ version: :sv,
43
+ permissions: :sp,
44
+ start: :st,
45
+ expiry: :se,
46
+ identifier: :si
47
+ }
48
+
49
+ BLOB_KEY_MAPPINGS = {
50
+ resource: :sr,
51
+ cache_control: :rscc,
52
+ content_disposition: :rscd,
53
+ content_encoding: :rsce,
54
+ content_language: :rscl,
55
+ content_type: :rsct
56
+ }
57
+
58
+ TABLE_KEY_MAPPINGS = {
59
+ tablename: :tn,
60
+ startpk: :spk,
61
+ endpk: :epk,
62
+ startrk: :srk,
63
+ endrk: :erk
64
+ }
65
+
66
+ OPTIONAL_QUERY_PARAMS = [:sp, :si, :rscc, :rscd, :rsce, :rscl, :rsct, :spk, :srk, :epk, :erk]
67
+
68
+ attr :account_name
69
+
70
+ # Public: Initialize the SharedAccessSignature generator
71
+ #
72
+ # @param account_name [String] The account name. Defaults to the one in the global configuration.
73
+ # @param access_key [String] The access_key encoded in Base64. Defaults to the one in the global configuration.
74
+ def initialize(account_name=Azure::Storage.storage_account_name, access_key=Azure::Storage.storage_access_key)
75
+ @account_name = account_name
76
+ @signer = Azure::Core::Auth::Signer.new(access_key)
77
+ end
78
+
79
+ # Shared Access Signature for the given path and options
80
+ # @param path [String] Path of the URI
81
+ # @param options [Hash]
82
+ #
83
+ # ==== Options
84
+ #
85
+ # * +:permissions+ - String. Combination of 'r','w','d','l' (container only) in this order. Default 'r'
86
+ # * +:start+ - String. UTC Date/Time in ISO8601 format. Optional.
87
+ # * +:expiry+ - String. UTC Date/Time in ISO8601 format. Optional. Default now + 30 minutes.
88
+ # * +:identifier+ - String. Identifier for stored access policy. Optional
89
+ #
90
+ # Below options for Blob only
91
+ # * +:resource+ - String. Resource type, either 'b' (blob) or 'c' (container). Default 'b'
92
+ # * +:cache_control+ - String. Response header override. Optional.
93
+ # * +:content_disposition+ - String. Response header override. Optional.
94
+ # * +:content_encoding+ - String. Response header override. Optional.
95
+ # * +:content_language+ - String. Response header override. Optional.
96
+ # * +:content_type+ - String. Response header override. Optional.
97
+ #
98
+ # Below options for table only
99
+ # * +:startpk+ - String. The start partition key of a specified partition key range. Optional but startpk must accompany startrk.
100
+ # * +:endpk+ - String. The end partition key of a specified partition key range. Optional but endpk must accompany endrk.
101
+ # * +:startrk+ - String. The start row key of a specified row key range. Optional.
102
+ # * +:endrk+ - String. The end row key of a specified row key range. Optional.
103
+ def generate(path, options={})
104
+ service_type = options[:service_type] || Azure::Storage::ServiceType::BLOB
105
+ options.delete(:service_type) if options.key?(:service_type)
106
+
107
+ options[:expiry] ||= (Time.now + 60*30).utc.iso8601
108
+
109
+ raise InvalidOptionsError,"SAS version cannot be set" if options[:version]
110
+
111
+ defs = DEFAULTS
112
+ valid_mappings = KEY_MAPPINGS
113
+ if service_type == Azure::Storage::ServiceType::BLOB
114
+ defs.merge!(resource: 'b')
115
+ valid_mappings.merge!(BLOB_KEY_MAPPINGS)
116
+ elsif service_type == Azure::Storage::ServiceType::TABLE
117
+ defs.merge!(tablename: path)
118
+ valid_mappings.merge!(TABLE_KEY_MAPPINGS)
119
+ end
120
+
121
+ invalid_options = options.reject { |k,v| valid_mappings.key?(k) }
122
+ raise InvalidOptionsError,"invalid options #{invalid_options} provided for SAS token generate" if invalid_options.length > 0
123
+
124
+ options.merge!(defs)
125
+
126
+ # Order is significant
127
+ # The newlines from empty strings here are required
128
+ signable_string =
129
+ [
130
+ options[:permissions],
131
+ options[:start],
132
+ options[:expiry],
133
+ "/#{service_type}/#{account_name}#{path.start_with?('/') ? '' : '/'}#{path}",
134
+ options[:identifier],
135
+ options[:version],
136
+ options[:cache_control],
137
+ options[:content_disposition],
138
+ options[:content_encoding],
139
+ options[:content_language],
140
+ options[:content_type]
141
+ ].join("\n")
142
+
143
+ query_hash = Hash[options.map { |k, v| [KEY_MAPPINGS[k], v] }]
144
+ .reject { |k, v| OPTIONAL_QUERY_PARAMS.include?(k) && v.to_s == '' }
145
+ .merge( sig: @signer.sign(signable_string) )
146
+
147
+ sas_params = URI.encode_www_form(query_hash)
148
+ end
149
+
150
+ # A customised URI reflecting options for the resource signed with Shared Access Signature
151
+ # @param uri [URI] uri to resource including query options
152
+ # @param options [Hash]
153
+ #
154
+ # ==== Options
155
+ #
156
+ # * +:permissions+ - String. Combination of 'r','w','d','l' (container only) in this order. Default 'r'
157
+ # * +:start+ - String. UTC Date/Time in ISO8601 format. Optional.
158
+ # * +:expiry+ - String. UTC Date/Time in ISO8601 format. Optional. Default now + 30 minutes.
159
+ # * +:identifier+ - String. Identifier for stored access policy. Optional
160
+ #
161
+ # Below options for Blob only
162
+ # * +:resource+ - String. Resource type, either 'b' (blob) or 'c' (container). Default 'b'
163
+ # * +:cache_control+ - String. Response header override. Optional.
164
+ # * +:content_disposition+ - String. Response header override. Optional.
165
+ # * +:content_encoding+ - String. Response header override. Optional.
166
+ # * +:content_language+ - String. Response header override. Optional.
167
+ # * +:content_type+ - String. Response header override. Optional.
168
+ #
169
+ # Below options for table only
170
+ # * +:tablename+ - String. Table name for SAS
171
+ # * +:startpk+ - String. The start partition key of a specified partition key range. Optional but startpk must accompany startrk.
172
+ # * +:endpk+ - String. The end partition key of a specified partition key range. Optional but endpk must accompany endrk.
173
+ # * +:startrk+ - String. The start row key of a specified row key range. Optional.
174
+ # * +:endrk+ - String. The end row key of a specified row key range. Optional.
175
+ def sign_uri(uri, options)
176
+ parsed_query = CGI::parse(uri.query || '').inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
177
+
178
+ if parsed_query.has_key?(:restype)
179
+ options[:resource] = parsed_query[:restype].first == 'container' ? 'c' : 'b'
180
+ end
181
+
182
+ if options[:service_type] == nil
183
+ host_splits = uri.host.split('.')
184
+ options[:service_type] = host_splits[1] if host_splits.length > 1 && host_splits[0] == account_name
185
+ end
186
+
187
+ sas_params = generate(uri.path, options)
188
+
189
+ URI.parse(uri.to_s + (uri.query.nil? ? '?' : '&') + sas_params)
190
+ end
191
+
192
+ end
193
+ end
194
+ end