Skip to content

Commit

Permalink
[347] improved auth logic
Browse files Browse the repository at this point in the history
  • Loading branch information
moklidia committed Oct 11, 2023
1 parent c5a708d commit ec9fdc1
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 24 deletions.
18 changes: 14 additions & 4 deletions lib/uffizzi/auth_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Uffizzi
module AuthHelper
class << self
def signed_in?
config_data_exists? || Uffizzi::Token.exists?
config_data_exists? && authorized?
end

def sign_out
Expand All @@ -16,13 +16,23 @@ def sign_out
Uffizzi::Token.delete if Uffizzi::Token.exists?
end

def check_login(options = nil)
raise Uffizzi::Error.new('You are not logged in. Run `uffizzi login`.') unless signed_in?
return unless options

raise Uffizzi::Error.new('This command needs project to be set in config file') unless CommandService.project_set?(options)
end

private

def config_data_exists?
ConfigFile.exists? &&
ConfigFile.option_has_value?(:account) &&
ConfigFile.option_has_value?(:cookie) &&
ConfigFile.option_has_value?(:server)
ConfigFile.option_has_value?(:server) &&
ConfigFile.option_has_value?(:account)
end

def authorized?
ConfigFile.option_has_value?(:cookie) || Uffizzi::Token.exists?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/uffizzi/cli/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def set_default(account_name)
private

def run(command, account_name = nil)
return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
Uffizzi::AuthHelper.check_login

case command
when 'list'
Expand Down
8 changes: 4 additions & 4 deletions lib/uffizzi/cli/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'uffizzi/services/cluster_service'
require 'uffizzi/services/kubeconfig_service'
require 'uffizzi/services/cluster/disconnect_service'
require 'byebug'

module Uffizzi
class Cli::Cluster < Thor
Expand Down Expand Up @@ -65,9 +66,7 @@ def disconnect

def run(command, command_args = {})
Uffizzi.ui.output_format = options[:output]
raise Uffizzi::Error.new('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
raise Uffizzi::Error.new('This command needs project to be set in config file') unless CommandService.project_set?(options)

Uffizzi::AuthHelper.check_login(options)
project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]

case command
Expand Down Expand Up @@ -335,7 +334,6 @@ def handle_succeed_create_response(cluster_data)
end

def save_kubeconfig(kubeconfig, kubeconfig_path)
kubeconfig_path = kubeconfig_path.nil? ? KubeconfigService.default_path : kubeconfig_path
is_update_current_context = options[:'update-current-context']

KubeconfigService.save_to_filepath(kubeconfig_path, kubeconfig) do |kubeconfig_by_path|
Expand All @@ -354,6 +352,8 @@ def save_kubeconfig(kubeconfig, kubeconfig_path)
merged_kubeconfig
end
end

Uffizzi.ui.say("Kubeconfig was updated by the path: #{kubeconfig_path}") if is_update_current_context
end

def update_clusters_config(id, params)
Expand Down
8 changes: 2 additions & 6 deletions lib/uffizzi/cli/dev.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'uffizzi/services/cluster_service'
require 'uffizzi/services/dev_service'
require 'uffizzi/services/kubeconfig_service'
require 'uffizzi/auth_helper'

module Uffizzi
class Cli::Dev < Thor
Expand All @@ -14,10 +15,10 @@ class Cli::Dev < Thor
method_option :'default-repo', type: :string
method_option :kubeconfig, type: :string
def start(config_path = 'skaffold.yaml')
Uffizzi::AuthHelper.check_login(options)
DevService.check_skaffold_existence
DevService.check_running_daemon if options[:quiet]
DevService.check_skaffold_config_existence(config_path)
check_login
cluster_id, cluster_name = start_create_cluster
kubeconfig = wait_cluster_creation(cluster_name)

Expand Down Expand Up @@ -49,11 +50,6 @@ def stop

private

def check_login
raise Uffizzi::Error.new('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
raise Uffizzi::Error.new('This command needs project to be set in config file') unless CommandService.project_set?(options)
end

def start_create_cluster
cluster_name = ClusterService.generate_name
creation_source = ClusterService::MANUAL_CREATION_SOURCE
Expand Down
3 changes: 1 addition & 2 deletions lib/uffizzi/cli/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def events(deployment_name)

def run(command, file_path: nil, deployment_name: nil)
Uffizzi.ui.output_format = options[:output]
raise Uffizzi::Error.new('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
raise Uffizzi::Error.new('This command needs project to be set in config file') unless CommandService.project_set?(options)
Uffizzi::AuthHelper.check_login(options)

project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]

Expand Down
6 changes: 2 additions & 4 deletions lib/uffizzi/cli/preview/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class Cli::Preview::Service < Thor

desc 'logs [LOGS_TYPE] [DEPLOYMENT_ID] [CONTAINER_NAME]', 'Show the logs for a container service of a preview'
def logs(logs_type, deployment_name, container_name = args)
return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
return Uffizzi.ui.say('This command needs project to be set in config file') unless CommandService.project_set?(options)
Uffizzi::AuthHelper.check_login(options)

deployment_id = PreviewService.read_deployment_id(deployment_name)
response = service_logs_response(logs_type, deployment_id, container_name)
Expand All @@ -28,8 +27,7 @@ def logs(logs_type, deployment_name, container_name = args)

desc 'list [DEPLOYMENT_ID]', 'List the container services of a given compose environment (preview)'
def list(deployment_name)
return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
return Uffizzi.ui.say('This command needs project to be set in config file') unless CommandService.project_set?(options)
Uffizzi::AuthHelper.check_login(options)

project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]
server = ConfigFile.read_option(:server)
Expand Down
2 changes: 1 addition & 1 deletion lib/uffizzi/cli/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def delete(project_slug)
private

def run(command, project_slug: nil)
return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
Uffizzi::AuthHelper.check_login

case command
when 'list'
Expand Down
3 changes: 1 addition & 2 deletions lib/uffizzi/cli/project/compose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def describe
private

def run(command)
return Uffizzi.ui.say('You are not logged in.') unless Uffizzi::AuthHelper.signed_in?
return Uffizzi.ui.say('This command needs project to be set in config file') unless CommandService.project_set?(options)
Uffizzi::AuthHelper.check_login(options)

project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]
file_path = options[:file]
Expand Down

0 comments on commit ec9fdc1

Please sign in to comment.