Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/modules/ROOT/partials/rust/errors/ConnectionError.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[options="header"]
|===
|Variant
a| `AbsentTlsConfigForTlsConnection`
a| `AddressTranslationMismatch`
a| `BrokenPipe`
a| `ClusterAllNodesFailed`
Expand All @@ -23,13 +24,15 @@ a| `InvalidResponseField`
a| `ListsNotImplemented`
a| `MissingPort`
a| `MissingResponseField`
a| `NonTlsConnectionWithHttps`
a| `QueryStreamNoResponse`
a| `RPCMethodUnavailable`
a| `SSLCertificateNotValidated`
a| `ServerConnectionFailed`
a| `ServerConnectionFailedStatusError`
a| `ServerConnectionFailedWithError`
a| `ServerConnectionIsClosed`
a| `TlsConnectionWithoutHttps`
a| `TokenCredentialInvalid`
a| `TransactionIsClosed`
a| `TransactionIsClosedWithErrors`
Expand Down
2 changes: 1 addition & 1 deletion python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TypeDBExample:
def typedb_example(self):
# Open a driver connection. Specify your parameters if needed
# The connection will be automatically closed on the "with" block exit
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions()) as driver:
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions(is_tls_enabled=False)) as driver:
# Create a database
driver.databases.create("typedb")
database = driver.databases.get("typedb")
Expand Down
2 changes: 1 addition & 1 deletion python/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TypeDBExample:
def typedb_example(self):
# Open a driver connection. Specify your parameters if needed
# The connection will be automatically closed on the "with" block exit
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions()) as driver:
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions(is_tls_enabled=False)) as driver:
# Create a database
driver.databases.create("typedb")
database = driver.databases.get("typedb")
Expand Down
2 changes: 1 addition & 1 deletion python/tests/behaviour/background/cluster/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def create_driver(context, host="localhost", port=None, username=None, password=
if password is None:
password = "password"
credentials = Credentials(username, password)
return TypeDB.driver(address=f"{host}:{port}", credentials=credentials, driver_options=DriverOptions())
return TypeDB.driver(address=f"{host}:{port}", credentials=credentials, driver_options=DriverOptions(is_tls_enabled=False))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests started failing without this flag. Which is good.



def after_scenario(context: Context, scenario):
Expand Down
2 changes: 1 addition & 1 deletion python/tests/behaviour/background/community/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def create_driver(context, host="localhost", port=None, username=None, password=
if password is None:
password = "password"
credentials = Credentials(username, password)
return TypeDB.driver(address=f"{host}:{port}", credentials=credentials, driver_options=DriverOptions())
return TypeDB.driver(address=f"{host}:{port}", credentials=credentials, driver_options=DriverOptions(is_tls_enabled=False))


def after_scenario(context: Context, scenario):
Expand Down
2 changes: 1 addition & 1 deletion python/tests/deployment/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TestDeployedPythonDriver(TestCase):
def setUpClass(cls):
super(TestDeployedPythonDriver, cls).setUpClass()
global driver
driver = TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions())
driver = TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions(is_tls_enabled=False))

@classmethod
def tearDownClass(cls):
Expand Down
2 changes: 1 addition & 1 deletion python/tests/integration/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class TestDebug(TestCase):

def setUp(self):
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions()) as driver:
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions(is_tls_enabled=False)) as driver:
if TYPEDB not in [db.name for db in driver.databases.all()]:
driver.databases.create(TYPEDB)

Expand Down
4 changes: 2 additions & 2 deletions python/tests/integration/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestExample(TestCase):
# EXAMPLE END MARKER

def setUp(self):
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions()) as driver:
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions(is_tls_enabled=False)) as driver:
if driver.databases.contains("typedb"):
driver.databases.get("typedb").delete()

Expand All @@ -36,7 +36,7 @@ def setUp(self):
def test_example(self):
# Open a driver connection. Specify your parameters if needed
# The connection will be automatically closed on the "with" block exit
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions()) as driver:
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions(is_tls_enabled=False)) as driver:
# Create a database
driver.databases.create("typedb")
database = driver.databases.get("typedb")
Expand Down
8 changes: 4 additions & 4 deletions python/tests/integration/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
class TestValues(TestCase):

def setUp(self):
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions()) as driver:
with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions(is_tls_enabled=False)) as driver:
if driver.databases.contains(TYPEDB):
driver.databases.get(TYPEDB).delete()
driver.databases.create(TYPEDB)
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_values(self):
"expiration": "P1Y10M7DT15H44M5.00394892S"
}

