Skip to content

Commit

Permalink
feat(server): Add tls support
Browse files Browse the repository at this point in the history
  • Loading branch information
mxxntype committed May 23, 2024
1 parent 9c6873e commit 1affbe1
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export SERVER_PORT="9001"
export LLM_PORT="11434"

# A complete URLs for future use.
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:localhost/${POSTGRES_DB}"
export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:${PGPORT}/${POSTGRES_DB}"
export DOCKER_DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${PGPORT}/${POSTGRES_DB}"
export KV_URL="redis://localhost:${REDIS_PORT}"
export DOCKER_KV_URL="redis://${REDIS_HOST}:${REDIS_PORT}"
102 changes: 101 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ prost = "0.12.4"
prost-types = "0.12.4"
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] }
tokio-stream = "0.1.15"
tonic = "0.11.0"
tonic = { version = "0.11.0", features = ["tls"] }
uuid = { version = "1.8.0", features = ["v4"] }
promkit = "0.4.2"
crossterm = "0.27.0"
Expand Down
20 changes: 19 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ use tcp_chat::proto::{RoomWithUserCreationRequest, UserCredentials, UserLookupRe
use tcp_chat::{auth::AuthenticatedRequest, proto};
use tokio_stream::StreamExt;
use tonic::service::interceptor::InterceptedService;
use tonic::transport::{Certificate, ClientTlsConfig};
use tonic::{transport::Channel, Request, Status};
use uuid::Uuid;

const CERT: &str = include_str!("../../tls/ca.pem");

#[tokio::main]
async fn main() {
let _ = color_eyre::install();
Expand Down Expand Up @@ -41,10 +44,19 @@ async fn main() {
.run()
.unwrap();

let mut registry = RegistryClient::connect("http://localhost:9001")
let channel = Channel::from_static("https://localhost:9001")
.tls_config(
ClientTlsConfig::new()
.ca_certificate(Certificate::from_pem(CERT))
.domain_name("example.com"),
)
.unwrap()
.connect()
.await
.unwrap();

let mut registry = RegistryClient::new(channel);

match auth_strategy.as_str() {
"Login" => {
let auth_pair = registry
Expand All @@ -69,6 +81,12 @@ async fn main() {

async fn list_rooms(auth_pair: AuthPair) {
let chat = Channel::from_static("https://localhost:9001")
.tls_config(
ClientTlsConfig::new()
.ca_certificate(Certificate::from_pem(CERT))
.domain_name("example.com"),
)
.unwrap()
.connect()
.await
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ streebog = { version = "0.10.2", optional = true } # GOST 34.11-2012 Hash functi
thiserror = "1.0.61"
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] }
tokio-util = "0.7.11"
tonic = "0.11.0"
tonic = { version = "0.11.0", features = ["tls"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
uuid = { version = "1.8.0", features = ["v4"] }
Expand Down
9 changes: 8 additions & 1 deletion server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ use crate::proto::chat_server::ChatServer;
use crate::proto::registry_server::RegistryServer;
use crate::services::{chat::Chat, registry::Registry};
use std::env;
use tonic::transport::Server;
use tonic::transport::{Certificate, Identity, Server, ServerTlsConfig};
use tracing_subscriber::fmt;

const CERT: &str = include_str!("../../tls/server.pem");
const KEY: &str = include_str!("../../tls/server.key");

#[derive(Debug, Default)]
pub struct TCPChat {}

Expand Down Expand Up @@ -47,8 +50,12 @@ impl TCPChat {
let registry = Registry::with_persistence_pool(persistence_pool.clone());
let registry = RegistryServer::new(registry);

let identity = Identity::from_pem(CERT, KEY);

tracing::info!(message = "Starting server", ?addr);
Server::builder()
.tls_config(ServerTlsConfig::new().identity(identity))
.unwrap()
.trace_fn(|_| tracing::info_span!("server"))
.add_service(registry)
.add_service(chat)
Expand Down
2 changes: 2 additions & 0 deletions tls/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> [!CAUTION]
> These TLS CA certificates and private keys are given as example ones to showcase SSL/TLS capabilities of the server and client. We are not generating them automatically using Certbot or something else for ease of deployment.
28 changes: 28 additions & 0 deletions tls/ca.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN CERTIFICATE-----
MIIE3DCCA0SgAwIBAgIRAObeYbJFiVQSGR8yk44dsOYwDQYJKoZIhvcNAQELBQAw
gYUxHjAcBgNVBAoTFW1rY2VydCBkZXZlbG9wbWVudCBDQTEtMCsGA1UECwwkbHVj
aW9ATHVjaW9zLVdvcmstTUJQIChMdWNpbyBGcmFuY28pMTQwMgYDVQQDDCtta2Nl
cnQgbHVjaW9ATHVjaW9zLVdvcmstTUJQIChMdWNpbyBGcmFuY28pMB4XDTE5MDky
OTIzMzUzM1oXDTI5MDkyOTIzMzUzM1owgYUxHjAcBgNVBAoTFW1rY2VydCBkZXZl
bG9wbWVudCBDQTEtMCsGA1UECwwkbHVjaW9ATHVjaW9zLVdvcmstTUJQIChMdWNp
byBGcmFuY28pMTQwMgYDVQQDDCtta2NlcnQgbHVjaW9ATHVjaW9zLVdvcmstTUJQ
IChMdWNpbyBGcmFuY28pMIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEA
y/vE61ItbN/1qMYt13LMf+le1svwfkCCOPsygk7nWeRXmomgUpymqn1LnWiuB0+e
4IdVH2f5E9DknWEpPhKIDMRTCbz4jTwQfHrxCb8EGj3I8oO73pJO5S/xCedM9OrZ
qWcYWwN0GQ8cO/ogazaoZf1uTrRNHyzRyQsKyb412kDBTNEeldJZ2ljKgXXvh4HO
2ZIk9K/ZAaAf6VN8K/89rlJ9/KPgRVNsyAapE+Pb8XXKtpzeFiEcUfuXVYWtkoW+
xyn/Zu8A1L2CXMQ1sARh7P/42BTMKr5pfraYgcBGxKXLrxoySpxCO9KqeVveKy1q
fPm5FCwFsXDr0koFLrCiR58mcIO/04Q9DKKTV4Z2a+LoqDJRY37KfBSc8sDMPhw5
k7g3WPoa6QwXRjZTCA5fHWVgLOtcwLsnju5tBE4LDxwF6s+1wPF8NI5yUfufcEjJ
Z6JBwgoWYosVj27Lx7KBNLU/57PX9ryee691zmtswt0tP0WVBAgalhYWg99RXoa3
AgMBAAGjRTBDMA4GA1UdDwEB/wQEAwICBDASBgNVHRMBAf8ECDAGAQH/AgEAMB0G
A1UdDgQWBBQdvlE4Bdcsjc9oaxjDCRu5FiuZkzANBgkqhkiG9w0BAQsFAAOCAYEA
BP/6o1kPINksMJZSSXgNCPZskDLyGw7auUZBnQ0ocDT3W6gXQvT/27LM1Hxoj9Eh
qU1TYdEt7ppecLQSGvzQ02MExG7H75art75oLiB+A5agDira937YbK4MCjqW481d
bDhw6ixJnY1jIvwjEZxyH6g94YyL927aSPch51fys0kSnjkFzC2RmuzDADScc4XH
5P1+/3dnIm3M5yfpeUzoaOrTXNmhn8p0RDIGrZ5kA5eISIGGD3Mm8FDssUNKndtO
g4ojHUsxb14icnAYGeye1NOhGiqN6TEFcgr6MPd0XdFNZ5c0HUaBCfN6bc+JxDV5
MKZVJdNeJsYYwilgJNHAyZgCi30JC20xeYVtTF7CEEsMrFDGJ70Kz7o/FnRiFsA1
ZSwVVWhhkHG2VkT4vlo0O3fYeZpenYicvy+wZNTbGK83gzHWqxxNC1z3Etg5+HRJ
F9qeMWPyfA3IHYXygiMcviyLcyNGG/SJ0EhUpYBN/Gg7wI5yFkcsxUDPPzd23O0M
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions tls/server.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----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rHTwFk6UV+xdrt9MBdd1
XdSn7bwOPQKBgC9VQAko8uDvUf+C8PXiv2uONrl13PPJJY3WpR9qFEVOREnDxszS
HNzVwxPZdTJiykbkCjoqPadfQJDzopZxGQLAifU29lTamKcSx3CMe3gOFDxaovXa
zD5XAxP0hfJwZsdu1G6uj5dsTrJ0oJ+L+wc0pZBqwGIU/L/XOo9/g1DZAoGAUebL
kuH98ik7EUK2VJq8EJERI9/ailLsQb6I+WIxtZGiPqwHhWencpkrNQZtj8dbB9JT
rLwUHrMgZOlAoRafgTyez4zMzS3wJJ/Mkp8U67hM4h7JPwMSvUpIrMYDiJSjIA9L
er/qSw1/Pypx22uWMHmAZWRAgvLPtAQrB0Wqk4kCgYEAr2H1PvfbwZwkSvlMt5o8
WLnBbxcM3AKglLRbkShxxgiZYdEP71/uOtRMiL26du5XX8evItITN0DsvmXL/kcd
h29LK7LM5uLw7efz0Qxs03G6kEyIHVkacowHi5I5Ul1qI61SoV3yMB1TjIU+bXZt
0ZjC07totO0fqPOLQxonjQg=
-----END PRIVATE KEY-----
27 changes: 27 additions & 0 deletions tls/server.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN CERTIFICATE-----
MIIEmDCCAwCgAwIBAgIQVEJFCgU/CZk9JEwTucWPpzANBgkqhkiG9w0BAQsFADCB
hTEeMBwGA1UEChMVbWtjZXJ0IGRldmVsb3BtZW50IENBMS0wKwYDVQQLDCRsdWNp
b0BMdWNpb3MtV29yay1NQlAgKEx1Y2lvIEZyYW5jbykxNDAyBgNVBAMMK21rY2Vy
dCBsdWNpb0BMdWNpb3MtV29yay1NQlAgKEx1Y2lvIEZyYW5jbykwHhcNMTkwNjAx
MDAwMDAwWhcNMjkwOTI5MjMzNTM0WjBYMScwJQYDVQQKEx5ta2NlcnQgZGV2ZWxv
cG1lbnQgY2VydGlmaWNhdGUxLTArBgNVBAsMJGx1Y2lvQEx1Y2lvcy1Xb3JrLU1C
UCAoTHVjaW8gRnJhbmNv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