From f87802142598176c520ca048b84e3db7be6c31a2 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Mon, 1 Jul 2024 11:48:52 -0300 Subject: [PATCH] Reorg daemon code a bit --- shard.yml | 2 +- src/daemon-secrets.cr | 44 ------------------ src/{daemon.cr => daemon/main.cr} | 4 +- .../proxyconf.cr} | 0 src/daemon/secrets.cr | 46 +++++++++++++++++++ 5 files changed, 49 insertions(+), 47 deletions(-) delete mode 100644 src/daemon-secrets.cr rename src/{daemon.cr => daemon/main.cr} (96%) rename src/{daemon-proxyconf.cr => daemon/proxyconf.cr} (100%) create mode 100644 src/daemon/secrets.cr diff --git a/shard.yml b/shard.yml index eed8c4d..be71f9c 100644 --- a/shard.yml +++ b/shard.yml @@ -8,7 +8,7 @@ targets: faaso: main: src/main.cr faaso-daemon: - main: src/daemon.cr + main: src/daemon/main.cr crystal: ">= 1.12.2" diff --git a/src/daemon-secrets.cr b/src/daemon-secrets.cr deleted file mode 100644 index 39f93d0..0000000 --- a/src/daemon-secrets.cr +++ /dev/null @@ -1,44 +0,0 @@ -require "kemal" - -SECRETS = Hash(String, String).new -SECRET_PATH = "./secrets/" - -# TODO: sanitize all inputs - -# Store secrets in a tree of files -def update_secrets - # Save new secrets - SECRETS.map do |_name, value| - funko, name = _name.split("-", 2) - funko_dir = Path.new(SECRET_PATH, funko) - Dir.mkdir_p(funko_dir) - File.write(Path.new(funko_dir, name), value) - end - # Delete secrets not in the hash - Dir.glob(Path.new(SECRET_PATH, "*")).each do |funko_dir| - funko = File.basename(funko_dir) - Dir.glob(Path.new(funko_dir, "*")).each do |secret_file| - name = File.basename(secret_file) - unless SECRETS.has_key?("#{funko}-#{name}") - File.delete(secret_file) - end - end - end -end - -# Gets a secret in form {"name": "funko_name-secret_name", "value": "secret_value"} -post "/secrets/" do |env| - name = env.params.json["name"].as(String) - value = env.params.json["value"].as(String) - SECRETS[name] = value - update_secrets - halt env, status_code: 201, response: "Created" -end - -# Deletes a secret from the disk and memory -delete "/secrets/:name/" do |env| - name = env.params.url["name"] - SECRETS.delete(name) - update_secrets - halt env, status_code: 204, response: "Deleted" -end diff --git a/src/daemon.cr b/src/daemon/main.cr similarity index 96% rename from src/daemon.cr rename to src/daemon/main.cr index 67b0c67..d941212 100644 --- a/src/daemon.cr +++ b/src/daemon/main.cr @@ -1,5 +1,5 @@ -require "./daemon-secrets.cr" -require "./daemon-proxyconf.cr" +require "./secrets.cr" +require "./proxyconf.cr" require "compress/gzip" require "crystar" require "docr" diff --git a/src/daemon-proxyconf.cr b/src/daemon/proxyconf.cr similarity index 100% rename from src/daemon-proxyconf.cr rename to src/daemon/proxyconf.cr diff --git a/src/daemon/secrets.cr b/src/daemon/secrets.cr new file mode 100644 index 0000000..cd58801 --- /dev/null +++ b/src/daemon/secrets.cr @@ -0,0 +1,46 @@ +require "kemal" + +module Secrets + SECRETS = Hash(String, String).new + SECRET_PATH = "./secrets/" + + # TODO: sanitize all inputs + + # Store secrets in a tree of files + def self.update_secrets + # Save new secrets + SECRETS.map do |_name, value| + funko, name = _name.split("-", 2) + funko_dir = Path.new(SECRET_PATH, funko) + Dir.mkdir_p(funko_dir) + File.write(Path.new(funko_dir, name), value) + end + # Delete secrets not in the hash + Dir.glob(Path.new(SECRET_PATH, "*")).each do |funko_dir| + funko = File.basename(funko_dir) + Dir.glob(Path.new(funko_dir, "*")).each do |secret_file| + name = File.basename(secret_file) + unless SECRETS.has_key?("#{funko}-#{name}") + File.delete(secret_file) + end + end + end + end + + # Gets a secret in form {"name": "funko_name-secret_name", "value": "secret_value"} + post "/secrets/" do |env| + name = env.params.json["name"].as(String) + value = env.params.json["value"].as(String) + SECRETS[name] = value + Secrets.update_secrets + halt env, status_code: 201, response: "Created" + end + + # Deletes a secret from the disk and memory + delete "/secrets/:name/" do |env| + name = env.params.url["name"] + SECRETS.delete(name) + update_secrets + halt env, status_code: 204, response: "Deleted" + end +end