-
Notifications
You must be signed in to change notification settings - Fork 20
Add TLS/LDAPS utilities for certificate management #246
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
Draft
shridhargadekar
wants to merge
2
commits into
SSSD:master
Choose a base branch
from
shridhargadekar:add-tls-ldaps-utilities
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| """TLS and certificate management utilities.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pytest_mh import MultihostHost, MultihostUtility | ||
| from pytest_mh.conn import ProcessResult | ||
|
|
||
| __all__ = [ | ||
| "TLSUtils", | ||
| ] | ||
|
|
||
|
|
||
| class TLSUtils(MultihostUtility[MultihostHost]): | ||
| """ | ||
| Interface for TLS/SSL certificate operations. | ||
|
|
||
| .. code-block:: python | ||
| :caption: Example usage | ||
|
|
||
| @pytest.mark.topology(KnownTopology.AD) | ||
| def test_ldaps(client: Client, ad: AD): | ||
| # Export and trust AD's root CA certificate | ||
| ca_cert = ad.host.export_root_ca_certificate() | ||
| client.tls.trust_ca_certificate(ca_cert, "ad-root-ca") | ||
|
|
||
| # Now LDAPS operations work | ||
| r = client.adcli.join( | ||
| domain=ad.host.domain, | ||
| login_user=ad.host.adminuser, | ||
| password=ad.host.adminpw, | ||
| args=["--use-ldaps"] | ||
| ) | ||
| assert r.rc == 0 | ||
| """ | ||
|
|
||
| def trust_ca_certificate( | ||
| self, | ||
| certificate_content: str, | ||
| certificate_name: str | None = None, | ||
| ) -> ProcessResult: | ||
| """ | ||
| Trust a CA certificate by installing it to system trust store. | ||
|
|
||
| :param certificate_content: PEM-formatted certificate content. | ||
| :type certificate_content: str | ||
| :param certificate_name: Optional certificate filename (without extension). | ||
| :type certificate_name: str | None | ||
| :return: Result of update-ca-trust command. | ||
| :rtype: ProcessResult | ||
| """ | ||
| if certificate_name is None: | ||
| certificate_name = "custom-ca" | ||
|
|
||
| cert_path = f"/etc/pki/ca-trust/source/anchors/{certificate_name}.crt" | ||
|
|
||
| # Write certificate to trust anchors | ||
| self.host.conn.run( | ||
| f"cat > {cert_path}", | ||
| input=certificate_content, | ||
| ) | ||
|
|
||
| # Update system trust store | ||
| return self.host.conn.run("update-ca-trust") | ||
|
shridhargadekar marked this conversation as resolved.
Outdated
|
||
|
|
||
| def trust_ca_certificate_file( | ||
| self, | ||
| certificate_path: str, | ||
| certificate_name: str | None = None, | ||
| ) -> ProcessResult: | ||
| """ | ||
| Trust a CA certificate from file path. | ||
|
|
||
| :param certificate_path: Path to certificate file on local machine. | ||
| :type certificate_path: str | ||
| :param certificate_name: Optional certificate filename (without extension). | ||
| :type certificate_name: str | None | ||
| :return: Result of update-ca-trust command. | ||
| :rtype: ProcessResult | ||
| """ | ||
| with open(certificate_path) as f: | ||
| cert_content = f.read() | ||
|
|
||
| return self.trust_ca_certificate(cert_content, certificate_name) | ||
|
|
||
| def configure_ldap_tls( | ||
| self, | ||
| *, | ||
| tls_reqcert: str = "demand", | ||
| tls_cacertdir: str | None = None, | ||
| tls_cacert: str | None = None, | ||
| ) -> None: | ||
| """ | ||
| Configure LDAP client TLS settings in /etc/openldap/ldap.conf. | ||
|
|
||
| :param tls_reqcert: Certificate verification level (never, allow, try, demand, hard). | ||
| :type tls_reqcert: str | ||
| :param tls_cacertdir: Path to CA certificate directory. | ||
| :type tls_cacertdir: str | None | ||
| :param tls_cacert: Path to specific CA certificate file. | ||
| :type tls_cacert: str | None | ||
| """ | ||
| config_lines = [f"TLS_REQCERT {tls_reqcert}"] | ||
|
|
||
| if tls_cacertdir: | ||
| config_lines.append(f"TLS_CACERTDIR {tls_cacertdir}") | ||
|
|
||
| if tls_cacert: | ||
| config_lines.append(f"TLS_CACERT {tls_cacert}") | ||
|
|
||
| config_content = "\n".join(config_lines) + "\n" | ||
| self.host.conn.run( | ||
| "cat > /etc/openldap/ldap.conf", | ||
| input=config_content, | ||
| ) | ||
|
shridhargadekar marked this conversation as resolved.
|
||
|
|
||
| def disable_certificate_verification(self) -> None: | ||
| """ | ||
| Disable TLS certificate verification (for testing only). | ||
|
|
||
| .. warning:: | ||
| This is insecure and should only be used for development/testing. | ||
| """ | ||
| self.configure_ldap_tls(tls_reqcert="never") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.