From 4aa307c65ce3c34e796aa42c5bd88ce5455e48a8 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Sun, 7 Jul 2024 20:48:32 -0300 Subject: [PATCH] Implemented basic client config for auth --- TODO.md | 1 + src/commands/build.cr | 4 ++-- src/commands/login.cr | 29 +++++++++++++++++++++++++++++ src/commands/scale.cr | 4 ++-- src/commands/secret.cr | 4 ++-- src/commands/status.cr | 2 +- src/config.cr | 26 ++++++++++++++++++++++++++ src/faaso.cr | 17 +++++++++++++---- src/main.cr | 5 +++++ 9 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 src/commands/login.cr create mode 100644 src/config.cr diff --git a/TODO.md b/TODO.md index bcdd76e..ccbb2ea 100644 --- a/TODO.md +++ b/TODO.md @@ -28,6 +28,7 @@ * ✅ Direct error and above to stderr, others to stdout, while keeping logging level configurable * ✅ Fix proxy reload / Make it reload on file changes +* Implement `faaso help command` # Things to do but not before release diff --git a/src/commands/build.cr b/src/commands/build.cr index f2cf5c4..6d0b296 100644 --- a/src/commands/build.cr +++ b/src/commands/build.cr @@ -44,10 +44,10 @@ module Faaso outf << buf end - url = "#{FAASO_SERVER}funkos/build/" + url = "#{Faaso.server}funkos/build/" begin - Log.info { "Uploading funko to #{FAASO_SERVER}" } + Log.info { "Uploading funko to #{Faaso.server}" } Log.info { "Starting remote build:" } Crest.post( url, diff --git a/src/commands/login.cr b/src/commands/login.cr new file mode 100644 index 0000000..3029850 --- /dev/null +++ b/src/commands/login.cr @@ -0,0 +1,29 @@ +module Faaso + module Commands + struct Login + def run(options) : Int32 + server = Faaso.server + Log.info { "Enter password for #{server}" } + if STDIN.tty? + password = (STDIN.noecho &.gets.try &.chomp).to_s + else + password = STDIN.gets.to_s + end + # Testing with auth/ which is guaranteed locked + Crest.get( + "#{server}auth/", \ + user: "admin", password: password).body + # If we got here the password is ok + CONFIG.hosts[server] = {"admin", password} + Config.save + 0 + rescue ex : Crest::Unauthorized + Log.error { "Wrong password" } + 1 + rescue ex : Socket::ConnectError + Log.error { "Connection refused" } + 1 + end + end + end +end diff --git a/src/commands/scale.cr b/src/commands/scale.cr index 7d49fdd..4bfd94b 100644 --- a/src/commands/scale.cr +++ b/src/commands/scale.cr @@ -30,7 +30,7 @@ module Faaso Faaso.check_version if !scale Crest.get( - "#{FAASO_SERVER}funkos/#{name}/scale/", \ + "#{Faaso.server}funkos/#{name}/scale/", \ user: "admin", password: "admin") do |response| loop do Log.info { response.body_io.gets } @@ -39,7 +39,7 @@ module Faaso end else Crest.post( - "#{FAASO_SERVER}funkos/#{name}/scale/", + "#{Faaso.server}funkos/#{name}/scale/", {"scale" => scale}, user: "admin", password: "admin") do |response| loop do Log.info { response.body_io.gets } diff --git a/src/commands/secret.cr b/src/commands/secret.cr index ed046b2..c32cef0 100644 --- a/src/commands/secret.cr +++ b/src/commands/secret.cr @@ -16,7 +16,7 @@ module Faaso Faaso.check_version if options["--add"] Crest.post( - "#{FAASO_SERVER}secrets/", + "#{Faaso.server}secrets/", { "funko" => funko, "name" => name, @@ -25,7 +25,7 @@ module Faaso Log.info { "Secret created" } elsif options["--delete"] Crest.delete( - "#{FAASO_SERVER}secrets/#{funko}/#{name}", + "#{Faaso.server}secrets/#{funko}/#{name}", user: "admin", password: "admin") end 0 diff --git a/src/commands/status.cr b/src/commands/status.cr index 13a3cbc..b7fda69 100644 --- a/src/commands/status.cr +++ b/src/commands/status.cr @@ -28,7 +28,7 @@ module Faaso def remote(options, name) : Int32 Faaso.check_version Crest.get( - "#{FAASO_SERVER}funkos/#{name}/status/", \ + "#{Faaso.server}funkos/#{name}/status/", \ user: "admin", password: "admin") do |response| loop do Log.info { response.body_io.gets } diff --git a/src/config.cr b/src/config.cr new file mode 100644 index 0000000..fe95b33 --- /dev/null +++ b/src/config.cr @@ -0,0 +1,26 @@ +require "yaml" + +CONFIG = Config.load + +class Config + include YAML::Serializable + + property hosts : Hash(String, {String, String}) = Hash(String, {String, String}).new + + def initialize + @hosts = {} of String => {String, String} + end + + def self.load : Config + if File.file? ".faaso.yml" + return Config.from_yaml(File.read(".faaso.yml")) + end + Config.new + end + + def self.save + File.open(".faaso.yml", "w") do |outf| + outf << CONFIG.to_yaml + end + end +end diff --git a/src/faaso.cr b/src/faaso.cr index c3c0ae9..01ad37d 100644 --- a/src/faaso.cr +++ b/src/faaso.cr @@ -1,5 +1,6 @@ require "./commands/build.cr" require "./commands/export.cr" +require "./commands/login.cr" require "./commands/new.cr" require "./commands/scale.cr" require "./commands/secret.cr" @@ -11,9 +12,6 @@ require "docr/utils.cr" require "json" require "uuid" -# API if you just ran faaso-daemon -FAASO_SERVER = ENV.fetch("FAASO_SERVER", "http://localhost:3000/") - # Functions as a Service, Ops! module Faaso VERSION = "0.1.0" @@ -30,10 +28,21 @@ module Faaso raise ex if ex.status_code != 409 # Network already exists end + def self.server : String + url = ENV.fetch("FAASO_SERVER", nil) + if url.nil? + Log.warn { "FAASO_SERVER not set" } + url = "http://localhost:3000/" + end + url += "/" unless url.ends_with? "/" + Log.info { "Using server #{url}" } + url + end + # Compare version with server's def self.check_version server_version = Crest.get( - "#{FAASO_SERVER}version/", \ + "#{self.server}version/", \ user: "admin", password: "admin").body local_version = "#{version}" diff --git a/src/main.cr b/src/main.cr index d2cb339..a08ddbd 100644 --- a/src/main.cr +++ b/src/main.cr @@ -1,3 +1,4 @@ +require "./config.cr" require "./faaso.cr" require "./log.cr" require "colorize" @@ -14,11 +15,13 @@ FaaSO CLI tool. Usage: faaso build FOLDER ... [-v ] [-l] [--no-runtime] faaso export SOURCE DESTINATION [-v ] + faaso login [-v ] faaso new -r runtime FOLDER [-v ] faaso scale FUNKO [SCALE] [-v ] [-l] faaso secret (-d|-a) FUNKO SECRET [-v ] [-l] faaso status FUNKO [-v ] [-l] faaso version + faaso help COMMAND Options: -a --add Add @@ -40,6 +43,8 @@ when .fetch("build", false) status = Faaso::Commands::Build.new.run(ans, ans["FOLDER"].as(Array(String))) when .fetch("export", false) status = Faaso::Commands::Export.new.run(ans, ans["SOURCE"].as(String), ans["DESTINATION"].as(String)) +when .fetch("login", false) + status = Faaso::Commands::Login.new.run(ans) when .fetch("new", false) status = Faaso::Commands::New.new.run(ans, ans["FOLDER"].as(Array(String))[0]) when .fetch("scale", false)