Skip to content

fix: make service connection identity optional - #656

Merged
Honny1 merged 1 commit into
containers:mainfrom
pratham8499:fix/optional-connection-identity
Oct 26, 2026
Merged

Honny1 merged 1 commit into
containers:mainfrom
pratham8499:fix/optional-connection-identity

Conversation

@pratham8499

Copy link
Copy Markdown
Contributor

Before the fix:
a) PodmanConfig().services['local'].identity
-> failed: TypeError expected str, bytes or os.PathLike object, not NoneType
b) PodmanClient(connection='local')
-> failed: TypeError expected str, bytes or os.PathLike object, not NoneType
c) PodmanClient(connection='local', identity='')
-> failed: TypeError expected str, bytes or os.PathLike object, not NoneType
d) PodmanClient(connection='production')
-> success

After the fix:
a) PodmanConfig().services['local'].identity
-> success: None
b) PodmanClient(connection='local')
-> success
c) PodmanClient(connection='local', identity='')
-> success
d) PodmanClient(connection='production')
-> success

The explicit identity= argument was ignored because the default fallback str(connection.identity) was evaluated eagerly, raising a TypeError before the fallback was even used. This fix lazily computes the fallback.

Fixes: #652
Fixes: #653
Signed-off-by: Pratham [email protected]

#654 (from_env ignoring CONTAINER_CONNECTION) is a separate fix and depends on this one; happy to follow up.

@Honny1 Honny1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I have some comments.

Comment thread podman/domain/config.py Resolved
Comment thread podman/client.py Outdated
Comment on lines +71 to +75
identity = kwargs.get("identity") or connection.identity
if identity:
api_kwargs["identity"] = str(identity)
elif "identity" in api_kwargs:
del api_kwargs["identity"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I see this part as uplicate with rows 81-85. Maybe put that into a separate function?

Comment thread podman/domain/config.py Outdated
@@ -54,10 +54,12 @@ def url(self):

@cached_property
def identity(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def identity(self):
def identity(self) -> Optional[Path]:

@Honny1 Honny1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also linter complains:

 F841 Local variable `client` is assigned to but never used
     --> podman/tests/unit/test_podmanclient.py:101:60
      |
   99 |     def test_connect_no_identity(self, mock_api_init, mock_api_close):
  100 |         with mock.patch.multiple(Path, open=self.mocked_open, exists=MagicMock(return_value=True)):
  101 |             with PodmanClient(connection="no_identity") as client:
      |                                                            ^^^^^^
  102 |                 mock_api_init.assert_called_once()
  103 |                 kwargs = mock_api_init.call_args[1]
      |
  help: Remove assignment to unused variable `client`
  
  F841 Local variable `client` is assigned to but never used
     --> podman/tests/unit/test_podmanclient.py:111:84
      |
  109 |     def test_connect_explicit_identity(self, mock_api_init, mock_api_close):
  110 |         with mock.patch.multiple(Path, open=self.mocked_open, exists=MagicMock(return_value=True)):
  111 |             with PodmanClient(connection="no_identity", identity="/custom/key") as client:
      |                                                                                    ^^^^^^
  112 |                 mock_api_init.assert_called_once()
  113 |                 kwargs = mock_api_init.call_args[1]
      |
  help: Remove assignment to unused variable `client`
  
  F841 Local variable `client` is assigned to but never used
     --> podman/tests/unit/test_podmanclient.py:120:81
      |
  118 |     def test_connect_explicit_identity_override(self, mock_api_init, mock_api_close):
  119 |         with mock.patch.multiple(Path, open=self.mocked_open, exists=MagicMock(return_value=True)):
  120 |             with PodmanClient(connection="testing", identity="/custom/key2") as client:
      |                                                                                 ^^^^^^
  121 |                 mock_api_init.assert_called_once()
  122 |                 kwargs = mock_api_init.call_args[1]
      |
  help: Remove assignment to unused variable `client`
  
  F841 Local variable `client` is assigned to but never used
     --> podman/tests/unit/test_podmanclient.py:136:36
      |
  135 |         with mock.patch('podman.client.PodmanConfig', return_value=mock_config):
  136 |             with PodmanClient() as client:
      |                                    ^^^^^^
  137 |                 mock_api_init.assert_called_once()
  138 |                 kwargs = mock_api_init.call_args[1]
      |
  help: Remove assignment to unused variable `client`
  
  E501 Line too long (147 > 100)
     --> podman/tests/unit/test_podmanclient.py:154:101
      |
  152 | … / "podman.sock")
  153 | …
  154 | …l(), expected.replace('%5C', '\\') if '\\' in client.api.base_url.geturl() else expected)
      |                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  155 | …
  156 | …or a user
      |
  
  Found 5 errors.
  No fixes available (4 hidden fixes can be enabled with the `--unsafe-fixes` option).
  
  ruff format..............................................................Failed

@pratham8499
pratham8499 force-pushed the fix/optional-connection-identity branch from 2ef5d3a to 3bdeb20 Compare September 24, 2026 23:16

@Honny1 Honny1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please sqoush commits and move unrealted changes to separate commit. Thanks!

Comment thread Makefile Outdated
unittest:
coverage run -m unittest discover -s podman/tests/unit
coverage report -m --skip-covered --fail-under=80 --omit=./podman/tests/* --omit=.tox/* --omit=/usr/lib/*
coverage report -m --skip-covered --fail-under=85 --omit=./podman/tests/* --omit=.tox/* --omit=/usr/lib/*

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those changes are unrelated to what is fixed. Please put them into a separate commit.

@Honny1

Honny1 commented Sep 25, 2026

Copy link
Copy Markdown
Member

I have enabled CI. Please check if it will pass.

@Honny1 Honny1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didnt read last chnges fully but. Linter is complaining:

  +++ b/podman/tests/unit/test_podmanclient.py
  @@ -101,7 +101,9 @@ class PodmanClientTestCase(unittest.TestCase):
               with PodmanClient(connection="no_identity"):
                   mock_api_init.assert_called_once()
                   kwargs = mock_api_init.call_args[1]
  -                self.assertEqual(kwargs["base_url"], "ssh://root@localhost:22/run/podman/podman.sock")
  +                self.assertEqual(
  +                    kwargs["base_url"], "ssh://root@localhost:22/run/podman/podman.sock"
  +                )
                   self.assertNotIn("identity", kwargs)
   
       @mock.patch('podman.client.APIClient.close')
  @@ -136,7 +138,9 @@ class PodmanClientTestCase(unittest.TestCase):
               with PodmanClient():
                   mock_api_init.assert_called_once()
                   kwargs = mock_api_init.call_args[1]
  -                self.assertEqual(kwargs["base_url"], "http+ssh://root@localhost:22/run/podman/podman.sock")
  +                self.assertEqual(
  +                    kwargs["base_url"], "http+ssh://root@localhost:22/run/podman/podman.sock"
  +                )
                   self.assertNotIn("identity", kwargs)
   
       def test_connect_404(self):
  Error: Process completed with exit code 1.

@Honny1 Honny1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please link fixed issues in the commit msg with Fixes: #NUMBER_OF_ISSUE. Thanks!

@pratham8499

Copy link
Copy Markdown
Contributor Author

@Honny1 I have addressed all the feedback and force-pushed the changes:

Fixed the unused variable and line-length linter errors.
Removed the unrelated Makefile changes.
Added the Fixes: #652 and Fixes: #653 tags to the commit message.
Could you please approve the workflows to run the CI again? Also, the testing-farm checks that failed previously seem unrelated to these changes. Thanks

@Honny1

Honny1 commented Oct 6, 2026

Copy link
Copy Markdown
Member

I think this line from commit msg: https://github.com/containers/podman-py/issues/654 (from_env ignoring CONTAINER_CONNECTION) is a separate fix and depends on this one; happy to follow up. Is not needed.

rjmcgee53192 added a commit to rjmcgee53192/podman-py that referenced this pull request Oct 13, 2026
Matches the podman CLI, which checks CONTAINER_CONNECTION before CONTAINER_HOST. An explicit CONTAINER_HOST URL no longer silently wins, so podman-py and the CLI always resolve to the same host.

Also notes the dependency on PR containers#656 for identity-less connections.

Signed-off-by: rjmcgee53192 <[email protected]>
@Honny1

Honny1 commented Oct 20, 2026

Copy link
Copy Markdown
Member

@pratham8499 Any updates on this? Please rebase on upstream main. CI should be happy.

@rjmcgee53192

Copy link
Copy Markdown
Contributor

Hi — I'm the author of #679, which was just closed as a duplicate of this PR, so I read this diff closely. We landed on the same bug from opposite ends.

The core approach matches what I found independently: return None from ServiceConnection.identity when nothing is configured, check both the identity and Identity spellings, and don't stringify the value at the client call sites. Your _set_identity helper also handles a case mine didn't — an explicitly passed identity='' — and computing the fallback lazily fixes the eager str(connection.identity) default that raised before the fallback could even be used. That part is better than my version.

Two small things, in that spirit:

  1. The duplication Honny1 mentioned back in September is still in identity — the two branches differ only in the key name. A short loop over ("identity", "Identity") would collapse them and make the precedence between the two spellings explicit.
  2. In test_config.py, the no_identity service was added to the JSON-style fixture only. A unix:// connection with no identity in the TOML variant would pin the same behavior for the other config format — mine hit the crash through active_service as well, and it's good to see that path covered here.

Otherwise this looks right to me: the client tests cover no-identity, explicit identity, override, and active_service. Glad to see this land — it also unblocks my #673, which depends on this fix.

— Ryan

@Honny1 Honny1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nonblocking comment.Otherwise,e LGTM

Comment thread podman/domain/config.py Outdated
Comment on lines +58 to +64
if "identity" in self.attrs:
val = self.attrs["identity"]
return Path(val) if val else None
if "Identity" in self.attrs:
val = self.attrs["Identity"]
return Path(val) if val else None
return None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if "identity" in self.attrs:
val = self.attrs["identity"]
return Path(val) if val else None
if "Identity" in self.attrs:
val = self.attrs["Identity"]
return Path(val) if val else None
return None
for key in ("identity", "Identity"):
if key in self.attrs:
val = self.attrs[key]
return Path(val) if val else None
return None

@TomSweeneyRedHat

Copy link
Copy Markdown
Member

LGTM
especially after applying @Honny1 's suggestion

@pratham8499
pratham8499 force-pushed the fix/optional-connection-identity branch from 2d96dca to 4af4364 Compare October 23, 2026 09:53
@pratham8499

pratham8499 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Applied @Honny1's suggestion to loop over ('identity', 'Identity')\ to eliminate duplication and amended the commit.

@Honny1 Honny1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Honny1

Honny1 commented Oct 23, 2026

Copy link
Copy Markdown
Member

@pratham8499, can you please rebase on upstream main? Thanks! To make CI happy.

Before the fix:
a) PodmanConfig().services['local'].identity
  -> failed: TypeError expected str, bytes or os.PathLike object, not NoneType
b) PodmanClient(connection='local')
  -> failed: TypeError expected str, bytes or os.PathLike object, not NoneType
c) PodmanClient(connection='local', identity='<dummy>')
  -> failed: TypeError expected str, bytes or os.PathLike object, not NoneType
d) PodmanClient(connection='production')
  -> success

After the fix:
a) PodmanConfig().services['local'].identity
  -> success: None
b) PodmanClient(connection='local')
  -> success
c) PodmanClient(connection='local', identity='<dummy>')
  -> success
d) PodmanClient(connection='production')
  -> success

The explicit `identity=` argument was ignored because the default fallback `str(connection.identity)` was evaluated eagerly, raising a TypeError before the fallback was even used. This fix lazily computes the fallback.

Fixes: containers#652
Fixes: containers#653
Signed-off-by: Pratham <[email protected]>
@pratham8499
pratham8499 force-pushed the fix/optional-connection-identity branch from 4af4364 to 7af28b2 Compare October 23, 2026 17:26
@pratham8499

Copy link
Copy Markdown
Contributor Author

@Honny1 Rebased on upstream main and resolved the conflict. Ready for CI!

@TomSweeneyRedHat TomSweeneyRedHat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Honny1
Honny1 merged commit 3e7dffb into containers:main Oct 26, 2026
23 checks passed

This branch was previously deployed

1 inactive deployment
build — 7af28b24 Deployed Oct 26, 2026 by pratham8499 via Test Build Python distribution 📦 #339
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants