Skip to content

Commit

Permalink
Merge pull request #80 from RedPillAnalytics/develop
Browse files Browse the repository at this point in the history
Basic Authentication
  • Loading branch information
stewartbryson authored Jan 29, 2020
2 parents df831f1 + 3cf0063 commit afb9d03
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
62 changes: 62 additions & 0 deletions src/ksqlServerTest/groovy/KsqlRestAuthTest.groovy
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 0 additions & 1 deletion src/ksqlServerTest/groovy/KsqlRestTest.groovy
Original file line number Diff line number Diff line change
@@ -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

Expand Down
12 changes: 6 additions & 6 deletions src/main/groovy/com/redpillanalytics/KsqlRest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ 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<String> 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()

// 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)

Expand Down Expand Up @@ -229,7 +229,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..."
Expand Down

0 comments on commit afb9d03

Please sign in to comment.