azure-storage-common 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/common/lib/azure/storage/common.rb +26 -0
  3. data/common/lib/azure/storage/common/autoload.rb +61 -0
  4. data/common/lib/azure/storage/common/client.rb +159 -0
  5. data/common/lib/azure/storage/common/client_options.rb +356 -0
  6. data/common/lib/azure/storage/common/client_options_error.rb +41 -0
  7. data/common/lib/azure/storage/common/configurable.rb +212 -0
  8. data/common/lib/azure/storage/common/core.rb +35 -0
  9. data/common/lib/azure/storage/common/core/auth/anonymous_signer.rb +43 -0
  10. data/common/lib/azure/storage/common/core/auth/shared_access_signature.rb +30 -0
  11. data/common/lib/azure/storage/common/core/auth/shared_access_signature_generator.rb +352 -0
  12. data/common/lib/azure/storage/common/core/auth/shared_access_signature_signer.rb +57 -0
  13. data/common/lib/azure/storage/common/core/auth/shared_key.rb +60 -0
  14. data/common/lib/azure/storage/common/core/autoload.rb +50 -0
  15. data/common/lib/azure/storage/common/core/error.rb +43 -0
  16. data/common/lib/azure/storage/common/core/filter/exponential_retry_filter.rb +64 -0
  17. data/common/lib/azure/storage/common/core/filter/linear_retry_filter.rb +55 -0
  18. data/common/lib/azure/storage/common/core/filter/retry_filter.rb +302 -0
  19. data/common/lib/azure/storage/common/core/http_client.rb +65 -0
  20. data/common/lib/azure/storage/common/core/sr.rb +85 -0
  21. data/common/lib/azure/storage/common/core/utility.rb +255 -0
  22. data/common/lib/azure/storage/common/default.rb +868 -0
  23. data/common/lib/azure/storage/common/service/access_policy.rb +37 -0
  24. data/common/lib/azure/storage/common/service/cors.rb +38 -0
  25. data/common/lib/azure/storage/common/service/cors_rule.rb +48 -0
  26. data/common/lib/azure/storage/common/service/enumeration_results.rb +32 -0
  27. data/common/lib/azure/storage/common/service/geo_replication.rb +40 -0
  28. data/common/lib/azure/storage/common/service/logging.rb +47 -0
  29. data/common/lib/azure/storage/common/service/metrics.rb +45 -0
  30. data/common/lib/azure/storage/common/service/retention_policy.rb +37 -0
  31. data/common/lib/azure/storage/common/service/serialization.rb +335 -0
  32. data/common/lib/azure/storage/common/service/signed_identifier.rb +40 -0
  33. data/common/lib/azure/storage/common/service/storage_service.rb +322 -0
  34. data/common/lib/azure/storage/common/service/storage_service_properties.rb +48 -0
  35. data/common/lib/azure/storage/common/service/storage_service_stats.rb +39 -0
  36. data/common/lib/azure/storage/common/version.rb +49 -0
  37. metadata +216 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 41081cda483c6d82249299d9c2533020857be81f
