Implemented basic client config for auth

This commit is contained in:
Roberto Alsina 2024-07-07 20:48:32 -03:00
parent 2ddbda5a4f
commit 4aa307c65c
9 changed files with 81 additions and 11 deletions

View File

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

View File

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

29
src/commands/login.cr Normal file
View File

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

View File

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

View File

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

View File

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

26
src/config.cr Normal file
View File

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

View File

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

View File

@ -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 <level>] [-l] [--no-runtime]
faaso export SOURCE DESTINATION [-v <level>]
faaso login [-v <level>]
faaso new -r runtime FOLDER [-v <level>]
faaso scale FUNKO [SCALE] [-v <level>] [-l]
faaso secret (-d|-a) FUNKO SECRET [-v <level>] [-l]
faaso status FUNKO [-v <level>] [-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)