Merge "oci: fix auth config loading"

This commit is contained in:
Zuul 2025-02-14 22:47:29 +00:00 committed by Gerrit Code Review
commit baf7ff9c18
2 changed files with 19 additions and 26 deletions

View File

@ -224,7 +224,7 @@ class RegistrySessionHelper(object):
auth = None auth = None
try: try:
with open(CONF.oci.authentication_config, 'r') as auth_file: with open(CONF.oci.authentication_config, 'r') as auth_file:
auth_dict = json.loads(auth_file) auth_dict = json.load(auth_file)
except OSError as e: except OSError as e:
LOG.error('Failed to load pre-shared authentication token ' LOG.error('Failed to load pre-shared authentication token '
'data: %s', e) 'data: %s', e)

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import builtins
import hashlib import hashlib
import io import io
import json import json
@ -866,38 +865,32 @@ class TestRegistrySessionHelper(base.TestCase):
oci_registry.RegistrySessionHelper.get_token_from_config( oci_registry.RegistrySessionHelper.get_token_from_config(
'foo.bar')) 'foo.bar'))
@mock.patch.object(builtins, 'open', autospec=True) def test_get_token_from_config(self):
def test_get_token_from_config(self, mock_open):
CONF.set_override('authentication_config', '/tmp/foo', CONF.set_override('authentication_config', '/tmp/foo',
group='oci') group='oci')
mock_file = mock.MagicMock(spec=io.BytesIO) read_data = """{"auths": {"foo.fqdn": {"auth": "secret"}}}"""
mock_file.__enter__.return_value = \ with mock.patch('builtins.open', mock.mock_open(
"""{"auths": {"foo.fqdn": {"auth": "secret"}}}""" read_data=read_data)):
mock_open.return_value = mock_file
res = oci_registry.RegistrySessionHelper.get_token_from_config( res = oci_registry.RegistrySessionHelper.get_token_from_config(
'foo.fqdn') 'foo.fqdn')
self.assertEqual('secret', res) self.assertEqual('secret', res)
@mock.patch.object(builtins, 'open', autospec=True) def test_get_token_from_config_no_match(self):
def test_get_token_from_config_no_match(self, mock_open):
CONF.set_override('authentication_config', '/tmp/foo', CONF.set_override('authentication_config', '/tmp/foo',
group='oci') group='oci')
mock_file = mock.MagicMock(spec=io.BytesIO) read_data = """{"auths": {"bar.fqdn": {}}}"""
mock_file.__enter__.return_value = \ with mock.patch('builtins.open', mock.mock_open(
"""{"auths": {"bar.fqdn": {}}}""" read_data=read_data)):
mock_open.return_value = mock_file
res = oci_registry.RegistrySessionHelper.get_token_from_config( res = oci_registry.RegistrySessionHelper.get_token_from_config(
'foo.fqdn') 'foo.fqdn')
self.assertIsNone(res) self.assertIsNone(res)
@mock.patch.object(builtins, 'open', autospec=True) def test_get_token_from_config_bad_file(self):
def test_get_token_from_config_bad_file(self, mock_open):
CONF.set_override('authentication_config', '/tmp/foo', CONF.set_override('authentication_config', '/tmp/foo',
group='oci') group='oci')
mock_file = mock.MagicMock(spec=io.BytesIO) read_data = """{"auths":..."""
mock_file.__enter__.return_value = \ with mock.patch('builtins.open', mock.mock_open(
"""{"auths":...""" read_data=read_data)):
mock_open.return_value = mock_file
res = oci_registry.RegistrySessionHelper.get_token_from_config( res = oci_registry.RegistrySessionHelper.get_token_from_config(
'foo.fqdn') 'foo.fqdn')
self.assertIsNone(res) self.assertIsNone(res)