forked from conduktor/kafka-security-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
140 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,11 @@ ksm { | |
|
||
} | ||
|
||
server { | ||
port = 8080 | ||
port = ${?SERVER_PORT} | ||
} | ||
|
||
parser { | ||
csv { | ||
delimiter = "," | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.conduktor.ksm.web | ||
|
||
import com.sun.net.httpserver.{HttpExchange, HttpHandler} | ||
|
||
trait Probe { | ||
// implementation should be non blocking | ||
def isSuccessful: Boolean | ||
} | ||
|
||
class ProbeHandler(probes: List[Probe]) extends HttpHandler { | ||
override def handle(exc: HttpExchange): Unit = { | ||
val checkup = probes.forall(p => p.isSuccessful) | ||
val payload = Server.responseMapper | ||
.createObjectNode() | ||
.put("success", checkup) | ||
val response = Server.responseMapper.writeValueAsString(payload) | ||
val responseCode = if (checkup) 200 else 500 | ||
exc.sendResponseHeaders(responseCode, response.length()) | ||
val os = exc.getResponseBody | ||
os.write(response.getBytes) | ||
os.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.conduktor.ksm.web | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.sun.net.httpserver.HttpServer | ||
import org.slf4j.LoggerFactory | ||
|
||
import java.net.InetSocketAddress | ||
import java.util.concurrent.Executors | ||
|
||
object Server { | ||
val responseMapper = new ObjectMapper() | ||
} | ||
|
||
class Server(port: Int, livenessProbes: List[Probe]) { | ||
private val log = LoggerFactory.getLogger(Server.getClass) | ||
private val server = HttpServer.create(new InetSocketAddress(port), 0) | ||
server.createContext("/api/probe/ready", new ProbeHandler(List())) | ||
server.createContext("/api/probe/alive", new ProbeHandler(livenessProbes)) | ||
|
||
def start(): Unit = { | ||
log.info("Staring server on {}", port) | ||
server.setExecutor(Executors.newSingleThreadExecutor()) | ||
server.start() | ||
} | ||
|
||
def stop(): Unit = { | ||
server.stop(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.conduktor.ksm.web | ||
|
||
import org.scalatest.{BeforeAndAfterAll, FlatSpec} | ||
import skinny.http.{HTTP, Request} | ||
|
||
class LivenessTestProbe extends Probe { | ||
var success = false; | ||
|
||
override def isSuccessful: Boolean = { | ||
success | ||
} | ||
} | ||
|
||
class ServerTest extends FlatSpec with BeforeAndAfterAll { | ||
private val livenessTestProbe1 = new LivenessTestProbe | ||
private val livenessTestProbe2 = new LivenessTestProbe | ||
private val testSubject = | ||
new Server(7777, List(livenessTestProbe1, livenessTestProbe2)) | ||
private val testServerUrl = "http://localhost:7777"; | ||
private val testReadyEndpoint = testServerUrl + "/api/probe/ready" | ||
private val testAliveEndpoint = testServerUrl + "/api/probe/alive" | ||
|
||
override protected def beforeAll(): Unit = { | ||
testSubject.start() | ||
} | ||
|
||
override protected def afterAll(): Unit = { | ||
testSubject.stop() | ||
} | ||
|
||
"get ready probe endpoint" should "return 200 with success true" in { | ||
val response = HTTP.get(Request(testReadyEndpoint)) | ||
assert(response.status == 200) | ||
assert(new String(response.body) == "{\"success\":true}") | ||
} | ||
|
||
"get alive probe endpoint" should "return 200 with success true, if all probes are successful" in { | ||
livenessTestProbe1.success = true | ||
livenessTestProbe2.success = true | ||
val response = HTTP.get(Request(testAliveEndpoint)) | ||
assert(response.status == 200) | ||
assert(new String(response.body) == "{\"success\":true}") | ||
} | ||
|
||
"get alive probe endpoint" should "return 500 with success false, if some probes are un-successful" in { | ||
livenessTestProbe1.success = true | ||
livenessTestProbe2.success = false | ||
val response = HTTP.get(Request(testAliveEndpoint)) | ||
assert(response.status == 500) | ||
assert(new String(response.body) == "{\"success\":false}") | ||
} | ||
|
||
"get alive probe endpoint" should "return 500 with success false, if all probes are un-successful" in { | ||
livenessTestProbe1.success = false | ||
livenessTestProbe2.success = false | ||
val response = HTTP.get(Request(testAliveEndpoint)) | ||
assert(response.status == 500) | ||
assert(new String(response.body) == "{\"success\":false}") | ||
} | ||
} |