-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Refactor Certificate Expiry Sensor #32066
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1ab2156
Cert Expiry refactor
jjlawren 6026151
Unused parameter
jjlawren 051688a
Reduce delay
jjlawren 60cf3fb
Deprecate 'name' config
jjlawren 6ea4b81
Use config entry unique_id
jjlawren 53485d9
Fix logic bugs found with tests
jjlawren bfa2ffb
Rewrite tests to use config flow core interfaces, validate created se…
jjlawren 2fe8c28
Update strings
jjlawren fc61049
Minor consistency fix
jjlawren e652e8d
Review fixes, complete test coverage
jjlawren f38181d
Move error handling to helper
jjlawren 60b4b93
Subclass exceptions
jjlawren 91a7eb9
Better tests
jjlawren 42d872e
Use first object reference
jjlawren 2f63069
Fix docstring
jjlawren 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 |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| """Const for Cert Expiry.""" | ||
|
|
||
| DOMAIN = "cert_expiry" | ||
| DEFAULT_NAME = "SSL Certificate Expiry" | ||
| DEFAULT_PORT = 443 | ||
| TIMEOUT = 10.0 |
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,26 @@ | ||
| """Errors for the cert_expiry integration.""" | ||
| from homeassistant.exceptions import HomeAssistantError | ||
|
|
||
|
|
||
| class CertExpiryException(HomeAssistantError): | ||
| """Base class for cert_expiry exceptions.""" | ||
|
|
||
|
|
||
| class TemporaryFailure(CertExpiryException): | ||
| """Temporary failure has occurred.""" | ||
|
|
||
|
|
||
| class ValidationFailure(CertExpiryException): | ||
| """Certificate validation failure has occurred.""" | ||
|
|
||
|
|
||
| class ResolveFailed(TemporaryFailure): | ||
| """Name resolution failed.""" | ||
|
|
||
|
|
||
| class ConnectionTimeout(TemporaryFailure): | ||
| """Network connection timed out.""" | ||
|
|
||
|
|
||
| class ConnectionRefused(TemporaryFailure): | ||
| """Network connection refused.""" |
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 |
|---|---|---|
| @@ -1,16 +1,44 @@ | ||
| """Helper functions for the Cert Expiry platform.""" | ||
| from datetime import datetime | ||
| import socket | ||
| import ssl | ||
|
|
||
| from .const import TIMEOUT | ||
| from .errors import ( | ||
| ConnectionRefused, | ||
| ConnectionTimeout, | ||
| ResolveFailed, | ||
| ValidationFailure, | ||
| ) | ||
|
|
||
|
|
||
| def get_cert(host, port): | ||
| """Get the ssl certificate for the host and port combination.""" | ||
| """Get the certificate for the host and port combination.""" | ||
| ctx = ssl.create_default_context() | ||
| address = (host, port) | ||
| with socket.create_connection(address, timeout=TIMEOUT) as sock: | ||
| with ctx.wrap_socket(sock, server_hostname=address[0]) as ssock: | ||
| # pylint disable: https://github.com/PyCQA/pylint/issues/3166 | ||
| cert = ssock.getpeercert() # pylint: disable=no-member | ||
| return cert | ||
|
|
||
|
|
||
| async def get_cert_time_to_expiry(hass, hostname, port): | ||
| """Return the certificate's time to expiry in days.""" | ||
| try: | ||
| cert = await hass.async_add_executor_job(get_cert, hostname, port) | ||
| except socket.gaierror: | ||
| raise ResolveFailed(f"Cannot resolve hostname: {hostname}") | ||
| except socket.timeout: | ||
| raise ConnectionTimeout(f"Connection timeout with server: {hostname}:{port}") | ||
| except ConnectionRefusedError: | ||
| raise ConnectionRefused(f"Connection refused by server: {hostname}:{port}") | ||
| except ssl.CertificateError as err: | ||
| raise ValidationFailure(err.verify_message) | ||
| except ssl.SSLError as err: | ||
| raise ValidationFailure(err.args[0]) | ||
|
|
||
| ts_seconds = ssl.cert_time_to_seconds(cert["notAfter"]) | ||
| timestamp = datetime.fromtimestamp(ts_seconds) | ||
| expiry = timestamp - datetime.today() | ||
| return expiry.days |
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.