4
+ data.tar.gz: 4db91f0e0a0da049ac28dead1a0b6baa4e7ef94e
5
+ SHA512:
6
+ metadata.gz: 4fe95f90a14ed47278813f8e30a963b2f7cf257e220bcdd0ef65a7e8f7746581509efd0bcec77c323018020979d8e555ae5b9db3687ab206cfeeed6be288cb96
7
+ data.tar.gz: 504924e2d27f5173a0cc2930c5cde9f8a904617d256ebba830a0e7bf1903c0ef0a42df65ccb3cafc529447f55ac970272463cc198b3a20e13550c209fdeb5935
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ #-------------------------------------------------------------------------
4
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
5
+ #
6
+ # The MIT License(MIT)
7
+
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files(the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions :
14
+
15
+ # The above copyright notice and this permission notice shall be included in
16
+ # all copies or substantial portions of the Software.
17
+
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ # THE SOFTWARE.
25
+ #--------------------------------------------------------------------------
26
+ require "azure/storage/common/autoload"
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ #-------------------------------------------------------------------------
4
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
5
+ #
6
+ # The MIT License(MIT)
7
+
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files(the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions :
14
+
15
+ # The above copyright notice and this permission notice shall be included in
16
+ # all copies or substantial portions of the Software.
17
+
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ # THE SOFTWARE.
25
+ #--------------------------------------------------------------------------
26
+
27
+ require "rubygems"
28
+ require "nokogiri"
29
+ require "base64"
30
+ require "openssl"
31
+ require "uri"
32
+ require "faraday"
33
+ require "faraday_middleware"
34
+
35
+ require "azure/storage/common/core/autoload"
36
+ require "azure/storage/common/default"
37
+
38
+ module Azure
39
+ module Storage
40
+ autoload :Common, "azure/storage/common/core"
41
+ module Common
42
+ autoload :Default, "azure/storage/common/default"
43
+ autoload :Configurable, "azure/storage/common/configurable"
44
+ autoload :Client, "azure/storage/common/client"
45
+ autoload :ClientOptions, "azure/storage/common/client_options"
46
+
47
+ module Auth
48
+ autoload :SharedAccessSignature, "azure/storage/common/core/auth/shared_access_signature"
49
+ end
50
+
51
+ module Service
52
+ autoload :Serialization, "azure/storage/common/service/serialization"
53
+ autoload :SignedIdentifier, "azure/storage/common/service/signed_identifier"
54
+ autoload :AccessPolicy, "azure/storage/common/service/access_policy"
55
+ autoload :StorageService, "azure/storage/common/service/storage_service"
56
+ autoload :CorsRule, "azure/storage/common/service/cors_rule"
57
+ autoload :EnumerationResults, "azure/storage/common/service/enumeration_results"
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,159 @@
1
+ # frozen_string_literal: true
2
+
3
+ #-------------------------------------------------------------------------
4
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
5
+ #
6
+ # The MIT License(MIT)
7
+
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files(the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions :
14
+
15
+ # The above copyright notice and this permission notice shall be included in
16
+ # all copies or substantial portions of the Software.
17
+
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ # THE SOFTWARE.
25
+ #--------------------------------------------------------------------------
26
+
27
+ module Azure::Storage::Common
28
+ class Client
29
+ include Azure::Storage::Common::Configurable
30
+ include Azure::Storage::Common::ClientOptions
31
+ include Azure::Storage::Common::Core::HttpClient
32
+
33
+ # Public: Creates an instance of [Azure::Storage::Common::Client]
34
+ #
35
+ # ==== Attributes
36
+ #
37
+ # * +options+ - Hash. Optional parameters.
38
+ #
39
+ # ==== Options
40
+ #
41
+ # Accepted key/value pairs in options parameter are:
42
+ #
43
+ # * +:use_development_storage+ - TrueClass|FalseClass. Whether to use storage emulator.
44
+ # * +:development_storage_proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
45
+ # * +:storage_connection_string+ - String. The storage connection string.
46
+ # * +:storage_account_name+ - String. The name of the storage account.
47
+ # * +:storage_access_key+ - Base64 String. The access key of the storage account.
48
+ # * +:storage_sas_token+ - String. The signed access signature for the storage account or one of its service.
49
+ # * +:storage_blob_host+ - String. Specified Blob serivce endpoint or hostname
50
+ # * +:storage_table_host+ - String. Specified Table serivce endpoint or hostname
51
+ # * +:storage_queue_host+ - String. Specified Queue serivce endpoint or hostname
52
+ # * +:storage_dns_suffix+ - String. The suffix of a regional Storage Serivce, to
53
+ # * +:default_endpoints_protocol+ - String. http or https
54
+ # * +:use_path_style_uri+ - String. Whether use path style URI for specified endpoints
55
+ # * +:ca_file+ - String. File path of the CA file if having issue with SSL
56
+ # * +:user_agent_prefix+ - String. The user agent prefix that can identify the application calls the library
57
+ #
58
+ # The valid set of options include:
59
+ # * Storage Emulator: +:use_development_storage+ required, +:development_storage_proxy_uri+ optionally
60
+ # * Storage account name and key: +:storage_account_name+ and +:storage_access_key+ required, set +:storage_dns_suffix+ necessarily
61
+ # * Storage account name and SAS token: +:storage_account_name+ and +:storage_sas_token+ required, set +:storage_dns_suffix+ necessarily
62
+ # * 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
63
+ # * Anonymous Blob: only +:storage_blob_host+, if it is to only access blobs within a container
64
+ #
65
+ # Additional notes:
66
+ # * Specified hosts can be set when use account name with access key or sas token
67
+ # * +:default_endpoints_protocol+ can be set if the scheme is not specified in hosts
68
+ # * Storage emulator always use path style URI
69
+ # * +:ca_file+ is independent.
70
+ #
71
+ # When empty options are given, it will try to read settings from Environment Variables. Refer to [Azure::Storage::Common::ClientOptions.env_vars_mapping] for the mapping relationship
72
+ #
73
+ # @return [Azure::Storage::Common::Client]
74
+ def initialize(options = {}, &block)
75
+ if options.is_a?(Hash) && options.has_key?(:user_agent_prefix)
76
+ Azure::Storage::Common::Service::StorageService.user_agent_prefix = options[:user_agent_prefix]
77
+ options.delete :user_agent_prefix
78
+ end
79
+ Azure::Storage::Common::Service::StorageService.register_request_callback(&block) if block_given?
80
+ reset!(options)
81
+ end
82
+
83
+ class << self
84
+ # Public: Creates an instance of [Azure::Storage::Common::Client]
85
+ #
86
+ # ==== Attributes
87
+ #
88
+ # * +options+ - Hash. Optional parameters.
89
+ #
90
+ # ==== Options
91
+ #
92
+ # Accepted key/value pairs in options parameter are:
93
+ #
94
+ # * +:use_development_storage+ - TrueClass|FalseClass. Whether to use storage emulator.
95
+ # * +:development_storage_proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
96
+ # * +:storage_account_name+ - String. The name of the storage account.
97
+ # * +:storage_access_key+ - Base64 String. The access key of the storage account.
98
+ # * +:storage_sas_token+ - String. The signed access signature for the storage account or one of its service.
99
+ # * +:storage_blob_host+ - String. Specified Blob service endpoint or hostname
100
+ # * +:storage_table_host+ - String. Specified Table service endpoint or hostname
101
+ # * +:storage_queue_host+ - String. Specified Queue service endpoint or hostname
102
+ # * +:storage_dns_suffix+ - String. The suffix of a regional Storage Service, to
103
+ # * +:default_endpoints_protocol+ - String. http or https
104
+ # * +:use_path_style_uri+ - String. Whether use path style URI for specified endpoints
105
+ # * +:ca_file+ - String. File path of the CA file if having issue with SSL
106
+ # * +:user_agent_prefix+ - String. The user agent prefix that can identify the application calls the library
107
+ #
108
+ # The valid set of options include:
109
+ # * Storage Emulator: +:use_development_storage+ required, +:development_storage_proxy_uri+ optionally
110
+ # * Storage account name and key: +:storage_account_name+ and +:storage_access_key+ required, set +:storage_dns_suffix+ necessarily
111
+ # * Storage account name and SAS token: +:storage_account_name+ and +:storage_sas_token+ required, set +:storage_dns_suffix+ necessarily
112
+ # * 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
113
+ # * Anonymous Blob: only +:storage_blob_host+, if it is to only access blobs within a container
114
+ #
115
+ # Additional notes:
116
+ # * Specified hosts can be set when use account name with access key or sas token
117
+ # * +:default_endpoints_protocol+ can be set if the scheme is not specified in hosts
118
+ # * Storage emulator always use path style URI
119
+ # * +:ca_file+ is independent.
120
+ #
121
+ # When empty options are given, it will try to read settings from Environment Variables. Refer to [Azure::Storage::Common::ClientOptions.env_vars_mapping] for the mapping relationship
122
+ #
123
+ # @return [Azure::Storage::Common::Client]
124
+ def create(options = {}, &block)
125
+ client = Client.new(options, &block)
126
+ end
127
+
128
+ # Public: Creates an instance of [Azure::Storage::Common::Client] with Storage Emulator
129
+ #
130
+ # ==== Attributes
131
+ #
132
+ # * +proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
133
+ #
134
+ # @return [Azure::Storage::Common::Client]
135
+ def create_development(proxy_uri = nil, &block)
136
+ proxy_uri ||= StorageServiceClientConstants::DEV_STORE_URI
137
+ client = create(use_development_storage: true, development_storage_proxy_uri: proxy_uri, &block)
138
+ end
139
+
140
+ # Public: Creates an instance of [Azure::Storage::Common::Client] from Environment Variables
141
+ #
142
+ # @return [Azure::Storage::Client]
143
+ def create_from_env(&block)
144
+ client = create(&block)
145
+ end
146
+
147
+ # Public: Creates an instance of [Azure::Storage::Common::Client] from Environment Variables
148
+ #
149
+ # ==== Attributes
150
+ #
151
+ # * +connection_string+ - String. Please refer to https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/.
152
+ #
153
+ # @return [Azure::Storage::Common::Client]
154
+ def create_from_connection_string(connection_string, &block)
155
+ client = Client.new(connection_string, &block)
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,356 @@
1
+ # frozen_string_literal: true
2
+
3
+ #-------------------------------------------------------------------------
4
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
5
+ #
6
+ # The MIT License(MIT)
7
+
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files(the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions :
14
+
15
+ # The above copyright notice and this permission notice shall be included in
16
+ # all copies or substantial portions of the Software.
17
+
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ # THE SOFTWARE.
25
+ #--------------------------------------------------------------------------
26
+
27
+ require "uri"
28
+ require "azure/storage/common/client_options_error"
29
+ require "azure/storage/common/core/auth/anonymous_signer"
30
+
31
+ module Azure::Storage::Common
32
+ module ClientOptions
33
+ attr_accessor :ca_file
34
+
35
+ # Public: Reset options for [Azure::Storage::Common::Client]
36
+ #
37
+ # ==== Attributes
38
+ #
39
+ # * +options+ - Hash | String. Optional parameters or storage connection string.
40
+ #
41
+ # ==== Options
42
+ #
43
+ # Accepted key/value pairs in options parameter are:
44
+ #
45
+ # * +:use_development_storage+ - TrueClass|FalseClass. Whether to use storage emulator.
46
+ # * +:development_storage_proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
47
+ # * +:storage_connection_string+ - String. The storage connection string.
48
+ # * +:storage_account_name+ - String. The name of the storage account.
49
+ # * +:storage_access_key+ - Base64 String. The access key of the storage account.
50
+ # * +:storage_sas_token+ - String. The signed access signature for the storage account or one of its service.
51
+ # * +:storage_blob_host+ - String. Specified Blob serivce endpoint or hostname
52
+ # * +:storage_table_host+ - String. Specified Table serivce endpoint or hostname
53
+ # * +:storage_queue_host+ - String. Specified Queue serivce endpoint or hostname
54
+ # * +:storage_dns_suffix+ - String. The suffix of a regional Storage Serivce, to
55
+ # * +:default_endpoints_protocol+ - String. http or https
56
+ # * +:use_path_style_uri+ - String. Whether use path style URI for specified endpoints
57
+ # * +:ca_file+ - String. File path of the CA file if having issue with SSL
58
+ #
59
+ # The valid set of options include:
60
+ # * Storage Emulator: +:use_development_storage+ required, +:development_storage_proxy_uri+ optionally
61
+ # * Storage account name and key: +:storage_account_name+ and +:storage_access_key+ required, set +:storage_dns_suffix+ necessarily
62
+ # * Storage account name and SAS token: +:storage_account_name+ and +:storage_sas_token+ required, set +:storage_dns_suffix+ necessarily
63
+ # * 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
64
+ # * Anonymous Blob: only +:storage_blob_host+, if it is to only access blobs within a container
65
+ #
66
+ # Additional notes:
67
+ # * Specified hosts can be set when use account name with access key or sas token
68
+ # * +:default_endpoints_protocol+ can be set if the scheme is not specified in hosts
69
+ # * Storage emulator always use path style URI
70
+ #
71
+ # When empty options are given, it will try to read settings from Environment Variables. Refer to [Azure::Storage::Common:ClientOptions.env_vars_mapping] for the mapping relationship
72
+ #
73
+ # @return [Azure::Storage::Common::Client]
74
+ def reset!(options = {})
75
+ if options.is_a? String
76
+ options = parse_connection_string(options)
77
+ elsif options.is_a? Hash
78
+ # When the options are provided via singleton setup: Azure::Storage.setup()
79
+ options = setup_options if options.length == 0
80
+
81
+ options = parse_connection_string(options[:storage_connection_string]) if options[:storage_connection_string]
82
+ end
83
+
84
+ # Load from environment when no valid input
85
+ options = load_env if options.length == 0
86
+
87
+ @ca_file = options.delete(:ca_file)
88
+ @options = filter(options)
89
+ self.send(:reset_config!, @options) if self.respond_to?(:reset_config!)
90
+ self
91
+ end
92
+
93
+ # Check if this client is configured with the same options
94
+ def same_options?(opts)
95
+ opts.length == 0 || opts.hash == options.hash
96
+ end
97
+
98
+ # The options after validated and normalized
99
+ #
100
+ # @return [Hash]
101
+ def options
102
+ @options ||= {}
103
+ end
104
+
105
+ # The valid options for the storage client
106
+ #
107
+ # @return [Array]
108
+ def self.valid_options
109
+ @valid_options ||= [
110
+ :use_development_storage,
111
+ :development_storage_proxy_uri,
112
+ :storage_account_name,
113
+ :storage_access_key,
114
+ :storage_connection_string,
115
+ :storage_sas_token,
116
+ :storage_blob_host,
117
+ :storage_table_host,
118
+ :storage_queue_host,
119
+ :storage_file_host,
120
+ :storage_dns_suffix,
121
+ :default_endpoints_protocol,
122
+ :use_path_style_uri
123
+ ]
124
+ end
125
+
126
+ # The mapping between Storage Environment Variables and the options name
127
+ #
128
+ # @return [Hash]
129
+ def self.env_vars_mapping
130
+ @env_vars_mapping ||= {
131
+ "EMULATED" => :use_development_storage,
132
+ "AZURE_STORAGE_ACCOUNT" => :storage_account_name,
133
+ "AZURE_STORAGE_ACCESS_KEY" => :storage_access_key,
134
+ "AZURE_STORAGE_CONNECTION_STRING" => :storage_connection_string,
135
+ "AZURE_STORAGE_BLOB_HOST" => :storage_blob_host,
136
+ "AZURE_STORAGE_TABLE_HOST" => :storage_table_host,
137
+ "AZURE_STORAGE_QUEUE_HOST" => :storage_queue_host,
138
+ "AZURE_STORAGE_FILE_HOST" => :storage_file_host,
139
+ "AZURE_STORAGE_SAS_TOKEN" => :storage_sas_token,
140
+ "AZURE_STORAGE_DNS_SUFFIX" => :storage_dns_suffix
141
+ }
142
+ end
143
+
144
+ # The mapping between Storage Connection String items and the options name
145
+ #
146
+ # @return [Hash]
147
+ def self.connection_string_mapping
148
+ @connection_string_mapping ||= {
149
+ "UseDevelopmentStorage" => :use_development_storage,
150
+ "DevelopmentStorageProxyUri" => :development_storage_proxy_uri,
151
+ "DefaultEndpointsProtocol" => :default_endpoints_protocol,
152
+ "AccountName" => :storage_account_name,
153
+ "AccountKey" => :storage_access_key,
154
+ "BlobEndpoint" => :storage_blob_host,
155
+ "TableEndpoint" => :storage_table_host,
156
+ "QueueEndpoint" => :storage_queue_host,
157
+ "FileEndpoint" => :storage_file_host,
158
+ "SharedAccessSignature" => :storage_sas_token,
159
+ "EndpointSuffix" => :storage_dns_suffix
160
+ }
161
+ end
162
+
163
+ private
164
+
165
+ def method_missing(method_name, *args, &block)
166
+ return super unless options.key? method_name
167
+ options[method_name]
168
+ end
169
+
170
+ def filter(opts = {})
171
+ results = {}
172
+
173
+ # P1 - develpoment storage
174
+ begin
175
+ results = validated_options(opts,
176
+ required: [:use_development_storage],
177
+ optional: [:development_storage_proxy_uri])
178
+ results[:use_development_storage] = true
179
+ proxy_uri = results[:development_storage_proxy_uri] ||= StorageServiceClientConstants::DEV_STORE_URI
180
+ results.merge!(storage_account_name: StorageServiceClientConstants::DEVSTORE_STORAGE_ACCOUNT,
181
+ storage_access_key: StorageServiceClientConstants::DEVSTORE_STORAGE_ACCESS_KEY,
182
+ storage_blob_host: "#{proxy_uri}:#{StorageServiceClientConstants::DEVSTORE_BLOB_HOST_PORT}",
183
+ storage_table_host: "#{proxy_uri}:#{StorageServiceClientConstants::DEVSTORE_TABLE_HOST_PORT}",
184
+ storage_queue_host: "#{proxy_uri}:#{StorageServiceClientConstants::DEVSTORE_QUEUE_HOST_PORT}",
185
+ storage_file_host: "#{proxy_uri}:#{StorageServiceClientConstants::DEVSTORE_FILE_HOST_PORT}",
186
+ use_path_style_uri: true)
187
+ return results
188
+ rescue InvalidOptionsError => e
189
+ end
190
+
191
+ # P2 - explicit hosts with account connection string
192
+ begin
193
+ results = validated_options(opts,
194
+ required: [:storage_connection_string],
195
+ optional: [:use_path_style_uri])
196
+ results[:use_path_style_uri] = results.key?(:use_path_style_uri)
197
+ normalize_hosts(results)
198
+ return results
199
+ rescue InvalidOptionsError => e
200
+ end
201
+
202
+ # P3 - account name and key or sas with default hosts or an end suffix
203
+ begin
204
+ results = validated_options(opts,
205
+ required: [:storage_account_name],
206
+ only_one: [:storage_access_key, :storage_sas_token],
207
+ optional: [:default_endpoints_protocol, :storage_dns_suffix])
208
+ protocol = results[:default_endpoints_protocol] ||= StorageServiceClientConstants::DEFAULT_PROTOCOL
209
+ suffix = results[:storage_dns_suffix] ||= StorageServiceClientConstants::DEFAULT_ENDPOINT_SUFFIX
210
+ account = results[:storage_account_name]
211
+ results.merge!(storage_blob_host: "#{protocol}://#{account}.#{ServiceType::BLOB}.#{suffix}",
212
+ storage_table_host: "#{protocol}://#{account}.#{ServiceType::TABLE}.#{suffix}",
213
+ storage_queue_host: "#{protocol}://#{account}.#{ServiceType::QUEUE}.#{suffix}",
214
+ storage_file_host: "#{protocol}://#{account}.#{ServiceType::FILE}.#{suffix}",
215
+ use_path_style_uri: false)
216
+ return results
217
+ rescue InvalidOptionsError => e
218
+ end
219
+
220
+ # P4 - explicit hosts with account name and key
221
+ begin
222
+ results = validated_options(opts,
223
+ required: [:storage_account_name, :storage_access_key],
224
+ at_least_one: [:storage_blob_host, :storage_table_host, :storage_file_host, :storage_queue_host],
225
+ optional: [:use_path_style_uri, :default_endpoints_protocol])
226
+ results[:use_path_style_uri] = results.key?(:use_path_style_uri)
227
+ normalize_hosts(results)
228
+ return results
229
+ rescue InvalidOptionsError => e
230
+ end
231
+
232
+ # P5 - anonymous or sas only for one or more particular services, options with account name/key + hosts should be already validated in P4
233
+ begin
234
+ results = validated_options(opts,
235
+ at_least_one: [:storage_blob_host, :storage_table_host, :storage_file_host, :storage_queue_host],
236
+ optional: [:use_path_style_uri, :default_endpoints_protocol, :storage_sas_token])
237
+ results[:use_path_style_uri] = results.key?(:use_path_style_uri)
238
+ normalize_hosts(results)
239
+ # Adds anonymous signer if no sas token
240
+ results[:signer] = Azure::Storage::Common::Core::Auth::AnonymousSigner.new unless results.key?(:storage_sas_token)
241
+ return results
242
+ rescue InvalidOptionsError => e
243
+ end
244
+
245
+ # P6 - account name and key or sas with explicit hosts
246
+ begin
247
+ results = validated_options(opts,
248
+ required: [:storage_account_name],
249
+ only_one: [:storage_access_key, :storage_sas_token],
250
+ at_least_one: [:storage_blob_host, :storage_table_host, :storage_file_host, :storage_queue_host])
251
+ results[:use_path_style_uri] = results.key?(:use_path_style_uri)
252
+ normalize_hosts(results)
253
+ return results
254
+ rescue InvalidOptionsError => e
255
+ end
256
+
257
+ raise InvalidOptionsError, "options provided are not valid set: #{opts}" # wrong opts if move to this line
258
+ end
259
+
260
+ def normalize_hosts(options)
261
+ if options[:default_endpoints_protocol]
262
+ [:storage_blob_host, :storage_table_host, :storage_file_host, :storage_queue_host].each do |k|
263
+ if options[k]
264
+ raise InvalidOptionsError, "Explict host cannot contain scheme if default_endpoints_protocol is set." if options[k] =~ /^https?/
265
+ options[k] = "#{options[:default_endpoints_protocol]}://#{options[k]}"
266
+ end
267
+ end
268
+ end
269
+ end
270
+
271
+ def is_base64_encoded
272
+ Proc.new do |i|
273
+ i.is_a?(String) && i =~ /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$/
274
+ end
275
+ end
276
+
277
+ def is_url
278
+ Proc.new do |i|
279
+ i = "http://" + i unless i =~ /\Ahttps?:\/\//
280
+ i =~ URI.regexp(["http", "https"])
281
+ end
282
+ end
283
+
284
+ def is_true
285
+ Proc.new { |i| i == true || (i.is_a?(String) && i.downcase == "true") }
286
+ end
287
+
288
+ def is_non_empty_string
289
+ Proc.new { |i| i && i.is_a?(String) && i.strip.length }
290
+ end
291
+
292
+ def validated_options(opts, requirements = {})
293
+ raise InvalidOptionsError, 'nil is not allowed for option\'s value' if opts.values.any? { |v| v == nil }
294
+ required = requirements[:required] || []
295
+ at_least_one = requirements[:at_least_one] || []
296
+ only_one = requirements[:only_one] || []
297
+ optional = requirements[:optional] || []
298
+
299
+ raise InvalidOptionsError, "Not all required keys are provided: #{required}" if required.any? { |k| !opts.key? k }
300
+ raise InvalidOptionsError, "Only one of #{only_one} is required" unless only_one.length == 0 || only_one.count { |k| opts.key? k } == 1
301
+ raise InvalidOptionsError, "At least one of #{at_least_one} is required" unless at_least_one.length == 0 || at_least_one.any? { |k| opts.key? k }
302
+
303
+ @@option_validators ||= {
304
+ use_development_storage: is_true,
305
+ development_storage_proxy_uri: is_url,
306
+ storage_account_name: lambda { |i| i.is_a?(String) },
307
+ storage_access_key: is_base64_encoded,
308
+ storage_sas_token: lambda { |i| i.is_a?(String) },
309
+ storage_blob_host: is_url,
310
+ storage_table_host: is_url,
311
+ storage_queue_host: is_url,
312
+ storage_file_host: is_url,
313
+ storage_dns_suffix: is_url,
314
+ default_endpoints_protocol: lambda { |i| ["http", "https"].include? i.downcase },
315
+ use_path_style_uri: is_true
316
+ }
317
+
318
+ valid_options = required + at_least_one + only_one + optional
319
+ results = {}
320
+
321
+ opts.each do |k, v|
322
+ raise InvalidOptionsError, "#{k} is not included in valid options" unless valid_options.length == 0 || valid_options.include?(k)
323
+ unless @@option_validators.key?(k) && @@option_validators[k].call(v)
324
+ raise InvalidOptionsError, "#{k} is invalid"
325
+ end
326
+ results[k] = v
327
+ end
328
+ results
329
+ end
330
+
331
+ def load_env
332
+ cs = ENV["AZURE_STORAGE_CONNECTION_STRING"]
333
+ return parse_connection_string(cs) if cs
334
+
335
+ opts = {}
336
+ ClientOptions.env_vars_mapping.each { |k, v| opts[v] = ENV[k] if ENV[k] }
337
+ opts
338
+ end
339
+
340
+ def parse_connection_string(connection_string)
341
+ opts = {}
342
+ connection_string.split(";").each do |i|
343
+ e = i.index("=")
344
+ raise InvalidConnectionStringError, Azure::Storage::Common::Core::SR::INVALID_CONNECTION_STRING if e < 0 || e == i.length - 1
345
+ key, value = i[0..e - 1], i[e + 1..i.length - 1]
346
+ raise InvalidConnectionStringError, Azure::Storage::Common::Core::SR::INVALID_CONNECTION_STRING_BAD_KEY % key unless ClientOptions.connection_string_mapping.key? key
347
+ raise InvalidConnectionStringError, Azure::Storage::Common::Core::SR::INVALID_CONNECTION_STRING_EMPTY_KEY % key if value.length == 0
348
+ raise InvalidConnectionStringError, Azure::Storage::Common::Core::SR::INVALID_CONNECTION_STRING_DUPLICATE_KEY % key if opts.key? key
349
+ opts[ClientOptions.connection_string_mapping[key]] = value
350
+ end
351
+ raise InvalidConnectionStringError, Azure::Storage::Common::Core::SR::INVALID_CONNECTION_STRING if opts.length == 0
352
+
353
+ opts
354
+ end
355
+ end
356
+ end