Skip to content

Commit

Permalink
feat(prometheus): added authentication for HTTP mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Cubxity committed Oct 1, 2021
1 parent 3d83d3b commit c4fc3a7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,30 @@ enum class PrometheusMode {
@Serializable
data class PrometheusHttpConfig(
val host: String = "0.0.0.0",
val port: Int = 9100
val port: Int = 9100,
val authentication: AuthenticationConfig = AuthenticationConfig()
)

@Serializable
data class PushGatewayConfig(
val job: String = "unifiedmetrics",
val url: String = "http://pushgateway:9091",
val authentication: PushGatewayAuthenticationConfig = PushGatewayAuthenticationConfig(),
val authentication: AuthenticationConfig = AuthenticationConfig(),
val interval: Long = 10
)

@Serializable
data class PushGatewayAuthenticationConfig(
val scheme: PushGatewayAuthenticationScheme = PushGatewayAuthenticationScheme.Basic,
val username: String = "username",
val password: String = "password"
)

@Serializable
enum class PushGatewayAuthenticationScheme {
enum class AuthenticationScheme {
@SerialName("NONE")
None,

@SerialName("BASIC")
Basic
}
}

@Serializable
data class AuthenticationConfig(
val scheme: AuthenticationScheme = AuthenticationScheme.None,
val username: String = "username",
val password: String = "password"
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,48 @@

package dev.cubxity.plugins.metrics.prometheus.exporter

import com.sun.net.httpserver.BasicAuthenticator
import dev.cubxity.plugins.metrics.api.UnifiedMetrics
import dev.cubxity.plugins.metrics.prometheus.PrometheusMetricsDriver
import dev.cubxity.plugins.metrics.prometheus.collector.UnifiedMetricsCollector
import dev.cubxity.plugins.metrics.prometheus.config.AuthenticationScheme
import io.prometheus.client.CollectorRegistry
import io.prometheus.client.exporter.HTTPServer
import java.net.InetSocketAddress

class PrometheusHTTPExporter(private val api: UnifiedMetrics, private val driver: PrometheusMetricsDriver) : PrometheusExporter {
class PrometheusHTTPExporter(
private val api: UnifiedMetrics,
private val driver: PrometheusMetricsDriver
) : PrometheusExporter {
private var server: HTTPServer? = null

override fun initialize() {
val registry = CollectorRegistry()
registry.register(UnifiedMetricsCollector(api))

server = HTTPServer(InetSocketAddress(driver.config.http.host, driver.config.http.port), registry)
server = HTTPServer.Builder()
.withHostname(driver.config.http.host)
.withPort(driver.config.http.port)
.withRegistry(registry)
.apply {
with(driver.config.http.authentication) {
if (scheme == AuthenticationScheme.Basic) {
withAuthenticator(Authenticator(username, password))
}
}
}
.build()
}

override fun close() {
server?.close()
server = null
}

private class Authenticator(
private val username: String,
private val password: String
) : BasicAuthenticator("unifiedmetrics") {
override fun checkCredentials(username: String?, password: String?): Boolean =
this.username == username && this.password == password
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package dev.cubxity.plugins.metrics.prometheus.exporter
import dev.cubxity.plugins.metrics.api.UnifiedMetrics
import dev.cubxity.plugins.metrics.prometheus.PrometheusMetricsDriver
import dev.cubxity.plugins.metrics.prometheus.collector.MetricSamplesCollection
import dev.cubxity.plugins.metrics.prometheus.config.PushGatewayAuthenticationScheme
import dev.cubxity.plugins.metrics.prometheus.config.AuthenticationScheme
import io.prometheus.client.exporter.BasicAuthHttpConnectionFactory
import io.prometheus.client.exporter.PushGateway
import kotlinx.coroutines.*
Expand All @@ -42,7 +42,7 @@ class PushGatewayExporter(

gateway = PushGateway(URL(config.url)).apply {
with(config.authentication) {
if (scheme == PushGatewayAuthenticationScheme.Basic) {
if (scheme == AuthenticationScheme.Basic) {
setConnectionFactory(BasicAuthHttpConnectionFactory(username, password))
}
}
Expand Down

0 comments on commit c4fc3a7

Please sign in to comment.