Reorg daemon code a bit

This commit is contained in:
Roberto Alsina 2024-07-01 11:48:52 -03:00
parent 20c0e0f5de
commit f878021425
5 changed files with 49 additions and 47 deletions

View File

@ -8,7 +8,7 @@ targets:
faaso: faaso:
main: src/main.cr main: src/main.cr
faaso-daemon: faaso-daemon:
main: src/daemon.cr main: src/daemon/main.cr
crystal: ">= 1.12.2" crystal: ">= 1.12.2"

View File

@ -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

View File

@ -1,5 +1,5 @@
require "./daemon-secrets.cr" require "./secrets.cr"
require "./daemon-proxyconf.cr" require "./proxyconf.cr"
require "compress/gzip" require "compress/gzip"
require "crystar" require "crystar"
require "docr" require "docr"

46
src/daemon/secrets.cr Normal file
View File

@ -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