Authentication & User Management
Overview
Section titled “Overview”HaruDB provides comprehensive authentication and user management features including:
- User Management: Create, delete, and manage database users
- Role-based Access Control: Admin, User, and ReadOnly roles
- Session Management: Secure session handling with automatic cleanup
- TLS Encryption: Optional TLS support for secure connections
- Password Security: SHA-256 password hashing
User Roles
Section titled “User Roles”HaruDB supports three distinct user roles with different permission levels:
Admin Role
Section titled “Admin Role”- Full database access: Can perform all operations
- User management: Create, delete, and modify users
- Backup and restore: Access to backup and restore operations
- System administration: Full control over database settings
User Role
Section titled “User Role”- Standard database operations: Create tables, insert, update, delete data
- Transaction management: Full transaction support
- Query operations: All SELECT operations and WHERE clauses
- Index management: Create and use indexes
ReadOnly Role
Section titled “ReadOnly Role”- Read-only access: Can only perform SELECT operations
- No modifications: Cannot insert, update, or delete data
- No schema changes: Cannot create or drop tables
- Limited transactions: Can only read data within transactions
User Management Commands
Section titled “User Management Commands”Authenticate with existing user credentials.
-- Login with username and passwordLOGIN username password
-- ExampleLOGIN admin admin123LOGIN john mypasswordLOGIN readonly readpass
Notes:
- Default admin user:
admin
/admin123
- ⚠️ SECURITY WARNING: Change the default password immediately after first login!
- Passwords are case-sensitive
- Sessions are automatically managed
- Authentication is now REQUIRED for all database operations
LOGOUT
Section titled “LOGOUT”End the current user session.
LOGOUT
CHANGE PASSWORD
Section titled “CHANGE PASSWORD”Change your current password.
-- Change passwordCHANGE PASSWORD old_password new_password
-- ExampleCHANGE PASSWORD admin123 mynewpassword
Notes:
- You must be logged in to change your password
- Old password must be correct
- New password will be hashed and stored securely
CREATE USER
Section titled “CREATE USER”Create a new database user with specified role.
-- Create user with standard roleCREATE USER username password USER
-- Create user with admin roleCREATE USER username password ADMIN
-- Create user with readonly roleCREATE USER username password READONLY
-- ExamplesCREATE USER john mypassword USERCREATE USER jane herpassword ADMINCREATE USER readonly readpass READONLY
Notes:
- Only admin users can create new users
- Usernames must be unique
- Passwords are hashed using SHA-256
DELETE USER
Section titled “DELETE USER”Remove a user from the database.
-- Delete userDELETE USER username
-- ExampleDELETE USER john
Notes:
- Only admin users can delete users
- Cannot delete the current logged-in user
- Cannot delete the default admin user
LIST USERS
Section titled “LIST USERS”Display all users in the database (admin only).
LIST USERS
Output example:
Users:- admin (ADMIN)- john (USER)- jane (ADMIN)- readonly (READONLY)
Complete Authentication Example
Section titled “Complete Authentication Example”-- Login as default adminLOGIN admin admin123
-- Create different types of usersCREATE USER alice alicepass USERCREATE USER bob bobpass ADMINCREATE USER viewer viewpass READONLY
-- List all usersLIST USERS
-- Logout and login as different userLOGOUTLOGIN alice alicepass
-- Try to create user (will fail - not admin)CREATE USER test testpass USER-- Error: Insufficient permissions
-- Logout and login as adminLOGOUTLOGIN bob bobpass
-- Create another userCREATE USER charlie charliepass USER
-- List users againLIST USERS
-- Delete a userDELETE USER alice
-- Final user listLIST USERS
Role-Based Access Control Examples
Section titled “Role-Based Access Control Examples”Admin User Operations
Section titled “Admin User Operations”-- Login as adminLOGIN admin admin123
-- Full database operationsCREATE TABLE users (id, name, email);INSERT INTO users VALUES (1, 'Alice', 'alice@example.com');SELECT * FROM users;
-- User managementCREATE USER john johnpass USERLIST USERS
-- Backup operationsBACKUP TO ./backup.db DESCRIPTION "Admin backup"
-- All operations allowedUPDATE users SET name = 'Alice Updated' ROW 0;DELETE FROM users ROW 0;DROP TABLE users;
Standard User Operations
Section titled “Standard User Operations”-- Login as standard userLOGIN john johnpass
-- Standard database operationsCREATE TABLE products (id, name, price);INSERT INTO products VALUES (1, 'Laptop', '999.99');SELECT * FROM products;
-- Transaction operationsBEGIN TRANSACTION;INSERT INTO products VALUES (2, 'Mouse', '29.99');COMMIT;
-- Index operationsCREATE INDEX ON products (name);SELECT * FROM products WHERE name = 'Laptop';
-- Update and delete operationsUPDATE products SET price = '1099.99' ROW 0;DELETE FROM products ROW 1;
-- These operations will fail (not admin)CREATE USER test testpass USER-- Error: Insufficient permissions
LIST USERS-- Error: Insufficient permissions
ReadOnly User Operations
Section titled “ReadOnly User Operations”-- Login as readonly userLOGIN viewer viewpass
-- Read operations onlySELECT * FROM products;SELECT * FROM products WHERE price > 100;SELECT COUNT(*) FROM products;
-- Transaction for read consistencyBEGIN TRANSACTION;SELECT * FROM products;SELECT * FROM products WHERE name = 'Laptop';COMMIT;
-- These operations will fail (readonly)INSERT INTO products VALUES (3, 'Keyboard', '79.99');-- Error: Insufficient permissions
UPDATE products SET price = '1199.99' ROW 0;-- Error: Insufficient permissions
CREATE TABLE orders (id, product_id, quantity);-- Error: Insufficient permissions
TLS Encryption
Section titled “TLS Encryption”HaruDB supports TLS encryption for secure connections.
Server Configuration
Section titled “Server Configuration”# Start server with TLS./harudb --data-dir ./data --tls
# Start server with custom TLS settings./harudb --data-dir ./data --tls --tls-cert ./cert.pem --tls-key ./key.pem
Client Connection
Section titled “Client Connection”# Connect with TLSharu-cli --tls
# Connect to remote server with TLSharu-cli --host remote-server.com --port 54321 --tls
Session Management
Section titled “Session Management”Automatic Session Cleanup
Section titled “Automatic Session Cleanup”HaruDB automatically manages user sessions:
- Session timeout: Inactive sessions are automatically closed
- Connection limits: Maximum concurrent connections per user
- Cleanup on disconnect: Sessions are properly cleaned up
Session Information
Section titled “Session Information”-- Check current user (if supported)SELECT CURRENT_USER();
-- Check session statusSELECT SESSION_ID();
Security Best Practices
Section titled “Security Best Practices”Password Security
Section titled “Password Security”- Use strong passwords: Minimum 8 characters with mixed case, numbers, and symbols
- Regular password updates: Change passwords periodically
- Avoid default passwords: Never use default admin password in production
User Management
Section titled “User Management”- Principle of least privilege: Assign minimum required permissions
- Regular user audits: Review and clean up unused accounts
- Role separation: Use different roles for different purposes
Network Security
Section titled “Network Security”- Use TLS encryption: Always use TLS in production environments
- Firewall configuration: Restrict database access to trusted networks
- Regular updates: Keep HaruDB updated with latest security patches
Common Authentication Patterns
Section titled “Common Authentication Patterns”Application User Pattern
Section titled “Application User Pattern”-- Create application-specific usersCREATE USER app_user apppass USERCREATE USER app_readonly appreadpass READONLY
-- Application connects with app_user for writesLOGIN app_user apppass-- Perform write operations
-- Application connects with app_readonly for readsLOGOUTLOGIN app_readonly appreadpass-- Perform read operations
Multi-tenant Pattern
Section titled “Multi-tenant Pattern”-- Create tenant-specific usersCREATE USER tenant1_user tenant1pass USERCREATE USER tenant2_user tenant2pass USERCREATE USER tenant1_readonly tenant1readpass READONLYCREATE USER tenant2_readonly tenant2readpass READONLY
-- Each tenant uses their own userLOGIN tenant1_user tenant1pass-- Tenant 1 operations
LOGOUTLOGIN tenant2_user tenant2pass-- Tenant 2 operations
Backup User Pattern
Section titled “Backup User Pattern”-- Create dedicated backup userCREATE USER backup_user backuppass ADMIN
-- Automated backup scriptLOGIN backup_user backuppassBACKUP TO ./daily_backup.db DESCRIPTION "Daily automated backup"LOGOUT
Troubleshooting Authentication
Section titled “Troubleshooting Authentication”Common Issues
Section titled “Common Issues”Login failed:
-- Check username and passwordLOGIN admin admin123
-- Verify user existsLIST USERS
Permission denied:
-- Check current user role-- Admin users can perform all operations-- Standard users cannot manage users-- Readonly users can only read data
Session expired:
-- Re-loginLOGIN username password
Error Messages
Section titled “Error Messages”- “Invalid credentials”: Username or password incorrect
- “User not found”: Username doesn’t exist
- “Insufficient permissions”: User role doesn’t allow operation
- “Session expired”: User session has timed out
Security Best Practices
Section titled “Security Best Practices”First-Time Setup
Section titled “First-Time Setup”- Change Default Password: Immediately change the default admin password after first login
- Create Additional Users: Create specific users for different roles instead of using admin for everything
- Use Strong Passwords: Use complex passwords with mixed case, numbers, and symbols
- Regular Password Updates: Change passwords periodically
Production Deployment
Section titled “Production Deployment”- Enable TLS: Use
--tls
flag for encrypted connections - Network Security: Restrict database access to trusted networks only
- Regular Backups: Implement automated backup procedures
- Monitor Access: Regularly review user accounts and sessions
- Remove Default Users: Consider removing or disabling default accounts after setup
Example Secure Setup
Section titled “Example Secure Setup”-- 1. Login with default adminLOGIN admin admin123
-- 2. Change admin password immediatelyCHANGE PASSWORD admin123 MySecurePassword123!
-- 3. Create application userCREATE USER app_user AppPassword123 USER
-- 4. Create readonly user for monitoringCREATE USER monitor MonitorPass456 READONLY
-- 5. List users to verifyLIST USERS
-- 6. Logout and test with new credentialsLOGOUTLOGIN app_user AppPassword123
For more information about HaruDB’s security features, see the TLS documentation and Backup documentation.