generated from rochacbruno/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/add generation of device tls cert #64
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 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
efce2c7
on device registration post request, generate a tls certificate and s…
giusebar 164b8e3
add flag to decide whether the cert should be generated on the fly or…
giusebar 5fe5534
add requirement
giusebar ae922c6
update openapi.yaml
giusebar 536e4c4
add endpoint for devices to request generation of certificates, updat…
giusebar 5ba7b9c
fix test remove unused bool in fields passed to register device
giusebar 0324df7
fix linting
giusebar 665e20d
update openapi
giusebar 9411fc3
change device address check when generating cert
giusebar e8e1db9
improve description
giusebar 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
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,52 @@ | ||
"""API utils.""" | ||
|
||
import ipaddress | ||
from datetime import datetime, timedelta | ||
|
||
from cryptography import x509 | ||
from cryptography.hazmat.primitives import hashes, serialization | ||
from cryptography.hazmat.primitives.asymmetric import rsa | ||
from cryptography.x509.oid import NameOID | ||
|
||
|
||
def generate_tls_certificate( | ||
device_uid: str, device_ip: str | ||
) -> dict[str, str]: | ||
"""Generate a self-signed TLS certificate with device IP in SAN. | ||
|
||
device_uid: the uid of the device. | ||
device_ip: the ip of the device. | ||
""" | ||
private_key = rsa.generate_private_key( | ||
public_exponent=65537, key_size=2048 | ||
) | ||
|
||
subject = issuer = x509.Name( | ||
[ | ||
x509.NameAttribute(NameOID.COMMON_NAME, device_uid), | ||
] | ||
) | ||
|
||
san_list = [x509.IPAddress(ipaddress.ip_address(device_ip))] | ||
|
||
cert = ( | ||
x509.CertificateBuilder() | ||
.subject_name(subject) | ||
.issuer_name(issuer) | ||
.public_key(private_key.public_key()) | ||
.serial_number(x509.random_serial_number()) | ||
.not_valid_before(datetime.utcnow()) | ||
.not_valid_after(datetime.utcnow() + timedelta(days=365)) | ||
.add_extension(x509.SubjectAlternativeName(san_list), critical=False) | ||
.sign(private_key, hashes.SHA256()) | ||
) | ||
|
||
private_key_pem = private_key.private_bytes( | ||
encoding=serialization.Encoding.PEM, | ||
format=serialization.PrivateFormat.PKCS8, | ||
encryption_algorithm=serialization.NoEncryption(), | ||
).decode() | ||
|
||
cert_pem = cert.public_bytes(encoding=serialization.Encoding.PEM).decode() | ||
|
||
return {"certificate": cert_pem, "private_key": private_key_pem} |
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 |
---|---|---|
|
@@ -7,3 +7,4 @@ whitenoise | |
tzdata | ||
gunicorn | ||
pyyaml | ||
cryptography |
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.