| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Linq; |
| | | 9 | | using AccountSetting = System.Collections.Generic.KeyValuePair<string, System.Func<string, bool>>; |
| | | 10 | | using ConnectionStringFilter = System.Func<System.Collections.Generic.IDictionary<string, string>, System.Collections.Ge |
| | | 11 | | |
| | | 12 | | #pragma warning disable SA1402 // File may only contain a single type |
| | | 13 | | |
| | | 14 | | namespace Azure.Storage |
| | | 15 | | { |
| | | 16 | | internal class StorageConnectionString |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets or sets a value indicating whether the FISMA MD5 setting will be used. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <value><c>false</c> to use the FISMA MD5 setting; <c>true</c> to use the .NET default implementation.</value |
| | 0 | 22 | | internal static bool UseV1MD5 => true; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Validator for the UseDevelopmentStorage setting. Must be "true". |
| | | 26 | | /// </summary> |
| | 2 | 27 | | private static readonly AccountSetting s_useDevelopmentStorageSetting = Setting(Constants.ConnectionStrings.UseD |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Validator for the DevelopmentStorageProxyUri setting. Must be a valid Uri. |
| | | 31 | | /// </summary> |
| | 2 | 32 | | private static readonly AccountSetting s_developmentStorageProxyUriSetting = Setting(Constants.ConnectionStrings |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Validator for the DefaultEndpointsProtocol setting. Must be either "http" or "https". |
| | | 36 | | /// </summary> |
| | 2 | 37 | | private static readonly AccountSetting s_defaultEndpointsProtocolSetting = Setting(Constants.ConnectionStrings.D |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Validator for the AccountName setting. No restrictions. |
| | | 41 | | /// </summary> |
| | 2 | 42 | | private static readonly AccountSetting s_accountNameSetting = Setting(Constants.ConnectionStrings.AccountNameSet |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Validator for the AccountKey setting. No restrictions. |
| | | 46 | | /// </summary> |
| | 2 | 47 | | private static readonly AccountSetting s_accountKeyNameSetting = Setting(Constants.ConnectionStrings.AccountKeyN |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Validator for the AccountKey setting. Must be a valid base64 string. |
| | | 51 | | /// </summary> |
| | 2 | 52 | | private static readonly AccountSetting s_accountKeySetting = Setting(Constants.ConnectionStrings.AccountKeySetti |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Validator for the BlobEndpoint setting. Must be a valid Uri. |
| | | 56 | | /// </summary> |
| | 2 | 57 | | private static readonly AccountSetting s_blobEndpointSetting = Setting(Constants.ConnectionStrings.BlobEndpointS |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Validator for the QueueEndpoint setting. Must be a valid Uri. |
| | | 61 | | /// </summary> |
| | 2 | 62 | | private static readonly AccountSetting s_queueEndpointSetting = Setting(Constants.ConnectionStrings.QueueEndpoin |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Validator for the FileEndpoint setting. Must be a valid Uri. |
| | | 66 | | /// </summary> |
| | 2 | 67 | | private static readonly AccountSetting s_fileEndpointSetting = Setting(Constants.ConnectionStrings.FileEndpointS |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Validator for the TableEndpoint setting. Must be a valid Uri. |
| | | 71 | | /// </summary> |
| | 2 | 72 | | private static readonly AccountSetting s_tableEndpointSetting = Setting(Constants.ConnectionStrings.TableEndpoin |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Validator for the BlobSecondaryEndpoint setting. Must be a valid Uri. |
| | | 76 | | /// </summary> |
| | 2 | 77 | | private static readonly AccountSetting s_blobSecondaryEndpointSetting = Setting(Constants.ConnectionStrings.Blob |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Validator for the QueueSecondaryEndpoint setting. Must be a valid Uri. |
| | | 81 | | /// </summary> |
| | 2 | 82 | | private static readonly AccountSetting s_queueSecondaryEndpointSetting = Setting(Constants.ConnectionStrings.Que |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Validator for the FileSecondaryEndpoint setting. Must be a valid Uri. |
| | | 86 | | /// </summary> |
| | 2 | 87 | | private static readonly AccountSetting s_fileSecondaryEndpointSetting = Setting(Constants.ConnectionStrings.File |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Validator for the TableSecondaryEndpoint setting. Must be a valid Uri. |
| | | 91 | | /// </summary> |
| | 2 | 92 | | private static readonly AccountSetting s_tableSecondaryEndpointSetting = Setting(Constants.ConnectionStrings.Tab |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Validator for the EndpointSuffix setting. Must be a valid Uri. |
| | | 96 | | /// </summary> |
| | 2 | 97 | | private static readonly AccountSetting s_endpointSuffixSetting = Setting(Constants.ConnectionStrings.EndpointSuf |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Validator for the SharedAccessSignature setting. No restrictions. |
| | | 101 | | /// </summary> |
| | 2 | 102 | | private static readonly AccountSetting s_sharedAccessSignatureSetting = Setting(Constants.ConnectionStrings.Shar |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Singleton instance for the development storage account. |
| | | 106 | | /// </summary> |
| | | 107 | | private static StorageConnectionString s_devStoreAccount; |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Initializes a new instance of the <see cref="StorageConnectionString"/> class using the specified |
| | | 111 | | /// account credentials and service endpoints. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="storageCredentials">A StorageCredentials object.</param> |
| | | 114 | | /// <param name="blobStorageUri">A <see cref="System.Uri"/> specifying the Blob service endpoint or endpoints.</ |
| | | 115 | | /// <param name="queueStorageUri">A <see cref="System.Uri"/> specifying the Queue service endpoint or endpoints. |
| | | 116 | | /// <param name="tableStorageUri">A <see cref="System.Uri"/> specifying the Table service endpoint or endpoints. |
| | | 117 | | /// <param name="fileStorageUri">A <see cref="System.Uri"/> specifying the File service endpoint or endpoints.</ |
| | 216 | 118 | | public StorageConnectionString( |
| | 216 | 119 | | object storageCredentials, |
| | 216 | 120 | | (Uri, Uri) blobStorageUri = default, |
| | 216 | 121 | | (Uri, Uri) queueStorageUri = default, |
| | 216 | 122 | | (Uri, Uri) tableStorageUri = default, |
| | 216 | 123 | | (Uri, Uri) fileStorageUri = default) |
| | | 124 | | { |
| | 216 | 125 | | Credentials = storageCredentials; |
| | 216 | 126 | | BlobStorageUri = blobStorageUri; |
| | 216 | 127 | | QueueStorageUri = queueStorageUri; |
| | 216 | 128 | | TableStorageUri = tableStorageUri; |
| | 216 | 129 | | FileStorageUri = fileStorageUri; |
| | 216 | 130 | | DefaultEndpoints = false; |
| | 216 | 131 | | } |
| | | 132 | | |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Gets a <see cref="StorageConnectionString"/> object that references the well-known development storage accou |
| | | 136 | | /// </summary> |
| | | 137 | | /// <value>A <see cref="StorageConnectionString"/> object representing the development storage account.</value> |
| | | 138 | | public static StorageConnectionString DevelopmentStorageAccount |
| | | 139 | | { |
| | | 140 | | get |
| | | 141 | | { |
| | 0 | 142 | | if (s_devStoreAccount == null) |
| | | 143 | | { |
| | 0 | 144 | | s_devStoreAccount = GetDevelopmentStorageAccount(null); |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | return s_devStoreAccount; |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Indicates whether this account is a development storage account. |
| | | 153 | | /// </summary> |
| | 0 | 154 | | internal bool IsDevStoreAccount { get; set; } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// The storage service hostname suffix set by the user, if any. |
| | | 158 | | /// </summary> |
| | 180 | 159 | | internal string EndpointSuffix { get; set; } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// The connection string parsed into settings. |
| | | 163 | | /// </summary> |
| | 480 | 164 | | internal IDictionary<string, string> Settings { get; set; } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// True if the user used a constructor that auto-generates endpoints. |
| | | 168 | | /// </summary> |
| | 284 | 169 | | internal bool DefaultEndpoints { get; set; } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Gets the primary endpoint for the Blob service, as configured for the storage account. |
| | | 173 | | /// </summary> |
| | | 174 | | /// <value>A <see cref="System.Uri"/> containing the primary Blob service endpoint.</value> |
| | 268 | 175 | | public Uri BlobEndpoint => BlobStorageUri.PrimaryUri; |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Gets the primary endpoint for the Queue service, as configured for the storage account. |
| | | 179 | | /// </summary> |
| | | 180 | | /// <value>A <see cref="System.Uri"/> containing the primary Queue service endpoint.</value> |
| | 52 | 181 | | public Uri QueueEndpoint => QueueStorageUri.PrimaryUri; |
| | | 182 | | |
| | | 183 | | /// <summary> |
| | | 184 | | /// Gets the primary endpoint for the Table service, as configured for the storage account. |
| | | 185 | | /// </summary> |
| | | 186 | | /// <value>A <see cref="System.Uri"/> containing the primary Table service endpoint.</value> |
| | 0 | 187 | | public Uri TableEndpoint => TableStorageUri.PrimaryUri; |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Gets the primary endpoint for the File service, as configured for the storage account. |
| | | 191 | | /// </summary> |
| | | 192 | | /// <value>A <see cref="System.Uri"/> containing the primary File service endpoint.</value> |
| | 52 | 193 | | public Uri FileEndpoint => FileStorageUri.PrimaryUri; |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Gets the endpoints for the Blob service at the primary and secondary location, as configured for the storage |
| | | 197 | | /// </summary> |
| | | 198 | | /// <value>A <see cref="System.Uri"/> containing the Blob service endpoints.</value> |
| | 596 | 199 | | public (Uri PrimaryUri, Uri SecondaryUri) BlobStorageUri { get; set; } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Gets the endpoints for the Queue service at the primary and secondary location, as configured for the storag |
| | | 203 | | /// </summary> |
| | | 204 | | /// <value>A <see cref="System.Uri"/> containing the Queue service endpoints.</value> |
| | 328 | 205 | | public (Uri PrimaryUri, Uri SecondaryUri) QueueStorageUri { get; set; } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Gets the endpoints for the Table service at the primary and secondary location, as configured for the storag |
| | | 209 | | /// </summary> |
| | | 210 | | /// <value>A <see cref="System.Uri"/> containing the Table service endpoints.</value> |
| | 0 | 211 | | public (Uri PrimaryUri, Uri SecondaryUri) TableStorageUri { get; set; } |
| | | 212 | | |
| | | 213 | | /// <summary> |
| | | 214 | | /// Gets the endpoints for the File service at the primary and secondary location, as configured for the storage |
| | | 215 | | /// </summary> |
| | | 216 | | /// <value>A <see cref="System.Uri"/> containing the File service endpoints.</value> |
| | 328 | 217 | | public (Uri PrimaryUri, Uri SecondaryUri) FileStorageUri { get; set; } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Gets the credentials used to create this <see cref="StorageConnectionString"/> object. |
| | | 221 | | /// </summary> |
| | | 222 | | /// <value>A StorageCredentials object.</value> |
| | 532 | 223 | | public object Credentials { get; set; } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Private record of the account name for use in ToString(bool). |
| | | 227 | | /// </summary> |
| | | 228 | | internal string _accountName; |
| | | 229 | | |
| | | 230 | | /// <summary> |
| | | 231 | | /// Parses a connection string and returns a <see cref="StorageConnectionString"/> created |
| | | 232 | | /// from the connection string. |
| | | 233 | | /// </summary> |
| | | 234 | | /// <param name="connectionString">A valid connection string.</param> |
| | | 235 | | /// <exception cref="ArgumentNullException">Thrown if <paramref name="connectionString"/> is null or empty.</exc |
| | | 236 | | /// <exception cref="FormatException">Thrown if <paramref name="connectionString"/> is not a valid connection st |
| | | 237 | | /// <exception cref="ArgumentException">Thrown if <paramref name="connectionString"/> cannot be parsed.</excepti |
| | | 238 | | /// <returns>A <see cref="StorageConnectionString"/> object constructed from the values provided in the connecti |
| | | 239 | | public static StorageConnectionString Parse(string connectionString) |
| | | 240 | | { |
| | | 241 | | |
| | 156 | 242 | | if (string.IsNullOrEmpty(connectionString)) |
| | | 243 | | { |
| | 0 | 244 | | throw Errors.ArgumentNull(nameof(connectionString)); |
| | | 245 | | } |
| | | 246 | | |
| | 0 | 247 | | if (ParseCore(connectionString, out StorageConnectionString ret, err => { throw Errors.InvalidFormat(err); } |
| | | 248 | | { |
| | 156 | 249 | | return ret; |
| | | 250 | | } |
| | | 251 | | |
| | 0 | 252 | | throw Errors.ParsingConnectionStringFailed(); |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Indicates whether a connection string can be parsed to return a <see cref="StorageConnectionString"/> object |
| | | 257 | | /// </summary> |
| | | 258 | | /// <param name="connectionString">The connection string to parse.</param> |
| | | 259 | | /// <param name="account">A <see cref="StorageConnectionString"/> object to hold the instance returned if |
| | | 260 | | /// the connection string can be parsed.</param> |
| | | 261 | | /// <returns><b>true</b> if the connection string was successfully parsed; otherwise, <b>false</b>.</returns> |
| | | 262 | | public static bool TryParse(string connectionString, out StorageConnectionString account) |
| | | 263 | | { |
| | 0 | 264 | | if (string.IsNullOrEmpty(connectionString)) |
| | | 265 | | { |
| | 0 | 266 | | account = null; |
| | 0 | 267 | | return false; |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | try |
| | | 271 | | { |
| | 0 | 272 | | return ParseCore(connectionString, out account, err => { }); |
| | | 273 | | } |
| | 0 | 274 | | catch (Exception) |
| | | 275 | | { |
| | 0 | 276 | | account = null; |
| | 0 | 277 | | return false; |
| | | 278 | | } |
| | 0 | 279 | | } |
| | | 280 | | |
| | | 281 | | ///// <summary> |
| | | 282 | | ///// Returns a shared access signature for the account. |
| | | 283 | | ///// </summary> |
| | | 284 | | ///// <param name="policy">A <see cref="SharedAccessAccountPolicy"/> object specifying the access policy for the |
| | | 285 | | ///// <returns>A shared access signature, as a URI query string.</returns> |
| | | 286 | | ///// <remarks>The query string returned includes the leading question mark.</remarks> |
| | | 287 | | //public string GetSharedAccessSignature(SharedAccessAccountPolicy policy) |
| | | 288 | | //{ |
| | | 289 | | // if (!this.Credentials.IsSharedKey) |
| | | 290 | | // { |
| | | 291 | | // var errorMessage = String.Format(CultureInfo.CurrentCulture, SR.CannotCreateSASWithoutAccountKey); |
| | | 292 | | // throw new InvalidOperationException(errorMessage); |
| | | 293 | | // } |
| | | 294 | | |
| | | 295 | | // StorageAccountKey accountKey = this.Credentials.Key; |
| | | 296 | | |
| | | 297 | | // string signature = SharedAccessSignatureHelper.GetHash(policy, this.Credentials.AccountName, Constants.Hea |
| | | 298 | | // UriQueryBuilder builder = SharedAccessSignatureHelper.GetSignature(policy, signature, accountKey.KeyName, |
| | | 299 | | // return builder.ToString(); |
| | | 300 | | //} |
| | | 301 | | |
| | | 302 | | /// <summary> |
| | | 303 | | /// Returns a <see cref="StorageConnectionString"/> with development storage credentials using the specified pro |
| | | 304 | | /// </summary> |
| | | 305 | | /// <param name="proxyUri">The proxy endpoint to use.</param> |
| | | 306 | | /// <returns>The new <see cref="StorageConnectionString"/>.</returns> |
| | | 307 | | private static StorageConnectionString GetDevelopmentStorageAccount(Uri proxyUri) |
| | | 308 | | { |
| | 0 | 309 | | UriBuilder builder = proxyUri != null ? |
| | 0 | 310 | | new UriBuilder(proxyUri.Scheme, proxyUri.Host) : |
| | 0 | 311 | | new UriBuilder("http", "127.0.0.1"); |
| | | 312 | | |
| | 0 | 313 | | builder.Path = Constants.ConnectionStrings.DevStoreAccountName; |
| | | 314 | | |
| | 0 | 315 | | builder.Port = Constants.ConnectionStrings.BlobEndpointPortNumber; |
| | 0 | 316 | | Uri blobEndpoint = builder.Uri; |
| | | 317 | | |
| | 0 | 318 | | builder.Port = Constants.ConnectionStrings.TableEndpointPortNumber; |
| | 0 | 319 | | Uri tableEndpoint = builder.Uri; |
| | | 320 | | |
| | 0 | 321 | | builder.Port = Constants.ConnectionStrings.QueueEndpointPortNumber; |
| | 0 | 322 | | Uri queueEndpoint = builder.Uri; |
| | | 323 | | |
| | 0 | 324 | | builder.Path = Constants.ConnectionStrings.DevStoreAccountName + Constants.ConnectionStrings.SecondaryLocati |
| | | 325 | | |
| | 0 | 326 | | builder.Port = Constants.ConnectionStrings.BlobEndpointPortNumber; |
| | 0 | 327 | | Uri blobSecondaryEndpoint = builder.Uri; |
| | | 328 | | |
| | 0 | 329 | | builder.Port = Constants.ConnectionStrings.QueueEndpointPortNumber; |
| | 0 | 330 | | Uri queueSecondaryEndpoint = builder.Uri; |
| | | 331 | | |
| | 0 | 332 | | builder.Port = Constants.ConnectionStrings.TableEndpointPortNumber; |
| | 0 | 333 | | Uri tableSecondaryEndpoint = builder.Uri; |
| | | 334 | | |
| | 0 | 335 | | var credentials = new StorageSharedKeyCredential(Constants.ConnectionStrings.DevStoreAccountName, Constants. |
| | | 336 | | #pragma warning disable IDE0017 // Simplify object initialization |
| | 0 | 337 | | var account = new StorageConnectionString( |
| | 0 | 338 | | credentials, |
| | 0 | 339 | | blobStorageUri: (blobEndpoint, blobSecondaryEndpoint), |
| | 0 | 340 | | queueStorageUri: (queueEndpoint, queueSecondaryEndpoint), |
| | 0 | 341 | | tableStorageUri: (tableEndpoint, tableSecondaryEndpoint), |
| | 0 | 342 | | fileStorageUri: (default, default) /* fileStorageUri */); |
| | | 343 | | #pragma warning restore IDE0017 // Simplify object initialization |
| | | 344 | | |
| | | 345 | | #pragma warning disable IDE0028 // Simplify collection initialization |
| | 0 | 346 | | account.Settings = new Dictionary<string, string>(); |
| | | 347 | | #pragma warning restore IDE0028 // Simplify collection initialization |
| | 0 | 348 | | account.Settings.Add(Constants.ConnectionStrings.UseDevelopmentSetting, "true"); |
| | 0 | 349 | | if (proxyUri != null) |
| | | 350 | | { |
| | 0 | 351 | | account.Settings.Add(Constants.ConnectionStrings.DevelopmentProxyUriSetting, proxyUri.ToString()); |
| | | 352 | | } |
| | | 353 | | |
| | 0 | 354 | | account.IsDevStoreAccount = true; |
| | | 355 | | |
| | 0 | 356 | | return account; |
| | | 357 | | } |
| | | 358 | | |
| | | 359 | | /// <summary> |
| | | 360 | | /// Internal implementation of Parse/TryParse. |
| | | 361 | | /// </summary> |
| | | 362 | | /// <param name="connectionString">The string to parse.</param> |
| | | 363 | | /// <param name="accountInformation">The <see cref="StorageConnectionString"/> to return.</param> |
| | | 364 | | /// <param name="error">A callback for reporting errors.</param> |
| | | 365 | | /// <returns>If true, the parse was successful. Otherwise, false.</returns> |
| | | 366 | | internal static bool ParseCore(string connectionString, out StorageConnectionString accountInformation, Action<s |
| | | 367 | | { |
| | 156 | 368 | | IDictionary<string, string> settings = ParseStringIntoSettings(connectionString, error); |
| | | 369 | | |
| | | 370 | | // malformed settings string |
| | | 371 | | |
| | 156 | 372 | | if (settings == null) |
| | | 373 | | { |
| | 0 | 374 | | accountInformation = null; |
| | | 375 | | |
| | 0 | 376 | | return false; |
| | | 377 | | } |
| | | 378 | | |
| | | 379 | | // helper method |
| | | 380 | | |
| | | 381 | | string settingOrDefault(string key) |
| | | 382 | | { |
| | 1716 | 383 | | settings.TryGetValue(key, out var result); |
| | | 384 | | |
| | 1716 | 385 | | return result; |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | // devstore case |
| | | 389 | | |
| | 156 | 390 | | if (MatchesSpecification( |
| | 156 | 391 | | settings, |
| | 156 | 392 | | AllRequired(s_useDevelopmentStorageSetting), |
| | 156 | 393 | | Optional(s_developmentStorageProxyUriSetting))) |
| | | 394 | | { |
| | | 395 | | |
| | 0 | 396 | | accountInformation = |
| | 0 | 397 | | settings.TryGetValue(Constants.ConnectionStrings.DevelopmentProxyUriSetting, out var proxyUri) |
| | 0 | 398 | | ? GetDevelopmentStorageAccount(new Uri(proxyUri)) |
| | 0 | 399 | | : DevelopmentStorageAccount; |
| | | 400 | | |
| | 0 | 401 | | accountInformation.Settings = s_validCredentials(settings); |
| | | 402 | | |
| | 0 | 403 | | return true; |
| | | 404 | | } |
| | | 405 | | |
| | | 406 | | // non-devstore case |
| | | 407 | | |
| | 156 | 408 | | ConnectionStringFilter endpointsOptional = |
| | 156 | 409 | | Optional( |
| | 156 | 410 | | s_blobEndpointSetting, s_blobSecondaryEndpointSetting, |
| | 156 | 411 | | s_queueEndpointSetting, s_queueSecondaryEndpointSetting, |
| | 156 | 412 | | s_fileEndpointSetting, s_fileSecondaryEndpointSetting, |
| | 156 | 413 | | s_tableEndpointSetting, s_tableSecondaryEndpointSetting // not supported but we don't want default c |
| | 156 | 414 | | ); |
| | | 415 | | |
| | 156 | 416 | | ConnectionStringFilter primaryEndpointRequired = |
| | 156 | 417 | | AtLeastOne( |
| | 156 | 418 | | s_blobEndpointSetting, |
| | 156 | 419 | | s_queueEndpointSetting, |
| | 156 | 420 | | s_fileEndpointSetting, |
| | 156 | 421 | | s_tableEndpointSetting |
| | 156 | 422 | | ); |
| | | 423 | | |
| | 156 | 424 | | ConnectionStringFilter secondaryEndpointsOptional = |
| | 156 | 425 | | Optional( |
| | 156 | 426 | | s_blobSecondaryEndpointSetting, |
| | 156 | 427 | | s_queueSecondaryEndpointSetting, |
| | 156 | 428 | | s_fileSecondaryEndpointSetting, |
| | 156 | 429 | | s_tableSecondaryEndpointSetting |
| | 156 | 430 | | ); |
| | | 431 | | |
| | 156 | 432 | | ConnectionStringFilter automaticEndpointsMatchSpec = |
| | 156 | 433 | | MatchesExactly(MatchesAll( |
| | 156 | 434 | | MatchesOne( |
| | 156 | 435 | | MatchesAll(AllRequired(s_accountKeySetting), Optional(s_accountKeyNameSetting)), // Key + Name, |
| | 156 | 436 | | AllRequired(s_sharedAccessSignatureSetting) // SAS + Name, Endpoints optional |
| | 156 | 437 | | ), |
| | 156 | 438 | | AllRequired(s_accountNameSetting), // Name required to automatically create URIs |
| | 156 | 439 | | endpointsOptional, |
| | 156 | 440 | | Optional(s_defaultEndpointsProtocolSetting, s_endpointSuffixSetting) |
| | 156 | 441 | | )); |
| | | 442 | | |
| | 156 | 443 | | ConnectionStringFilter explicitEndpointsMatchSpec = |
| | 156 | 444 | | MatchesExactly(MatchesAll( // Any Credentials, Endpoints must be explicitly declared |
| | 156 | 445 | | s_validCredentials, |
| | 156 | 446 | | primaryEndpointRequired, |
| | 156 | 447 | | secondaryEndpointsOptional |
| | 156 | 448 | | )); |
| | | 449 | | |
| | 156 | 450 | | var matchesAutomaticEndpointsSpec = MatchesSpecification(settings, automaticEndpointsMatchSpec); |
| | 156 | 451 | | var matchesExplicitEndpointsSpec = MatchesSpecification(settings, explicitEndpointsMatchSpec); |
| | | 452 | | |
| | 156 | 453 | | if (matchesAutomaticEndpointsSpec || matchesExplicitEndpointsSpec) |
| | | 454 | | { |
| | 156 | 455 | | if (matchesAutomaticEndpointsSpec && !settings.ContainsKey(Constants.ConnectionStrings.DefaultEndpointsP |
| | | 456 | | { |
| | 124 | 457 | | settings.Add(Constants.ConnectionStrings.DefaultEndpointsProtocolSetting, "https"); |
| | | 458 | | } |
| | | 459 | | |
| | 156 | 460 | | var blobEndpoint = settingOrDefault(Constants.ConnectionStrings.BlobEndpointSetting); |
| | 156 | 461 | | var queueEndpoint = settingOrDefault(Constants.ConnectionStrings.QueueEndpointSetting); |
| | 156 | 462 | | var tableEndpoint = settingOrDefault(Constants.ConnectionStrings.TableEndpointSetting); |
| | 156 | 463 | | var fileEndpoint = settingOrDefault(Constants.ConnectionStrings.FileEndpointSetting); |
| | 156 | 464 | | var blobSecondaryEndpoint = settingOrDefault(Constants.ConnectionStrings.BlobSecondaryEndpointSetting); |
| | 156 | 465 | | var queueSecondaryEndpoint = settingOrDefault(Constants.ConnectionStrings.QueueSecondaryEndpointSetting) |
| | 156 | 466 | | var tableSecondaryEndpoint = settingOrDefault(Constants.ConnectionStrings.TableSecondaryEndpointSetting) |
| | 156 | 467 | | var fileSecondaryEndpoint = settingOrDefault(Constants.ConnectionStrings.FileSecondaryEndpointSetting); |
| | 156 | 468 | | var sasToken = settingOrDefault(Constants.ConnectionStrings.SharedAccessSignatureSetting); |
| | | 469 | | |
| | | 470 | | // if secondary is specified, primary must also be specified |
| | | 471 | | |
| | | 472 | | static bool s_isValidEndpointPair(string primary, string secondary) => |
| | 624 | 473 | | !string.IsNullOrWhiteSpace(primary) |
| | 624 | 474 | | || /* primary is null, and... */ string.IsNullOrWhiteSpace(secondary); |
| | | 475 | | |
| | | 476 | | (Uri, Uri) createStorageUri(string primary, string secondary, string sasToken, Func<IDictionary<string, |
| | | 477 | | { |
| | 624 | 478 | | return |
| | 624 | 479 | | !string.IsNullOrWhiteSpace(secondary) && !string.IsNullOrWhiteSpace(primary) |
| | 624 | 480 | | ? (CreateUri(primary, sasToken), CreateUri(secondary, sasToken)) |
| | 624 | 481 | | : !string.IsNullOrWhiteSpace(primary) |
| | 624 | 482 | | ? (CreateUri(primary, sasToken), default) |
| | 624 | 483 | | : matchesAutomaticEndpointsSpec && factory != null |
| | 624 | 484 | | ? factory(settings) |
| | 624 | 485 | | : (default, default); |
| | | 486 | | |
| | | 487 | | static Uri CreateUri(string endpoint, string sasToken) |
| | | 488 | | { |
| | 632 | 489 | | var builder = new UriBuilder(endpoint); |
| | 632 | 490 | | if (!string.IsNullOrEmpty(builder.Query)) |
| | | 491 | | { |
| | 0 | 492 | | builder.Query += "&" + sasToken; |
| | | 493 | | } |
| | | 494 | | else |
| | | 495 | | { |
| | 632 | 496 | | builder.Query = sasToken; |
| | | 497 | | } |
| | 632 | 498 | | return builder.Uri; |
| | | 499 | | } |
| | | 500 | | } |
| | | 501 | | |
| | 156 | 502 | | if ( |
| | 156 | 503 | | s_isValidEndpointPair(blobEndpoint, blobSecondaryEndpoint) |
| | 156 | 504 | | && s_isValidEndpointPair(queueEndpoint, queueSecondaryEndpoint) |
| | 156 | 505 | | && s_isValidEndpointPair(tableEndpoint, tableSecondaryEndpoint) |
| | 156 | 506 | | && s_isValidEndpointPair(fileEndpoint, fileSecondaryEndpoint) |
| | 156 | 507 | | ) |
| | | 508 | | { |
| | 156 | 509 | | accountInformation = |
| | 156 | 510 | | new StorageConnectionString( |
| | 156 | 511 | | GetCredentials(settings), |
| | 156 | 512 | | blobStorageUri: createStorageUri(blobEndpoint, blobSecondaryEndpoint, sasToken, ConstructBlo |
| | 156 | 513 | | queueStorageUri: createStorageUri(queueEndpoint, queueSecondaryEndpoint, sasToken, Construct |
| | 156 | 514 | | tableStorageUri: createStorageUri(tableEndpoint, tableSecondaryEndpoint, sasToken, Construct |
| | 156 | 515 | | fileStorageUri: createStorageUri(fileEndpoint, fileSecondaryEndpoint, sasToken, ConstructFil |
| | 156 | 516 | | ) |
| | 156 | 517 | | { |
| | 156 | 518 | | EndpointSuffix = settingOrDefault(Constants.ConnectionStrings.EndpointSuffixSetting), |
| | 156 | 519 | | Settings = s_validCredentials(settings) |
| | 156 | 520 | | }; |
| | | 521 | | |
| | 156 | 522 | | accountInformation._accountName = settingOrDefault(Constants.ConnectionStrings.AccountNameSetting); |
| | | 523 | | |
| | 156 | 524 | | return true; |
| | | 525 | | } |
| | | 526 | | } |
| | | 527 | | |
| | | 528 | | // not valid |
| | 0 | 529 | | accountInformation = null; |
| | | 530 | | |
| | 0 | 531 | | error("No valid combination of account information found."); |
| | | 532 | | |
| | 0 | 533 | | return false; |
| | | 534 | | } |
| | | 535 | | |
| | | 536 | | /// <summary> |
| | | 537 | | /// Tokenizes input and stores name value pairs. |
| | | 538 | | /// </summary> |
| | | 539 | | /// <param name="connectionString">The string to parse.</param> |
| | | 540 | | /// <param name="error">Error reporting delegate.</param> |
| | | 541 | | /// <returns>Tokenized collection.</returns> |
| | | 542 | | private static IDictionary<string, string> ParseStringIntoSettings(string connectionString, Action<string> error |
| | | 543 | | { |
| | 156 | 544 | | IDictionary<string, string> settings = new Dictionary<string, string>(); |
| | 156 | 545 | | var splitted = connectionString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); |
| | | 546 | | |
| | 2184 | 547 | | foreach (var nameValue in splitted) |
| | | 548 | | { |
| | 936 | 549 | | var splittedNameValue = nameValue.Split(new char[] { '=' }, 2); |
| | | 550 | | |
| | 936 | 551 | | if (splittedNameValue.Length != 2) |
| | | 552 | | { |
| | 0 | 553 | | error("Settings must be of the form \"name=value\"."); |
| | 0 | 554 | | return null; |
| | | 555 | | } |
| | | 556 | | |
| | 936 | 557 | | if (settings.ContainsKey(splittedNameValue[0])) |
| | | 558 | | { |
| | 0 | 559 | | error(string.Format(CultureInfo.InvariantCulture, "Duplicate setting '{0}' found.", splittedNameValu |
| | 0 | 560 | | return null; |
| | | 561 | | } |
| | | 562 | | |
| | 936 | 563 | | settings.Add(splittedNameValue[0], splittedNameValue[1]); |
| | | 564 | | } |
| | | 565 | | |
| | 156 | 566 | | return settings; |
| | | 567 | | } |
| | | 568 | | |
| | | 569 | | /// <summary> |
| | | 570 | | /// Encapsulates a validation rule for an enumeration based account setting. |
| | | 571 | | /// </summary> |
| | | 572 | | /// <param name="name">The name of the setting.</param> |
| | | 573 | | /// <param name="validValues">A list of valid values for the setting.</param> |
| | | 574 | | /// <returns>An <see cref="AccountSetting"/> representing the enumeration constraint.</returns> |
| | | 575 | | private static AccountSetting Setting(string name, params string[] validValues) => |
| | 10 | 576 | | new AccountSetting( |
| | 10 | 577 | | name, |
| | 854 | 578 | | (settingValue) => validValues.Length == 0 ? true : validValues.Contains(settingValue) |
| | 10 | 579 | | ); |
| | | 580 | | |
| | | 581 | | /// <summary> |
| | | 582 | | /// Encapsulates a validation rule using a func. |
| | | 583 | | /// </summary> |
| | | 584 | | /// <param name="name">The name of the setting.</param> |
| | | 585 | | /// <param name="isValid">A func that determines if the value is valid.</param> |
| | | 586 | | /// <returns>An <see cref="AccountSetting"/> representing the constraint.</returns> |
| | 22 | 587 | | private static AccountSetting Setting(string name, Func<string, bool> isValid) => new AccountSetting(name, isVal |
| | | 588 | | |
| | | 589 | | /// <summary> |
| | | 590 | | /// Determines whether the specified setting value is a valid base64 string. |
| | | 591 | | /// </summary> |
| | | 592 | | /// <param name="settingValue">The setting value.</param> |
| | | 593 | | /// <returns><c>true</c> if the specified setting value is a valid base64 string; otherwise, <c>false</c>.</retu |
| | | 594 | | private static bool IsValidBase64String(string settingValue) |
| | | 595 | | { |
| | | 596 | | try |
| | | 597 | | { |
| | 620 | 598 | | Convert.FromBase64String(settingValue); |
| | | 599 | | |
| | 620 | 600 | | return true; |
| | | 601 | | } |
| | 0 | 602 | | catch (FormatException) |
| | | 603 | | { |
| | 0 | 604 | | return false; |
| | | 605 | | } |
| | 620 | 606 | | } |
| | | 607 | | |
| | | 608 | | /// <summary> |
| | | 609 | | /// Validation function that validates Uris. |
| | | 610 | | /// </summary> |
| | | 611 | | /// <param name="settingValue">Value to validate.</param> |
| | | 612 | | /// <returns><c>true</c> if the specified setting value is a valid Uri; otherwise, <c>false</c>.</returns> |
| | 1216 | 613 | | private static bool IsValidUri(string settingValue) => Uri.IsWellFormedUriString(settingValue, UriKind.Absolute) |
| | | 614 | | |
| | | 615 | | /// <summary> |
| | | 616 | | /// Validation function that validates a domain name. |
| | | 617 | | /// </summary> |
| | | 618 | | /// <param name="settingValue">Value to validate.</param> |
| | | 619 | | /// <returns><c>true</c> if the specified setting value is a valid domain; otherwise, <c>false</c>.</returns> |
| | 8 | 620 | | private static bool IsValidDomain(string settingValue) => Uri.CheckHostName(settingValue).Equals(UriHostNameType |
| | | 621 | | |
| | | 622 | | /// <summary> |
| | | 623 | | /// Settings filter that requires all specified settings be present and valid. |
| | | 624 | | /// </summary> |
| | | 625 | | /// <param name="requiredSettings">A list of settings that must be present.</param> |
| | | 626 | | /// <returns>The remaining settings or <c>null</c> if the filter's requirement is not satisfied.</returns> |
| | | 627 | | private static ConnectionStringFilter AllRequired(params AccountSetting[] requiredSettings) => |
| | 628 | 628 | | (settings) => |
| | 628 | 629 | | { |
| | 1876 | 630 | | IDictionary<string, string> result = new Dictionary<string, string>(settings); |
| | 628 | 631 | | |
| | 5500 | 632 | | foreach (AccountSetting requirement in requiredSettings) |
| | 628 | 633 | | { |
| | 2140 | 634 | | if (result.TryGetValue(requirement.Key, out var value) && requirement.Value(value)) |
| | 628 | 635 | | { |
| | 1492 | 636 | | result.Remove(requirement.Key); |
| | 628 | 637 | | } |
| | 628 | 638 | | else |
| | 628 | 639 | | { |
| | 1276 | 640 | | return null; |
| | 628 | 641 | | } |
| | 628 | 642 | | } |
| | 628 | 643 | | |
| | 1228 | 644 | | return result; |
| | 628 | 645 | | }; |
| | | 646 | | |
| | | 647 | | /// <summary> |
| | | 648 | | /// Settings filter that removes optional values. |
| | | 649 | | /// </summary> |
| | | 650 | | /// <param name="optionalSettings">A list of settings that are optional.</param> |
| | | 651 | | /// <returns>The remaining settings or <c>null</c> if the filter's requirement is not satisfied.</returns> |
| | | 652 | | private static ConnectionStringFilter Optional(params AccountSetting[] optionalSettings) => |
| | 784 | 653 | | (settings) => |
| | 784 | 654 | | { |
| | 1632 | 655 | | IDictionary<string, string> result = new Dictionary<string, string>(settings); |
| | 784 | 656 | | |
| | 7176 | 657 | | foreach (AccountSetting requirement in optionalSettings) |
| | 784 | 658 | | { |
| | 3132 | 659 | | if (result.TryGetValue(requirement.Key, out var value) && requirement.Value(value)) |
| | 784 | 660 | | { |
| | 1716 | 661 | | result.Remove(requirement.Key); |
| | 784 | 662 | | } |
| | 784 | 663 | | } |
| | 784 | 664 | | |
| | 1632 | 665 | | return result; |
| | 784 | 666 | | }; |
| | | 667 | | |
| | | 668 | | /// <summary> |
| | | 669 | | /// Settings filter that ensures that at least one setting is present. |
| | | 670 | | /// </summary> |
| | | 671 | | /// <param name="atLeastOneSettings">A list of settings of which one must be present.</param> |
| | | 672 | | /// <returns>The remaining settings or <c>null</c> if the filter's requirement is not satisfied.</returns> |
| | | 673 | | [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = |
| | | 674 | | private static ConnectionStringFilter AtLeastOne(params AccountSetting[] atLeastOneSettings) => |
| | 156 | 675 | | (settings) => |
| | 156 | 676 | | { |
| | 312 | 677 | | IDictionary<string, string> result = new Dictionary<string, string>(settings); |
| | 312 | 678 | | var foundOne = false; |
| | 156 | 679 | | |
| | 1716 | 680 | | foreach (AccountSetting requirement in atLeastOneSettings) |
| | 156 | 681 | | { |
| | 780 | 682 | | if (result.TryGetValue(requirement.Key, out var value) && requirement.Value(value)) |
| | 156 | 683 | | { |
| | 472 | 684 | | result.Remove(requirement.Key); |
| | 472 | 685 | | foundOne = true; |
| | 156 | 686 | | } |
| | 156 | 687 | | } |
| | 156 | 688 | | |
| | 312 | 689 | | return foundOne ? result : null; |
| | 156 | 690 | | }; |
| | | 691 | | |
| | | 692 | | /// <summary> |
| | | 693 | | /// Settings filter that ensures that none of the specified settings are present. |
| | | 694 | | /// </summary> |
| | | 695 | | /// <param name="atLeastOneSettings">A list of settings of which one must not be present.</param> |
| | | 696 | | /// <returns>The remaining settings or <c>null</c> if the filter's requirement is not satisfied.</returns> |
| | | 697 | | [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1305:FieldNamesMustNotUseHungarianNotation", Justification = |
| | | 698 | | private static ConnectionStringFilter None(params AccountSetting[] atLeastOneSettings) => |
| | 6 | 699 | | (settings) => |
| | 6 | 700 | | { |
| | 630 | 701 | | IDictionary<string, string> result = new Dictionary<string, string>(settings); |
| | 630 | 702 | | var foundOne = false; |
| | 6 | 703 | | |
| | 4502 | 704 | | foreach (AccountSetting requirement in atLeastOneSettings) |
| | 6 | 705 | | { |
| | 1630 | 706 | | if (result.TryGetValue(requirement.Key, out var value) && requirement.Value(value)) |
| | 6 | 707 | | { |
| | 582 | 708 | | foundOne = true; |
| | 6 | 709 | | } |
| | 6 | 710 | | } |
| | 6 | 711 | | |
| | 630 | 712 | | return foundOne ? null : result; |
| | 6 | 713 | | }; |
| | | 714 | | |
| | | 715 | | /// <summary> |
| | | 716 | | /// Settings filter that ensures that all of the specified filters match. |
| | | 717 | | /// </summary> |
| | | 718 | | /// <param name="filters">A list of filters of which all must match.</param> |
| | | 719 | | /// <returns>The remaining settings or <c>null</c> if the filter's requirement is not satisfied.</returns> |
| | | 720 | | private static ConnectionStringFilter MatchesAll(params ConnectionStringFilter[] filters) => |
| | 472 | 721 | | (settings) => |
| | 472 | 722 | | { |
| | 1564 | 723 | | IDictionary<string, string> result = new Dictionary<string, string>(settings); |
| | 472 | 724 | | |
| | 8160 | 725 | | foreach (ConnectionStringFilter filter in filters) |
| | 472 | 726 | | { |
| | 3412 | 727 | | if (result == null) |
| | 472 | 728 | | { |
| | 472 | 729 | | break; |
| | 472 | 730 | | } |
| | 472 | 731 | | |
| | 3036 | 732 | | result = filter(result); |
| | 472 | 733 | | } |
| | 472 | 734 | | |
| | 1564 | 735 | | return result; |
| | 472 | 736 | | }; |
| | | 737 | | |
| | | 738 | | /// <summary> |
| | | 739 | | /// Settings filter that ensures that exactly one filter matches. |
| | | 740 | | /// </summary> |
| | | 741 | | /// <param name="filters">A list of filters of which exactly one must match.</param> |
| | | 742 | | /// <returns>The remaining settings or <c>null</c> if the filter's requirement is not satisfied.</returns> |
| | | 743 | | private static ConnectionStringFilter MatchesOne(params ConnectionStringFilter[] filters) => |
| | 158 | 744 | | (settings) => |
| | 158 | 745 | | { |
| | 626 | 746 | | IDictionary<string, string>[] results = |
| | 626 | 747 | | filters |
| | 1874 | 748 | | .Select(filter => filter(new Dictionary<string, string>(settings))) |
| | 1874 | 749 | | .Where(result => result != null) |
| | 626 | 750 | | .Take(2) |
| | 626 | 751 | | .ToArray(); |
| | 158 | 752 | | |
| | 626 | 753 | | return results.Length != 1 ? null : results.First(); |
| | 158 | 754 | | }; |
| | | 755 | | |
| | | 756 | | /// <summary> |
| | | 757 | | /// Settings filter that ensures that the specified filter is an exact match. |
| | | 758 | | /// </summary> |
| | | 759 | | /// <param name="filter">A list of filters of which ensures that the specified filter is an exact match.</param> |
| | | 760 | | /// <returns>The remaining settings or <c>null</c> if the filter's requirement is not satisfied.</returns> |
| | | 761 | | private static ConnectionStringFilter MatchesExactly(ConnectionStringFilter filter) => |
| | 312 | 762 | | (settings) => |
| | 312 | 763 | | { |
| | 624 | 764 | | IDictionary<string, string> results = filter(settings); |
| | 312 | 765 | | |
| | 624 | 766 | | return results == null || results.Any() ? null : results; |
| | 312 | 767 | | }; |
| | | 768 | | |
| | | 769 | | /// <summary> |
| | | 770 | | /// Settings filter that ensures that a valid combination of credentials is present. |
| | | 771 | | /// </summary> |
| | | 772 | | /// <returns>The remaining settings or <c>null</c> if the filter's requirement is not satisfied.</returns> |
| | 2 | 773 | | private static readonly ConnectionStringFilter s_validCredentials = |
| | 2 | 774 | | MatchesOne( |
| | 2 | 775 | | MatchesAll(AllRequired(s_accountNameSetting, s_accountKeySetting), Optional(s_accountKeyNameSetting), No |
| | 2 | 776 | | MatchesAll(AllRequired(s_sharedAccessSignatureSetting), Optional(s_accountNameSetting), None(s_accountKe |
| | 2 | 777 | | None(s_accountNameSetting, s_accountKeySetting, s_accountKeyNameSetting, s_sharedAccessSignatureSetting) |
| | 2 | 778 | | ); |
| | | 779 | | |
| | | 780 | | /// <summary> |
| | | 781 | | /// Tests to see if a given list of settings matches a set of filters exactly. |
| | | 782 | | /// </summary> |
| | | 783 | | /// <param name="settings">The settings to check.</param> |
| | | 784 | | /// <param name="constraints">A list of filters to check.</param> |
| | | 785 | | /// <returns> |
| | | 786 | | /// If any filter returns null, false. |
| | | 787 | | /// If there are any settings left over after all filters are processed, false. |
| | | 788 | | /// Otherwise true. |
| | | 789 | | /// </returns> |
| | | 790 | | private static bool MatchesSpecification( |
| | | 791 | | IDictionary<string, string> settings, |
| | | 792 | | params ConnectionStringFilter[] constraints) |
| | | 793 | | { |
| | 1684 | 794 | | foreach (ConnectionStringFilter constraint in constraints) |
| | | 795 | | { |
| | 468 | 796 | | IDictionary<string, string> remainingSettings = constraint(settings); |
| | | 797 | | |
| | 468 | 798 | | if (remainingSettings == null) |
| | | 799 | | { |
| | 188 | 800 | | return false; |
| | | 801 | | } |
| | | 802 | | else |
| | | 803 | | { |
| | 280 | 804 | | settings = remainingSettings; |
| | | 805 | | } |
| | | 806 | | } |
| | | 807 | | |
| | 280 | 808 | | return !settings.Any(); |
| | | 809 | | } |
| | | 810 | | |
| | | 811 | | /// <summary> |
| | | 812 | | /// Gets a StorageCredentials object corresponding to whatever credentials are supplied in the given settings. |
| | | 813 | | /// </summary> |
| | | 814 | | /// <param name="settings">The settings to check.</param> |
| | | 815 | | /// <returns>The StorageCredentials object specified in the settings.</returns> |
| | | 816 | | private static object GetCredentials(IDictionary<string, string> settings) |
| | | 817 | | { |
| | | 818 | | |
| | 156 | 819 | | settings.TryGetValue(Constants.ConnectionStrings.AccountNameSetting, out var accountName); |
| | 156 | 820 | | settings.TryGetValue(Constants.ConnectionStrings.AccountKeySetting, out var accountKey); |
| | 156 | 821 | | settings.TryGetValue(Constants.ConnectionStrings.SharedAccessSignatureSetting, out var sharedAccessSignature |
| | | 822 | | |
| | | 823 | | // The accountKeyName isn't used |
| | | 824 | | //settings.TryGetValue(Constants.ConnectionStrings.AccountKeyNameSetting, out var accountKeyName); |
| | | 825 | | |
| | 156 | 826 | | return |
| | 156 | 827 | | accountName != null && accountKey != null && sharedAccessSignature == null |
| | 156 | 828 | | ? new StorageSharedKeyCredential(accountName, accountKey/*, accountKeyName */) |
| | 156 | 829 | | : (object)(accountKey == null /* && accountKeyName == null */ && sharedAccessSignature != null |
| | 156 | 830 | | ? new SharedAccessSignatureCredentials(sharedAccessSignature) |
| | 156 | 831 | | : null); |
| | | 832 | | } |
| | | 833 | | |
| | | 834 | | /// <summary> |
| | | 835 | | /// Gets the default blob endpoint using specified settings. |
| | | 836 | | /// </summary> |
| | | 837 | | /// <param name="settings">The settings to use.</param> |
| | | 838 | | /// <returns>The default blob endpoint.</returns> |
| | 8 | 839 | | private static (Uri, Uri) ConstructBlobEndpoint(IDictionary<string, string> settings) => ConstructBlobEndpoint( |
| | 8 | 840 | | settings[Constants.ConnectionStrings.DefaultEndpointsProtocolSetting], |
| | 8 | 841 | | settings[Constants.ConnectionStrings.AccountNameSetting], |
| | 8 | 842 | | settings.ContainsKey(Constants.ConnectionStrings.EndpointSuffixSetting) ? settings[Constants.ConnectionS |
| | 8 | 843 | | settings.ContainsKey(Constants.ConnectionStrings.SharedAccessSignatureSetting) ? settings[Constants.Conn |
| | | 844 | | |
| | | 845 | | /// <summary> |
| | | 846 | | /// Gets the default blob endpoint using the specified protocol and account name. |
| | | 847 | | /// </summary> |
| | | 848 | | /// <param name="scheme">The protocol to use.</param> |
| | | 849 | | /// <param name="accountName">The name of the storage account.</param> |
| | | 850 | | /// <param name="endpointSuffix">The Endpoint DNS suffix; use <c>null</c> for default.</param> |
| | | 851 | | /// <param name="sasToken">The sas token; use <c>null</c> for default.</param> |
| | | 852 | | /// <returns>The default blob endpoint.</returns> |
| | | 853 | | internal static (Uri, Uri) ConstructBlobEndpoint(string scheme, string accountName, string endpointSuffix, strin |
| | | 854 | | { |
| | 40 | 855 | | if (string.IsNullOrEmpty(scheme)) |
| | | 856 | | { |
| | 0 | 857 | | throw Errors.ArgumentNull(nameof(scheme)); |
| | | 858 | | } |
| | | 859 | | |
| | 40 | 860 | | if (string.IsNullOrEmpty(accountName)) |
| | | 861 | | { |
| | 0 | 862 | | throw Errors.ArgumentNull(nameof(accountName)); |
| | | 863 | | } |
| | | 864 | | |
| | 40 | 865 | | if (string.IsNullOrEmpty(endpointSuffix)) |
| | | 866 | | { |
| | 24 | 867 | | endpointSuffix = Constants.ConnectionStrings.DefaultEndpointSuffix; |
| | | 868 | | } |
| | | 869 | | |
| | 40 | 870 | | return ConstructUris(scheme, accountName, Constants.ConnectionStrings.DefaultBlobHostnamePrefix, endpointSuf |
| | | 871 | | } |
| | | 872 | | |
| | | 873 | | /// <summary> |
| | | 874 | | /// Gets the default file endpoint using specified settings. |
| | | 875 | | /// </summary> |
| | | 876 | | /// <param name="settings">The settings to use.</param> |
| | | 877 | | /// <returns>The default file endpoint.</returns> |
| | 48 | 878 | | private static (Uri, Uri) ConstructFileEndpoint(IDictionary<string, string> settings) => ConstructFileEndpoint( |
| | 48 | 879 | | settings[Constants.ConnectionStrings.DefaultEndpointsProtocolSetting], |
| | 48 | 880 | | settings[Constants.ConnectionStrings.AccountNameSetting], |
| | 48 | 881 | | settings.ContainsKey(Constants.ConnectionStrings.EndpointSuffixSetting) ? settings[Constants.ConnectionS |
| | 48 | 882 | | settings.ContainsKey(Constants.ConnectionStrings.SharedAccessSignatureSetting) ? settings[Constants.Conn |
| | | 883 | | |
| | | 884 | | /// <summary> |
| | | 885 | | /// Gets the default file endpoint using the specified protocol and account name. |
| | | 886 | | /// </summary> |
| | | 887 | | /// <param name="scheme">The protocol to use.</param> |
| | | 888 | | /// <param name="accountName">The name of the storage account.</param> |
| | | 889 | | /// <param name="endpointSuffix">The Endpoint DNS suffix; use <c>null</c> for default.</param> |
| | | 890 | | /// <param name="sasToken">The sas token; use <c>null</c> for default.</param> |
| | | 891 | | /// <returns>The default file endpoint.</returns> |
| | | 892 | | internal static (Uri, Uri) ConstructFileEndpoint(string scheme, string accountName, string endpointSuffix, strin |
| | | 893 | | { |
| | 56 | 894 | | if (string.IsNullOrEmpty(scheme)) |
| | | 895 | | { |
| | 0 | 896 | | throw Errors.ArgumentNull(nameof(scheme)); |
| | | 897 | | } |
| | | 898 | | |
| | 56 | 899 | | if (string.IsNullOrEmpty(accountName)) |
| | | 900 | | { |
| | 0 | 901 | | throw Errors.ArgumentNull(nameof(accountName)); |
| | | 902 | | } |
| | | 903 | | |
| | 56 | 904 | | if (string.IsNullOrEmpty(endpointSuffix)) |
| | | 905 | | { |
| | 40 | 906 | | endpointSuffix = Constants.ConnectionStrings.DefaultEndpointSuffix; |
| | | 907 | | } |
| | | 908 | | |
| | 56 | 909 | | return ConstructUris(scheme, accountName, Constants.ConnectionStrings.DefaultFileHostnamePrefix, endpointSuf |
| | | 910 | | } |
| | | 911 | | |
| | | 912 | | /// <summary> |
| | | 913 | | /// Gets the default queue endpoint using the specified settings. |
| | | 914 | | /// </summary> |
| | | 915 | | /// <param name="settings">The settings.</param> |
| | | 916 | | /// <returns>The default queue endpoint.</returns> |
| | 48 | 917 | | private static (Uri, Uri) ConstructQueueEndpoint(IDictionary<string, string> settings) => ConstructQueueEndpoint |
| | 48 | 918 | | settings[Constants.ConnectionStrings.DefaultEndpointsProtocolSetting], |
| | 48 | 919 | | settings[Constants.ConnectionStrings.AccountNameSetting], |
| | 48 | 920 | | settings.ContainsKey(Constants.ConnectionStrings.EndpointSuffixSetting) ? settings[Constants.ConnectionS |
| | 48 | 921 | | settings.ContainsKey(Constants.ConnectionStrings.SharedAccessSignatureSetting) ? settings[Constants.Conn |
| | | 922 | | |
| | | 923 | | /// <summary> |
| | | 924 | | /// Gets the default queue endpoint using the specified protocol and account name. |
| | | 925 | | /// </summary> |
| | | 926 | | /// <param name="scheme">The protocol to use.</param> |
| | | 927 | | /// <param name="accountName">The name of the storage account.</param> |
| | | 928 | | /// <param name="endpointSuffix">The Endpoint DNS suffix; use <c>null</c> for default.</param> |
| | | 929 | | /// <param name="sasToken">The sas token; use <c>null</c> for default.</param> |
| | | 930 | | /// <returns>The default queue endpoint.</returns> |
| | | 931 | | internal static (Uri, Uri) ConstructQueueEndpoint(string scheme, string accountName, string endpointSuffix, stri |
| | | 932 | | { |
| | 56 | 933 | | if (string.IsNullOrEmpty(scheme)) |
| | | 934 | | { |
| | 0 | 935 | | throw Errors.ArgumentNull(nameof(scheme)); |
| | | 936 | | } |
| | | 937 | | |
| | 56 | 938 | | if (string.IsNullOrEmpty(accountName)) |
| | | 939 | | { |
| | 0 | 940 | | throw Errors.ArgumentNull(nameof(accountName)); |
| | | 941 | | } |
| | | 942 | | |
| | 56 | 943 | | if (string.IsNullOrEmpty(endpointSuffix)) |
| | | 944 | | { |
| | 40 | 945 | | endpointSuffix = Constants.ConnectionStrings.DefaultEndpointSuffix; |
| | | 946 | | } |
| | | 947 | | |
| | 56 | 948 | | return ConstructUris(scheme, accountName, Constants.ConnectionStrings.DefaultQueueHostnamePrefix, endpointSu |
| | | 949 | | } |
| | | 950 | | |
| | | 951 | | /// <summary> |
| | | 952 | | /// Gets the default table endpoint using the specified settings. |
| | | 953 | | /// </summary> |
| | | 954 | | /// <param name="settings">The settings.</param> |
| | | 955 | | /// <returns>The default table endpoint.</returns> |
| | 132 | 956 | | private static (Uri, Uri) ConstructTableEndpoint(IDictionary<string, string> settings) => ConstructTableEndpoint |
| | 132 | 957 | | settings[Constants.ConnectionStrings.DefaultEndpointsProtocolSetting], |
| | 132 | 958 | | settings[Constants.ConnectionStrings.AccountNameSetting], |
| | 132 | 959 | | settings.ContainsKey(Constants.ConnectionStrings.EndpointSuffixSetting) ? settings[Constants.ConnectionS |
| | 132 | 960 | | settings.ContainsKey(Constants.ConnectionStrings.SharedAccessSignatureSetting) ? settings[Constants.Conn |
| | | 961 | | |
| | | 962 | | /// <summary> |
| | | 963 | | /// Gets the default queue endpoint using the specified protocol and account name. |
| | | 964 | | /// </summary> |
| | | 965 | | /// <param name="scheme">The protocol to use.</param> |
| | | 966 | | /// <param name="accountName">The name of the storage account.</param> |
| | | 967 | | /// <param name="endpointSuffix">The Endpoint DNS suffix; use <c>null</c> for default.</param> |
| | | 968 | | /// <param name="sasToken">The sas token; use <c>null</c> for default.</param> |
| | | 969 | | /// <returns>The default table endpoint.</returns> |
| | | 970 | | internal static (Uri, Uri) ConstructTableEndpoint(string scheme, string accountName, string endpointSuffix, stri |
| | | 971 | | { |
| | 136 | 972 | | if (string.IsNullOrEmpty(scheme)) |
| | | 973 | | { |
| | 0 | 974 | | throw Errors.ArgumentNull(nameof(scheme)); |
| | | 975 | | } |
| | | 976 | | |
| | 136 | 977 | | if (string.IsNullOrEmpty(accountName)) |
| | | 978 | | { |
| | 0 | 979 | | throw Errors.ArgumentNull(nameof(accountName)); |
| | | 980 | | } |
| | | 981 | | |
| | 136 | 982 | | if (string.IsNullOrEmpty(endpointSuffix)) |
| | | 983 | | { |
| | 128 | 984 | | endpointSuffix = Constants.ConnectionStrings.DefaultEndpointSuffix; |
| | | 985 | | } |
| | | 986 | | |
| | 136 | 987 | | return ConstructUris(scheme, accountName, Constants.ConnectionStrings.DefaultTableHostnamePrefix, endpointSu |
| | | 988 | | } |
| | | 989 | | |
| | | 990 | | /// <summary> |
| | | 991 | | /// Construct the Primary/Secondary Uri tuple. |
| | | 992 | | /// </summary> |
| | | 993 | | /// <param name="scheme">The protocol to use.</param> |
| | | 994 | | /// <param name="accountName">The name of the storage account.</param> |
| | | 995 | | /// <param name="hostNamePrefix">Prefix that appears before the host name, e.g. "blob".</param> |
| | | 996 | | /// <param name="endpointSuffix">The Endpoint DNS suffix; use <c>null</c> for default.</param> |
| | | 997 | | /// <param name="sasToken">The sas token; use <c>null</c> for default.</param> |
| | | 998 | | /// <returns></returns> |
| | | 999 | | private static (Uri, Uri) ConstructUris( |
| | | 1000 | | string scheme, |
| | | 1001 | | string accountName, |
| | | 1002 | | string hostNamePrefix, |
| | | 1003 | | string endpointSuffix, |
| | | 1004 | | string sasToken) |
| | | 1005 | | { |
| | 288 | 1006 | | var primaryUriBuilder = new UriBuilder |
| | 288 | 1007 | | { |
| | 288 | 1008 | | Scheme = scheme, |
| | 288 | 1009 | | Host = string.Format( |
| | 288 | 1010 | | CultureInfo.InvariantCulture, |
| | 288 | 1011 | | "{0}.{1}.{2}", |
| | 288 | 1012 | | accountName, |
| | 288 | 1013 | | hostNamePrefix, |
| | 288 | 1014 | | endpointSuffix), |
| | 288 | 1015 | | Query = sasToken |
| | 288 | 1016 | | }; |
| | | 1017 | | |
| | 288 | 1018 | | var secondaryUriBuilder = new UriBuilder |
| | 288 | 1019 | | { |
| | 288 | 1020 | | Scheme = scheme, |
| | 288 | 1021 | | Host = string.Format( |
| | 288 | 1022 | | CultureInfo.InvariantCulture, |
| | 288 | 1023 | | "{0}{1}.{2}.{3}", |
| | 288 | 1024 | | accountName, |
| | 288 | 1025 | | Constants.ConnectionStrings.SecondaryLocationAccountSuffix, |
| | 288 | 1026 | | hostNamePrefix, |
| | 288 | 1027 | | endpointSuffix), |
| | 288 | 1028 | | Query = sasToken |
| | 288 | 1029 | | }; |
| | | 1030 | | |
| | 288 | 1031 | | return (primaryUriBuilder.Uri, secondaryUriBuilder.Uri); |
| | | 1032 | | } |
| | | 1033 | | } |
| | | 1034 | | } |