This Flask-based API allows you to securely access the contents of specified files over HTTPS. It enforces secure communication, checks access permissions using a secret key, and ensures only authorized files are accessible.
- HTTPS Enforcement: Redirects all HTTP requests to HTTPS to ensure secure communication.
- File Access Control: Only allows access to files explicitly listed in a configuration file.
- Secret Key Authentication: Requires a valid secret key in the request headers to authorize access.
- Logging: Logs important events, warnings, and errors for debugging and monitoring.
- Python 3.7 or higher
- SSL certificates (server.crt and server.key) for HTTPS
-
Clone the Repository:
git clone https://github.com/JonathanStross/GDA.git cd GDA -
Create and Activate a Virtual Environment:
python -m venv venv source venv/bin/activate # On macOS/Linux venv\Scripts\activate # On Windows
-
Install Dependencies:
pip install -r requirements.txt
- Ensure
requirements.txtincludes the following dependencies:Flask Werkzeug
- Ensure
-
Set Up Environment Variables:
- You need to set the
SECRET_KEYandCONFIG_PATHenvironment variables. - On macOS/Linux:
export SECRET_KEY="your_secret_key" export CONFIG_PATH="/path/to/your/config.json"
- On Windows:
set SECRET_KEY=your_secret_key set CONFIG_PATH=C:\path\to\your\config.json
- You need to set the
-
Prepare Your Configuration File:
- Create a
config.jsonfile with the following structure:{ "allowed_files": [ "/path/to/your/file1.txt", "/path/to/your/file2.txt" ] } - Replace
/path/to/your/file1.txtand/path/to/your/file2.txtwith the absolute paths to the files you want to allow access to.
- Create a
-
Development Mode:
- Run the Flask app directly (for development or testing purposes):
python main.py
- Run the Flask app directly (for development or testing purposes):
-
Production Mode with Gunicorn:
- Use Gunicorn to run the app in a production environment:
gunicorn -w 4 -b 0.0.0.0:8080 main:app --certfile=/path/to/server.crt --keyfile=/path/to/server.key --config=/Users/js/PycharmProjects/GenericDataAgent/config.json
- Options:
-w 4: Number of worker processes (adjust based on your server's resources).-b 0.0.0.0:8080: Binds the server to all interfaces on port 443 for HTTPS.--certfileand--keyfile: Paths to your SSL certificate and key.--config: Path to the config.json with the whitelisted files
- Use Gunicorn to run the app in a production environment:
-
Create a Batch File:
- Create a
.batfile, for example,start_flask_app.batwith the following content:@echo off cd C:\path\to\location\ call venv\Scripts\activate set SECRET_KEY=your_secret_key set CONFIG_PATH=C:\path\to\your\config.json gunicorn -w 4 -b 0.0.0.0:8080 --certfile=server.crt --keyfile=server.key your_script:app
- Save this file in a location you can easily access.
- Create a
-
Schedule the Batch File to Run at Startup:
- Open Task Scheduler.
- Click on Create Task.
- Under the General tab, name your task (e.g., "Start Flask App").
- Under the Triggers tab, click New and select At startup.
- Under the Actions tab, click New, select Start a program, and browse to your
.batfile. - Click OK to save and enable the task.
-
Create a Shell Script:
- Create a shell script, for example,
start_flask_app.shwith the following content:#!/bin/bash cd /path/to/your/location source venv/bin/activate export SECRET_KEY="your_secret_key" export CONFIG_PATH="/path/to/your/config.json" gunicorn -w 4 -b 0.0.0.0:8080 --certfile=server.crt --keyfile=server.key your_script:app
- Make the script executable:
chmod +x start_flask_app.sh
- Create a shell script, for example,
-
Add the Script to Startup Items:
- Open System Preferences > Users & Groups.
- Select your user and go to the Login Items tab.
- Click the + button and add the
start_flask_app.shscript.
-
Create a Systemd Service:
-
Create a new service file, for example,
/etc/systemd/system/flask_app.service:[Unit] Description=GDA Generic Data Access After=network.target [Service] User=your_username WorkingDirectory=/path/to/your/location Environment="SECRET_KEY=your_secret_key" Environment="CONFIG_PATH=/path/to/your/config.json" ExecStart=/path/to/your/project/venv/bin/gunicorn -w 4 -b 0.0.0.0:443 --certfile=server.crt --keyfile=server.key your_script:app Restart=always [Install] WantedBy=multi-user.target
-
Replace:
your_usernamewith your Linux username./path/to/your/locationwith the path to your project directory.
-
-
Enable and Start the Service:
sudo systemctl daemon-reload sudo systemctl enable flask_app.service sudo systemctl start flask_app.service
- Method:
GET - Description: Retrieves the content of a specified file.
- Headers:
Secret-Key: Your secret key for authentication.
- Query Parameters:
filepath: The absolute path of the file to be accessed.
- Response:
- 200 OK: Returns the content of the file.
- 403 Forbidden: If the secret key is invalid or the file is not allowed.
- 404 Not Found: If the file does not exist.
- 500 Internal Server Error: If an error occurs while reading the file.
Example Request:
curl -X GET "https://localhost:8080/getfile_content?filepath=/path/to/your/file.txt" -H "Secret-Key: your_secret_key"- Always use HTTPS: The app enforces HTTPS to encrypt data in transit.
- Protect Your Secret Key: Never expose your secret key publicly.
- Restrict File Access: Only add files to
allowed_filesinconfig.jsonthat are safe to share.
- The app uses Python's
loggingmodule to log important events. - Logs are printed to the console. You can configure it to log to a file for more persistent logging.
-
This project is licensed under the MIT License. You are free to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, under the following conditions:
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Author: Jonathan Stross
- Email: jonathan.stross@pathlock.com
- GitHub: JonathanStross
Feel free to open issues or contribute to this project!