Add support for multi-token management in SmartCardUtils#239
Add support for multi-token management in SmartCardUtils#239krishnavema wants to merge 1 commit intoSSSD:masterfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly extends the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds support for managing multiple smart card tokens by introducing initialize_additional_token to create new tokens without wiping existing ones, and register_for_p11_child to make them visible to p11_child. The add_cert and add_key methods are also updated to allow targeting a specific token. The changes are logical, but I have a couple of suggestions to improve maintainability and security. I've identified some code duplication that could be refactored and an instance of overly permissive file permissions that should be tightened.
| args: CLIBuilderArgs = { | ||
| "label": (self.cli.option.VALUE, label), | ||
| "free": (self.cli.option.SWITCH, True), | ||
| "so-pin": (self.cli.option.VALUE, so_pin), | ||
| "pin": (self.cli.option.VALUE, user_pin), | ||
| } | ||
| self.host.conn.run( | ||
| self.cli.command("softhsm2-util --init-token", args), env={"SOFTHSM2_CONF": self.SOFTHSM2_CONF_PATH} | ||
| ) |
There was a problem hiding this comment.
The logic for building arguments and calling softhsm2-util --init-token in this new method is identical to the one in the initialize_card method (lines 69-77). This code duplication should be avoided by extracting the common logic into a private helper method. This will improve code maintainability.
There was a problem hiding this comment.
I could be missing something here but, I don't see any code duplication. I think this can be ignored unless someone sees the duplicate code.
| f"> /etc/systemd/system/sssd.service.d/softhsm.conf" | ||
| ) | ||
| self.host.conn.run("systemctl daemon-reload") | ||
| self.host.conn.run("chmod -R o+rX /opt/test_ca/") |
There was a problem hiding this comment.
The command chmod -R o+rX /opt/test_ca/ grants read permissions to all files under /opt/test_ca/ to all users on the system, which is overly permissive. While this is a test environment, it's better to apply the principle of least privilege. A more precise command would only grant the necessary permissions, for example by only allowing traversal of the directory and read access to the configuration file.
| self.host.conn.run("chmod -R o+rX /opt/test_ca/") | |
| self.host.conn.run(f"chmod o+x /opt/test_ca && chmod o+r {conf}") |
spoore1
left a comment
There was a problem hiding this comment.
I'll have more comments in the test PR but this is a start.
bcdf5e8 to
4823cbb
Compare
spoore1
left a comment
There was a problem hiding this comment.
I have a few questions and suggestions. Mostly changing some of the shell commands to fs calls for the file edits/manipulations.
| rc, _, _, _ = self.vfido_passkey_with_output(username=username, pin=pin, command=command) | ||
| return rc == 0 | ||
|
|
||
| def smartcard_with_su_output(self, username: str, pin: str, *, num_certs: int = 1) -> ProcessResult: |
There was a problem hiding this comment.
I would rename this to smartcard_with_output to better match the other functions in the class. And since we'll call it with client.auth.su.smartcard_with_output I don't think we need the "su" in the method name.
| for attempt in range(USER_RESOLVABLE_ATTEMPTS): | ||
| time.sleep(USER_RESOLVABLE_INTERVAL_S) | ||
| check = self.host.conn.run(f"getent passwd {username}", raise_on_error=False) | ||
| if check.rc == 0: | ||
| break | ||
| if attempt == USER_RESOLVABLE_CACHE_EXPIRY_ATTEMPT: | ||
| self.host.conn.run("sss_cache -E", raise_on_error=False) | ||
| else: | ||
| raise AssertionError( | ||
| f"User '{username}' was not resolvable by SSSD after {USER_RESOLVABLE_ATTEMPTS} attempts" | ||
| ) |
There was a problem hiding this comment.
Did you run into issues resolving the users when you wrote this? We haven't usually needed this type of check in the past. I don't have anything against the extra checks but, I'm wondering if they were added because of a particular issue we might need to fix elsewhere.
| f"User '{username}' was not resolvable by SSSD after {USER_RESOLVABLE_ATTEMPTS} attempts" | ||
| ) | ||
|
|
||
| su_input = f"1\n{pin}" if num_certs > 1 else pin |
There was a problem hiding this comment.
While this may work as expected with multiple tokens (I'm not 100% sure ATM), won't this force selection of the first certificate from a token that has multiple certificates? We might want an optional argument for certificate selection with 1 being the default.
| raise_on_error=False, | ||
| ) | ||
|
|
||
| def smartcard_with_su(self, username: str, pin: str, *, num_certs: int = 1) -> bool: |
There was a problem hiding this comment.
Like the other method, I would suggest renaming this one to just smartcard to closer match the other methods in the class.
| args: CLIBuilderArgs = { | ||
| "label": (self.cli.option.VALUE, label), | ||
| "free": (self.cli.option.SWITCH, True), | ||
| "so-pin": (self.cli.option.VALUE, so_pin), | ||
| "pin": (self.cli.option.VALUE, user_pin), | ||
| } | ||
| self.host.conn.run( | ||
| self.cli.command("softhsm2-util --init-token", args), env={"SOFTHSM2_CONF": self.SOFTHSM2_CONF_PATH} | ||
| ) |
There was a problem hiding this comment.
I could be missing something here but, I don't see any code duplication. I think this can be ignored unless someone sees the duplicate code.
| smartcard.host.conn.run(f"grep -q 'slots.removable' {conf} || echo 'slots.removable = true' >> {conf}") | ||
| smartcard.host.conn.run(f"cp {conf} /etc/softhsm2.conf") |
There was a problem hiding this comment.
I believe we want to try to use python native solutions when possible over these types of file edits. I think you can do something like:
softhsm_conf = smartcard.fs.read(conf)
if "slots.removable" not in softhsm_conf:
smartcard.fs.append(conf, "\nslots.removable = true")
If you want to work in a temp file before copying to the real one, you could also make use of the fs.copy() method.
|
|
||
| smartcard.host.conn.run(f"grep -q 'slots.removable' {conf} || echo 'slots.removable = true' >> {conf}") | ||
| smartcard.host.conn.run(f"cp {conf} /etc/softhsm2.conf") | ||
| smartcard.host.conn.run(f'echo "module: {module}" > /etc/pkcs11/modules/softhsm2.module') |
There was a problem hiding this comment.
Same thing here. Use the fs methods where possible.
| smartcard.host.conn.run( | ||
| f'printf "[Service]\\nEnvironment=SOFTHSM2_CONF={conf}\\n" ' | ||
| f"> /etc/systemd/system/sssd.service.d/softhsm.conf" |
There was a problem hiding this comment.
I think you can do this one with a simple fs.write()
| f'printf "[Service]\\nEnvironment=SOFTHSM2_CONF={conf}\\n" ' | ||
| f"> /etc/systemd/system/sssd.service.d/softhsm.conf" | ||
| ) | ||
| smartcard.host.conn.run("systemctl daemon-reload") |
There was a problem hiding this comment.
Use smartcard.svc.reload_daemon() here I think.
| smartcard.host.conn.run("chmod -R o+rX /opt/test_ca/") | ||
| smartcard.host.conn.run(f"chown -R sssd:sssd {token_storage}/ && chmod -R 770 {token_storage}/") |
There was a problem hiding this comment.
I think you should be able to use fs.chown() and fs.chmod() here.
No description provided.