-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathauthenticate.rs
84 lines (67 loc) · 2.65 KB
/
authenticate.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
use crate::common::utils::{setup_tracing, unique_keyspace_name, PerformDDL};
use async_trait::async_trait;
use bytes::{BufMut, BytesMut};
use scylla::authentication::{AuthError, AuthenticatorProvider, AuthenticatorSession};
use std::sync::Arc;
#[tokio::test]
#[ignore]
async fn authenticate_superuser() {
setup_tracing();
let uri = std::env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
println!("Connecting to {} with cassandra superuser ...", uri);
let session = scylla::client::session_builder::SessionBuilder::new()
.known_node(uri)
.user("cassandra", "cassandra")
.build()
.await
.unwrap();
let ks = unique_keyspace_name();
session.ddl(format!("CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = {{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}}", ks)).await.unwrap();
session.use_keyspace(ks, false).await.unwrap();
session.ddl("DROP TABLE IF EXISTS t;").await.unwrap();
println!("Ok.");
}
struct CustomAuthenticator;
#[async_trait]
impl AuthenticatorSession for CustomAuthenticator {
async fn evaluate_challenge(
&mut self,
_token: Option<&[u8]>,
) -> Result<Option<Vec<u8>>, AuthError> {
Err("Challenges are not expected".to_string())
}
async fn success(&mut self, _token: Option<&[u8]>) -> Result<(), AuthError> {
Ok(())
}
}
struct CustomAuthenticatorProvider;
#[async_trait]
impl AuthenticatorProvider for CustomAuthenticatorProvider {
async fn start_authentication_session(
&self,
_authenticator_name: &str,
) -> Result<(Option<Vec<u8>>, Box<dyn AuthenticatorSession>), AuthError> {
let mut response = BytesMut::new();
let cred = "\0cassandra\0cassandra";
response.put_slice(cred.as_bytes());
Ok((Some(response.to_vec()), Box::new(CustomAuthenticator)))
}
}
#[tokio::test]
#[ignore]
async fn custom_authentication() {
setup_tracing();
let uri = std::env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
println!("Connecting to {} with cassandra superuser ...", uri);
let session = scylla::client::session_builder::SessionBuilder::new()
.known_node(uri)
.authenticator_provider(Arc::new(CustomAuthenticatorProvider))
.build()
.await
.unwrap();
let ks = unique_keyspace_name();
session.ddl(format!("CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = {{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}}", ks)).await.unwrap();
session.use_keyspace(ks, false).await.unwrap();
session.ddl("DROP TABLE IF EXISTS t;").await.unwrap();
println!("Ok.");
}