Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[db] Unexpected Error with Repeated QueryBuilder Usage and db_audit #1122 #1124

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
20 changes: 16 additions & 4 deletions agdb_server/src/db_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ use axum::http::StatusCode;
use server_db::ServerDb;
use server_db::ServerDbImpl;
use std::collections::HashMap;
use std::io::Seek;
use std::io::SeekFrom;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
Expand Down Expand Up @@ -498,12 +501,21 @@ impl DbPool {

Ok(results)
});
if r.is_ok() {
let log = std::fs::OpenOptions::new()
if r.is_ok() && !audit.is_empty() {
let mut log = std::fs::OpenOptions::new()
.create(true)
.append(true)
.truncate(false)
.write(true)
.open(db_audit_file(owner, db, config))?;
serde_json::to_writer(log, &audit)?;
let len = log.seek(SeekFrom::End(0))?;
if len == 0 {
serde_json::to_writer(&log, &audit)?;
} else {
let mut data = serde_json::to_vec(&audit)?;
data[0] = b',';
log.seek(SeekFrom::End(-1))?;
log.write_all(&data)?;
}
}
r
}
Expand Down
44 changes: 44 additions & 0 deletions agdb_server/tests/routes/db_audit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,47 @@ async fn audit_no_token() -> anyhow::Result<()> {
assert_eq!(status, 401);
Ok(())
}

#[tokio::test]
async fn repeated_query_with_db_audit() -> anyhow::Result<()> {
let mut server = TestServer::new().await?;
let owner = &server.next_user_name();
let db = &server.next_db_name();
server.api.user_login(ADMIN, ADMIN).await?;
server.api.admin_user_add(owner, owner).await?;
server.api.user_login(owner, owner).await?;
server.api.db_add(owner, db, DbType::Mapped).await?;
server
.api
.db_exec(
owner,
db,
&vec![QueryBuilder::insert()
.nodes()
.aliases("root")
.query()
.into()],
)
.await?;
let (status, audit) = server.api.db_audit(owner, db).await?;
assert_eq!(status, 200);
assert!(!audit.0.is_empty());
server
.api
.db_exec(
owner,
db,
&vec![QueryBuilder::insert()
.nodes()
.aliases("root")
.query()
.into()],
)
.await?;
let (status, audit2) = server.api.db_audit(owner, db).await?;
assert_eq!(status, 200);
assert_eq!(audit2.0.len(), 2);
assert_eq!(audit2.0[0], audit.0[0]);
assert_eq!(audit2.0[1], audit.0[0]);
Ok(())
}