From f14a8d1c3977692fb3ffd7618500e545685b3579 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Sun, 7 Jul 2024 21:07:20 -0300 Subject: [PATCH] Use configurable auth everywhere (not tested) --- src/commands/build.cr | 7 ++++--- src/commands/login.cr | 2 +- src/commands/scale.cr | 9 +++++---- src/commands/secret.cr | 9 +++++---- src/commands/status.cr | 5 +++-- src/config.cr | 18 ++++++++++++++++++ src/faaso.cr | 16 +++------------- 7 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/commands/build.cr b/src/commands/build.cr index 6d0b296..65a243d 100644 --- a/src/commands/build.cr +++ b/src/commands/build.cr @@ -44,15 +44,16 @@ module Faaso outf << buf end - url = "#{Faaso.server}funkos/build/" + url = "#{Config.server}funkos/build/" begin - Log.info { "Uploading funko to #{Faaso.server}" } + user, password = Config.auth + Log.info { "Uploading funko to #{Config.server}" } Log.info { "Starting remote build:" } Crest.post( url, {"funko.tgz" => File.open(tmp), "name" => "funko.tgz"}, - user: "admin", password: "admin" + user: user, password: password ) do |response| loop do Log.info { response.body_io.gets } diff --git a/src/commands/login.cr b/src/commands/login.cr index 3029850..09d5899 100644 --- a/src/commands/login.cr +++ b/src/commands/login.cr @@ -2,7 +2,7 @@ module Faaso module Commands struct Login def run(options) : Int32 - server = Faaso.server + server = Config.server Log.info { "Enter password for #{server}" } if STDIN.tty? password = (STDIN.noecho &.gets.try &.chomp).to_s diff --git a/src/commands/scale.cr b/src/commands/scale.cr index 4bfd94b..b6558cd 100644 --- a/src/commands/scale.cr +++ b/src/commands/scale.cr @@ -27,11 +27,12 @@ module Faaso end def remote(options, name, scale) : Int32 + user, password = Config.auth Faaso.check_version if !scale Crest.get( - "#{Faaso.server}funkos/#{name}/scale/", \ - user: "admin", password: "admin") do |response| + "#{Config.server}funkos/#{name}/scale/", \ + user: user, password: password) do |response| loop do Log.info { response.body_io.gets } break if response.body_io.closed? @@ -39,8 +40,8 @@ module Faaso end else Crest.post( - "#{Faaso.server}funkos/#{name}/scale/", - {"scale" => scale}, user: "admin", password: "admin") do |response| + "#{Config.server}funkos/#{name}/scale/", + {"scale" => scale}, user: user, password: password) do |response| loop do Log.info { response.body_io.gets } break if response.body_io.closed? diff --git a/src/commands/secret.cr b/src/commands/secret.cr index c32cef0..4ae4fad 100644 --- a/src/commands/secret.cr +++ b/src/commands/secret.cr @@ -14,19 +14,20 @@ module Faaso def remote(options, funko, name, secret) : Int32 Faaso.check_version + user, password = Config.auth if options["--add"] Crest.post( - "#{Faaso.server}secrets/", + "#{Config.server}secrets/", { "funko" => funko, "name" => name, "value" => secret, - }, user: "admin", password: "admin") + }, user: user, password: password) Log.info { "Secret created" } elsif options["--delete"] Crest.delete( - "#{Faaso.server}secrets/#{funko}/#{name}", - user: "admin", password: "admin") + "#{Config.server}secrets/#{funko}/#{name}", + user: user, password: password) end 0 rescue ex : Crest::RequestFailed diff --git a/src/commands/status.cr b/src/commands/status.cr index b7fda69..768382d 100644 --- a/src/commands/status.cr +++ b/src/commands/status.cr @@ -27,9 +27,10 @@ module Faaso def remote(options, name) : Int32 Faaso.check_version + user, password = Config.auth Crest.get( - "#{Faaso.server}funkos/#{name}/status/", \ - user: "admin", password: "admin") do |response| + "#{Config.server}funkos/#{name}/status/", \ + user: user, password: password) do |response| loop do Log.info { response.body_io.gets } break if response.body_io.closed? diff --git a/src/config.cr b/src/config.cr index fe95b33..5f4b179 100644 --- a/src/config.cr +++ b/src/config.cr @@ -23,4 +23,22 @@ class Config outf << CONFIG.to_yaml end end + + @@already_warned = false + + def self.server : String + @@already_warned = true + url = ENV.fetch("FAASO_SERVER", nil) + if url.nil? + Log.warn { "FAASO_SERVER not set" } unless @@already_warned + url = "http://localhost:3000/" + end + url += "/" unless url.ends_with? "/" + Log.info { "Using server #{url}" } unless @@already_warned + url + end + + def self.auth : {String, String} + CONFIG.hosts.fetch(server, {"admin", ""}) + end end diff --git a/src/faaso.cr b/src/faaso.cr index 01ad37d..1fa9db7 100644 --- a/src/faaso.cr +++ b/src/faaso.cr @@ -28,22 +28,12 @@ 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 + user, password = Config.auth server_version = Crest.get( - "#{self.server}version/", \ - user: "admin", password: "admin").body + "#{Config.server}version/", \ + user: user, password: password).body local_version = "#{version}"