Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fluffy-melli/MoliDB

Repository files navigation

KR / EN

🌟 MoliDB - Secure Memory Database

MoliDB is an open-source, secure memory database that allows easy management of data through its REST API.
All data is transmitted and received using AES encryption, ensuring sensitive information is handled securely.
You can find client code examples in example.md.

Unallowed IPs will be denied access
MoliDB blocks connections from IPs that are not included in the configured allowed IP list, further enhancing the security of the database.

⚠️Warning: If allow-ip is an empty array ([]) in the config.json, all connections will be allowed. This setting can be very insecure, so use it with caution.


πŸš€ Installation Instructions

Here are the steps to build and run the project:

⚠️Note: Be sure to modify the SECRET_KEY and API_KEY values in the .env file before running the project.

Running with Docker

$ docker build -t molidb .
$ docker run -d -p 17233:17233 molidb

πŸ“¦ Installing the Client

To use the MoliDB client in Python, you can install the library from PyPI using the following command:

$ pip install molidb

πŸ“œ Example Code (Python)

from molidb import molidb
db = molidb() # You can modify the server URL, secret key, and API token here.
print(db.list_collection())
print(db.update_collection('user', [{'id':'molidb','money':10}]))
userlist = db.get_collection('user')
print(userlist)
for user in userlist:
 if user['id'] == 'molidb':
 user['money'] += 20
print(db.get_collection('user'))
print(db.update_collection('user', userlist))
print(db.list_collection())

To use the MoliDB client in JavaScript, you can install the library from npm using the following command:

$ npm install molidb

πŸ“œ Example Code

const Molidb = require('molidb');
(async () => {
 try {
 const db = new Molidb(); // You can modify the server URL / secret key / API token here
 console.log(JSON.stringify(await db.listCollection()));
 console.log(JSON.stringify(await db.updateCollection('user', [{ id: 'molidb', money: 10 }])));
 let userlist = await db.getCollection('user');
 console.log(JSON.stringify(userlist));
 userlist.forEach(user => {
 if (user.id === 'molidb') {
 user.money += 20;
 }
 });
 console.log(JSON.stringify(await db.getCollection('user')));
 console.log(JSON.stringify(await db.updateCollection('user', userlist)));
 console.log(JSON.stringify(await db.listCollection()));
 await db.deleteCollection('user');
 console.log(JSON.stringify(await db.listCollection()));
 } catch (error) {
 console.error('Error:', error.message);
 }
})();

πŸ” Encryption Method

  • Step-by-step Process
  1. Data Compression (gzip)
    First, the data is compressed using the gzip algorithm. gzip reduces the size of the data, improving transmission and storage efficiency.

  2. Data Encryption (AES)
    The compressed data is encrypted using the AES (Advanced Encryption Standard) algorithm.
    AES is a symmetric key encryption method, meaning the same key is used to both encrypt and decrypt the data.
    The key used for this process is the SECRET_KEY from the .env file.

This encryption process ensures both data efficiency and security.

graph TD
 subgraph c_crypto[Encryption]
 BGZIP[gzip] --> BAES[AES]
 end
 subgraph cb_crypto[Decryption]
 CBAES[AES] --> CBGZIP[gzip]
 end
 subgraph s_crypto[Encryption]
 AGZIP[gzip] --> AAES[AES]
 end
 subgraph sb_crypto[Decryption]
 CAAES[AES] --> CAGZIP[gzip]
 end
 subgraph client
 Respond0[HTTP Respond] --> cb_crypto --> G0[processing]
 G0 --> c_crypto --> Request0[HTTP Request]
 end
 subgraph server
 Request1[HTTP Request] --> sb_crypto --> G1[processing]
 G1 --> s_crypto --> Respond1[HTTP Respond]
 end
Loading

πŸ“‘ REST API Structure

