Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRAYSAT-1649: add error logging when unable to find token file path #293

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements.lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sat/cli/auth/main.py
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down Expand Up @@ -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?'):
Expand Down
21 changes: 13 additions & 8 deletions sat/session.py
Original file line number Diff line number Diff line change
@@ -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"),
Expand All @@ -26,6 +26,8 @@
"""

import logging
import os
import sys

from csm_api_client.session import UserSession

Expand All @@ -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".
"""

Expand All @@ -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)
2 changes: 2 additions & 0 deletions tests/cli/bootsys/test_bos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions tests/cli/bootsys/test_service_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
ethanholen-hpe marked this conversation as resolved.
Show resolved Hide resolved
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 = []
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions tests/cli/firmware/test_firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/cli/slscheck/test_slscheck.py
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tests/cli/swap/test_ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_session.py
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down Expand Up @@ -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))
Loading