Setup TLS
Setting up TLS is a painful becuase generating the keys and certificates. Thus, FAsterAPI
included utility function to do it for you!
generate_root_ca()
Create a root CA certificate and private key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expiration_days |
int
|
the nuber of days before expiration. Defaults to 3650. |
3650
|
common_name |
str
|
the common name. Defaults to "Root CA". |
'Root CA'
|
subject_alternative_names |
Optional[List[str]]
|
the subject alternative names. Defaults to None. |
None
|
directory |
Optional[str]
|
the directory to save the files. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Tuple[RSAPrivateKey, Certificate]
|
Tuple[rsa.RSAPrivateKey, Certificate]: returns the CA key and certifcate |
Source code in FasterAPI\utils.py
The above function create a root ca (key and certificate). If a directory is given, the generated files will be saved as PEM format. This should be your first step for server certifcate generation.
generate_key_and_csr()
Generate a private key and certificate signing request (CSR).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
common_name |
str
|
the common name of the server. |
required |
san_dns_names |
List[str]
|
the subject alternative names of the server. |
required |
directory |
Optional[str]
|
the directory to save the files. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
Certificate |
Tuple[RSAPrivateKey, CertificateSigningRequest]
|
returns the sever private key and certifcate signing request. |
Source code in FasterAPI\utils.py
The above function generate the private key and certificate signing request for your sever. If a directory is given, both files will be saved as PEM format. Note that, the signing request (CSR) is not a certifcate yet. It must be signed by a CA, which can be the one created by step one or your own.
sign_certificate()
Sign the certifcate signing request
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csr |
CertificateSigningRequest
|
the certificate signing request. |
required |
issuer_key |
Optional[RSAPrivateKey]
|
the issuer private key. |
None
|
issuer_key_path |
Optional[str]
|
the issuer private key path. |
None
|
issuer_cert |
Optional[Certificate]
|
the issuer certificate. |
None
|
issuer_cert_path |
Optional[str]
|
the issuer certificate path. |
None
|
validity_days |
int
|
the number of days before expiration. Defaults to 365. |
365
|
directory |
Optional[str]
|
the directory to save the files. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
IssuerKeyNotDefined
|
raise if both issuer_key and issuer_key_path are provided or both ot provided. |
IssuerCertNotDefined
|
raise if both issuer_cert and issuer_cert_path are provided or both ot provided. |
Returns:
Name | Type | Description |
---|---|---|
Certificate |
Certificate
|
returns the signed certificate. |
Source code in FasterAPI\utils.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
|
The above function signs the server certifcate with a CA given by your choice.