Пример #1
0
        public void UpdateSharedAccessSignatureShouldUpdateTheSharedAccessSignature()
        {
            var resource       = "amqps://before/path";
            var beforeKeyName  = "beforeKeyName";
            var afterKeyName   = "afterKeyName";
            var beforeKeyValue = "beforeKeyValue";
            var afterKeyValue  = "afterKeyValue";
            var validSpan      = TimeSpan.FromHours(4);
            var signature      = new SharedAccessSignature(resource, beforeKeyName, beforeKeyValue, validSpan.Add(TimeSpan.FromHours(2)));
            var keyCredential  = new ServiceBusSharedAccessKeyCredential(signature.Value);
            var sasCredential  = keyCredential.AsSharedAccessSignatureCredential(resource, validSpan);

            // Updates
            var newSignature = new SharedAccessSignature(resource, afterKeyName, afterKeyValue, validSpan);

            keyCredential.UpdateSharedAccessSignature(newSignature.Value);

            Assert.That(sasCredential, Is.Not.Null, "A shared access signature credential should have been created.");

            var credentialSignature = GetSharedAccessSignature(sasCredential);

            Assert.That(credentialSignature, Is.Not.Null, "The SAS credential should contain a shared access signature.");
            Assert.That(credentialSignature.Resource, Is.EqualTo(signature.Resource), "The resource should match.");
            Assert.That(credentialSignature.SharedAccessKeyName, Is.EqualTo(afterKeyName), "The shared access key name should match.");
            Assert.That(credentialSignature.SharedAccessKey, Is.Null, "The shared access key should not have been set.");
            Assert.That(credentialSignature.SignatureExpiration, Is.EqualTo(newSignature.SignatureExpiration).Within(TimeSpan.FromSeconds(5)), "The expiration should match.");
        }
        public async Task ClientCanConnectUsingSharedKeyCredentialWithSharedKey()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: true, enableSession: false))
            {
                var options    = new ServiceBusClientOptions();
                var audience   = ServiceBusConnection.BuildConnectionResource(options.TransportType, TestEnvironment.FullyQualifiedNamespace, scope.QueueName);
                var credential = new ServiceBusSharedAccessKeyCredential(TestEnvironment.SharedAccessKeyName, TestEnvironment.SharedAccessKey);

                await using (var client = new ServiceBusClient(TestEnvironment.FullyQualifiedNamespace, credential, options))
                {
                    Assert.That(async() =>
                    {
                        ServiceBusReceiver receiver = null;

                        try
                        {
                            receiver = client.CreateReceiver(scope.QueueName);
                        }
                        finally
                        {
                            await(receiver?.DisposeAsync() ?? new ValueTask());
                        }
                    }, Throws.Nothing);
                }
            }
        }
Пример #3
0
        public void ConstructorInitializesSharedSignatureProperties()
        {
            var signature  = new SharedAccessSignature("RESOURCE", "keyname", "keyvalue").Value;
            var credential = new ServiceBusSharedAccessKeyCredential(signature);

            Assert.That(credential.SharedAccessSignature, Is.EqualTo(signature), "The shared access signature name should have been set.");
            Assert.That(credential.SharedAccessKeyName, Is.Null, "The shared key name should have been set.");
            Assert.That(credential.SharedAccessKey, Is.Null, "The shared access key should not have been set.");
        }
        public void ConstructorWithSharedKeyCredentialArgumentsValidatesOptions()
        {
            var token          = new ServiceBusSharedAccessKeyCredential("key", "value");
            var invalidOptions = new ServiceBusClientOptions {
                TransportType = ServiceBusTransportType.AmqpTcp, WebProxy = Mock.Of <IWebProxy>()
            };

            Assert.That(() => new ServiceBusClient("fullyQualifiedNamespace", Mock.Of <TokenCredential>(), invalidOptions), Throws.InstanceOf <ArgumentException>(), "The expanded argument constructor should validate client options");
        }
Пример #5
0
        public void ConstructorInitializesSharedKeyProperties()
        {
            var name       = "KeyName";
            var value      = "KeyValue";
            var credential = new ServiceBusSharedAccessKeyCredential(name, value);

            Assert.That(credential.SharedAccessKeyName, Is.EqualTo(name), "The shared key name should have been set.");
            Assert.That(credential.SharedAccessKey, Is.EqualTo(value), "The shared key should have been set.");
            Assert.That(credential.SharedAccessSignature, Is.Null, "The shared access signature should not have been set.");
        }
        private ServiceBusAdministrationClient GetSharedKeyTokenClient()
        {
            var properties = ServiceBusConnectionStringProperties.Parse(GetConnectionString());
            var credential = new ServiceBusSharedAccessKeyCredential(properties.SharedAccessKeyName, properties.SharedAccessKey);

            return(InstrumentClient(
                       new ServiceBusAdministrationClient(
                           TestEnvironment.FullyQualifiedNamespace,
                           credential,
                           InstrumentClientOptions(new ServiceBusAdministrationClientOptions()))));
        }
        /// <summary>
        ///   Provides the invalid test cases for the constructor tests.
        /// </summary>
        ///
        public static IEnumerable <object[]> ConstructorSharedKeyCredentialArgumentInvalidCases()
        {
            var credential = new ServiceBusSharedAccessKeyCredential("name", "value");

            yield return(new object[] { null, credential });

            yield return(new object[] { "", credential });

            yield return(new object[] { "FakeNamespace", null });

            yield return(new object[] { "sb://fakenamspace.com", credential });
        }
Пример #8
0
        public void ServiceBusSharedAccessKeyCredentialShouldHoldAReferenceToASharedAccessKey()
        {
            var resource      = "amqps://some.hub.com/path";
            var span          = TimeSpan.FromHours(4);
            var keyName       = "keyName";
            var keyValue      = "keyValue";
            var keyCredential = new ServiceBusSharedAccessKeyCredential(keyName, keyValue);

            SharedAccessSignatureCredential sasCredential     = keyCredential.AsSharedAccessSignatureCredential(resource, span);
            SharedAccessSignatureCredential wrappedCredential = GetSharedAccessSignatureCredential(keyCredential);

            Assert.That(wrappedCredential, Is.EqualTo(sasCredential), "The credentials should be wrapped.");
        }
Пример #9
0
        public void AsSharedAccessSignatureCredentialProducesTheExpectedCredentialForSharedKeys()
        {
            var resource      = "amqps://some.hub.com/path";
            var keyName       = "sharedKey";
            var keyValue      = "keyValue";
            var validSpan     = TimeSpan.FromHours(4);
            var signature     = new SharedAccessSignature(resource, keyName, keyValue, validSpan);
            var keyCredential = new ServiceBusSharedAccessKeyCredential(keyName, keyValue);
            var sasCredential = keyCredential.AsSharedAccessSignatureCredential(resource, validSpan);

            Assert.That(sasCredential, Is.Not.Null, "A shared access signature credential should have been created.");

            var credentialSignature = GetSharedAccessSignature(sasCredential);

            Assert.That(credentialSignature, Is.Not.Null, "The SAS credential should contain a shared access signature.");
            Assert.That(credentialSignature.Resource, Is.EqualTo(signature.Resource), "The resource should match.");
            Assert.That(credentialSignature.SharedAccessKeyName, Is.EqualTo(signature.SharedAccessKeyName), "The shared access key name should match.");
            Assert.That(credentialSignature.SharedAccessKey, Is.EqualTo(signature.SharedAccessKey), "The shared access key should match.");
            Assert.That(credentialSignature.SignatureExpiration, Is.EqualTo(signature.SignatureExpiration).Within(TimeSpan.FromSeconds(5)), "The expiration should match.");
        }
Пример #10
0
        public void UpdateSharedAccessKeyShouldNotChangeOtherPropertiesForTheSharedAccessSignature()
        {
            var resource       = "amqps://before/path";
            var validSpan      = TimeSpan.FromHours(4);
            var beforeKeyName  = "beforeKeyName";
            var afterKeyName   = "afterKeyName";
            var beforeKeyValue = "beforeKeyValue";
            var afterKeyValue  = "afterKeyValue";
            var keyCredential  = new ServiceBusSharedAccessKeyCredential(beforeKeyName, beforeKeyValue);

            // Needed to instantiate a SharedAccessTokenCredential
            var sasCredential       = keyCredential.AsSharedAccessSignatureCredential(resource, validSpan);
            var credentialSignature = GetSharedAccessSignature(sasCredential);

            var signatureExpiration = credentialSignature.SignatureExpiration;

            keyCredential.UpdateSharedAccessKey(afterKeyName, afterKeyValue);

            Assert.That(credentialSignature.SignatureExpiration, Is.EqualTo(signatureExpiration), "The expiration of a signature should not change when the credentials are rolled.");
            Assert.That(credentialSignature.Resource, Is.EqualTo(resource), "The resource of a signature should not change when the credentials are rolled.");
        }
