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, * ✅ Direct error and above to stderr, others to stdout,
while keeping logging level configurable while keeping logging level configurable
* ✅ Fix proxy reload / Make it reload on file changes * ✅ Fix proxy reload / Make it reload on file changes
* Implement `faaso help command`
# Things to do but not before release # Things to do but not before release

View File

@ -44,10 +44,10 @@ module Faaso
outf << buf outf << buf
end end
url = "#{FAASO_SERVER}funkos/build/" url = "#{Faaso.server}funkos/build/"
begin begin
Log.info { "Uploading funko to #{FAASO_SERVER}" } Log.info { "Uploading funko to #{Faaso.server}" }
Log.info { "Starting remote build:" } Log.info { "Starting remote build:" }
Crest.post( Crest.post(
url, 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 Faaso.check_version
if !scale if !scale
Crest.get( Crest.get(
"#{FAASO_SERVER}funkos/#{name}/scale/", \ "#{Faaso.server}funkos/#{name}/scale/", \
user: "admin", password: "admin") do |response| user: "admin", password: "admin") do |response|
loop do loop do
Log.info { response.body_io.gets } Log.info { response.body_io.gets }
@ -39,7 +39,7 @@ module Faaso
end end
else else
Crest.post( Crest.post(
"#{FAASO_SERVER}funkos/#{name}/scale/", "#{Faaso.server}funkos/#{name}/scale/",
{"scale" => scale}, user: "admin", password: "admin") do |response| {"scale" => scale}, user: "admin", password: "admin") do |response|
loop do loop do
Log.info { response.body_io.gets } Log.info { response.body_io.gets }

View File

@ -16,7 +16,7 @@ module Faaso
Faaso.check_version Faaso.check_version
if options["--add"] if options["--add"]
Crest.post( Crest.post(
"#{FAASO_SERVER}secrets/", "#{Faaso.server}secrets/",
{ {
"funko" => funko, "funko" => funko,
"name" => name, "name" => name,
@ -25,7 +25,7 @@ module Faaso
Log.info { "Secret created" } Log.info { "Secret created" }
elsif options["--delete"] elsif options["--delete"]
Crest.delete( Crest.delete(
"#{FAASO_SERVER}secrets/#{funko}/#{name}", "#{Faaso.server}secrets/#{funko}/#{name}",
user: "admin", password: "admin") user: "admin", password: "admin")
end end
0 0

View File

@ -28,7 +28,7 @@ module Faaso
def remote(options, name) : Int32 def remote(options, name) : Int32
Faaso.check_version Faaso.check_version
Crest.get( Crest.get(
"#{FAASO_SERVER}funkos/#{name}/status/", \ "#{Faaso.server}funkos/#{name}/status/", \
user: "admin", password: "admin") do |response| user: "admin", password: "admin") do |response|
loop do loop do
Log.info { response.body_io.gets } 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/build.cr"
require "./commands/export.cr" require "./commands/export.cr"
require "./commands/login.cr"
require "./commands/new.cr" require "./commands/new.cr"
require "./commands/scale.cr" require "./commands/scale.cr"
require "./commands/secret.cr" require "./commands/secret.cr"
@ -11,9 +12,6 @@ require "docr/utils.cr"
require "json" require "json"
require "uuid" require "uuid"
# API if you just ran faaso-daemon
FAASO_SERVER = ENV.fetch("FAASO_SERVER", "http://localhost:3000/")
# Functions as a Service, Ops! # Functions as a Service, Ops!
module Faaso module Faaso
VERSION = "0.1.0" VERSION = "0.1.0"
@ -30,10 +28,21 @@ module Faaso
raise ex if ex.status_code != 409 # Network already exists raise ex if ex.status_code != 409 # Network already exists
end 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 # Compare version with server's
def self.check_version def self.check_version
server_version = Crest.get( server_version = Crest.get(
"#{FAASO_SERVER}version/", \ "#{self.server}version/", \
user: "admin", password: "admin").body user: "admin", password: "admin").body
local_version = "#{version}" local_version = "#{version}"

View File

@ -1,3 +1,4 @@
require "./config.cr"
require "./faaso.cr" require "./faaso.cr"
require "./log.cr" require "./log.cr"
require "colorize" require "colorize"
@ -14,11 +15,13 @@ FaaSO CLI tool.
Usage: Usage:
faaso build FOLDER ... [-v <level>] [-l] [--no-runtime] faaso build FOLDER ... [-v <level>] [-l] [--no-runtime]
faaso export SOURCE DESTINATION [-v <level>] faaso export SOURCE DESTINATION [-v <level>]
faaso login [-v <level>]
faaso new -r runtime FOLDER [-v <level>] faaso new -r runtime FOLDER [-v <level>]
faaso scale FUNKO [SCALE] [-v <level>] [-l] faaso scale FUNKO [SCALE] [-v <level>] [-l]
faaso secret (-d|-a) FUNKO SECRET [-v <level>] [-l] faaso secret (-d|-a) FUNKO SECRET [-v <level>] [-l]
faaso status FUNKO [-v <level>] [-l] faaso status FUNKO [-v <level>] [-l]
faaso version faaso version
faaso help COMMAND
Options: Options:
-a --add Add -a --add Add
@ -40,6 +43,8 @@ when .fetch("build", false)
status = Faaso::Commands::Build.new.run(ans, ans["FOLDER"].as(Array(String))) status = Faaso::Commands::Build.new.run(ans, ans["FOLDER"].as(Array(String)))
when .fetch("export", false) when .fetch("export", false)
status = Faaso::Commands::Export.new.run(ans, ans["SOURCE"].as(String), ans["DESTINATION"].as(String)) 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) when .fetch("new", false)
status = Faaso::Commands::New.new.run(ans, ans["FOLDER"].as(Array(String))[0]) status = Faaso::Commands::New.new.run(ans, ans["FOLDER"].as(Array(String))[0])
when .fetch("scale", false) when .fetch("scale", false)