Skip to content

Commit

Permalink
Merge pull request #356 from UffizziCloud/feature/347_improve_auth_logic
Browse files Browse the repository at this point in the history
[347] improved auth logic
  • Loading branch information
moklidia authored Oct 16, 2023
2 parents c5a708d + ccff663 commit 58f13d8
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 43 deletions.
20 changes: 16 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,25 @@ def sign_out
Uffizzi::Token.delete if Uffizzi::Token.exists?
end

def check_login(project_option)
raise Uffizzi::Error.new('You are not logged in. Run `uffizzi login`.') unless signed_in?
raise Uffizzi::Error.new('This command needs project to be set in config file') unless project_set?(project_option)
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

def project_set?(project_option)
!project_option.nil? || (Uffizzi::ConfigFile.exists? && Uffizzi::ConfigFile.option_has_value?(:project))
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(options[:project])

case command
when 'list'
Expand Down
8 changes: 3 additions & 5 deletions lib/uffizzi/cli/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require 'uffizzi/auth_helper'
require 'uffizzi/helpers/config_helper'
require 'uffizzi/services/preview_service'
require 'uffizzi/services/command_service'
require 'uffizzi/services/cluster_service'
require 'uffizzi/services/kubeconfig_service'
require 'uffizzi/services/cluster/disconnect_service'
Expand Down Expand Up @@ -65,9 +64,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])
project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]

case command
Expand Down Expand Up @@ -335,7 +332,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 +350,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
9 changes: 2 additions & 7 deletions lib/uffizzi/cli/dev.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require 'uffizzi/services/command_service'
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 +14,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[:project])
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 +49,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
4 changes: 1 addition & 3 deletions lib/uffizzi/cli/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require 'uffizzi'
require 'uffizzi/auth_helper'
require 'uffizzi/services/preview_service'
require 'uffizzi/services/command_service'
require 'uffizzi/services/github_service'

module Uffizzi
Expand Down Expand Up @@ -55,8 +54,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])

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

Expand Down
7 changes: 2 additions & 5 deletions lib/uffizzi/cli/preview/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
require 'uffizzi/auth_helper'
require 'uffizzi/response_helper'
require 'uffizzi/services/preview_service'
require 'uffizzi/services/command_service'

module Uffizzi
class Cli::Preview::Service < Thor
include ApiClient

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[:project])

deployment_id = PreviewService.read_deployment_id(deployment_name)
response = service_logs_response(logs_type, deployment_id, container_name)
Expand All @@ -28,8 +26,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])

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(options[:project])

case command
when 'list'
Expand Down
4 changes: 1 addition & 3 deletions lib/uffizzi/cli/project/compose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'uffizzi/response_helper'
require 'uffizzi/services/compose_file_service'
require 'uffizzi/services/env_variables_service'
require 'uffizzi/services/command_service'

module Uffizzi
class Cli::Project::Compose < Thor
Expand All @@ -29,8 +28,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])

project_slug = options[:project].nil? ? ConfigFile.read_option(:project) : options[:project]
file_path = options[:file]
Expand Down
9 changes: 0 additions & 9 deletions lib/uffizzi/services/command_service.rb

This file was deleted.

1 change: 1 addition & 0 deletions test/uffizzi/cli/account_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class AccountTest < Minitest::Test
def setup
@account = Uffizzi::Cli::Account.new
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')

sign_in
end
Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/cluster_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setup
Uffizzi.stubs(:prompt).returns(@mock_prompt)

sign_in
Uffizzi::ConfigFile.write_option(:project, 'dbp')
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')
@project_slug = Uffizzi::ConfigFile.read_option(:project)
ENV['GITHUB_OUTPUT'] = '/tmp/.env'
ENV['GITHUB_ACTIONS'] = 'true'
Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/dev_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setup
Uffizzi.stubs(:prompt).returns(@mock_prompt)

sign_in
Uffizzi::ConfigFile.write_option(:project, 'dbp')
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')
@project_slug = Uffizzi::ConfigFile.read_option(:project)
tmp_dir_name = (Time.now.utc.to_f * 100_000).to_i
@kubeconfig_path = "/tmp/test/#{tmp_dir_name}/test-kubeconfig.yaml"
Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/preview/service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def setup
@service = Uffizzi::Cli::Preview::Service.new

sign_in
Uffizzi::ConfigFile.write_option(:project, 'dbp')
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')
@project_slug = Uffizzi::ConfigFile.read_option(:project)
end

Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/preview_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def setup
@preview = Uffizzi::Cli::Preview.new

sign_in
Uffizzi::ConfigFile.write_option(:project, 'dbp')
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')
@project_slug = Uffizzi::ConfigFile.read_option(:project)
ENV.delete('IMAGE')
ENV.delete('CONFIG_SOURCE')
Expand Down
2 changes: 1 addition & 1 deletion test/uffizzi/cli/project/compose_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def setup
@compose = Uffizzi::Cli::Project::Compose.new

sign_in
Uffizzi::ConfigFile.write_option(:project, 'dbp')
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')
@project_slug = Uffizzi::ConfigFile.read_option(:project)
ENV.delete('IMAGE')
ENV.delete('CONFIG_SOURCE')
Expand Down
1 change: 1 addition & 0 deletions test/uffizzi/cli/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class ProjectsTest < Minitest::Test
def setup
@project = Uffizzi::Cli::Project.new
Uffizzi::ConfigFile.write_option(:project, 'uffizzi')

sign_in
end
Expand Down

0 comments on commit 58f13d8

Please sign in to comment.