2023-08-23 19:26:31 +00:00
|
|
|
// RUST_LOG=warn cargo make ci-cli-integration
|
2023-07-19 14:35:56 +00:00
|
|
|
mod common;
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
mod cli_integration {
|
2024-03-12 09:27:28 +00:00
|
|
|
use crate::remove_debug_info;
|
2023-08-23 19:26:31 +00:00
|
|
|
use assert_fs::prelude::{FileTouch, FileWriteStr, PathChild};
|
2024-07-15 15:57:28 +00:00
|
|
|
use chrono::Duration as ChronoDuration;
|
|
|
|
use chrono::Utc;
|
2024-01-09 15:27:03 +00:00
|
|
|
use common::Format;
|
|
|
|
use common::Socket;
|
2024-07-15 15:57:28 +00:00
|
|
|
use jsonwebtoken::{encode, Algorithm, EncodingKey, Header};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2024-02-14 11:12:11 +00:00
|
|
|
use serde_json::json;
|
2023-08-23 19:26:31 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::time;
|
2024-03-18 12:30:31 +00:00
|
|
|
use std::time::Duration;
|
2024-02-20 11:11:49 +00:00
|
|
|
use surrealdb::fflags::FFLAGS;
|
2023-08-23 19:26:31 +00:00
|
|
|
use test_log::test;
|
|
|
|
use tokio::time::sleep;
|
|
|
|
use tracing::info;
|
2023-11-21 09:08:13 +00:00
|
|
|
use ulid::Ulid;
|
2023-08-11 09:42:08 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
use super::common::{self, StartServerArguments, PASS, USER};
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test]
|
2024-01-20 21:53:59 +00:00
|
|
|
fn version_command() {
|
2023-08-23 19:26:31 +00:00
|
|
|
assert!(common::run("version").output().is_ok());
|
2023-07-19 14:35:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test]
|
2024-01-20 21:53:59 +00:00
|
|
|
fn version_flag_short() {
|
|
|
|
assert!(common::run("-V").output().is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn version_flag_long() {
|
|
|
|
assert!(common::run("--version").output().is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_command() {
|
2023-08-23 19:26:31 +00:00
|
|
|
assert!(common::run("help").output().is_ok());
|
2023-07-19 14:35:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test]
|
2024-01-20 21:53:59 +00:00
|
|
|
fn help_flag_short() {
|
|
|
|
assert!(common::run("-h").output().is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_flag_long() {
|
|
|
|
assert!(common::run("--help").output().is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-08-23 19:26:31 +00:00
|
|
|
fn nonexistent_subcommand() {
|
|
|
|
assert!(common::run("nonexistent").output().is_err());
|
2023-07-19 14:35:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test]
|
|
|
|
fn nonexistent_option() {
|
|
|
|
assert!(common::run("version --turbo").output().is_err());
|
2023-07-19 14:35:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn all_commands() {
|
|
|
|
// Commands without credentials when auth is disabled, should succeed
|
2023-08-30 18:01:30 +00:00
|
|
|
let (addr, _server) = common::start_server(StartServerArguments {
|
|
|
|
auth: false,
|
|
|
|
args: "--allow-all".to_string(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-08-23 19:26:31 +00:00
|
|
|
let creds = ""; // Anonymous user
|
2023-11-21 09:08:13 +00:00
|
|
|
let ns = Ulid::new();
|
|
|
|
let db = Ulid::new();
|
2023-08-23 19:26:31 +00:00
|
|
|
|
2024-03-12 09:27:28 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2024-07-17 22:44:05 +00:00
|
|
|
{
|
2024-07-30 05:51:28 +00:00
|
|
|
info!("* Development builds contain debug message");
|
2024-07-17 22:44:05 +00:00
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} {creds} --ns {ns} --db {db} --multi --hide-welcome"
|
|
|
|
);
|
|
|
|
let output = common::run(&args).input("CREATE any:any;\n").output().unwrap();
|
2024-07-30 05:51:28 +00:00
|
|
|
assert!(output.contains("Development builds are not intended for production use"));
|
2024-07-17 22:44:05 +00:00
|
|
|
}
|
2024-03-12 09:27:28 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Create a record");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} {creds} --ns {ns} --db {db} --multi --hide-welcome"
|
|
|
|
);
|
2024-03-12 09:27:28 +00:00
|
|
|
let output = common::run(&args).input("CREATE thing:one;\n").output().unwrap();
|
|
|
|
assert!(output.contains("[[{ id: thing:one }]]\n\n"), "failed to send sql: {args}");
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Export to stdout");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!("export --conn http://{addr} {creds} --ns {ns} --db {db} -");
|
2023-08-23 19:26:31 +00:00
|
|
|
let output = common::run(&args).output().expect("failed to run stdout export: {args}");
|
2024-03-19 11:20:58 +00:00
|
|
|
assert!(output.contains("DEFINE TABLE thing TYPE ANY SCHEMALESS PERMISSIONS NONE;"));
|
2024-06-04 15:04:07 +00:00
|
|
|
assert!(output.contains("INSERT [ { id: thing:one } ];"));
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Export to file");
|
|
|
|
let exported = {
|
|
|
|
let exported = common::tmp_file("exported.surql");
|
2023-11-21 09:08:13 +00:00
|
|
|
let args =
|
|
|
|
format!("export --conn http://{addr} {creds} --ns {ns} --db {db} {exported}");
|
2023-08-23 19:26:31 +00:00
|
|
|
common::run(&args).output().expect("failed to run file export: {args}");
|
|
|
|
exported
|
|
|
|
};
|
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let db2 = Ulid::new();
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Import the exported file");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args =
|
|
|
|
format!("import --conn http://{addr} {creds} --ns {ns} --db {db2} {exported}");
|
2023-08-23 19:26:31 +00:00
|
|
|
common::run(&args).output().expect("failed to run import: {args}");
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Query from the import (pretty-printed this time)");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} {creds} --ns {ns} --db {db2} --pretty --hide-welcome"
|
|
|
|
);
|
2024-01-06 20:05:58 +00:00
|
|
|
let output = common::run(&args).input("SELECT * FROM thing;\n").output().unwrap();
|
2024-03-12 09:27:28 +00:00
|
|
|
let output = remove_debug_info(output);
|
2024-01-06 20:05:58 +00:00
|
|
|
let (line1, rest) = output.split_once('\n').expect("response to have multiple lines");
|
2024-03-12 09:27:28 +00:00
|
|
|
assert!(line1.starts_with("-- Query 1"), "Expected on {line1}, and rest was {rest}");
|
2024-01-06 20:05:58 +00:00
|
|
|
assert!(line1.contains("execution time"));
|
|
|
|
assert_eq!(rest, "[\n\t{\n\t\tid: thing:one\n\t}\n]\n\n", "failed to send sql: {args}");
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2024-01-19 17:35:50 +00:00
|
|
|
info!("* Advanced uncomputed variable to be computed before saving");
|
|
|
|
{
|
|
|
|
let args = format!(
|
|
|
|
"sql --conn ws://{addr} {creds} --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(
|
|
|
|
"DEFINE PARAM $something VALUE <set>[1, 2, 3]; \
|
|
|
|
$something;
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(output.contains("[1, 2, 3]"), "missing success in {output}");
|
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Multi-statement (and multi-line) query including error(s) over WS");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"sql --conn ws://{addr} {creds} --ns {throwaway} --db {throwaway} --multi --pretty",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
let output = common::run(&args)
|
|
|
|
.input(
|
2023-08-24 19:02:44 +00:00
|
|
|
"CREATE thing:success; \
|
2023-08-23 19:26:31 +00:00
|
|
|
CREATE thing:fail SET bad=rand('evil'); \
|
|
|
|
SELECT * FROM sleep(10ms) TIMEOUT 1ms; \
|
|
|
|
CREATE thing:also_success;
|
2023-08-24 19:02:44 +00:00
|
|
|
",
|
2023-08-23 19:26:31 +00:00
|
|
|
)
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert!(output.contains("thing:success"), "missing success in {output}");
|
|
|
|
assert!(output.contains("rgument"), "missing argument error in {output}");
|
|
|
|
assert!(
|
|
|
|
output.contains("time") && output.contains("out"),
|
|
|
|
"missing timeout error in {output}"
|
|
|
|
);
|
|
|
|
assert!(output.contains("thing:also_success"), "missing also_success in {output}")
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Multi-statement (and multi-line) transaction including error(s) over WS");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"sql --conn ws://{addr} {creds} --ns {throwaway} --db {throwaway} --multi --pretty",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
let output = common::run(&args)
|
|
|
|
.input(
|
2023-08-24 19:02:44 +00:00
|
|
|
"BEGIN; \
|
2023-08-23 19:26:31 +00:00
|
|
|
CREATE thing:success; \
|
|
|
|
CREATE thing:fail SET bad=rand('evil'); \
|
|
|
|
SELECT * FROM sleep(10ms) TIMEOUT 1ms; \
|
|
|
|
CREATE thing:also_success; \
|
|
|
|
COMMIT;
|
2023-08-24 19:02:44 +00:00
|
|
|
",
|
2023-08-23 19:26:31 +00:00
|
|
|
)
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
output.lines().filter(|s| s.contains("transaction")).count(),
|
|
|
|
3,
|
|
|
|
"missing failed txn errors in {output:?}"
|
|
|
|
);
|
|
|
|
assert!(output.contains("rgument"), "missing argument error in {output}");
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Pass neither ns nor db");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} {creds}");
|
|
|
|
let output = common::run(&args)
|
2023-11-21 09:08:13 +00:00
|
|
|
.input(&format!(
|
2024-01-10 16:43:56 +00:00
|
|
|
"USE NS `{throwaway}` DB `{throwaway}`; CREATE thing:one;\n",
|
2023-11-21 09:08:13 +00:00
|
|
|
throwaway = Ulid::new()
|
|
|
|
))
|
2023-08-23 19:26:31 +00:00
|
|
|
.output()
|
|
|
|
.expect("neither ns nor db");
|
|
|
|
assert!(output.contains("thing:one"), "missing thing:one in {output}");
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Pass only ns");
|
|
|
|
{
|
2024-01-10 16:43:56 +00:00
|
|
|
let args = format!("sql --conn http://{addr} {creds} --ns {ns}");
|
2023-08-23 19:26:31 +00:00
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(&format!("USE DB `{db}`; SELECT * FROM thing:one;\n"))
|
2023-08-23 19:26:31 +00:00
|
|
|
.output()
|
|
|
|
.expect("only ns");
|
|
|
|
assert!(output.contains("thing:one"), "missing thing:one in {output}");
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Pass only db and expect an error");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} {creds} --db {throwaway}",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
common::run(&args).output().expect_err("only db");
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn start_tls() {
|
|
|
|
let (_, server) = common::start_server(StartServerArguments {
|
|
|
|
tls: true,
|
|
|
|
wait_is_ready: false,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(5000));
|
|
|
|
let output = server.kill().output().err().unwrap();
|
|
|
|
|
|
|
|
// Test the crt/key args but the keys are self signed so don't actually connect.
|
|
|
|
assert!(output.contains("Started web server"), "couldn't start web server: {output}");
|
2023-07-19 14:35:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn with_root_auth() {
|
|
|
|
// Commands with credentials when auth is enabled, should succeed
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2023-08-23 19:26:31 +00:00
|
|
|
let creds = format!("--user {USER} --pass {PASS}");
|
|
|
|
let sql_args = format!("sql --conn http://{addr} --multi --pretty");
|
|
|
|
|
|
|
|
info!("* Query over HTTP");
|
|
|
|
{
|
|
|
|
let args = format!("{sql_args} {creds}");
|
|
|
|
let input = "INFO FOR ROOT;";
|
|
|
|
let output = common::run(&args).input(input).output();
|
|
|
|
assert!(output.is_ok(), "failed to query over HTTP: {}", output.err().unwrap());
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Query over WS");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn ws://{addr} --multi --pretty {creds}");
|
|
|
|
let input = "INFO FOR ROOT;";
|
|
|
|
let output = common::run(&args).input(input).output();
|
|
|
|
assert!(output.is_ok(), "failed to query over WS: {}", output.err().unwrap());
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Root user can do exports");
|
|
|
|
let exported = {
|
|
|
|
let exported = common::tmp_file("exported.surql");
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"export --conn http://{addr} {creds} --ns {throwaway} --db {throwaway} {exported}",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-11-28 14:53:40 +00:00
|
|
|
common::run(&args).output().expect("failed to run export");
|
2023-08-23 19:26:31 +00:00
|
|
|
exported
|
|
|
|
};
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Root user can do imports");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"import --conn http://{addr} {creds} --ns {throwaway} --db {throwaway} {exported}",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
common::run(&args).output().unwrap_or_else(|_| panic!("failed to run import: {args}"));
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-07-29 18:47:25 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 14:53:40 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn with_auth_level() {
|
|
|
|
// Commands with credentials for different auth levels
|
2024-05-27 08:15:15 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2023-11-28 14:53:40 +00:00
|
|
|
let creds = format!("--user {USER} --pass {PASS}");
|
|
|
|
let ns = Ulid::new();
|
|
|
|
let db = Ulid::new();
|
|
|
|
|
|
|
|
info!("* Create users with identical credentials at ROOT, NS and DB levels");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --db {db} --ns {ns} {creds}");
|
|
|
|
let _ = common::run(&args)
|
|
|
|
.input(format!("DEFINE USER {USER} ON ROOT PASSWORD '{PASS}' ROLES OWNER;
|
|
|
|
DEFINE USER {USER} ON NAMESPACE PASSWORD '{PASS}' ROLES OWNER;
|
|
|
|
DEFINE USER {USER} ON DATABASE PASSWORD '{PASS}' ROLES OWNER;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass root auth level and access root info");
|
|
|
|
{
|
|
|
|
let args =
|
|
|
|
format!("sql --conn http://{addr} --db {db} --ns {ns} --auth-level root {creds}");
|
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR ROOT;\n").as_str())
|
2023-11-28 14:53:40 +00:00
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("namespaces: {"),
|
|
|
|
"auth level root should be able to access root info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass root auth level and access namespace info");
|
|
|
|
{
|
|
|
|
let args =
|
|
|
|
format!("sql --conn http://{addr} --db {db} --ns {ns} --auth-level root {creds}");
|
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR NS;\n").as_str())
|
2023-11-28 14:53:40 +00:00
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("databases: {"),
|
|
|
|
"auth level root should be able to access namespace info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass root auth level and access database info");
|
|
|
|
{
|
|
|
|
let args =
|
|
|
|
format!("sql --conn http://{addr} --db {db} --ns {ns} --auth-level root {creds}");
|
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR DB;\n").as_str())
|
2023-11-28 14:53:40 +00:00
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("tables: {"),
|
|
|
|
"auth level root should be able to access database info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass namespace auth level and access root info");
|
|
|
|
{
|
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} --db {db} --ns {ns} --auth-level namespace {creds}"
|
|
|
|
);
|
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR ROOT;\n").as_str())
|
2023-11-28 14:53:40 +00:00
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("IAM error: Not enough permissions to perform this action"),
|
|
|
|
"auth level namespace should not be able to access root info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass namespace auth level and access namespace info");
|
|
|
|
{
|
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} --db {db} --ns {ns} --auth-level namespace {creds}"
|
|
|
|
);
|
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR NS;\n").as_str())
|
2023-11-28 14:53:40 +00:00
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("databases: {"),
|
|
|
|
"auth level namespace should be able to access namespace info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass namespace auth level and access database info");
|
|
|
|
{
|
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} --db {db} --ns {ns} --auth-level namespace {creds}"
|
|
|
|
);
|
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR DB;\n").as_str())
|
2023-11-28 14:53:40 +00:00
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("tables: {"),
|
|
|
|
"auth level namespace should be able to access database info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass database auth level and access root info");
|
|
|
|
{
|
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} --db {db} --ns {ns} --auth-level database {creds}"
|
|
|
|
);
|
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR ROOT;\n").as_str())
|
2023-11-28 14:53:40 +00:00
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("IAM error: Not enough permissions to perform this action"),
|
2023-12-04 08:47:37 +00:00
|
|
|
"auth level database should not be able to access root info: {output}",
|
2023-11-28 14:53:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass database auth level and access namespace info");
|
|
|
|
{
|
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} --db {db} --ns {ns} --auth-level database {creds}"
|
|
|
|
);
|
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR NS;\n").as_str())
|
2023-11-28 14:53:40 +00:00
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("IAM error: Not enough permissions to perform this action"),
|
2023-12-04 08:47:37 +00:00
|
|
|
"auth level database should not be able to access namespace info: {output}",
|
2023-11-28 14:53:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass database auth level and access database info");
|
|
|
|
{
|
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} --db {db} --ns {ns} --auth-level database {creds}"
|
|
|
|
);
|
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR DB;\n").as_str())
|
2023-11-28 14:53:40 +00:00
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("tables: {"),
|
2023-12-04 08:47:37 +00:00
|
|
|
"auth level database should be able to access database info: {output}"
|
2023-11-28 14:53:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass namespace auth level without specifying namespace");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --auth-level database {creds}");
|
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR NS;\n").as_str())
|
2023-11-28 14:53:40 +00:00
|
|
|
.output();
|
|
|
|
assert!(
|
|
|
|
output
|
|
|
|
.clone()
|
|
|
|
.unwrap_err()
|
|
|
|
.contains("Namespace is needed for authentication but it was not provided"),
|
2024-07-18 12:19:28 +00:00
|
|
|
"auth level namespace requires providing a namespace: {output:?}"
|
2023-11-28 14:53:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass database auth level without specifying database");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --ns {ns} --auth-level database {creds}");
|
|
|
|
let output = common::run(&args)
|
2024-01-10 16:43:56 +00:00
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR DB;\n").as_str())
|
2023-11-28 14:53:40 +00:00
|
|
|
.output();
|
|
|
|
assert!(
|
|
|
|
output
|
|
|
|
.clone()
|
|
|
|
.unwrap_err()
|
|
|
|
.contains("Database is needed for authentication but it was not provided"),
|
2024-07-18 12:19:28 +00:00
|
|
|
"auth level database requires providing a namespace and database: {output:?}"
|
2023-11-28 14:53:40 +00:00
|
|
|
);
|
|
|
|
}
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-11-28 14:53:40 +00:00
|
|
|
}
|
|
|
|
|
2024-07-15 15:57:28 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn with_auth_level_token() {
|
|
|
|
// Commands with token for different auth levels
|
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
|
|
|
let ac = "test";
|
|
|
|
let ns = Ulid::new();
|
|
|
|
let db = Ulid::new();
|
|
|
|
let exp = (Utc::now() + ChronoDuration::days(1)).timestamp();
|
|
|
|
let key = "secret";
|
|
|
|
let header = Header::new(Algorithm::HS512);
|
|
|
|
let key_enc = EncodingKey::from_secret(key.as_ref());
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
struct Claims {
|
|
|
|
exp: Option<i64>,
|
|
|
|
iss: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
ns: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
db: Option<String>,
|
|
|
|
ac: Option<String>,
|
|
|
|
rl: Option<Vec<String>>,
|
|
|
|
}
|
|
|
|
let claims_root = serde_json::from_str::<Claims>(
|
|
|
|
format!(
|
|
|
|
r#"
|
|
|
|
{{
|
|
|
|
"iss": "surrealdb-test",
|
|
|
|
"exp": {exp},
|
|
|
|
"ac": "{ac}",
|
|
|
|
"rl": ["Owner"]
|
|
|
|
}}
|
|
|
|
"#
|
|
|
|
)
|
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let token_root = match encode(&header, &claims_root, &key_enc) {
|
|
|
|
Ok(enc) => enc,
|
|
|
|
Err(err) => panic!("Failed to encode token: {:?}", err),
|
|
|
|
};
|
|
|
|
let claims_ns = serde_json::from_str::<Claims>(
|
|
|
|
format!(
|
|
|
|
r#"
|
|
|
|
{{
|
|
|
|
"iss": "surrealdb-test",
|
|
|
|
"exp": {exp},
|
|
|
|
"ac": "{ac}",
|
|
|
|
"ns": "{ns}",
|
|
|
|
"rl": ["Owner"]
|
|
|
|
}}
|
|
|
|
"#
|
|
|
|
)
|
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let token_ns = match encode(&header, &claims_ns, &key_enc) {
|
|
|
|
Ok(enc) => enc,
|
|
|
|
Err(err) => panic!("Failed to encode token: {:?}", err),
|
|
|
|
};
|
|
|
|
let claims_db = serde_json::from_str::<Claims>(
|
|
|
|
format!(
|
|
|
|
r#"
|
|
|
|
{{
|
|
|
|
"iss": "surrealdb-test",
|
|
|
|
"exp": {exp},
|
|
|
|
"ac": "{ac}",
|
|
|
|
"ns": "{ns}",
|
|
|
|
"db": "{db}",
|
|
|
|
"rl": ["Owner"]
|
|
|
|
}}
|
|
|
|
"#
|
|
|
|
)
|
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let token_db = match encode(&header, &claims_db, &key_enc) {
|
|
|
|
Ok(enc) => enc,
|
|
|
|
Err(err) => panic!("Failed to encode token: {:?}", err),
|
|
|
|
};
|
|
|
|
let record_user = "user:1";
|
|
|
|
let claims_record = serde_json::from_str::<Claims>(
|
|
|
|
format!(
|
|
|
|
r#"
|
|
|
|
{{
|
|
|
|
"iss": "surrealdb-test",
|
|
|
|
"exp": {exp},
|
|
|
|
"ac": "{ac}",
|
|
|
|
"ns": "{ns}",
|
|
|
|
"db": "{db}",
|
|
|
|
"id": "{record_user}"
|
|
|
|
}}
|
|
|
|
"#
|
|
|
|
)
|
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let token_record = match encode(&header, &claims_record, &key_enc) {
|
|
|
|
Ok(enc) => enc,
|
|
|
|
Err(err) => panic!("Failed to encode token: {:?}", err),
|
|
|
|
};
|
|
|
|
|
|
|
|
info!("* Create access methods with identical names at ROOT, NS and DB levels");
|
|
|
|
{
|
|
|
|
let args =
|
|
|
|
format!("sql --conn http://{addr} --db {db} --ns {ns} --user {USER} --pass {PASS}");
|
|
|
|
let _ = common::run(&args)
|
|
|
|
.input(format!("DEFINE ACCESS {ac} ON ROOT TYPE JWT ALGORITHM HS512 KEY '{key}';
|
|
|
|
DEFINE ACCESS {ac} ON NAMESPACE TYPE JWT ALGORITHM HS512 KEY '{key}';
|
|
|
|
DEFINE ACCESS {ac} ON DATABASE TYPE JWT ALGORITHM HS512 KEY '{key}';\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Create record that will be used as record user for authentication");
|
|
|
|
{
|
|
|
|
let args =
|
|
|
|
format!("sql --conn http://{addr} --db {db} --ns {ns} --user {USER} --pass {PASS}");
|
|
|
|
let _ = common::run(&args)
|
|
|
|
.input(format!("CREATE {record_user};").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass root token and access root info");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --db {db} --ns {ns} --token {token_root}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR ROOT;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("namespaces: {"),
|
|
|
|
"root token should be able to access root info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass root token and access namespace info");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --db {db} --ns {ns} --token {token_root}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR NS;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("databases: {"),
|
|
|
|
"root token should be able to access namespace info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass root auth level and access database info");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --db {db} --ns {ns} --token {token_root}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR DB;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("tables: {"),
|
|
|
|
"root token should be able to access database info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass namespace token and access root info");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --db {db} --ns {ns} --token {token_ns}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR ROOT;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("IAM error: Not enough permissions to perform this action"),
|
|
|
|
"namespace token should not be able to access root info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass namespace token and access namespace info");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --db {db} --ns {ns} --token {token_ns}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR NS;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("databases: {"),
|
|
|
|
"namespace token should be able to access namespace info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass namespace token and access database info");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --db {db} --ns {ns} --token {token_ns}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR DB;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("tables: {"),
|
|
|
|
"namespace token should be able to access database info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass database token and access root info");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --db {db} --ns {ns} --token {token_db}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR ROOT;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("IAM error: Not enough permissions to perform this action"),
|
|
|
|
"database token should not be able to access root info: {output}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass database token and access namespace info");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --db {db} --ns {ns} --token {token_db}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR NS;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("IAM error: Not enough permissions to perform this action"),
|
|
|
|
"database token should not be able to access namespace info: {output}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass database token and access database info");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --db {db} --ns {ns} --token {token_db}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR DB;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("tables: {"),
|
|
|
|
"database token should be able to access database info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass database token without specifying namespace or database");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --token {token_db}");
|
|
|
|
let output = common::run(&args).input("INFO FOR DB;\n").output().expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("tables: {"),
|
|
|
|
"namespace and database to use should be selected from token: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass record user token and access database info");
|
|
|
|
{
|
|
|
|
let args =
|
|
|
|
format!("sql --conn http://{addr} --db {db} --ns {ns} --token {token_record}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR DB;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("tables: {"),
|
|
|
|
"database token should be able to access database info: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass namespace token without specifying namespace");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --token {token_ns}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR NS;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("databases: {"),
|
|
|
|
"namespace should be selected from token: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass database token without specifying database");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn http://{addr} --ns {ns} --token {token_db}");
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR DB;\n").as_str())
|
|
|
|
.output()
|
|
|
|
.expect("success");
|
|
|
|
assert!(
|
|
|
|
output.contains("tables: {"),
|
|
|
|
"database should be selected from token: {output}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass token at the same time as credentials");
|
|
|
|
{
|
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} --ns {ns} --token {token_db} -u {USER} -p {PASS}"
|
|
|
|
);
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR DB;\n").as_str())
|
|
|
|
.output();
|
|
|
|
assert!(
|
|
|
|
output
|
|
|
|
.clone()
|
|
|
|
.unwrap_err()
|
|
|
|
.contains("error: the argument '--token <TOKEN>' cannot be used with"),
|
|
|
|
"the token argument should not be compatible with credentials: {:?}",
|
|
|
|
output
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* Pass token at the same time as different auth level");
|
|
|
|
{
|
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} --ns {ns} --token {token_db} --auth-level namespace"
|
|
|
|
);
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input(format!("USE NS `{ns}` DB `{db}`; INFO FOR DB;\n").as_str())
|
|
|
|
.output();
|
|
|
|
assert!(
|
|
|
|
output
|
|
|
|
.clone()
|
|
|
|
.unwrap_err()
|
|
|
|
.contains("error: the argument '--token <TOKEN>' cannot be used with"),
|
|
|
|
"the token argument should not be compatible with authentication level: {:?}",
|
|
|
|
output
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn with_anon_auth() {
|
|
|
|
// Commands without credentials when auth is enabled, should fail
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server_with_defaults().await.unwrap();
|
2023-08-23 19:26:31 +00:00
|
|
|
let creds = ""; // Anonymous user
|
|
|
|
let sql_args = format!("sql --conn http://{addr} --multi --pretty");
|
|
|
|
|
|
|
|
info!("* Query over HTTP");
|
|
|
|
{
|
|
|
|
let args = format!("{sql_args} {creds}");
|
|
|
|
let input = "";
|
|
|
|
let output = common::run(&args).input(input).output();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(output.is_ok(), "anonymous user should be able to query: {output:?}");
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
2023-07-29 18:47:25 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Query over WS");
|
|
|
|
{
|
|
|
|
let args = format!("sql --conn ws://{addr} --multi --pretty {creds}");
|
|
|
|
let input = "";
|
|
|
|
let output = common::run(&args).input(input).output();
|
2024-07-18 12:19:28 +00:00
|
|
|
assert!(output.is_ok(), "anonymous user should be able to query: {output:?}");
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
2023-07-29 18:47:25 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Can't do exports");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"export --conn http://{addr} {creds} --ns {throwaway} --db {throwaway} -",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
let output = common::run(&args).output();
|
|
|
|
assert!(
|
|
|
|
output.clone().unwrap_err().contains("Forbidden"),
|
2024-07-18 12:19:28 +00:00
|
|
|
"anonymous user shouldn't be able to export: {output:?}"
|
2023-08-23 19:26:31 +00:00
|
|
|
);
|
|
|
|
}
|
2023-07-29 18:47:25 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Can't do imports");
|
|
|
|
{
|
|
|
|
let tmp_file = common::tmp_file("exported.surql");
|
|
|
|
File::create(&tmp_file).expect("failed to create tmp file");
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"import --conn http://{addr} {creds} --ns {throwaway} --db {throwaway} {tmp_file}",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
let output = common::run(&args).output();
|
|
|
|
assert!(
|
|
|
|
output.clone().unwrap_err().contains("Forbidden"),
|
2024-07-18 12:19:28 +00:00
|
|
|
"anonymous user shouldn't be able to import: {output:?}"
|
2023-08-23 19:26:31 +00:00
|
|
|
);
|
|
|
|
}
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-11 09:42:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn node() {
|
|
|
|
// Commands without credentials when auth is disabled, should succeed
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
2023-08-23 19:26:31 +00:00
|
|
|
auth: false,
|
|
|
|
tls: false,
|
|
|
|
wait_is_ready: true,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let creds = ""; // Anonymous user
|
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let ns = Ulid::new();
|
|
|
|
let db = Ulid::new();
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Define a table");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} {creds} --ns {ns} --db {db} --multi --hide-welcome"
|
|
|
|
);
|
2024-03-19 11:20:58 +00:00
|
|
|
let output = common::run(&args)
|
|
|
|
.input("DEFINE TABLE thing TYPE ANY CHANGEFEED 1s;\n")
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
2024-03-12 09:27:28 +00:00
|
|
|
let output = remove_debug_info(output);
|
|
|
|
assert_eq!(output, "[NONE]\n\n".to_owned(), "failed to send sql: {args}");
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
2023-08-11 09:42:08 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Create a record");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} {creds} --ns {ns} --db {db} --multi --hide-welcome"
|
|
|
|
);
|
2024-03-12 09:27:28 +00:00
|
|
|
let output = common::run(&args)
|
|
|
|
.input("BEGIN TRANSACTION; CREATE thing:one; COMMIT;\n")
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
let output = remove_debug_info(output);
|
2023-08-23 19:26:31 +00:00
|
|
|
assert_eq!(
|
2024-03-12 09:27:28 +00:00
|
|
|
output,
|
|
|
|
"[[{ id: thing:one }]]\n\n".to_owned(),
|
2023-08-23 19:26:31 +00:00
|
|
|
"failed to send sql: {args}"
|
|
|
|
);
|
|
|
|
}
|
2023-08-11 09:42:08 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
info!("* Show changes");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} {creds} --ns {ns} --db {db} --multi --hide-welcome"
|
|
|
|
);
|
2024-02-20 11:11:49 +00:00
|
|
|
if FFLAGS.change_feed_live_queries.enabled() {
|
2024-03-12 09:27:28 +00:00
|
|
|
let output = common::run(&args)
|
|
|
|
.input("SHOW CHANGES FOR TABLE thing SINCE 0 LIMIT 10;\n")
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
2024-03-13 12:12:35 +00:00
|
|
|
let output = remove_debug_info(output).replace('\n', "");
|
2024-03-18 12:30:31 +00:00
|
|
|
// TODO: when enabling the feature flag, turn these to `create` not `update`
|
2024-03-13 12:12:35 +00:00
|
|
|
let allowed = [
|
2024-03-26 13:23:46 +00:00
|
|
|
// Delete these
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 1 }, { changes: [{ update: { id: thing:one } }], versionstamp: 2 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 1 }, { changes: [{ update: { id: thing:one } }], versionstamp: 3 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 2 }, { changes: [{ update: { id: thing:one } }], versionstamp: 3 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 2 }, { changes: [{ update: { id: thing:one } }], versionstamp: 4 }]]",
|
|
|
|
// Keep these
|
2024-03-18 12:30:31 +00:00
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 65536 }, { changes: [{ update: { id: thing:one } }], versionstamp: 131072 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 65536 }, { changes: [{ update: { id: thing:one } }], versionstamp: 196608 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 131072 }, { changes: [{ update: { id: thing:one } }], versionstamp: 196608 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 131072 }, { changes: [{ update: { id: thing:one } }], versionstamp: 262144 }]]",
|
2024-03-13 12:12:35 +00:00
|
|
|
];
|
|
|
|
allowed
|
|
|
|
.into_iter()
|
2024-03-18 12:30:31 +00:00
|
|
|
.find(|case| {
|
|
|
|
println!("Comparing 2:\n{case}\n{output}");
|
|
|
|
*case == output
|
|
|
|
})
|
2024-03-13 12:12:35 +00:00
|
|
|
.ok_or(format!("Output didnt match an example output: {output}"))
|
|
|
|
.unwrap();
|
2024-02-20 11:11:49 +00:00
|
|
|
} else {
|
2024-03-12 09:27:28 +00:00
|
|
|
let output = common::run(&args)
|
|
|
|
.input("SHOW CHANGES FOR TABLE thing SINCE 0 LIMIT 10;\n")
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
2024-03-13 12:12:35 +00:00
|
|
|
let output = remove_debug_info(output).replace('\n', "");
|
|
|
|
let allowed = [
|
2024-03-18 12:30:31 +00:00
|
|
|
// Delete these
|
2024-03-13 12:12:35 +00:00
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 1 }, { changes: [{ update: { id: thing:one } }], versionstamp: 2 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 1 }, { changes: [{ update: { id: thing:one } }], versionstamp: 3 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 2 }, { changes: [{ update: { id: thing:one } }], versionstamp: 3 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 2 }, { changes: [{ update: { id: thing:one } }], versionstamp: 4 }]]",
|
2024-03-18 12:30:31 +00:00
|
|
|
// Keep these
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 65536 }, { changes: [{ update: { id: thing:one } }], versionstamp: 131072 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 65536 }, { changes: [{ update: { id: thing:one } }], versionstamp: 196608 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 131072 }, { changes: [{ update: { id: thing:one } }], versionstamp: 196608 }]]",
|
|
|
|
"[[{ changes: [{ define_table: { name: 'thing' } }], versionstamp: 131072 }, { changes: [{ update: { id: thing:one } }], versionstamp: 262144 }]]",
|
2024-03-13 12:12:35 +00:00
|
|
|
];
|
|
|
|
allowed
|
|
|
|
.into_iter()
|
|
|
|
.find(|case| {
|
|
|
|
let a = *case == output;
|
|
|
|
println!("Comparing\n{case}\n{output}\n{a}");
|
|
|
|
a
|
|
|
|
})
|
|
|
|
.ok_or(format!("Output didnt match an example output: {output}"))
|
|
|
|
.unwrap();
|
2024-02-20 11:11:49 +00:00
|
|
|
}
|
|
|
|
};
|
2023-08-23 19:26:31 +00:00
|
|
|
|
2024-09-16 20:51:29 +00:00
|
|
|
sleep(Duration::from_secs(20)).await;
|
2023-08-23 19:26:31 +00:00
|
|
|
|
|
|
|
info!("* Show changes after GC");
|
|
|
|
{
|
2023-11-21 09:08:13 +00:00
|
|
|
let args = format!(
|
|
|
|
"sql --conn http://{addr} {creds} --ns {ns} --db {db} --multi --hide-welcome"
|
|
|
|
);
|
2024-03-12 09:27:28 +00:00
|
|
|
let output = common::run(&args)
|
|
|
|
.input("SHOW CHANGES FOR TABLE thing SINCE 0 LIMIT 10;\n")
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
let output = remove_debug_info(output);
|
|
|
|
assert_eq!(output, "[[]]\n\n".to_owned(), "failed to send sql: {args}");
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-11 09:42:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test]
|
|
|
|
fn validate_found_no_files() {
|
|
|
|
let temp_dir = assert_fs::TempDir::new().unwrap();
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
temp_dir.child("file.txt").touch().unwrap();
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
assert!(common::run_in_dir("validate", &temp_dir).output().is_err());
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test]
|
|
|
|
fn validate_succeed_for_valid_surql_files() {
|
|
|
|
let temp_dir = assert_fs::TempDir::new().unwrap();
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
let statement_file = temp_dir.child("statement.surql");
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
statement_file.touch().unwrap();
|
|
|
|
statement_file.write_str("CREATE thing:success;").unwrap();
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
assert!(common::run_in_dir("validate", &temp_dir).output().is_ok());
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test]
|
|
|
|
fn validate_failed_due_to_invalid_glob_pattern() {
|
|
|
|
let temp_dir = assert_fs::TempDir::new().unwrap();
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
const WRONG_GLOB_PATTERN: &str = "**/*{.txt";
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2024-07-18 12:19:28 +00:00
|
|
|
let args = format!("validate \"{WRONG_GLOB_PATTERN}\"");
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
assert!(common::run_in_dir(&args, &temp_dir).output().is_err());
|
|
|
|
}
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test]
|
|
|
|
fn validate_failed_due_to_invalid_surql_files_syntax() {
|
|
|
|
let temp_dir = assert_fs::TempDir::new().unwrap();
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
let statement_file = temp_dir.child("statement.surql");
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
statement_file.touch().unwrap();
|
|
|
|
statement_file.write_str("CREATE $thing WHERE value = '';").unwrap();
|
2023-07-19 14:35:56 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
assert!(common::run_in_dir("validate", &temp_dir).output().is_err());
|
|
|
|
}
|
2023-08-21 12:58:53 +00:00
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn test_server_graceful_shutdown() {
|
|
|
|
let (_, mut server) = common::start_server_with_defaults().await.unwrap();
|
|
|
|
|
|
|
|
info!("* Send SIGINT signal");
|
|
|
|
server
|
|
|
|
.send_signal(nix::sys::signal::Signal::SIGINT)
|
|
|
|
.expect("Failed to send SIGINT to server");
|
|
|
|
|
|
|
|
info!("* Waiting for server to exit gracefully ...");
|
|
|
|
tokio::select! {
|
|
|
|
_ = async {
|
|
|
|
loop {
|
|
|
|
if let Ok(Some(exit)) = server.status() {
|
|
|
|
assert!(exit.success(), "Server didn't shutdown successfully:\n{}", server.output().unwrap_err());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tokio::time::sleep(time::Duration::from_secs(1)).await;
|
2023-08-21 12:58:53 +00:00
|
|
|
}
|
2023-08-23 19:26:31 +00:00
|
|
|
} => {},
|
|
|
|
// Timeout after 5 seconds
|
|
|
|
_ = tokio::time::sleep(time::Duration::from_secs(5)) => {
|
|
|
|
panic!("Server didn't exit after receiving SIGINT");
|
2023-08-21 12:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn test_server_second_signal_handling() {
|
|
|
|
let (addr, mut server) = common::start_server_without_auth().await.unwrap();
|
|
|
|
|
|
|
|
// Create a long-lived WS connection so the server don't shutdown gracefully
|
2024-02-14 11:12:11 +00:00
|
|
|
let socket =
|
|
|
|
Socket::connect(&addr, None, Format::Json).await.expect("Failed to connect to server");
|
|
|
|
|
|
|
|
let send_future = socket.send_request("query", json!(["SLEEP 30s;"]));
|
|
|
|
|
|
|
|
let signal_send_fut = async {
|
|
|
|
// Make sure the SLEEP query is being executed
|
|
|
|
tokio::time::timeout(time::Duration::from_secs(10), async {
|
2023-08-23 19:26:31 +00:00
|
|
|
loop {
|
2024-02-14 11:12:11 +00:00
|
|
|
let err = server.stderr();
|
|
|
|
if err.contains("SLEEP 30s") {
|
2023-08-23 19:26:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
tokio::time::sleep(time::Duration::from_secs(1)).await;
|
|
|
|
}
|
2024-02-14 11:12:11 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.expect("Server didn't start executing the SLEEP query");
|
2023-08-21 12:58:53 +00:00
|
|
|
|
2024-02-14 11:12:11 +00:00
|
|
|
info!("* Send first SIGINT signal");
|
|
|
|
server
|
|
|
|
.send_signal(nix::sys::signal::Signal::SIGINT)
|
|
|
|
.expect("Failed to send SIGINT to server");
|
2023-08-23 19:26:31 +00:00
|
|
|
|
2024-02-14 11:12:11 +00:00
|
|
|
tokio::time::timeout(time::Duration::from_secs(10), async {
|
2023-08-23 19:26:31 +00:00
|
|
|
loop {
|
|
|
|
if let Ok(Some(exit)) = server.status() {
|
2024-07-18 12:19:28 +00:00
|
|
|
panic!("Server unexpectedly exited after receiving first SIGINT: {exit:?}");
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
2024-02-14 11:12:11 +00:00
|
|
|
tokio::time::sleep(time::Duration::from_millis(100)).await;
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
2024-02-14 11:12:11 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap_err();
|
2023-08-21 12:58:53 +00:00
|
|
|
|
2024-02-14 11:12:11 +00:00
|
|
|
info!("* Send second SIGINT signal");
|
2023-08-23 19:26:31 +00:00
|
|
|
|
2024-02-14 11:12:11 +00:00
|
|
|
server
|
|
|
|
.send_signal(nix::sys::signal::Signal::SIGINT)
|
|
|
|
.expect("Failed to send SIGINT to server");
|
|
|
|
|
|
|
|
tokio::time::timeout(time::Duration::from_secs(5), async {
|
2023-08-23 19:26:31 +00:00
|
|
|
loop {
|
|
|
|
if let Ok(Some(exit)) = server.status() {
|
|
|
|
assert!(exit.success(), "Server shutted down successfully");
|
|
|
|
break;
|
|
|
|
}
|
2024-02-14 11:12:11 +00:00
|
|
|
tokio::time::sleep(time::Duration::from_millis(100)).await;
|
2023-08-21 12:58:53 +00:00
|
|
|
}
|
2024-02-14 11:12:11 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
.expect("Server didn't exit after receiving two SIGINT signals");
|
|
|
|
};
|
|
|
|
|
|
|
|
let _ =
|
|
|
|
futures::future::join(async { send_future.await.unwrap_err() }, signal_send_fut).await;
|
|
|
|
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-21 12:58:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 19:26:31 +00:00
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn test_capabilities() {
|
2023-09-04 15:46:57 +00:00
|
|
|
// Default capabilities only allow functions
|
|
|
|
info!("* When default capabilities");
|
2023-08-23 19:26:31 +00:00
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
2023-08-30 18:01:30 +00:00
|
|
|
args: "".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-08-21 12:58:53 +00:00
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
|
2023-09-04 15:46:57 +00:00
|
|
|
let query = "RETURN http::get('http://127.0.0.1/');\n\n";
|
|
|
|
let output = common::run(&cmd).input(query).output().unwrap();
|
|
|
|
assert!(
|
2024-09-16 15:36:24 +00:00
|
|
|
output.contains("Access to network target '127.0.0.1:80' is not allowed"),
|
2023-09-04 15:46:57 +00:00
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN function() { return '1' };";
|
|
|
|
let output = common::run(&cmd).input(query).output().unwrap();
|
|
|
|
assert!(
|
2024-01-16 11:48:29 +00:00
|
|
|
output.contains("Scripting functions are not allowed")
|
|
|
|
|| output.contains("Embedded functions are not enabled"),
|
2023-09-04 15:46:57 +00:00
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
2024-02-14 11:12:11 +00:00
|
|
|
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-09-04 15:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deny all, denies all users to execute functions and access any network address
|
|
|
|
info!("* When all capabilities are denied");
|
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
2023-09-04 15:46:57 +00:00
|
|
|
args: "--deny-all".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-09-04 15:46:57 +00:00
|
|
|
|
2024-07-18 12:19:28 +00:00
|
|
|
let query = format!("RETURN http::get('http://{addr}/version');\n\n");
|
2023-08-23 19:26:31 +00:00
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
2023-08-30 18:01:30 +00:00
|
|
|
assert!(
|
|
|
|
output.contains("Function 'http::get' is not allowed"),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
|
|
|
|
let query = "RETURN function() { return '1' };";
|
|
|
|
let output = common::run(&cmd).input(query).output().unwrap();
|
2023-08-30 18:01:30 +00:00
|
|
|
assert!(
|
2024-01-16 11:48:29 +00:00
|
|
|
output.contains("Scripting functions are not allowed")
|
|
|
|
|| output.contains("Embedded functions are not enabled"),
|
2023-08-30 18:01:30 +00:00
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 18:01:30 +00:00
|
|
|
// When all capabilities are allowed, anyone (including non-authenticated users) can execute functions and access any network address
|
|
|
|
info!("* When all capabilities are allowed");
|
2023-08-23 19:26:31 +00:00
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
2023-08-30 18:01:30 +00:00
|
|
|
args: "--allow-all".to_owned(),
|
2023-08-23 19:26:31 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
|
2024-07-18 12:19:28 +00:00
|
|
|
let query = format!("RETURN http::get('http://{addr}/version');\n\n");
|
2023-08-23 19:26:31 +00:00
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
2024-06-18 18:49:12 +00:00
|
|
|
assert!(output.contains("['surrealdb-"), "unexpected output: {output:?}");
|
2023-08-23 19:26:31 +00:00
|
|
|
|
|
|
|
let query = "RETURN function() { return '1' };";
|
|
|
|
let output = common::run(&cmd).input(query).output().unwrap();
|
2024-06-18 18:49:12 +00:00
|
|
|
assert!(output.contains("['1']"), "unexpected output: {output:?}");
|
2024-02-14 11:12:11 +00:00
|
|
|
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When scripting is denied");
|
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
2023-08-23 19:26:31 +00:00
|
|
|
args: "--deny-scripting".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
|
|
|
|
let query = "RETURN function() { return '1' };";
|
|
|
|
let output = common::run(&cmd).input(query).output().unwrap();
|
|
|
|
assert!(
|
2024-01-16 11:48:29 +00:00
|
|
|
output.contains("Scripting functions are not allowed")
|
|
|
|
|| output.contains("Embedded functions are not enabled"),
|
2023-08-23 19:26:31 +00:00
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
|
|
|
|
2024-09-16 15:36:24 +00:00
|
|
|
info!("* When capabilities are denied globally, but functions are allowed generally");
|
2023-08-23 19:26:31 +00:00
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
2024-09-16 15:36:24 +00:00
|
|
|
args: "--deny-all --allow-funcs".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::len('123');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(output.contains("[3]"), "unexpected output: {output:?}");
|
|
|
|
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When capabilities are denied globally, but a function family is allowed specifically");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--deny-all --allow-funcs string::len".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::len('123');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(output.contains("[3]"), "unexpected output: {output:?}");
|
|
|
|
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When capabilities are allowed globally, but functions are denied generally");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--allow-all --deny-funcs".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::lowercase('SURREALDB');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(
|
|
|
|
output.contains("Function 'string::lowercase' is not allowed"),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
|
|
|
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When capabilities are allowed globally, but a function family is denied specifically");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--allow-all --deny-funcs string::lowercase".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::lowercase('SURREALDB');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(
|
|
|
|
output.contains("Function 'string::lowercase' is not allowed"),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::len('123');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(output.contains("[3]"), "unexpected output: {output:?}");
|
|
|
|
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When functions are denied generally, but allowed for a specific family");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--deny-funcs --allow-funcs string::len".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::lowercase('SURREALDB');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(
|
|
|
|
output.contains("Function 'string::lowercase' is not allowed"),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::len('123');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(output.contains("[3]"), "unexpected output: {output:?}");
|
|
|
|
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When functions are allowed generally, but denied for a specific family");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--allow-funcs --deny-funcs string::lowercase".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::lowercase('SURREALDB');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(
|
|
|
|
output.contains("Function 'string::lowercase' is not allowed"),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::len('123');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(output.contains("[3]"), "unexpected output: {output:?}");
|
|
|
|
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When functions are both allowed and denied specifically but denies are more specific");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--allow-funcs string --deny-funcs string::lowercase".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::lowercase('SURREALDB');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(
|
|
|
|
output.contains("Function 'string::lowercase' is not allowed"),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::len('123');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(output.contains("[3]"), "unexpected output: {output:?}");
|
|
|
|
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When functions are both allowed and denied specifically but allows are more specific");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--deny-funcs string --allow-funcs string::lowercase".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::lowercase('SURREALDB');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(
|
|
|
|
output.contains("Function 'string::lowercase' is not allowed"),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = "RETURN string::len('123');\n\n".to_string();
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(
|
|
|
|
output.contains("Function 'string::len' is not allowed"),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
|
|
|
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When capabilities are denied globally, but net is allowed generally");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--deny-all --allow-net --allow-funcs http::get".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = format!("RETURN http::get('http://{addr}/version');\n\n");
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(output.contains("['surrealdb-"), "unexpected output: {output:?}");
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When capabilities are denied globally, but net is allowed specifically");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--deny-all --allow-net 127.0.0.1 --allow-funcs http::get".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = format!("RETURN http::get('http://{addr}/version');\n\n");
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(output.contains("['surrealdb-"), "unexpected output: {output:?}");
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When capabilities are allowed globally, but net is denied generally");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--allow-all --deny-net --allow-funcs http::get".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = format!("RETURN http::get('http://{addr}/version');\n\n");
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(
|
|
|
|
output
|
|
|
|
.contains(format!("Access to network target '{addr}' is not allowed").as_str()),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When capabilities are allowed globally, but net is denied specifically");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--allow-all --deny-net 127.0.0.1 --allow-funcs http::get".to_owned(),
|
2023-08-23 19:26:31 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
|
2024-07-18 12:19:28 +00:00
|
|
|
let query = format!("RETURN http::get('http://{addr}/version');\n\n");
|
2023-08-23 19:26:31 +00:00
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(
|
2024-09-16 15:36:24 +00:00
|
|
|
output
|
|
|
|
.contains(format!("Access to network target '{addr}' is not allowed").as_str()),
|
2023-08-23 19:26:31 +00:00
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
|
|
|
|
2024-09-16 15:36:24 +00:00
|
|
|
info!("* When net is denied generally, but allowed for a specific address");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--deny-net --allow-net 127.0.0.1 --allow-funcs http::get".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = format!("RETURN http::get('http://{addr}/version');\n\n");
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(output.contains("['surrealdb-"), "unexpected output: {output:?}");
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When net is allowed generally, but denied for a specific address");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--allow-net --deny-net 127.0.0.1 --allow-funcs http::get".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = format!("RETURN http::get('http://{addr}/version');\n\n");
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(
|
|
|
|
output
|
|
|
|
.contains(format!("Access to network target '{addr}' is not allowed").as_str()),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When net is both allowed and denied specifically but denies are more specific");
|
2023-08-23 19:26:31 +00:00
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
2023-08-30 18:01:30 +00:00
|
|
|
args: "--allow-net 127.0.0.1 --deny-net 127.0.0.1:80 --allow-funcs http::get"
|
|
|
|
.to_owned(),
|
2023-08-23 19:26:31 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
|
2024-07-18 12:19:28 +00:00
|
|
|
let query = format!("RETURN http::get('http://{addr}/version');\n\n");
|
2023-08-23 19:26:31 +00:00
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
2024-06-18 18:49:12 +00:00
|
|
|
assert!(output.contains("['surrealdb-"), "unexpected output: {output:?}");
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-23 19:26:31 +00:00
|
|
|
}
|
|
|
|
|
2024-09-16 15:36:24 +00:00
|
|
|
info!("* When net is both allowed and denied specifically but allows are more specific");
|
2023-08-23 19:26:31 +00:00
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
2024-09-16 15:36:24 +00:00
|
|
|
args: "--deny-net 127.0.0.1 --allow-net 127.0.0.1:80 --allow-funcs http::get"
|
|
|
|
.to_owned(),
|
2023-08-23 19:26:31 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-23 19:26:31 +00:00
|
|
|
|
2024-09-16 15:36:24 +00:00
|
|
|
let query = format!("RETURN http::get('http://{addr}/version');\n\n");
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
2023-08-23 19:26:31 +00:00
|
|
|
assert!(
|
2024-09-16 15:36:24 +00:00
|
|
|
output
|
|
|
|
.contains(format!("Access to network target '{addr}' is not allowed").as_str()),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When net is denied specifically and functions are enabled specifically");
|
|
|
|
{
|
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
|
|
|
args: "--deny-net 127.0.0.1 --allow-funcs http::get".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} -u root -p root --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
|
|
|
|
|
|
|
let query = format!("RETURN http::get('http://{addr}/version');\n\n");
|
|
|
|
let output = common::run(&cmd).input(&query).output().unwrap();
|
|
|
|
assert!(
|
|
|
|
output
|
|
|
|
.contains(format!("Access to network target '{addr}' is not allowed").as_str()),
|
2023-08-23 19:26:31 +00:00
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-21 12:58:53 +00:00
|
|
|
}
|
2023-08-30 18:01:30 +00:00
|
|
|
|
|
|
|
info!("* When auth is enabled and guest access is allowed");
|
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
2023-08-30 18:01:30 +00:00
|
|
|
auth: true,
|
|
|
|
args: "--allow-guests".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-30 18:01:30 +00:00
|
|
|
|
|
|
|
let query = "RETURN 1;\n\n";
|
|
|
|
let output = common::run(&cmd).input(query).output().unwrap();
|
|
|
|
assert!(output.contains("[1]"), "unexpected output: {output:?}");
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-30 18:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When auth is enabled and guest access is denied");
|
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
2023-08-30 18:01:30 +00:00
|
|
|
auth: true,
|
|
|
|
args: "--deny-guests".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-30 18:01:30 +00:00
|
|
|
|
|
|
|
let query = "RETURN 1;\n\n";
|
|
|
|
let output = common::run(&cmd).input(query).output().unwrap();
|
|
|
|
assert!(
|
|
|
|
output.contains("Not enough permissions to perform this action"),
|
|
|
|
"unexpected output: {output:?}"
|
|
|
|
);
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-30 18:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info!("* When auth is disabled, guest access is always allowed");
|
|
|
|
{
|
2024-03-18 12:30:31 +00:00
|
|
|
let (addr, mut server) = common::start_server(StartServerArguments {
|
2023-08-30 18:01:30 +00:00
|
|
|
auth: false,
|
|
|
|
args: "--deny-guests".to_owned(),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2023-11-21 09:08:13 +00:00
|
|
|
let cmd = format!(
|
|
|
|
"sql --conn ws://{addr} --ns {throwaway} --db {throwaway} --multi",
|
|
|
|
throwaway = Ulid::new()
|
|
|
|
);
|
2023-08-30 18:01:30 +00:00
|
|
|
|
|
|
|
let query = "RETURN 1;\n\n";
|
|
|
|
let output = common::run(&cmd).input(query).output().unwrap();
|
|
|
|
assert!(output.contains("[1]"), "unexpected output: {output:?}");
|
2024-03-18 12:30:31 +00:00
|
|
|
server.finish().unwrap();
|
2023-08-30 18:01:30 +00:00
|
|
|
}
|
2023-08-21 12:58:53 +00:00
|
|
|
}
|
2024-03-28 16:29:55 +00:00
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn test_temporary_directory() {
|
|
|
|
info!("* The path is a non-existing directory");
|
|
|
|
{
|
|
|
|
let path = format!("surrealkv:{}", tempfile::tempdir().unwrap().path().display());
|
|
|
|
let res = common::start_server(StartServerArguments {
|
|
|
|
path: Some(path),
|
|
|
|
args: "".to_owned(),
|
|
|
|
temporary_directory: Some("/tmp/TELL-ME-THIS-FILE-DOES-NOT-EXISTS".to_owned()),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
match res {
|
|
|
|
Ok((_, mut server)) => {
|
|
|
|
server.finish().unwrap();
|
|
|
|
panic!("Should not be ok!");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(e.to_string(), "server failed to start", "{e:?}");
|
2024-03-28 16:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* The path is a file");
|
|
|
|
{
|
|
|
|
let path = format!("surrealkv:{}", tempfile::tempdir().unwrap().path().display());
|
|
|
|
let temp_file = tempfile::NamedTempFile::new().unwrap();
|
|
|
|
let res = common::start_server(StartServerArguments {
|
|
|
|
path: Some(path),
|
|
|
|
args: "".to_owned(),
|
|
|
|
temporary_directory: Some(format!("{}", temp_file.path().display())),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
match res {
|
|
|
|
Ok((_, mut server)) => {
|
|
|
|
server.finish().unwrap();
|
|
|
|
panic!("Should not be ok!");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2024-07-18 12:19:28 +00:00
|
|
|
assert_eq!(e.to_string(), "server failed to start", "{e:?}");
|
2024-03-28 16:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
temp_file.close().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("* The path is a valid directory");
|
2024-07-12 08:36:34 +00:00
|
|
|
#[cfg(feature = "storage-surrealkv")]
|
2024-03-28 16:29:55 +00:00
|
|
|
{
|
|
|
|
let path = format!("surrealkv:{}", tempfile::tempdir().unwrap().path().display());
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let (_, mut server) = common::start_server(StartServerArguments {
|
|
|
|
path: Some(path),
|
|
|
|
args: "".to_owned(),
|
|
|
|
temporary_directory: Some(format!("{}", temp_dir.path().display())),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
temp_dir.close().unwrap();
|
|
|
|
server.finish().unwrap();
|
|
|
|
}
|
|
|
|
}
|
2024-08-13 19:47:17 +00:00
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn double_create() {
|
|
|
|
info!("* check only one output created");
|
|
|
|
{
|
|
|
|
let args = "sql --conn memory --ns test --db test --pretty --hide-welcome".to_string();
|
|
|
|
let output = common::run(&args)
|
|
|
|
.input("let $a = create foo;\n")
|
|
|
|
.input("select * from foo;\n")
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
let output = remove_debug_info(output);
|
|
|
|
assert_eq!(output.matches("foo:").count(), 1);
|
|
|
|
}
|
|
|
|
}
|
2023-08-21 12:58:53 +00:00
|
|
|
}
|
2024-03-12 09:27:28 +00:00
|
|
|
|
|
|
|
fn remove_debug_info(output: String) -> String {
|
|
|
|
// Look... sometimes you just gotta copy paste
|
|
|
|
let output_warning = "\
|
|
|
|
┌─────────────────────────────────────────────────────────────────────────────┐
|
2024-07-30 05:51:28 +00:00
|
|
|
│ !!! THIS IS A DEVELOPMENT BUILD !!! │
|
|
|
|
│ Development builds are not intended for production use and include │
|
2024-09-15 22:31:10 +00:00
|
|
|
│ tooling and features that may affect the performance of the database. │
|
2024-03-12 09:27:28 +00:00
|
|
|
└─────────────────────────────────────────────────────────────────────────────┘
|
|
|
|
";
|
|
|
|
// The last line in the above is important
|
|
|
|
output.replace(output_warning, "")
|
|
|
|
}
|