solution: configure rpc server TLS certificate

This commit is contained in:
Igor Artamonov
2019-08-15 18:54:17 -04:00
parent e2520f4dbf
commit 38afb2a1a4
9 changed files with 43 additions and 50 deletions

View File

@@ -67,11 +67,11 @@ class UpstreamsConfig {
class EthereumConnection : UpstreamConnection() { class EthereumConnection : UpstreamConnection() {
var rpc: HttpEndpoint? = null var rpc: HttpEndpoint? = null
var ws: WsEndpoint? = null var ws: WsEndpoint? = null
var auth: BasicAuth? = null
} }
class HttpEndpoint(val url: URI) { class HttpEndpoint(val url: URI) {
var auth: Auth? = null var basicAuth: BasicAuth? = null
var tls: TlsAuth? = null
} }
class WsEndpoint(val url: URI) { class WsEndpoint(val url: URI) {
@@ -87,11 +87,11 @@ class UpstreamsConfig {
val password: String val password: String
) : Auth() ) : Auth()
class TlsAuth : Auth() { class TlsAuth(
var ca: String? = null var ca: String? = null,
var certificate: String? = null var certificate: String? = null,
var key: String? = null var key: String? = null
} ) : Auth()
//TODO make it unmodifiable after initial load //TODO make it unmodifiable after initial load
class Labels: HashMap<String, String>() { class Labels: HashMap<String, String>() {

View File

@@ -49,10 +49,8 @@ class UpstreamsConfigReader {
getValueAsString(node, "url")?.let { url -> getValueAsString(node, "url")?.let { url ->
val http = UpstreamsConfig.HttpEndpoint(URI(url)) val http = UpstreamsConfig.HttpEndpoint(URI(url))
connection.rpc = http connection.rpc = http
http.auth = readAuth(getMapping(node, "auth")) http.basicAuth = readBasicAuth(node)
} http.tls = readTls(node)
readAuth(getMapping(node, "auth"))?.let { auth ->
connection.auth = auth as UpstreamsConfig.BasicAuth
} }
} }
getMapping(connConfigNode, "ws")?.let { node -> getMapping(connConfigNode, "ws")?.let { node ->
@@ -79,7 +77,7 @@ class UpstreamsConfigReader {
getValueAsInt(connConfigNode, "port")?.let { getValueAsInt(connConfigNode, "port")?.let {
connection.port = it connection.port = it
} }
connection.auth = readAuth(getMapping(connConfigNode, "auth")) as UpstreamsConfig.TlsAuth? connection.auth = readTls(connConfigNode)
} }
} }
@@ -137,34 +135,29 @@ class UpstreamsConfigReader {
return options return options
} }
private fun readAuth(authNode: MappingNode?): UpstreamsConfig.Auth? { private fun readBasicAuth(node: MappingNode?): UpstreamsConfig.BasicAuth? {
return getValueAsString(authNode, "type")?.let { return getMapping(node, "basic-auth")?.let { authNode ->
return when (it) { val username = getValueAsString(authNode, "username")
"tls" -> { val password = getValueAsString(authNode, "password")
val auth = UpstreamsConfig.TlsAuth() if (username != null && password != null) {
auth.ca = getValueAsString(authNode, "ca") UpstreamsConfig.BasicAuth(username, password)
auth.certificate = getValueAsString(authNode, "certificate") } else {
auth.key = getValueAsString(authNode, "key") log.warn("Basic auth is not fully configured")
auth null
}
"basic" -> {
val username = getValueAsString(authNode, "username")
val password = getValueAsString(authNode, "password")
if (username != null && password != null) {
UpstreamsConfig.BasicAuth(username, password)
} else {
log.warn("Basic auth is not fully configured")
null
}
}
else -> {
log.warn("Invalid Auth type: $it")
null
}
} }
} }
} }
private fun readTls(node: MappingNode?): UpstreamsConfig.TlsAuth? {
return getMapping(node, "tls")?.let { authNode ->
val auth = UpstreamsConfig.TlsAuth()
auth.ca = getValueAsString(authNode, "ca")
auth.certificate = getValueAsString(authNode, "certificate")
auth.key = getValueAsString(authNode, "key")
auth
}
}
private fun hasAny(mappingNode: MappingNode?, key: String): Boolean { private fun hasAny(mappingNode: MappingNode?, key: String): Boolean {
if (mappingNode == null) { if (mappingNode == null) {
return false return false

View File

@@ -14,7 +14,6 @@ import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
import reactor.core.publisher.Flux import reactor.core.publisher.Flux
import reactor.core.publisher.TopicProcessor import reactor.core.publisher.TopicProcessor
import reactor.core.publisher.toFlux
import java.io.File import java.io.File
import java.net.URI import java.net.URI
import java.util.* import java.util.*
@@ -110,9 +109,14 @@ open class ConfiguredUpstreams(
val urls = ArrayList<URI>() val urls = ArrayList<URI>()
up.rpc?.let { endpoint -> up.rpc?.let { endpoint ->
val rpcTransport = DefaultRpcTransport(endpoint.url) val rpcTransport = DefaultRpcTransport(endpoint.url)
up.auth?.let { auth -> up.rpc?.basicAuth?.let { auth ->
rpcTransport.setBasicAuth(auth.username, auth.password) rpcTransport.setBasicAuth(auth.username, auth.password)
} }
up.rpc?.tls?.let { tls ->
tls.ca?.let { ca ->
File(ca).inputStream().use { cert -> rpcTransport.setTrustedCertificate(cert) }
}
}
val rpcClient = DefaultRpcClient(rpcTransport) val rpcClient = DefaultRpcClient(rpcTransport)
rpcApi = EthereumApi( rpcApi = EthereumApi(
rpcClient, rpcClient,

View File

@@ -26,6 +26,7 @@ class UpstreamValidator(
} }
return UpstreamAvailability.OK return UpstreamAvailability.OK
} catch (e: Throwable) { } catch (e: Throwable) {
e.printStackTrace()
return UpstreamAvailability.UNAVAILABLE return UpstreamAvailability.UNAVAILABLE
} }
} }

View File

@@ -39,9 +39,8 @@ class UpstreamsConfigReaderSpec extends Specification {
connection instanceof UpstreamsConfig.EthereumConnection connection instanceof UpstreamsConfig.EthereumConnection
with((UpstreamsConfig.EthereumConnection)connection) { with((UpstreamsConfig.EthereumConnection)connection) {
rpc.url == new URI("https://mainnet.infura.io/v3/fa28c968191849c1aff541ad1d8511f2") rpc.url == new URI("https://mainnet.infura.io/v3/fa28c968191849c1aff541ad1d8511f2")
rpc.auth != null rpc.basicAuth != null
rpc.auth instanceof UpstreamsConfig.BasicAuth with((UpstreamsConfig.BasicAuth)rpc.basicAuth) {
with((UpstreamsConfig.BasicAuth)rpc.auth) {
username == "4fc258fe41a68149c199ad8f281f2015" username == "4fc258fe41a68149c199ad8f281f2015"
password == "1a68f20154fc258fe4149c199ad8f281" password == "1a68f20154fc258fe4149c199ad8f281"
} }
@@ -65,8 +64,8 @@ class UpstreamsConfigReaderSpec extends Specification {
connection instanceof UpstreamsConfig.GrpcConnection connection instanceof UpstreamsConfig.GrpcConnection
with((UpstreamsConfig.GrpcConnection)connection) { with((UpstreamsConfig.GrpcConnection)connection) {
host == "10.2.0.15" host == "10.2.0.15"
auth instanceof UpstreamsConfig.TlsAuth auth != null
with((UpstreamsConfig.TlsAuth)auth) { with(auth) {
ca == "/etc/ca.myservice.com.crt" ca == "/etc/ca.myservice.com.crt"
certificate == "/etc/client1.myservice.com.crt" certificate == "/etc/client1.myservice.com.crt"
key == "/etc/client1.myservice.com.key" key == "/etc/client1.myservice.com.key"

View File

@@ -22,7 +22,6 @@ upstreams:
ethereum: ethereum:
rpc: rpc:
url: "https://mainnet.infura.io/v3/fa28c968191849c1aff541ad1d8511f2" url: "https://mainnet.infura.io/v3/fa28c968191849c1aff541ad1d8511f2"
auth: basic-auth:
type: basic
username: 4fc258fe41a68149c199ad8f281f2015 username: 4fc258fe41a68149c199ad8f281f2015
password: 1a68f20154fc258fe4149c199ad8f281 password: 1a68f20154fc258fe4149c199ad8f281

View File

@@ -11,8 +11,7 @@ upstreams:
connection: connection:
grpc: grpc:
host: "10.2.0.15" host: "10.2.0.15"
auth: tls:
type: tls
ca: /etc/ca.myservice.com.crt ca: /etc/ca.myservice.com.crt
certificate: /etc/client1.myservice.com.crt certificate: /etc/client1.myservice.com.crt
key: /etc/client1.myservice.com.key key: /etc/client1.myservice.com.key

View File

@@ -11,8 +11,7 @@ upstreams:
connection: connection:
grpc: grpc:
host: "10.2.0.15" host: "10.2.0.15"
auth: tls:
type: tls
ca: /etc/ca.myservice.com.crt ca: /etc/ca.myservice.com.crt
certificate: /etc/client1.myservice.com.crt certificate: /etc/client1.myservice.com.crt
key: /etc/client1.myservice.com.key key: /etc/client1.myservice.com.key

View File

@@ -26,7 +26,6 @@ upstreams:
ethereum: ethereum:
rpc: rpc:
url: "https://mainnet.infura.io/v3/fa28c968191849c1aff541ad1d8511f2" url: "https://mainnet.infura.io/v3/fa28c968191849c1aff541ad1d8511f2"
auth: basic-auth:
type: basic
username: 4fc258fe41a68149c199ad8f281f2015 username: 4fc258fe41a68149c199ad8f281f2015
password: 1a68f20154fc258fe4149c199ad8f281 password: 1a68f20154fc258fe4149c199ad8f281