예제 #1
0
 /// <summary>
 /// Sloppily serialize the tenant configuration as a string with one
 /// property per line.  This is only used for test recording.
 /// </summary>
 /// <param name="config">The tenant configuration.</param>
 /// <param name="sanitize">
 /// Whether to santize AccountKeys in the serialized value.  The
 /// default is true.
 /// </param>
 /// <returns>A string represenation of the tenant configuration.</returns>
 public static string Serialize(TenantConfiguration config, bool sanitize = true) =>
 string.Join(
     "\n",
     // Keep these in the same order as Parse below!
     config.TenantName,
     config.AccountName,
     !sanitize ?
     config.AccountKey :
     Convert.ToBase64String(Encoding.UTF8.GetBytes(SanitizeValue)),
     config.BlobServiceEndpoint,
     config.FileServiceEndpoint,
     config.QueueServiceEndpoint,
     config.TableServiceEndpoint,
     config.BlobSecurePortOverride,
     config.FileSecurePortOverride,
     config.TableSecurePortOverride,
     config.QueueSecurePortOverride,
     config.BlobServiceSecondaryEndpoint,
     config.FileServiceSecondaryEndpoint,
     config.QueueServiceSecondaryEndpoint,
     config.TableServiceSecondaryEndpoint,
     config.ActiveDirectoryApplicationId,
     !sanitize ?
     config.ActiveDirectoryApplicationSecret :
     SanitizeValue,
     config.ActiveDirectoryTenantId,
     config.ActiveDirectoryAuthEndpoint,
     config.TenantType.ToString(),
     !sanitize ?
     config.ConnectionString :
     config.BuildConnectionString(sanitize),
     config.EncryptionScope);
예제 #2
0
        /// <summary>
        /// Parse a string representation created by Serialize into a
        /// TenantConfiguration value.
        /// </summary>
        /// <param name="text">The string to parse.</param>
        /// <param name="playback">Whether or not we're in playback mode.</param>
        /// <returns>A TenantConfiguration value.</returns>
        public static TenantConfiguration Parse(string text, bool playback = false)
        {
            var values = text?.Split('\n');

            if (values == null || values.Length != 22)
            {
                throw new ArgumentException();
            }

            var config = new TenantConfiguration
            {
                // Keep these in the same order as Serialize above!
                TenantName                       = values[0],
                AccountName                      = values[1],
                AccountKey                       = values[2],
                BlobServiceEndpoint              = values[3],
                FileServiceEndpoint              = values[4],
                QueueServiceEndpoint             = values[5],
                TableServiceEndpoint             = values[6],
                BlobSecurePortOverride           = values[7],
                FileSecurePortOverride           = values[8],
                TableSecurePortOverride          = values[9],
                QueueSecurePortOverride          = values[10],
                BlobServiceSecondaryEndpoint     = values[11],
                FileServiceSecondaryEndpoint     = values[12],
                QueueServiceSecondaryEndpoint    = values[13],
                TableServiceSecondaryEndpoint    = values[14],
                ActiveDirectoryApplicationId     = values[15],
                ActiveDirectoryApplicationSecret = values[16],
                ActiveDirectoryTenantId          = values[17],
                ActiveDirectoryAuthEndpoint      = values[18],
                TenantType                       = ParseTenantType(values[19]),
                ConnectionString                 = values[20],
                EncryptionScope                  = values[21]
            };

            // HACK: Identity's update to the latest version of MSAL breaks
            // recordings when we're using a project reference.  This
            // temporarily works around the issue by disabling any playback
            // test using OAuth with version 1.2.* of Azure.Identity.
            if (playback && !string.IsNullOrEmpty(config.ActiveDirectoryApplicationId))
            {
                string version =
                    typeof(ClientSecretCredential).Assembly
                    .GetCustomAttribute <AssemblyInformationalVersionAttribute>()
                    .InformationalVersion;
                if (version.StartsWith("1.2"))
                {
                    throw new InconclusiveException($"Ignore Azure.Identity {version} for playback.");
                }
            }

            return(config);
        }
        public BlobServiceClient GetSecondaryStorageReadEnabledServiceClient(TenantConfiguration config, int numberOfReadFailuresToSimulate, bool simulate404 = false)
        {
            BlobClientOptions options = GetBlobOptions();

            options.GeoRedundantSecondaryUri = new Uri(config.BlobServiceSecondaryEndpoint);
            options.Retry.MaxRetries         = 4;
            options.AddPolicy(new TestExceptionPolicy(numberOfReadFailuresToSimulate, options.GeoRedundantSecondaryUri, simulate404), HttpPipelinePosition.PerRetry);

            return(InstrumentClient(
                       new BlobServiceClient(
                           new Uri(config.BlobServiceEndpoint),
                           new StorageSharedKeyCredential(config.AccountName, config.AccountKey),
                           options)));
        }
        /// <summary>
        /// Parse an XML representation into a TenantConfiguration value.
        /// </summary>
        /// <param name="tenant">The XML element to parse.</param>
        /// <returns>A TenantConfiguration value.</returns>
        public static TenantConfiguration Parse(XElement tenant)
        {
            string Get(string name) => (string)tenant.Element(name);

            var config = new TenantConfiguration
            {
                TenantName                       = Get("TenantName"),
                AccountName                      = Get("AccountName"),
                AccountKey                       = Get("AccountKey"),
                BlobServiceEndpoint              = Get("BlobServiceEndpoint"),
                FileServiceEndpoint              = Get("FileServiceEndpoint"),
                QueueServiceEndpoint             = Get("QueueServiceEndpoint"),
                TableServiceEndpoint             = Get("TableServiceEndpoint"),
                BlobServiceSecondaryEndpoint     = Get("BlobServiceSecondaryEndpoint"),
                FileServiceSecondaryEndpoint     = Get("FileServiceSecondaryEndpoint"),
                QueueServiceSecondaryEndpoint    = Get("QueueServiceSecondaryEndpoint"),
                TableServiceSecondaryEndpoint    = Get("TableServiceSecondaryEndpoint"),
                TenantType                       = ParseTenantType(Get("TenantType")),
                BlobSecurePortOverride           = Get("BlobSecurePortOverride"),
                FileSecurePortOverride           = Get("FileSecurePortOverride"),
                QueueSecurePortOverride          = Get("QueueSecurePortOverride"),
                TableSecurePortOverride          = Get("TableSecurePortOverride"),
                ActiveDirectoryApplicationId     = Get("ActiveDirectoryApplicationId"),
                ActiveDirectoryApplicationSecret = Get("ActiveDirectoryApplicationSecret"),
                ActiveDirectoryTenantId          = Get("ActiveDirectoryTenantId"),
                ActiveDirectoryAuthEndpoint      = Get("ActiveDirectoryAuthEndpoint"),
                ConnectionString                 = Get("ConnectionString"),
                EncryptionScope                  = Get("EncryptionScope"),
                ResourceGroupName                = Get("ResourceGroupName"),
                SubscriptionId                   = Get("SubscriptionId")
            };

            // Build a connection string from the other properties if one
            // wasn't provided with the configuration
            if (string.IsNullOrWhiteSpace(config.ConnectionString))
            {
                config.ConnectionString = config.BuildConnectionString(false);
            }

            return(config);
        }
예제 #5
0
        private static async Task <QueueServiceClient> GetServiceClientFromOauthConfig(TenantConfiguration config)
        {
            var initalToken = await TestHelper.GenerateOAuthToken(
                config.ActiveDirectoryAuthEndpoint,
                config.ActiveDirectoryTenantId,
                config.ActiveDirectoryApplicationId,
                config.ActiveDirectoryApplicationSecret);

            return(new QueueServiceClient(
                       new Uri(config.QueueServiceEndpoint),
                       GetOptions <QueueConnectionOptions>(new TokenCredentials(initalToken))));
        }
예제 #6
0
 private static BlobServiceClient GetServiceClientFromSharedKeyConfig(TenantConfiguration config)
 => new BlobServiceClient(
     new Uri(config.BlobServiceEndpoint),
     GetOptions <BlobConnectionOptions>(new SharedKeyCredentials(config.AccountName, config.AccountKey)));