forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.rs
92 lines (79 loc) · 2.89 KB
/
tls.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use anyhow::Result;
use scylla::transport::session::{IntoTypedRows, Session};
use scylla::SessionBuilder;
use std::env;
use std::fs;
use std::path::PathBuf;
use openssl::ssl::{SslContextBuilder, SslMethod, SslVerifyMode};
// How to run scylla instance with TLS:
//
// Edit your scylla.yaml file and add paths to certificates
// ex:
// client_encryption_options:
// enabled: true
// certificate: /etc/scylla/db.crt
// keyfile: /etc/scylla/db.key
//
// If using docker mount your scylla.yaml file and your cert files with option
// --volume $(pwd)/tls.yaml:/etc/scylla/scylla.yaml
//
// If python returns permission error 13 use "Z" flag
// --volume $(pwd)/tls.yaml:/etc/scylla/scylla.yaml:Z
//
// In your Rust program connect to port 9142 if it wasn't changed
// Create new SslContextBuilder with SslMethod that is used in your connection
// Set verification mode
// if SslVerifyMode::PEER with self-signed certificate you have to
// use set_ca_file method with path to your ca.crt file as an argument
// if SslVerifyMode::NONE you don't need to use any aditional methods
//
// Build it and add to scylla-rust-driver's SessionBuilder
#[tokio::main]
async fn main() -> Result<()> {
// Create connection
let uri = env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9142".to_string());
println!("Connecting to {} ...", uri);
let mut context_builder = SslContextBuilder::new(SslMethod::tls())?;
let ca_dir = fs::canonicalize(PathBuf::from("./test/tls/ca.crt"))?;
context_builder.set_ca_file(ca_dir.as_path())?;
context_builder.set_verify(SslVerifyMode::PEER);
let session: Session = SessionBuilder::new()
.known_node(uri)
.ssl_context(Some(context_builder.build()))
.build()
.await?;
session.query("CREATE KEYSPACE IF NOT EXISTS ks WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1}", &[]).await?;
session
.query(
"CREATE TABLE IF NOT EXISTS ks.t (a int, b int, c text, primary key (a, b))",
&[],
)
.await?;
session
.query("INSERT INTO ks.t (a, b, c) VALUES (?, ?, ?)", (3, 4, "def"))
.await?;
session
.query("INSERT INTO ks.t (a, b, c) VALUES (1, 2, 'abc')", &[])
.await?;
let prepared = session
.prepare("INSERT INTO ks.t (a, b, c) VALUES (?, 7, ?)")
.await?;
session
.execute(&prepared, (42_i32, "I'm prepared!"))
.await?;
session
.execute(&prepared, (43_i32, "I'm prepared 2!"))
.await?;
session
.execute(&prepared, (44_i32, "I'm prepared 3!"))
.await?;
// Rows can be parsed as tuples
if let Some(rows) = session.query("SELECT a, b, c FROM ks.t", &[]).await?.rows {
for row in rows.into_typed::<(i32, i32, String)>() {
let (a, b, c) = row?;
println!("a, b, c: {}, {}, {}", a, b, c);
}
}
println!("Ok.");
Ok(())
}