with (TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions()) as driver):
with (TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions(is_tls_enabled=False)) as driver):
database = driver.databases.get(TYPEDB)

with driver.transaction(database.name, SCHEMA) as tx:
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_datetime(self):
Datetime.fromstring("2024-09-21", tz_name="Asia/Calcutta", datetime_fmt="%Y-%m-%d")
Datetime.fromstring("21/09/24 18:34", tz_name="Africa/Cairo", datetime_fmt="%d/%m/%y %H:%M")

with (TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions()) as driver):
with (TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions(is_tls_enabled=False)) as driver):
database = driver.databases.get(TYPEDB)

with driver.transaction(database.name, SCHEMA) as tx:
Expand Down Expand Up @@ -374,7 +374,7 @@ def test_duration(self):
Duration.fromstring("P1Y10M7DT15H44M5.00394892S")
Duration.fromstring("P55W")

with (TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions()) as driver):
with (TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"), DriverOptions(is_tls_enabled=False)) as driver):
database = driver.databases.get(TYPEDB)

with driver.transaction(database.name, SCHEMA) as tx:
Expand Down
16 changes: 13 additions & 3 deletions rust/src/common/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,25 @@ use crate::{
error::ConnectionError,
};

#[derive(Clone, Hash, PartialEq, Eq)]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've actually just stolen a part of my code from the cluster branch.

#[derive(Clone, Hash, PartialEq, Eq, Default)]
pub struct Address {
uri: Uri,
}

impl Address {
const DEFAULT_SCHEME: &'static str = "http";

pub(crate) fn into_uri(self) -> Uri {
self.uri
}

pub(crate) fn uri_scheme(&self) -> Option<&http::uri::Scheme> {
self.uri.scheme()
}

pub(crate) fn is_https(&self) -> bool {
self.uri_scheme().map_or(false, |scheme| scheme == &http::uri::Scheme::HTTPS)
}
}

impl FromStr for Address {
Expand All @@ -44,7 +54,7 @@ impl FromStr for Address {
let uri = if address.contains("://") {
address.parse::<Uri>()?
} else {
format!("http://{address}").parse::<Uri>()?
format!("{}://{}", Self::DEFAULT_SCHEME, address).parse::<Uri>()?
};
if uri.port().is_none() {
return Err(Error::Connection(ConnectionError::MissingPort { address: address.to_owned() }));
Expand All @@ -61,6 +71,6 @@ impl fmt::Display for Address {

impl fmt::Debug for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
write!(f, "{:?}", self.uri)
}
}
6 changes: 6 additions & 0 deletions rust/src/common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ error_messages! { ConnectionError
32: "The database export channel is closed and no further operation is allowed.",
DatabaseExportStreamNoResponse =
33: "Didn't receive any server responses for the database export command.",
AbsentTlsConfigForTlsConnection =
34: "Could not establish a TLS connection without a TLS config specified. Please verify your driver options.",
TlsConnectionWithoutHttps =
35: "TLS connections can only be enabled when connecting to HTTPS endpoints, for example using 'https://<ip>:port'. Please modify the address, or disable TLS (WARNING: this will send passwords over plaintext).",
NonTlsConnectionWithHttps =
36: "Connecting to an https endpoint requires enabling TLS in driver options.",
}

error_messages! { ConceptError
Expand Down
21 changes: 21 additions & 0 deletions rust/src/connection/server_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ impl ServerConnection {
driver_lang: &str,
driver_version: &str,
) -> crate::Result<(Self, Vec<DatabaseInfo>)> {
Self::validate_tls(&address, &driver_options)?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We validate each connection (if there can be multiple) separately, which is correct.


let username = credentials.username().to_string();
let request_transmitter =
Arc::new(RPCTransmitter::start(address, credentials.clone(), driver_options, &background_runtime)?);
Expand Down Expand Up @@ -324,6 +326,25 @@ impl ServerConnection {
other => Err(InternalError::UnexpectedResponseType { response_type: format!("{other:?}") }.into()),
}
}

fn validate_tls(address: &Address, driver_options: &DriverOptions) -> crate::Result {
match driver_options.is_tls_enabled() {
true => {
if driver_options.tls_config().is_none() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not happen, but the model allows it, so why not.

return Err(ConnectionError::AbsentTlsConfigForTlsConnection {}.into());
}
if !address.is_https() {
return Err(ConnectionError::TlsConnectionWithoutHttps {}.into());
}
}
false => {
if address.is_https() {
return Err(ConnectionError::NonTlsConnectionWithHttps {}.into());
}
}
}
Ok(())
}
}

impl fmt::Debug for ServerConnection {
Expand Down