Skip to content

Commit

Permalink
Adding support for Basic Auth, confluentinc/schema-registry#907
Browse files Browse the repository at this point in the history
  • Loading branch information
Gajula, Vishnu (Contractor) authored and ImFlog committed Apr 11, 2019
1 parent dfe3a0d commit 6b387c3
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 19 deletions.
13 changes: 11 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.gradle.wrapper.WrapperExecutor

group = "com.github.imflog"
version = "0.6.0-SNAPSHOT"
version = "0.8.0-SNAPSHOT"

task<Wrapper>("wrap") {
gradleVersion = "4.10"
Expand All @@ -12,6 +12,7 @@ plugins {
kotlin("jvm").version("1.2.61")
id("java-gradle-plugin")
id("com.gradle.plugin-publish") version "0.9.10"
id ("maven-publish")
}

val kotlinVersion: String? by extra {
Expand All @@ -30,7 +31,7 @@ repositories {
dependencies {
compileOnly(gradleApi())
implementation("org.jetbrains.kotlin", "kotlin-stdlib", kotlinVersion)
implementation("io.confluent", "kafka-avro-serializer", "3.2.1")
implementation("io.confluent", "kafka-avro-serializer", "5.0.0")
.exclude("org.slf4j", "slf4j-log4j12")
implementation("org.apache.avro", "avro", "1.8.2")

Expand All @@ -42,6 +43,14 @@ dependencies {
testImplementation("com.github.tomakehurst", "wiremock-standalone", wiremockVersion)
}

publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
}
}

val registryPluginName = "com.github.imflog.kafka-schema-registry-gradle-plugin"
gradlePlugin {
plugins.invoke {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package com.github.imflog.schema.registry

import io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClientConfig
import java.util.HashMap



/**
* This is a singleton.
Expand All @@ -11,9 +15,16 @@ object RegistryClientWrapper {

private var registryClient: SchemaRegistryClient? = null

fun client(url: String): SchemaRegistryClient? {
fun client(url: String, userInfo: String): SchemaRegistryClient? {
if (registryClient == null) {
registryClient = CachedSchemaRegistryClient(url, 100)
val config = HashMap<String, String>()
if (!userInfo.isEmpty()) {
// Note that BASIC_AUTH_CREDENTIALS_SOURCE is not configurable as the plugin only supports
// a single schema registry URL, so there is no additional utility of the URL source.
config[SchemaRegistryClientConfig.BASIC_AUTH_CREDENTIALS_SOURCE] = "USER_INFO"
config[SchemaRegistryClientConfig.USER_INFO_CONFIG] = userInfo
}
registryClient = CachedSchemaRegistryClient(url, 100, config)
}
return registryClient
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ package com.github.imflog.schema.registry
open class SchemaRegistryExtension {

var url: String = "http://localhost:8081"

var userInfo: String = ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ class SchemaRegistryPlugin : Plugin<Project> {
tasks.create(
DOWNLOAD_SCHEMAS_TASK, DownloadTask::class.java).apply {
url = globalExtension.url
userInfo = globalExtension.userInfo
subjects = downloadExtension.subjects
}

tasks.create(
REGISTER_SCHEMAS_TASK, RegisterSchemasTask::class.java).apply {
url = globalExtension.url
userInfo = globalExtension.userInfo
subjects = registerExtension.subjects
}

tasks.create(
TEST_SCHEMAS_TASK, CompatibilityTask::class.java).apply {
url = globalExtension.url
userInfo = globalExtension.userInfo
subjects = compatibilityExtension.subjects
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ open class CompatibilityTask : DefaultTask() {

@Input
lateinit var url: String

@Input
lateinit var userInfo: String //username:password

@Input
lateinit var subjects: List<Triple<String, String, List<String>>>

@TaskAction
fun testCompatibility() {
val errorCount = CompatibilityTaskAction(
RegistryClientWrapper.client(url)!!,
RegistryClientWrapper.client(url, userInfo)!!,
subjects,
project.rootDir
).run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ open class DownloadTask : DefaultTask() {
@Input
lateinit var subjects: List<Pair<String, String>>

@Input
lateinit var userInfo: String //username:password

@Input
lateinit var url: String

@TaskAction
fun downloadSchemas() {
val errorCount = DownloadTaskAction(
RegistryClientWrapper.client(url)!!,
RegistryClientWrapper.client(url,userInfo)!!,
subjects,
project.rootDir)
.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ open class RegisterSchemasTask : DefaultTask() {
@Input
lateinit var url: String

@Input
lateinit var userInfo: String //username:password

@Input
lateinit var subjects: List<Triple<String, String, List<String>>>

@TaskAction
fun registerSchemas() {
val errorCount = RegisterTaskAction(
RegistryClientWrapper.client(url)!!,
RegistryClientWrapper.client(url, userInfo)!!,
subjects,
project.rootDir
).run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.github.imflog.schema.registry.register
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient
import org.apache.avro.Schema
import org.gradle.api.logging.Logging
import sun.misc.ExtensionDependency
import java.io.File

class RegisterTaskAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SchemaRegistryPluginTest {
}

@Test
fun `plugin should fail with wrong extension configuration`() {
fun `plugin should fail with wrong url extension configuration`() {
folderRule.create()
buildFile = folderRule.newFile("build.gradle")
buildFile.writeText("""
Expand All @@ -55,6 +55,7 @@ class SchemaRegistryPluginTest {
schemaRegistry {
urlFoo = 'http://localhost:$REGISTRY_FAKE_PORT/'
userInfo = 'username:password'
output = 'src/main/avro'
subjects = ['$subject']
}
Expand All @@ -73,4 +74,36 @@ class SchemaRegistryPluginTest {
Assertions.assertThat(ex.message).containsIgnoringCase("unknown property 'urlFoo'")
}
}

@Test
fun `plugin should fail with wrong userInfo extension configuration`() {
folderRule.create()
buildFile = folderRule.newFile("build.gradle")
buildFile.writeText("""
plugins {
id 'java'
id 'com.github.imflog.kafka-schema-registry-gradle-plugin'
}
schemaRegistry {
url = 'http://localhost:$REGISTRY_FAKE_PORT/'
userInfoBar = 'username:password'
output = 'src/main/avro'
subjects = ['$subject']
}
""")

try {
GradleRunner.create()
.withGradleVersion("4.9")
.withProjectDir(folderRule.root)
.withArguments(DOWNLOAD_SCHEMAS_TASK)
.withPluginClasspath()
.withDebug(true)
.build()
Assertions.fail("Should not reach this point")
} catch (ex: UnexpectedBuildFailure) {
Assertions.assertThat(ex.message).containsIgnoringCase("unknown property 'userInfoBar'")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class CompatibilityTaskActionTest {
val parser = Schema.Parser()
val testSchema = parser.parse("{\"type\": \"record\", \"name\": \"test\", \"fields\": [{ \"name\": \"name\", \"type\": \"string\" }]}")
val registryClient = MockSchemaRegistryClient()
registryClient.register("test", testSchema)
registryClient.register("testcompatibility", testSchema)
folderRule.newFolder("src", "main", "avro", "external")

val subjects = arrayListOf(Triple("test", "src/main/avro/external/test.avsc", emptyList<String>()))
val subjects = arrayListOf(Triple("testcompatibility", "src/main/avro/external/test.avsc", emptyList<String>()))
File(folderRule.root, "src/main/avro/external/test.avsc").writeText("""
{"type": "record",
"name": "test",
Expand Down Expand Up @@ -61,15 +61,15 @@ class CompatibilityTaskActionTest {
val parser = Schema.Parser()
val testSchema = parser.parse("{\"type\": \"record\", \"name\": \"test\", \"fields\": [{ \"name\": \"name\", \"type\": \"string\" }]}")
val registryClient = MockSchemaRegistryClient()
registryClient.register("test", testSchema)
registryClient.register("testdependencies", testSchema)

folderRule.newFolder("src", "main", "avro", "external")
File(folderRule.root, "src/main/avro/external/test.avsc").writeText("""
{"type": "record",
"name": "test",
"fields": [
{"name": "name", "type": "string" },
{"name": "address", "type": "Address"}
{"name": "address", "type": [ "null", "Address" ], "default": null}
]
}
""".trimIndent())
Expand All @@ -79,7 +79,7 @@ class CompatibilityTaskActionTest {
"name": "Address",
"fields": [
{"name": "city", "type": "string" },
{"name": "street", "type": "Street" }
{"name": "street", "type": [ "null", "Street" ] }
]
}
""".trimIndent())
Expand All @@ -88,15 +88,15 @@ class CompatibilityTaskActionTest {
{"type": "record",
"name": "Street",
"fields": [
{"name": "street", "type": "string" }
{"name": "street", "type": "string", "default": "" }
]
}
""".trimIndent())


val subjects = listOf(
Triple(
"test",
"testdependencies",
"src/main/avro/external/test.avsc",
listOf(
"src/main/avro/external/directDependency.avsc",
Expand All @@ -120,10 +120,10 @@ class CompatibilityTaskActionTest {
val parser = Schema.Parser()
val testSchema = parser.parse("{\"type\": \"record\", \"name\": \"test\", \"fields\": [{ \"name\": \"name\", \"type\": \"string\" }]}")
val registryClient = MockSchemaRegistryClient()
registryClient.register("test", testSchema)
registryClient.register("testing", testSchema)
folderRule.newFolder("src", "main", "avro", "external")

val subjects = arrayListOf(Triple("test", "src/main/avro/external/test.avsc", emptyList<String>()))
val subjects = arrayListOf(Triple("testing", "src/main/avro/external/test.avsc", emptyList<String>()))
File(folderRule.root, "src/main/avro/external/test.avsc").writeText("""
{"type": "record",
"name": "test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class CompatibilityTaskTest {
""".trimIndent()
coreAvsc.writeText(coreSchema)

val depAvsc = folderRule.newFile("avro/dependency_test.avsc")
val depAvsc = folderRule.newFile("avro/dependency.avsc")
val depSchema = """
{
"type":"record",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class DownloadTaskTest {
schemaRegistry {
url = 'http://localhost:$REGISTRY_FAKE_PORT/'
userInfo = ""
download {
subject('test-subject', 'src/main/avro/test')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class RegisterTaskTest {
schemaRegistry {
url = 'http://localhost:$REGISTRY_FAKE_PORT/'
userInfo = "username:password"
register {
subject('testSubject1', 'avro/test.avsc')
subject('testSubject2', 'avro/other_test.avsc')
Expand Down

0 comments on commit 6b387c3

Please sign in to comment.