public static void SslAuth(Config config)
        {
            var testSchema1 =
                "{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"Confluent.Kafka.Examples.AvroSpecific" +
                "\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"i" +
                "nt\",\"null\"]},{\"name\":\"favorite_color\",\"type\":[\"string\",\"null\"]}]}";

            // 1. valid configuration cases

            // 1.1. using SSL valid certificate.
            var conf = new SchemaRegistryConfig
            {
                Url = config.ServerWithSsl,
                SslKeystoreLocation = config.KeystoreLocation,
                SslKeystorePassword = config.KeystorePassword,
                SslCaLocation       = config.CaLocation,
                EnableSslCertificateVerification = bool.Parse(config.EnableSslCertificateVerification),
            };

            // some sanity checking of strongly typed config property name mappings.
            Assert.Equal(config.ServerWithSsl, conf.Get("schema.registry.url"));

            using (var sr = new CachedSchemaRegistryClient(conf))
            {
                var topicName = Guid.NewGuid().ToString();
                var subject   = SubjectNameStrategy.Topic.ToDelegate()(new SerializationContext(MessageComponentType.Value, topicName), null);
                var id        = sr.RegisterSchemaAsync(subject, testSchema1).Result;
                var schema    = sr.GetLatestSchemaAsync(subject).Result;
                Assert.Equal(schema.Id, id);
            }

            // try to connect with invalid SSL config. shouldn't work.
            Assert.Throws <HttpRequestException>(() =>
            {
                var sr = new CachedSchemaRegistryClient(new SchemaRegistryConfig {
                    Url = config.ServerWithSsl
                });
                var topicName = Guid.NewGuid().ToString();
                var subject   = SubjectNameStrategy.Topic.ConstructValueSubjectName(topicName, null);
                try
                {
                    var id = sr.RegisterSchemaAsync(subject, testSchema1).Result;
                }
                catch (Exception e)
                {
                    throw e.InnerException;
                }
            });
        }
Ejemplo n.º 2
0
        public static void BasicAuth(Config config)
        {
            var testSchema1 =
                "{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"Confluent.Kafka.Examples.AvroSpecific" +
                "\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"i" +
                "nt\",\"null\"]},{\"name\":\"favorite_color\",\"type\":[\"string\",\"null\"]}]}";

            // 1. valid configuration cases

            // 1.1. credentials specified as USER_INFO.
            var conf = new SchemaRegistryConfig
            {
                SchemaRegistryUrl = config.ServerWithAuth,
                SchemaRegistryBasicAuthCredentialsSource = AuthCredentialsSource.UserInfo,
                SchemaRegistryBasicAuthUserInfo          = $"{config.Username}:{config.Password}"
            };

            // some sanity checking of strongly typed config property name mappings.
            Assert.Equal(config.ServerWithAuth, conf.Get("schema.registry.url"));
            Assert.Equal("USER_INFO", conf.Get("schema.registry.basic.auth.credentials.source"));
            Assert.Equal($"{config.Username}:{config.Password}", conf.Get("schema.registry.basic.auth.user.info"));

            using (var sr = new CachedSchemaRegistryClient(conf))
            {
                var topicName = Guid.NewGuid().ToString();
                var subject   = sr.ConstructValueSubjectName(topicName);
                var id        = sr.RegisterSchemaAsync(subject, testSchema1).Result;
                var schema    = sr.GetLatestSchemaAsync(subject).Result;
                Assert.Equal(schema.Id, id);
            }

            // 1.2. credentials specified as USER_INFO implicitly (and using strongly typed SchemaRegistryConfig)
            var conf2 = new SchemaRegistryConfig
            {
                SchemaRegistryUrl = config.ServerWithAuth,
                SchemaRegistryBasicAuthUserInfo = $"{config.Username}:{config.Password}"
            };

            using (var sr = new CachedSchemaRegistryClient(conf2))
            {
                var topicName = Guid.NewGuid().ToString();
                var subject   = sr.ConstructValueSubjectName(topicName);
                var id        = sr.RegisterSchemaAsync(subject, testSchema1).Result;
                var schema    = sr.GetLatestSchemaAsync(subject).Result;
                Assert.Equal(schema.Id, id);
            }

            // 1.3. credentials specified as SASL_INHERIT.
            using (var sr = new CachedSchemaRegistryClient(
                       new Dictionary <string, string>
            {
                { "schema.registry.url", config.ServerWithAuth },
                { "schema.registry.basic.auth.credentials.source", "SASL_INHERIT" },
                { "sasl.username", config.Username },
                { "sasl.password", config.Password }
            }))
            {
                var topicName = Guid.NewGuid().ToString();
                var subject   = sr.ConstructValueSubjectName(topicName);
                var id        = sr.RegisterSchemaAsync(subject, testSchema1).Result;
                var schema    = sr.GetLatestSchemaAsync(subject).Result;
                Assert.Equal(schema.Id, id);
            }

            // 1.4. credentials specified as SASL_INHERIT via strongly typed config.
            var conf3 = new SchemaRegistryConfig {
                SchemaRegistryUrl = config.ServerWithAuth
            };

            conf3.SchemaRegistryBasicAuthCredentialsSource = AuthCredentialsSource.SaslInherit;
            conf3.Set("sasl.username", config.Username);
            conf3.Set("sasl.password", config.Password);
            using (var sr = new CachedSchemaRegistryClient(conf3))
            {
                var topicName = Guid.NewGuid().ToString();
                var subject   = sr.ConstructValueSubjectName(topicName);
                var id        = sr.RegisterSchemaAsync(subject, testSchema1).Result;
                var schema    = sr.GetLatestSchemaAsync(subject).Result;
                Assert.Equal(schema.Id, id);
            }


            // 2. invalid configuration cases

            Assert.Throws <ArgumentException>(() =>
            {
                var sr = new CachedSchemaRegistryClient(new Dictionary <string, string>
                {
                    { "schema.registry.url", config.ServerWithAuth },
                    { "schema.registry.basic.auth.credentials.source", "SASL_INHERIT" },
                    { "schema.registry.basic.auth.user.info", $"{config.Username:config.Password}" }
                });
            });

            Assert.Throws <ArgumentException>(() =>
            {
                var sr = new CachedSchemaRegistryClient(new Dictionary <string, string>
                {
                    { "schema.registry.url", config.ServerWithAuth },
                    { "schema.registry.basic.auth.credentials.source", "UBUTE_SOURCE" }
                });
            });

            Assert.Throws <ArgumentException>(() =>
            {
                var sr = new CachedSchemaRegistryClient(new Dictionary <string, string>
                {
                    { "schema.registry.url", config.ServerWithAuth },
                    { "schema.registry.basic.auth.credentials.source", "NONE" },
                    { "schema.registry.basic.auth.user.info", $"{config.Username:config.Password}" }
                });
            });

            // connect to authenticating without credentials. shouldn't work.
            Assert.Throws <HttpRequestException>(() =>
            {
                var sr = new CachedSchemaRegistryClient(new SchemaRegistryConfig {
                    SchemaRegistryUrl = config.ServerWithAuth
                });
                var topicName = Guid.NewGuid().ToString();
                var subject   = sr.ConstructValueSubjectName(topicName);
                try
                {
                    var id = sr.RegisterSchemaAsync(subject, testSchema1).Result;
                }
                catch (Exception e)
                {
                    throw e.InnerException;
                }
            });
        }