Use configurable auth everywhere (not tested)

This commit is contained in:
Roberto Alsina 2024-07-07 21:07:20 -03:00
parent 4aa307c65c
commit f14a8d1c39
7 changed files with 39 additions and 27 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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