From 9c9a8c40b0721795f226c1f329f5198e8356c4d2 Mon Sep 17 00:00:00 2001 From: Bram Kelchtermans Date: Sat, 25 May 2024 21:48:22 +0200 Subject: [PATCH] feat: Add Dockerfile and app.py for iac-ssh-authorized-keys service --- Dockers/iac-ssh-authorized-keys/Dockerfile | 20 +++++++++++++++ Dockers/iac-ssh-authorized-keys/app.py | 30 ++++++++++++++++++++++ ESPhome/shelly/shelly-1.yaml | 4 ++- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 Dockers/iac-ssh-authorized-keys/Dockerfile create mode 100644 Dockers/iac-ssh-authorized-keys/app.py diff --git a/Dockers/iac-ssh-authorized-keys/Dockerfile b/Dockers/iac-ssh-authorized-keys/Dockerfile new file mode 100644 index 0000000..28acc6f --- /dev/null +++ b/Dockers/iac-ssh-authorized-keys/Dockerfile @@ -0,0 +1,20 @@ +# Use the official Python image from the Docker Hub +FROM python:3.9-slim + +# Set environment variable for Python +ENV PYTHONUNBUFFERED=1 + +# Install Flask +RUN pip install flask + +# Create a directory for the app +WORKDIR /app + +# Copy the app files +COPY . /app + +# Expose port 8080 +EXPOSE 8080 + +# Run the application +CMD ["python", "app.py"] diff --git a/Dockers/iac-ssh-authorized-keys/app.py b/Dockers/iac-ssh-authorized-keys/app.py new file mode 100644 index 0000000..b664939 --- /dev/null +++ b/Dockers/iac-ssh-authorized-keys/app.py @@ -0,0 +1,30 @@ +from flask import Flask, request, jsonify +import os + +app = Flask(__name__) + +API_KEY = os.getenv("API_KEY") +AUTHORIZED_KEYS_PATH = "/authorized_keys" + + +@app.route("/", methods=["POST"]) +def root(): + data = request.get_json() + api_key = data.get("API_KEY") + file_contents = data.get("FILE_CONTENTS") + + if api_key == API_KEY: + try: + with open(AUTHORIZED_KEYS_PATH, "w") as f: + f.write(file_contents + "\n") + return jsonify({"message": "File contents overwritten successfully"}), 200 + except Exception as e: + return jsonify({"message": f"Failed to write to file: {str(e)}"}), 500 + else: + return jsonify({"message": "Unauthorized"}), 401 + + +if __name__ == "__main__": + if API_KEY is None: + raise ValueError("API_KEY environment variable is not set") + app.run(host="0.0.0.0", port=8080) diff --git a/ESPhome/shelly/shelly-1.yaml b/ESPhome/shelly/shelly-1.yaml index 9943d22..b229053 100644 --- a/ESPhome/shelly/shelly-1.yaml +++ b/ESPhome/shelly/shelly-1.yaml @@ -16,7 +16,9 @@ wifi: dns1: 1.1.1.1 dns2: 8.8.8.8 power_save_mode: none - + ap: + ssid: "Slaapkamer Fallback Hotspot" + password: !secret hotspot_password captive_portal: logger: