|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use async_trait::async_trait; |
| 4 | +use bytes::{BufMut, BytesMut}; |
| 5 | +use scylla::authentication::{AuthenticatorProvider, AuthenticatorSession}; |
| 6 | +use scylla::errors::AuthError; |
| 7 | +use tokio::sync::Mutex; |
| 8 | + |
| 9 | +use crate::ccm::cluster::{Cluster, ClusterOptions}; |
| 10 | +use crate::ccm::{run_ccm_test_with_configuration, CLUSTER_VERSION}; |
| 11 | +use crate::common::utils::{setup_tracing, unique_keyspace_name, PerformDDL}; |
| 12 | + |
| 13 | +fn cluster_1_node() -> ClusterOptions { |
| 14 | + ClusterOptions { |
| 15 | + name: "cluster_auth_1_node".to_string(), |
| 16 | + version: CLUSTER_VERSION.clone(), |
| 17 | + nodes: vec![1], |
| 18 | + ..ClusterOptions::default() |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +async fn run_ccm_auth_test_cluster_one_node<T, TFut>(test: T) |
| 23 | +where |
| 24 | + T: FnOnce(Arc<Mutex<Cluster>>) -> TFut, |
| 25 | + TFut: std::future::Future<Output = ()>, |
| 26 | +{ |
| 27 | + run_ccm_test_with_configuration( |
| 28 | + cluster_1_node, |
| 29 | + |cluster| async move { |
| 30 | + cluster |
| 31 | + .enable_password_authentication() |
| 32 | + .await |
| 33 | + .expect("Failed to enable password authenticator"); |
| 34 | + cluster |
| 35 | + }, |
| 36 | + test, |
| 37 | + ) |
| 38 | + .await |
| 39 | +} |
| 40 | + |
| 41 | +#[tokio::test] |
| 42 | +#[cfg_attr(not(ccm_tests), ignore)] |
| 43 | +async fn authenticate_superuser_cluster_one_node() { |
| 44 | + setup_tracing(); |
| 45 | + async fn test(cluster: Arc<Mutex<Cluster>>) { |
| 46 | + let cluster = cluster.lock().await; |
| 47 | + |
| 48 | + tracing::info!( |
| 49 | + "Connecting to {:?} with cassandra superuser...", |
| 50 | + cluster.nodes().get_contact_endpoints().await |
| 51 | + ); |
| 52 | + |
| 53 | + let session = cluster |
| 54 | + .make_session_builder() |
| 55 | + .await |
| 56 | + .user("cassandra", "cassandra") |
| 57 | + .build() |
| 58 | + .await |
| 59 | + .unwrap(); |
| 60 | + let ks = unique_keyspace_name(); |
| 61 | + |
| 62 | + session.ddl(format!("CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = {{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}}", ks)).await.unwrap(); |
| 63 | + session.use_keyspace(ks, false).await.unwrap(); |
| 64 | + session.ddl("DROP TABLE IF EXISTS t;").await.unwrap(); |
| 65 | + |
| 66 | + tracing::info!("Ok."); |
| 67 | + } |
| 68 | + |
| 69 | + run_ccm_auth_test_cluster_one_node(test).await |
| 70 | +} |
| 71 | + |
| 72 | +struct CustomAuthenticator; |
| 73 | + |
| 74 | +#[async_trait] |
| 75 | +impl AuthenticatorSession for CustomAuthenticator { |
| 76 | + async fn evaluate_challenge( |
| 77 | + &mut self, |
| 78 | + _token: Option<&[u8]>, |
| 79 | + ) -> Result<Option<Vec<u8>>, AuthError> { |
| 80 | + Err("Challenges are not expected".to_string()) |
| 81 | + } |
| 82 | + |
| 83 | + async fn success(&mut self, _token: Option<&[u8]>) -> Result<(), AuthError> { |
| 84 | + Ok(()) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +struct CustomAuthenticatorProvider; |
| 89 | + |
| 90 | +#[async_trait] |
| 91 | +impl AuthenticatorProvider for CustomAuthenticatorProvider { |
| 92 | + async fn start_authentication_session( |
| 93 | + &self, |
| 94 | + _authenticator_name: &str, |
| 95 | + ) -> Result<(Option<Vec<u8>>, Box<dyn AuthenticatorSession>), AuthError> { |
| 96 | + let mut response = BytesMut::new(); |
| 97 | + let cred = "\0cassandra\0cassandra"; |
| 98 | + |
| 99 | + response.put_slice(cred.as_bytes()); |
| 100 | + |
| 101 | + Ok((Some(response.to_vec()), Box::new(CustomAuthenticator))) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#[tokio::test] |
| 106 | +#[cfg_attr(not(ccm_tests), ignore)] |
| 107 | +async fn custom_authentication_cluster_one_node() { |
| 108 | + setup_tracing(); |
| 109 | + async fn test(cluster: Arc<Mutex<Cluster>>) { |
| 110 | + let cluster = cluster.lock().await; |
| 111 | + |
| 112 | + tracing::info!( |
| 113 | + "Connecting to {:?} with custom authenticator as cassandra superuser...", |
| 114 | + cluster.nodes().get_contact_endpoints().await |
| 115 | + ); |
| 116 | + |
| 117 | + let session = cluster |
| 118 | + .make_session_builder() |
| 119 | + .await |
| 120 | + .authenticator_provider(Arc::new(CustomAuthenticatorProvider)) |
| 121 | + .build() |
| 122 | + .await |
| 123 | + .unwrap(); |
| 124 | + let ks = unique_keyspace_name(); |
| 125 | + |
| 126 | + session.ddl(format!("CREATE KEYSPACE IF NOT EXISTS {} WITH REPLICATION = {{'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}}", ks)).await.unwrap(); |
| 127 | + session.use_keyspace(ks, false).await.unwrap(); |
| 128 | + session.ddl("DROP TABLE IF EXISTS t;").await.unwrap(); |
| 129 | + |
| 130 | + tracing::info!("Ok."); |
| 131 | + } |
| 132 | + |
| 133 | + run_ccm_auth_test_cluster_one_node(test).await |
| 134 | +} |
0 commit comments