Пример #11
0
        public void AsSharedAccessSignatureCredentialShouldNotRefreshTokenValidityForSharedAccessSignatures()
        {
            var beforeResource    = "amqps://before/path";
            var afterResource     = "amqps://after/path";
            var beforeSpan        = TimeSpan.FromHours(4);
            var afterSpan         = TimeSpan.FromHours(8);
            var keyName           = "keyName";
            var keyValue          = "keyValue";
            var expectedSignature = new SharedAccessSignature(beforeResource, keyName, keyValue, beforeSpan);
            var keyCredential     = new ServiceBusSharedAccessKeyCredential(expectedSignature.Value);

            SharedAccessSignatureCredential sasCredential   = keyCredential.AsSharedAccessSignatureCredential(beforeResource, beforeSpan);
            SharedAccessSignature           beforeSignature = GetSharedAccessSignature(sasCredential);

            Assert.That(beforeSignature.SignatureExpiration, Is.EqualTo(expectedSignature.SignatureExpiration).Within(TimeSpan.FromSeconds(5)), "The expiration should match.");

            sasCredential = keyCredential.AsSharedAccessSignatureCredential(afterResource, afterSpan);
            SharedAccessSignature afterSignature = GetSharedAccessSignature(sasCredential);

            Assert.That(afterSignature.SignatureExpiration, Is.EqualTo(expectedSignature.SignatureExpiration).Within(TimeSpan.FromSeconds(5)), "The expiration should match.");
        }
Пример #12
0
        public void UpdateSharedAccessKeyShoulRefreshServiceBusSharedAccessKeyCredentialNameAndKey()
        {
            var resource       = "amqps://before/path";
            var validSpan      = TimeSpan.FromHours(4);
            var beforeKeyName  = "beforeKeyName";
            var afterKeyName   = "afterKeyName";
            var beforeKeyValue = "beforeKeyValue";
            var afterKeyValue  = "afterKeyValue";
            var keyCredential  = new ServiceBusSharedAccessKeyCredential(beforeKeyName, beforeKeyValue);

            keyCredential.UpdateSharedAccessKey(afterKeyName, afterKeyValue);

            var keyName = keyCredential.SharedAccessKeyName;
            var key     = keyCredential.SharedAccessKey;

            var sasCredential       = keyCredential.AsSharedAccessSignatureCredential(resource, validSpan);
            var credentialSignature = GetSharedAccessSignature(sasCredential);

            Assert.That(credentialSignature.SharedAccessKeyName, Is.EqualTo(afterKeyName), "The shared access key name should change after being updated.");
            Assert.That(credentialSignature.SharedAccessKey, Is.EqualTo(afterKeyValue), "The shared access key value should change after being updated.");
            Assert.That(keyName, Is.EqualTo(afterKeyName), "The shared access key name should change after being updated.");
            Assert.That(key, Is.EqualTo(afterKeyValue), "The shared access key value should change after being updated.");
        }
Пример #13
0
 /// <summary>
 ///   Retrieves the ServiceBusSharedAccessKeyCredential from the credential using its private accessor.
 /// </summary>
 ///
 /// <param name="instance">The instance to retrieve the key from.</param>
 ///
 /// <returns>The shared access key.</returns>
 ///
 private static SharedAccessSignatureCredential GetSharedAccessSignatureCredential(ServiceBusSharedAccessKeyCredential instance) =>
 (SharedAccessSignatureCredential)
 typeof(ServiceBusSharedAccessKeyCredential)
 .GetProperty("SharedAccessSignatureCredential", BindingFlags.Instance | BindingFlags.NonPublic)
 .GetValue(instance, null);
 public ReadableOptionsMock(string fullyQualifiedNamespace,
                            ServiceBusSharedAccessKeyCredential credential,
                            ServiceBusClientOptions clientOptions = default) : base(fullyQualifiedNamespace, credential, clientOptions)
 {
 }
 public void ConstructorValidatesSharedKeyArguments(string fullyQualifiedNamespace,
                                                    ServiceBusSharedAccessKeyCredential credential)
 {
     Assert.That(() => new ServiceBusClient(fullyQualifiedNamespace, credential), Throws.InstanceOf <ArgumentException>());
 }