From f951740da95c3280d7fa7609288690c5758fc8c9 Mon Sep 17 00:00:00 2001 From: Stewart Bryson Date: Wed, 29 Jan 2020 11:10:50 -0500 Subject: [PATCH 1/2] #76 Using config() option to set basic authentication. --- src/main/groovy/com/redpillanalytics/KsqlRest.groovy | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/groovy/com/redpillanalytics/KsqlRest.groovy b/src/main/groovy/com/redpillanalytics/KsqlRest.groovy index f6bb6c4b..61e22395 100644 --- a/src/main/groovy/com/redpillanalytics/KsqlRest.groovy +++ b/src/main/groovy/com/redpillanalytics/KsqlRest.groovy @@ -44,6 +44,11 @@ class KsqlRest { def prepared = (ksql + ';').replace('\n', '').replace(';;', ';') if (['create', 'drop'].contains(getStatementType(ksql))) log.info prepared + + if (username && password) { + Unirest.config().setDefaultBasicAuth(username, password) + } + HttpResponse response = Unirest.post("${restUrl}/ksql") .header("Content-Type", "application/vnd.ksql.v1+json") .header("Cache-Control", "no-cache") @@ -51,10 +56,6 @@ class KsqlRest { .body(JsonOutput.toJson([ksql: prepared, streamsProperties: properties])) .asString() - // streamline the addition of basic credentials slightly - if (username && password) - response.header("Authorization", "Basic " + "${username}:${password}".bytes.encodeBase64().toString()) - log.debug "unirest response: ${response.dump()}" def body = new JsonSlurper().parseText(response.body) @@ -229,7 +230,7 @@ class KsqlRest { def result = execKsql(ksql, properties) - log.debug "result: ${result}" + log.debug "result: ${result}" if (result.status == 400 && result.body.message.contains('Incompatible data source type is STREAM')) { log.info "Type is now STREAM. Issuing DROP STREAM..." From 3cf00638288cc8b770dbba5f038d99fee205677e Mon Sep 17 00:00:00 2001 From: Stewart Bryson Date: Wed, 29 Jan 2020 11:30:44 -0500 Subject: [PATCH 2/2] #76 Removed postman-token --- .../groovy/KsqlRestAuthTest.groovy | 62 +++++++++++++++++++ src/ksqlServerTest/groovy/KsqlRestTest.groovy | 1 - .../com/redpillanalytics/KsqlRest.groovy | 1 - 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/ksqlServerTest/groovy/KsqlRestAuthTest.groovy diff --git a/src/ksqlServerTest/groovy/KsqlRestAuthTest.groovy b/src/ksqlServerTest/groovy/KsqlRestAuthTest.groovy new file mode 100644 index 00000000..27c7cb36 --- /dev/null +++ b/src/ksqlServerTest/groovy/KsqlRestAuthTest.groovy @@ -0,0 +1,62 @@ +import com.redpillanalytics.KsqlRest +import groovy.util.logging.Slf4j +import spock.lang.Shared +import spock.lang.Specification + +@Slf4j +class KsqlRestAuthTest extends Specification { + + @Shared + String pipelineEndpoint = System.getProperty("pipelineEndpoint") ?: 'http://localhost:8088' + String username = System.getProperty("pipelineUsername") ?: 'test' + String password = System.getProperty("pipelinePassword") ?: 'test' + + @Shared + def ksqlRest = new KsqlRest(restUrl: pipelineEndpoint, username: username, password: password) + + def "KSQL Server properties fetched"() { + + when: + def result = ksqlRest.getProperties() + + then: + log.warn "result: ${result.toString()}" + result + } + + def "KSQL extension directory path is returned"() { + + when: + def path = ksqlRest.getExtensionPath() + + then: + path + } + + def "KSQL extension directory file is returned"() { + + when: + def dir = ksqlRest.getExtensionDir() + + then: + dir + } + + def "KSQL REST URL is returned"() { + + when: + def url = ksqlRest.getSchemaRegistry() + + then: + url + } + + def "List of topics returned"() { + + when: + def topics = ksqlRest.getTopics() + + then: + topics + } +} diff --git a/src/ksqlServerTest/groovy/KsqlRestTest.groovy b/src/ksqlServerTest/groovy/KsqlRestTest.groovy index 80537178..4c627138 100644 --- a/src/ksqlServerTest/groovy/KsqlRestTest.groovy +++ b/src/ksqlServerTest/groovy/KsqlRestTest.groovy @@ -1,6 +1,5 @@ import com.redpillanalytics.KsqlRest import groovy.util.logging.Slf4j -import spock.lang.Ignore import spock.lang.Shared import spock.lang.Specification diff --git a/src/main/groovy/com/redpillanalytics/KsqlRest.groovy b/src/main/groovy/com/redpillanalytics/KsqlRest.groovy index 61e22395..27335eff 100644 --- a/src/main/groovy/com/redpillanalytics/KsqlRest.groovy +++ b/src/main/groovy/com/redpillanalytics/KsqlRest.groovy @@ -52,7 +52,6 @@ class KsqlRest { HttpResponse response = Unirest.post("${restUrl}/ksql") .header("Content-Type", "application/vnd.ksql.v1+json") .header("Cache-Control", "no-cache") - .header("Postman-Token", "473fbb05-9da1-4020-95c0-f2c60fed8289") .body(JsonOutput.toJson([ksql: prepared, streamsProperties: properties])) .asString()