From 642b0dea8c7bee65af958e270455ed046204b55d Mon Sep 17 00:00:00 2001 From: ethanholen-hpe Date: Mon, 25 Nov 2024 09:52:20 -0700 Subject: [PATCH] CRAYSAT-1649: added error and sys exit when unable to open the token file --- CHANGELOG.md | 5 +++++ requirements-dev.lock.txt | 2 +- requirements.lock.txt | 2 +- requirements.txt | 2 +- sat/cli/auth/main.py | 4 ++-- sat/session.py | 21 +++++++++++++-------- tests/cli/bootsys/test_bos.py | 2 ++ tests/cli/bootsys/test_service_activity.py | 6 ++++++ tests/cli/firmware/test_firmware.py | 3 +++ tests/cli/slscheck/test_slscheck.py | 4 ++-- tests/cli/swap/test_ports.py | 1 + tests/test_session.py | 6 +++--- 12 files changed, 40 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4785b24..e512a46d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [3.33.2] - 2024-11-26 + +### Fixed +- Added error message and system exit when token file is not found. + ## [3.33.1] - 2024-12-02 ### Fixed diff --git a/requirements-dev.lock.txt b/requirements-dev.lock.txt index eb203ff7..2f5afe33 100644 --- a/requirements-dev.lock.txt +++ b/requirements-dev.lock.txt @@ -14,7 +14,7 @@ coverage==6.3.2 cray-product-catalog==2.4.1 croniter==0.3.37 cryptography==43.0.1 -csm-api-client==2.3.1 +csm-api-client==2.3.2 dataclasses-json==0.5.6 docutils==0.17.1 google-auth==2.6.0 diff --git a/requirements.lock.txt b/requirements.lock.txt index bc7b75bb..6bd4e463 100644 --- a/requirements.lock.txt +++ b/requirements.lock.txt @@ -11,7 +11,7 @@ click==8.0.4 cray-product-catalog==2.4.1 croniter==0.3.37 cryptography==43.0.1 -csm-api-client==2.3.1 +csm-api-client==2.3.2 dataclasses-json==0.5.6 google-auth==2.6.0 htmlmin==0.1.12 diff --git a/requirements.txt b/requirements.txt index c5ec4f40..f406b7c6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,7 +22,7 @@ argcomplete boto3 botocore cray-product-catalog >= 2.4.1 -csm-api-client >= 2.3.1, <3.0 +csm-api-client >= 2.3.2, <3.0 croniter >= 0.3, < 1.0 inflect >= 0.2.5, < 3.0 Jinja2 >= 3.0, < 4.0 diff --git a/sat/cli/auth/main.py b/sat/cli/auth/main.py index c8badbf4..50a83735 100644 --- a/sat/cli/auth/main.py +++ b/sat/cli/auth/main.py @@ -1,7 +1,7 @@ # # MIT License # -# (C) Copyright 2019-2023 Hewlett Packard Enterprise Development LP +# (C) Copyright 2019-2024 Hewlett Packard Enterprise Development LP # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -53,7 +53,7 @@ def do_auth(args): None """ - session = SATSession(no_unauth_warn=True) + session = SATSession(no_unauth_err=True) if session.token and not pester( f'Token already exists for "{session.username}" on "{session.host}". ' f'Overwrite?'): diff --git a/sat/session.py b/sat/session.py index 36930351..c2d7e3be 100644 --- a/sat/session.py +++ b/sat/session.py @@ -1,7 +1,7 @@ # # MIT License # -# (C) Copyright 2019-2023 Hewlett Packard Enterprise Development LP +# (C) Copyright 2019-2024 Hewlett Packard Enterprise Development LP # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -26,6 +26,8 @@ """ import logging +import os +import sys from csm_api_client.session import UserSession @@ -40,11 +42,11 @@ class SATSession(UserSession): """Subclass of the csm-api-client UserSession which follows the config file""" - def __init__(self, no_unauth_warn=False): + def __init__(self, no_unauth_err=False): """Initialize a SATSession. Args: - no_unauth_warn (bool): Suppress session-is-not-authorized warning. + no_unauth_err (bool): Suppress session-is-not-authorized warning. used when fetching a new token with "sat auth". """ @@ -63,8 +65,11 @@ def __init__(self, no_unauth_warn=False): if tenant_name: self.session.headers[TENANT_HEADER_NAME] = tenant_name - if not (self.token or no_unauth_warn): - LOGGER.warning('Session is not authenticated. ' + - 'Username is "{}". '.format(username) + - 'Obtain a token with "auth" ' + - 'subcommand, or use --token-file on the command line.') + if not (self.token or no_unauth_err): + if not os.path.exists(self.token_filename): + LOGGER.error('No token file could be found at {}'.format(self.token_filename)) + LOGGER.error('Session is not authenticated. ' + + 'Username is "{}". '.format(username) + + 'Obtain a token with "auth" ' + + 'subcommand, or use --token-file on the command line.') + sys.exit(1) diff --git a/tests/cli/bootsys/test_bos.py b/tests/cli/bootsys/test_bos.py index 0d44dda4..25bb3c1b 100644 --- a/tests/cli/bootsys/test_bos.py +++ b/tests/cli/bootsys/test_bos.py @@ -177,6 +177,7 @@ def setUp(self): self.blade_xname = 'x3000c0s0' self.nodes_on_blade_xnames = [f'{self.blade_xname}b0n{node}' for node in range(4)] self.mock_hsm_client = patch('sat.cli.bootsys.bos.HSMClient').start() + self.mock_sat_session = patch('sat.cli.bootsys.bos.SATSession').start() self.mock_get_node_components = self.mock_hsm_client.return_value.get_node_components self.mock_get_node_components.return_value = [ {'ID': xname} for xname in self.nodes_on_blade_xnames @@ -234,6 +235,7 @@ def setUp(self): self.session_id = 'abcdef-012345-678901-fdecba' self.mock_bos_client = MagicMock() + self.mock_sat_session = patch('sat.cli.bootsys.bos.SATSession').start() self.mock_bos_client.get.return_value = MagicMock(ok=True, status_code=200, json=MagicMock(return_value={ 'status': 'complete', 'managed_components_count': 10, diff --git a/tests/cli/bootsys/test_service_activity.py b/tests/cli/bootsys/test_service_activity.py index 3a82432e..3c332a6b 100644 --- a/tests/cli/bootsys/test_service_activity.py +++ b/tests/cli/bootsys/test_service_activity.py @@ -401,9 +401,14 @@ class TestBOSV2ActivityChecker(unittest.TestCase): def setUp(self): self.mock_bos_client = MagicMock() + self.mock_sat_session = patch('sat.cli.bootsys.service_activity.SATSession').start() patch('sat.cli.bootsys.service_activity.BOSClientCommon.get_bos_client', return_value=self.mock_bos_client).start() + def tearDown(self): + """Stop mocks at the end of each unit test.""" + patch.stopall() + def test_finding_no_sessions(self): """Test that the BOSV2ServiceChecker returns no sessions when no sessions exist""" self.mock_bos_client.get_sessions.return_value = [] @@ -604,6 +609,7 @@ def mock_get_active_updates(): return self.fas_actions self.fw_client = patch('sat.cli.bootsys.service_activity.FASClient').start().return_value + self.mock_sat_session = patch('sat.cli.bootsys.service_activity.SATSession').start() self.fw_client.get_active_actions.side_effect = mock_get_active_updates def tearDown(self): diff --git a/tests/cli/firmware/test_firmware.py b/tests/cli/firmware/test_firmware.py index 56325384..6459863d 100644 --- a/tests/cli/firmware/test_firmware.py +++ b/tests/cli/firmware/test_firmware.py @@ -64,6 +64,9 @@ def setUp(self): # Fake print self.mock_print = mock.patch('sat.cli.firmware.main.print').start() + # Fake SatSession + self.mock_sat_session = mock.patch('sat.cli.firmware.main.SATSession').start() + # Fake get_config_value self.fake_config = { 'format.no_headings': False, diff --git a/tests/cli/slscheck/test_slscheck.py b/tests/cli/slscheck/test_slscheck.py index 04b5eb89..2e8265e4 100644 --- a/tests/cli/slscheck/test_slscheck.py +++ b/tests/cli/slscheck/test_slscheck.py @@ -1,7 +1,7 @@ # # MIT License # -# (C) Copyright 2021 Hewlett Packard Enterprise Development LP +# (C) Copyright 2021, 2024 Hewlett Packard Enterprise Development LP # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -204,7 +204,7 @@ def setUp(self): self.mock_hsm_client.get_all_components.return_value = self.all_hsm_components self.mock_hsm_client.get_bmcs_by_type.return_value = self.all_hsm_redfish_endpoints - self.mock_sat_session = mock.patch('sat.cli.nid2xname.main.SATSession').start() + self.mock_sat_session = mock.patch('sat.cli.slscheck.main.SATSession').start() self.mock_print = mock.patch('builtins.print', autospec=True).start() self.fake_args = Namespace() diff --git a/tests/cli/swap/test_ports.py b/tests/cli/swap/test_ports.py index 578ea120..9a685010 100644 --- a/tests/cli/swap/test_ports.py +++ b/tests/cli/swap/test_ports.py @@ -127,6 +127,7 @@ def setUp(self): } self.mock_get_switch = mock.patch('sat.cli.swap.ports.PortManager.get_switch', autospec=True).start() + self.mock_sat_session = mock.patch('sat.cli.swap.ports.SATSession').start() self.mock_get_switch.side_effect = lambda _, switch_xname: self.mock_switches.get(switch_xname) self.pm = PortManager() diff --git a/tests/test_session.py b/tests/test_session.py index b9e2bd8f..051e907d 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -1,7 +1,7 @@ # # MIT License # -# (C) Copyright 2023 Hewlett Packard Enterprise Development LP +# (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -40,11 +40,11 @@ def test_cray_tenant_name_is_set_properly(self): """Test that the Cray-Tenant-Name header is set from the config""" tenant_name = 'some_tenant_name' with config({'api_gateway': {'tenant_name': tenant_name}}): - s = SATSession() + s = SATSession(no_unauth_err=True) self.assertEqual(s.session.headers[TENANT_HEADER_NAME], tenant_name) def test_cray_tenant_name_not_set(self): """Test that the Cray-Tenant-Name header is not set when not configured""" with config({'api_gateway': {'tenant_name': ''}}): - s = SATSession() + s = SATSession(no_unauth_err=True) self.assertIsNone(s.session.headers.get(TENANT_HEADER_NAME))