graph TD
 subgraph crypto[Encryption]
 GZIP[gzip] --> AES[AES]
 end
 subgraph ccrypto[Decryption]
 CAES[AES] --> CGZIP[gzip]
 end
 subgraph router
 GET_collection[GET /collection] --> input[router]
 GET_collection_id[GET /collection/:id] --> input
 PUT_collection_id[PUT /collection/:id] --> input
 DELETE_collection_id[DELETE /collection/:id] --> input
 input --> error
 subgraph check
 error[API-Token]
 error --> Respond0[HTTP Respond 400]
 error --> ccrypto --> DB --> crypto --> Respond1[HTTP Respond 200]
 DB --> Respond2[HTTP Respond 500]
 end
 end
Loading

πŸ“ Router Explanation

The router is responsible for handling client requests and returning appropriate responses. Each request is mapped to a specific endpoint and HTTP method, allowing the server to understand and process the client's request.

Main Endpoints

  • GET /collection: Retrieves all collection data.
  • GET /collection/:id: Retrieves collection data for a specific ID.
  • PUT /collection/:id: Updates collection data for a specific ID.
  • DELETE /collection/:id: Deletes collection data for a specific ID.

Request Handling Process

  1. Router: The router receives the client’s request and routes it to the appropriate endpoint.
  2. API-Token Validation: The API-Token included in the request is validated. If invalid, a 400 response is returned.
  3. Encryption/Decryption: For valid requests, data is compressed using gzip and encrypted using AES before being stored in the database, or data retrieved from the database is decrypted and decompressed before being returned to the client.
  4. Response: Depending on the outcome, either a 200 or 400 response is returned.

This structure allows the server to efficiently process client requests while maintaining data security and integrity.


πŸ—οΈ Overall Architecture

graph TB
 Client((Client))
 
 subgraph "MoliDB System"
 subgraph "API Layer"
 RestAPI["REST API Server<br>Gin Framework"]
 SwaggerDocs["API Documentation<br>Swagger"]
 
 subgraph "Middleware Components"
 AuthMiddleware["Authentication<br>JWT"]
 RateLimiter["Rate Limiter<br>Token Bucket"]
 CollectionHandler["Collection Handler<br>Gin"]
 end
 end
 
 subgraph "Core Services"
 subgraph "Encryption Service"
 GzipComp["Compression<br>gzip"]
 AESCrypto["Encryption<br>AES-CBC"]
 end
 
 subgraph "Data Storage"
 CacheStore["In-Memory Store<br>Safe Map"]
 BackupService["Backup Service<br>Go"]
 end
 
 subgraph "Utility Services"
 Logger["Logger Service<br>Go"]
 ConfigManager["Config Manager<br>godotenv"]
 end
 end
 end
 %% Client Interactions
 Client -->|"HTTP Requests"| RestAPI
 
 %% API Layer Connections
 RestAPI -->|"Routes"| CollectionHandler
 RestAPI -->|"Documents"| SwaggerDocs
 RestAPI -->|"Validates"| AuthMiddleware
 RestAPI -->|"Controls Rate"| RateLimiter
 
 %% Middleware Flow
 CollectionHandler -->|"Processes"| GzipComp
 GzipComp -->|"Secures"| AESCrypto
 
 %% Data Flow
 AESCrypto -->|"Stores"| CacheStore
 CacheStore -->|"Backs up"| BackupService
 
 %% Utility Connections
 CollectionHandler -->|"Logs"| Logger
 RestAPI -->|"Loads Config"| ConfigManager
 
 %% Data Processing Flow
 CacheStore -->|"Retrieves"| AESCrypto
 AESCrypto -->|"Decompresses"| GzipComp
 GzipComp -->|"Returns"| CollectionHandler
 %% Styling
 classDef container fill:#e1eeff,stroke:#9dc1ff
 classDef component fill:#f9f9f9,stroke:#666
 class RestAPI,CacheStore container
 class AuthMiddleware,RateLimiter,CollectionHandler,GzipComp,AESCrypto,Logger,ConfigManager,BackupService component
Loading

πŸ“œ License

MoliDB follows the MIT License. Please comply with the license terms if modifying or distributing the code.

Copyright Β© All rights reserved.

About

This project is a secure memory DB with AES encryption applied, based on a REST API. AES encryption is used to protect sensitive data by encrypting it, and the data is stored and processed in a secure environment.

Topics

Resources

License

Stars

Watchers

Forks

Packages

Contributors

Languages

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /