Esempio n. 1
0
        /// <summary>
        /// Helper function to run a test scenario for client of type DocumentClientType.
        /// </summary>
        /// <param name="testFunc"></param>
        /// <param name="testName"></param>
        /// <param name="requestChargeHelper"></param>
        /// <param name="authTokenType">authTokenType for the client created for running the test</param>
        internal static void TestForEachClient(
            Func <DocumentClient, DocumentClientType, Task> testFunc,
            string testName,
            RequestChargeHelper requestChargeHelper = null,
            AuthorizationTokenType authTokenType    = AuthorizationTokenType.PrimaryMasterKey,
            ConsistencyLevel?consistencyLevel       = null)
        {
            IDictionary <DocumentClientType, DocumentClient> clients =
                new Dictionary <DocumentClientType, DocumentClient>
            {
                { DocumentClientType.Gateway, TestCommon.CreateClient(true, tokenType: authTokenType, defaultConsistencyLevel: consistencyLevel) },
                { DocumentClientType.DirectTcp, TestCommon.CreateClient(false, Protocol.Tcp, tokenType: authTokenType, defaultConsistencyLevel: consistencyLevel) },
                { DocumentClientType.DirectHttps, TestCommon.CreateClient(false, Protocol.Https, tokenType: authTokenType, defaultConsistencyLevel: consistencyLevel) }
            };

            foreach (var clientEntry in clients)
            {
                try
                {
                    RunTestForClient(testFunc, testName, clientEntry.Key, clientEntry.Value);
                }
                finally
                {
                    clientEntry.Value.Dispose();
                }
            }

            if (requestChargeHelper != null)
            {
                requestChargeHelper.CompareRequestCharge(testName);
            }
        }
Esempio n. 2
0
 public abstract ValueTask <string> GetUserAuthorizationTokenAsync(
     string resourceAddress,
     string resourceType,
     string requestVerb,
     INameValueCollection headers,
     AuthorizationTokenType tokenType,
     ITrace trace);
 public abstract ValueTask <string> GetUserAuthorizationTokenAsync(
     string resourceAddress,
     string resourceType,
     string requestVerb,
     INameValueCollection headers,
     AuthorizationTokenType tokenType,
     CosmosDiagnosticsContext diagnosticsContext);
Esempio n. 4
0
        public override ValueTask <(string token, string payload)> GetUserAuthorizationAsync(
            string resourceAddress,
            string resourceType,
            string requestVerb,
            INameValueCollection headers,
            AuthorizationTokenType tokenType)
        {
            // this is masterkey authZ
            headers[HttpConstants.HttpHeaders.XDate] = DateTime.UtcNow.ToString("r", CultureInfo.InvariantCulture);

            string authorizationToken = AuthorizationHelper.GenerateKeyAuthorizationSignature(
                requestVerb,
                resourceAddress,
                resourceType,
                headers,
                this.authKeyHashFunction,
                out AuthorizationHelper.ArrayOwner arrayOwner);

            using (arrayOwner)
            {
                string payload = null;
                if (arrayOwner.Buffer.Count > 0)
                {
                    payload = Encoding.UTF8.GetString(arrayOwner.Buffer.Array, arrayOwner.Buffer.Offset, (int)arrayOwner.Buffer.Count);
                }

                return(new ValueTask <(string token, string payload)>((authorizationToken, payload)));
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Helper function to run a test scenario for a random client of type DocumentClientType.
        /// </summary>
        /// <param name="testFunc"></param>
        /// <param name="testName"></param>
        /// <param name="requestChargeHelper"></param>
        /// <param name="authTokenType">authTokenType for the client created for running the test</param>
        internal static void TestForAnyClient(
            Func <DocumentClient, DocumentClientType, Task> testFunc,
            string testName,
            RequestChargeHelper requestChargeHelper = null,
            AuthorizationTokenType authTokenType    = AuthorizationTokenType.PrimaryMasterKey,
            ConsistencyLevel?consistencyLevel       = null)
        {
            IDictionary <DocumentClientType, DocumentClient> clients =
                new Dictionary <DocumentClientType, DocumentClient>
            {
                { DocumentClientType.Gateway, TestCommon.CreateClient(true, tokenType: authTokenType, defaultConsistencyLevel: consistencyLevel) },
                { DocumentClientType.DirectTcp, TestCommon.CreateClient(false, Protocol.Tcp, tokenType: authTokenType, defaultConsistencyLevel: consistencyLevel) },
                { DocumentClientType.DirectHttps, TestCommon.CreateClient(false, Protocol.Https, tokenType: authTokenType, defaultConsistencyLevel: consistencyLevel) }
            };

            int                seed         = (int)DateTime.Now.Ticks;
            Random             rand         = new Random(seed);
            DocumentClientType selectedType = clients.Keys.ElementAt(rand.Next(clients.Count));
            DocumentClient     client       = clients[selectedType];

            try
            {
                RunTestForClient(testFunc, testName, selectedType, client);
            }
            finally
            {
                client.Dispose();
            }

            if (requestChargeHelper != null)
            {
                requestChargeHelper.CompareRequestCharge(testName);
            }
        }
 string IAuthorizationTokenProvider.GetUserAuthorizationToken(
     string resourceAddress,
     string resourceType,
     string requestVerb,
     INameValueCollection headers,
     AuthorizationTokenType tokenType) /* unused, use token based upon what is passed in constructor */
 {
     return(null);
 }
 public override async ValueTask <string> GetUserAuthorizationTokenAsync(
     string resourceAddress,
     string resourceType,
     string requestVerb,
     INameValueCollection headers,
     AuthorizationTokenType tokenType,
     CosmosDiagnosticsContext diagnosticsContext)
 {
     return(AuthorizationTokenProviderTokenCredential.GenerateAadAuthorizationSignature(
                await this.tokenCredentialCache.GetTokenAsync(diagnosticsContext)));
 }
        public override async ValueTask AddAuthorizationHeaderAsync(
            INameValueCollection headersCollection,
            Uri requestAddress,
            string verb,
            AuthorizationTokenType tokenType)
        {
            string token = AuthorizationTokenProviderTokenCredential.GenerateAadAuthorizationSignature(
                await this.tokenCredentialCache.GetTokenAsync(EmptyCosmosDiagnosticsContext.Singleton));

            headersCollection.Add(HttpConstants.HttpHeaders.Authorization, token);
        }
        public override async ValueTask <(string token, string payload)> GetUserAuthorizationAsync(
            string resourceAddress,
            string resourceType,
            string requestVerb,
            INameValueCollection headers,
            AuthorizationTokenType tokenType)
        {
            string token = AuthorizationTokenProviderTokenCredential.GenerateAadAuthorizationSignature(
                await this.tokenCredentialCache.GetTokenAsync(EmptyCosmosDiagnosticsContext.Singleton));

            return(token, default);
        }
 public override ValueTask AddAuthorizationHeaderAsync(
     INameValueCollection headersCollection,
     Uri requestAddress,
     string verb,
     AuthorizationTokenType tokenType)
 {
     this.CheckAndRefreshTokenProvider();
     return(this.authorizationTokenProvider.AddAuthorizationHeaderAsync(
                headersCollection,
                requestAddress,
                verb,
                tokenType));
 }
Esempio n. 11
0
        public override async ValueTask AddAuthorizationHeaderAsync(
            INameValueCollection headersCollection,
            Uri requestAddress,
            string verb,
            AuthorizationTokenType tokenType)
        {
            using (Trace trace = Trace.GetRootTrace(nameof(GetUserAuthorizationTokenAsync), TraceComponent.Authorization, TraceLevel.Info))
            {
                string token = AuthorizationTokenProviderTokenCredential.GenerateAadAuthorizationSignature(
                    await this.tokenCredentialCache.GetTokenAsync(trace));

                headersCollection.Add(HttpConstants.HttpHeaders.Authorization, token);
            }
        }
Esempio n. 12
0
 public override async ValueTask <(string token, string payload)> GetUserAuthorizationAsync(
     string resourceAddress,
     string resourceType,
     string requestVerb,
     INameValueCollection headers,
     AuthorizationTokenType tokenType)
 {
     using (Trace trace = Trace.GetRootTrace(nameof(GetUserAuthorizationTokenAsync), TraceComponent.Authorization, TraceLevel.Info))
     {
         string token = AuthorizationTokenProviderTokenCredential.GenerateAadAuthorizationSignature(
             await this.tokenCredentialCache.GetTokenAsync(trace));
         return(token, default);
     }
 }
 public override ValueTask <(string token, string payload)> GetUserAuthorizationAsync(
     string resourceAddress,
     string resourceType,
     string requestVerb,
     INameValueCollection headers,
     AuthorizationTokenType tokenType)
 {
     this.CheckAndRefreshTokenProvider();
     return(this.authorizationTokenProvider.GetUserAuthorizationAsync(
                resourceAddress,
                resourceType,
                requestVerb,
                headers,
                tokenType));
 }
        string ICosmosAuthorizationTokenProvider.GetUserAuthorizationToken(
            string resourceAddress,
            string resourceType,
            string requestVerb,
            INameValueCollection headers,
            AuthorizationTokenType tokenType) // unused, use token based upon what is passed in constructor
        {
            // this is masterkey authZ
            headers[HttpConstants.HttpHeaders.XDate] = DateTime.UtcNow.ToString("r", CultureInfo.InvariantCulture);

            string authorization = AuthorizationHelper.GenerateKeyAuthorizationSignature(
                requestVerb, resourceAddress, resourceType, headers, this.authKeyHashFunction, out AuthorizationHelper.ArrayOwner payload);

            using (payload)
            {
                return(authorization);
            }
        }
Esempio n. 15
0
        public override ValueTask AddAuthorizationHeaderAsync(
            INameValueCollection headersCollection,
            Uri requestAddress,
            string verb,
            AuthorizationTokenType tokenType)
        {
            string dateTime = DateTime.UtcNow.ToString("r", CultureInfo.InvariantCulture);

            headersCollection[HttpConstants.HttpHeaders.XDate] = dateTime;

            string token = AuthorizationHelper.GenerateKeyAuthorizationSignature(
                verb,
                requestAddress,
                headersCollection,
                this.authKeyHashFunction);

            headersCollection.Add(HttpConstants.HttpHeaders.Authorization, token);
            return(default);
Esempio n. 16
0
        ValueTask <string> ICosmosAuthorizationTokenProvider.GetUserAuthorizationTokenAsync(
            string resourceAddress,
            string resourceType,
            string requestVerb,
            INameValueCollection headers,
            AuthorizationTokenType tokenType,
            CosmosDiagnosticsContext diagnosticsContext) // unused, use token based upon what is passed in constructor
        {
            // this is masterkey authZ
            headers[HttpConstants.HttpHeaders.XDate] = DateTime.UtcNow.ToString("r", CultureInfo.InvariantCulture);

            string authorization = AuthorizationHelper.GenerateKeyAuthorizationSignature(
                verb: requestVerb,
                resourceId: resourceAddress,
                resourceType: resourceType,
                headers: headers,
                stringHMACSHA256Helper: this.authKeyHashFunction,
                payload: out AuthorizationHelper.ArrayOwner payload);

            using (payload)
            {
                return(new ValueTask <string>(authorization));
            }
        }
Esempio n. 17
0
        public override ValueTask <string> GetUserAuthorizationTokenAsync(
            string resourceAddress,
            string resourceType,
            string requestVerb,
            INameValueCollection headers,
            AuthorizationTokenType tokenType,
            ITrace trace)
        {
            // this is masterkey authZ
            headers[HttpConstants.HttpHeaders.XDate] = DateTime.UtcNow.ToString("r", CultureInfo.InvariantCulture);

            string authorizationToken = AuthorizationHelper.GenerateKeyAuthorizationSignature(
                requestVerb,
                resourceAddress,
                resourceType,
                headers,
                this.authKeyHashFunction,
                out AuthorizationHelper.ArrayOwner arrayOwner);

            using (arrayOwner)
            {
                return(new ValueTask <string>(authorizationToken));
            }
        }
        ///// <summary>
        ///// Changes the replication mode to sync for server.
        ///// </summary>
        ///// <param name="fabricClient"></param>
        //internal static void SwitchServerToSyncReplication(COMMONNAMESPACE.IFabricClient fabricClient, string callingComponent)
        //{
        //    if (!IsCurrentReplicationModeAsync(fabricClient)["Server"])
        //    {
        //        return;
        //    }

        //    NamingServiceConfig.NamingServiceConfigurationWriter namingServiceWriter =
        //            new NamingServiceConfig.NamingServiceConfigurationWriter(fabricClient);

        //    NamingServiceConfig.DocumentServiceConfiguration config = new NamingServiceConfig.DocumentServiceConfiguration();
        //    config.DocumentServiceName = ConfigurationManager.AppSettings["DatabaseAccountId"];
        //    config.IsServerReplicationAsync = false;

        //    namingServiceWriter.UpdateDatabaseAccountConfigurationAsync(config).Wait();

        //    Task.Delay(TimeSpan.FromSeconds(ReplicationTests.ConfigurationRefreshIntervalInSec)).Wait();
        //    TestCommon.ForceRefreshNamingServiceConfigs(callingComponent, FabricServiceType.ServerService).Wait();
        //}

        ///// <summary>
        ///// Changes the replication mode to async for server.
        ///// </summary>
        ///// <param name="fabricClient"></param>
        //internal static void SwitchServerToAsyncReplication(COMMONNAMESPACE.IFabricClient fabricClient, string callingComponent)
        //{
        //    if (IsCurrentReplicationModeAsync(fabricClient)["Server"])
        //    {
        //        return;
        //    }

        //    NamingServiceConfig.NamingServiceConfigurationWriter namingServiceWriter =
        //        new NamingServiceConfig.NamingServiceConfigurationWriter(fabricClient);

        //    NamingServiceConfig.DocumentServiceConfiguration config = new NamingServiceConfig.DocumentServiceConfiguration();
        //    config.DocumentServiceName = ConfigurationManager.AppSettings["DatabaseAccountId"];
        //    config.IsServerReplicationAsync = true;

        //    namingServiceWriter.UpdateDatabaseAccountConfigurationAsync(config).Wait();

        //    Task.Delay(TimeSpan.FromSeconds(ReplicationTests.ConfigurationRefreshIntervalInSec)).Wait();
        //    TestCommon.ForceRefreshNamingServiceConfigs(callingComponent, FabricServiceType.ServerService).Wait();
        //}

        #endregion

        #region Environment Configuration Helpers

        internal static DocumentClient[] GetClientsLocked(bool useGateway = false, Protocol protocol = Protocol.Tcp, int timeoutInSeconds = 10, ConsistencyLevel?defaultConsistencyLevel = null, AuthorizationTokenType tokenType = AuthorizationTokenType.PrimaryMasterKey)
        {
            var toReturn = new DocumentClient[TestCommon.ReplicationFactor];

            for (uint i = 0; i < toReturn.Length; i++)
            {
                toReturn[i] = TestCommon.CreateClient(useGateway, protocol, timeoutInSeconds, defaultConsistencyLevel, tokenType);
                toReturn[i].LockClient(i);
            }

            return(toReturn);
        }
 public string GetUserAuthorizationToken(string resourceAddress, string resourceType, string requestVerb, NameValueCollection headers, AuthorizationTokenType tokenType)
 {
     throw new NotImplementedException();
 }
 public abstract ValueTask AddAuthorizationHeaderAsync(
     INameValueCollection headersCollection,
     Uri requestAddress,
     string verb,
     AuthorizationTokenType tokenType);
 public abstract ValueTask <(string token, string payload)> GetUserAuthorizationAsync(
     string resourceAddress,
     string resourceType,
     string requestVerb,
     INameValueCollection headers,
     AuthorizationTokenType tokenType);
 public Task<string> GetSystemAuthorizationTokenAsync(string federationName, string resourceAddress, string resourceType, string requestVerb, NameValueCollection headers, AuthorizationTokenType tokenType)
 {
     throw new NotImplementedException();
 }