swarmauri_certs_azure 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- swarmauri_certs_azure-0.3.0/LICENSE +201 -0
- swarmauri_certs_azure-0.3.0/PKG-INFO +155 -0
- swarmauri_certs_azure-0.3.0/README.md +122 -0
- swarmauri_certs_azure-0.3.0/pyproject.toml +82 -0
- swarmauri_certs_azure-0.3.0/swarmauri_certs_azure/__init__.py +0 -0
- swarmauri_certs_azure-0.3.0/swarmauri_certs_azure/certs/AzureKeyVaultCertService.py +101 -0
- swarmauri_certs_azure-0.3.0/swarmauri_certs_azure/certs/__init__.py +0 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [2025] [Jacob Stewart @ Swarmauri]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: swarmauri_certs_azure
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Azure Key Vault certificate utilities for the Swarmauri ecosystem
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: swarmauri,certs,azure,key,vault,certificate,utilities,ecosystem
|
|
8
|
+
Author: Jacob Stewart
|
|
9
|
+
Author-email: jacob@swarmauri.com
|
|
10
|
+
Requires-Python: >=3.10,<3.13
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Natural Language :: English
|
|
16
|
+
Classifier: Development Status :: 3 - Alpha
|
|
17
|
+
Classifier: Intended Audience :: Developers
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
19
|
+
Provides-Extra: azure
|
|
20
|
+
Provides-Extra: crypto
|
|
21
|
+
Requires-Dist: asn1crypto
|
|
22
|
+
Requires-Dist: asn1crypto ; extra == "crypto"
|
|
23
|
+
Requires-Dist: azure-identity
|
|
24
|
+
Requires-Dist: azure-identity ; extra == "azure"
|
|
25
|
+
Requires-Dist: azure-keyvault-keys
|
|
26
|
+
Requires-Dist: azure-keyvault-keys ; extra == "azure"
|
|
27
|
+
Requires-Dist: cryptography
|
|
28
|
+
Requires-Dist: cryptography ; extra == "crypto"
|
|
29
|
+
Requires-Dist: swarmauri_base
|
|
30
|
+
Requires-Dist: swarmauri_core
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+

|
|
34
|
+
|
|
35
|
+
<p align="center">
|
|
36
|
+
<a href="https://pypi.org/project/swarmauri_certs_azure/">
|
|
37
|
+
<img src="https://img.shields.io/pypi/dm/swarmauri_certs_azure" alt="PyPI - Downloads"/></a>
|
|
38
|
+
<a href="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_certs_azure/">
|
|
39
|
+
<img alt="Hits" src="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_certs_azure.svg"/></a>
|
|
40
|
+
<a href="https://pypi.org/project/swarmauri_certs_azure/">
|
|
41
|
+
<img src="https://img.shields.io/pypi/pyversions/swarmauri_certs_azure" alt="PyPI - Python Version"/></a>
|
|
42
|
+
<a href="https://pypi.org/project/swarmauri_certs_azure/">
|
|
43
|
+
<img src="https://img.shields.io/pypi/l/swarmauri_certs_azure" alt="PyPI - License"/></a>
|
|
44
|
+
<a href="https://pypi.org/project/swarmauri_certs_azure/">
|
|
45
|
+
<img src="https://img.shields.io/pypi/v/swarmauri_certs_azure?label=swarmauri_certs_azure&color=green" alt="PyPI - swarmauri_certs_azure"/></a>
|
|
46
|
+
|
|
47
|
+
</p>
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
# swarmauri_certs_azure
|
|
52
|
+
|
|
53
|
+
Community-maintained utilities for working with X.509 certificates via Azure Key Vault.
|
|
54
|
+
|
|
55
|
+
## Features
|
|
56
|
+
- `AzureKeyVaultCertService` adapter that plugs into Swarmauri's certificate service architecture.
|
|
57
|
+
- RFC-aligned helpers for serial number generation (RFC 5280), PEM formatting (RFC 7468), and PKCS#10 CSR creation (RFC 2986).
|
|
58
|
+
- Native `DefaultAzureCredential` support so you can reuse the same authentication chain across tools.
|
|
59
|
+
- Works with RSA 2048-bit key material—perfect for Key Vault-backed certificate issuance flows.
|
|
60
|
+
|
|
61
|
+
## Prerequisites
|
|
62
|
+
- Python 3.10 or newer.
|
|
63
|
+
- An Azure Key Vault enabled for the Certificates and Keys resource providers.
|
|
64
|
+
- Exportable RSA key material (PEM) or an Azure Key Vault key that can be exported for CSR signing.
|
|
65
|
+
- Azure credentials configured for `DefaultAzureCredential` (e.g., `AZURE_CLIENT_ID`, managed identity, or CLI login).
|
|
66
|
+
|
|
67
|
+
## Installation
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
# pip
|
|
71
|
+
pip install swarmauri_certs_azure
|
|
72
|
+
|
|
73
|
+
# poetry
|
|
74
|
+
poetry add swarmauri_certs_azure
|
|
75
|
+
|
|
76
|
+
# uv (pyproject-based projects)
|
|
77
|
+
uv add swarmauri_certs_azure
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Quickstart
|
|
81
|
+
|
|
82
|
+
Generate a CSR using `AzureKeyVaultCertService` and store it for downstream issuance:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
import asyncio
|
|
86
|
+
from pathlib import Path
|
|
87
|
+
|
|
88
|
+
from azure.identity import DefaultAzureCredential
|
|
89
|
+
|
|
90
|
+
from swarmauri_certs_azure.certs import AzureKeyVaultCertService
|
|
91
|
+
from swarmauri_core.crypto.types import KeyRef
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
async def main() -> None:
|
|
95
|
+
service = AzureKeyVaultCertService(
|
|
96
|
+
vault_url="https://example-vault.vault.azure.net/",
|
|
97
|
+
credential=DefaultAzureCredential(),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
key_ref = KeyRef(material=Path("app-private-key.pem").read_bytes())
|
|
101
|
+
csr_bytes = await service.create_csr(
|
|
102
|
+
key=key_ref,
|
|
103
|
+
subject={"CN": "app.example.com"},
|
|
104
|
+
san={"dns": ["app.example.com", "www.app.example.com"]},
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
Path("app.csr").write_bytes(csr_bytes)
|
|
108
|
+
print("CSR written to app.csr")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
if __name__ == "__main__":
|
|
112
|
+
asyncio.run(main())
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Integrate with Azure Certificate Workflows
|
|
116
|
+
|
|
117
|
+
After generating the CSR, import it into Azure Key Vault or an external CA:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from pathlib import Path
|
|
121
|
+
|
|
122
|
+
from azure.identity import DefaultAzureCredential
|
|
123
|
+
from azure.keyvault.certificates import CertificateClient
|
|
124
|
+
|
|
125
|
+
vault_url = "https://example-vault.vault.azure.net/"
|
|
126
|
+
client = CertificateClient(vault_url=vault_url, credential=DefaultAzureCredential())
|
|
127
|
+
|
|
128
|
+
csr_bytes = Path("app.csr").read_bytes()
|
|
129
|
+
|
|
130
|
+
poller = client.begin_create_certificate(
|
|
131
|
+
certificate_name="app-cert",
|
|
132
|
+
policy={
|
|
133
|
+
"contentType": "application/x-pem-file",
|
|
134
|
+
"csr": csr_bytes,
|
|
135
|
+
},
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
certificate = poller.result()
|
|
139
|
+
print("Certificate operation state:", certificate.properties.x509_thumbprint)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
For external issuance, submit `app.csr` to your CA, then store the returned certificate chain back in Key Vault using `set_certificate_contacts` and `import_certificate`.
|
|
143
|
+
|
|
144
|
+
## Testing
|
|
145
|
+
Run tests with:
|
|
146
|
+
```bash
|
|
147
|
+
uv run --package swarmauri_certs_azure --directory community pytest
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Best Practices
|
|
151
|
+
- Prefer managed identities or workload identity federation over client secrets in production.
|
|
152
|
+
- Scope Key Vault permissions tightly (`get`, `sign`, `unwrapKey`) for the keys used by this service.
|
|
153
|
+
- Rotate keys and certificates ahead of expiry; the helper functions simplify CSR generation for renewals.
|
|
154
|
+
- Persist generated CSRs and issued certificates securely to aid in auditing and disaster recovery.
|
|
155
|
+
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://pypi.org/project/swarmauri_certs_azure/">
|
|
5
|
+
<img src="https://img.shields.io/pypi/dm/swarmauri_certs_azure" alt="PyPI - Downloads"/></a>
|
|
6
|
+
<a href="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_certs_azure/">
|
|
7
|
+
<img alt="Hits" src="https://hits.sh/github.com/swarmauri/swarmauri-sdk/tree/master/pkgs/community/swarmauri_certs_azure.svg"/></a>
|
|
8
|
+
<a href="https://pypi.org/project/swarmauri_certs_azure/">
|
|
9
|
+
<img src="https://img.shields.io/pypi/pyversions/swarmauri_certs_azure" alt="PyPI - Python Version"/></a>
|
|
10
|
+
<a href="https://pypi.org/project/swarmauri_certs_azure/">
|
|
11
|
+
<img src="https://img.shields.io/pypi/l/swarmauri_certs_azure" alt="PyPI - License"/></a>
|
|
12
|
+
<a href="https://pypi.org/project/swarmauri_certs_azure/">
|
|
13
|
+
<img src="https://img.shields.io/pypi/v/swarmauri_certs_azure?label=swarmauri_certs_azure&color=green" alt="PyPI - swarmauri_certs_azure"/></a>
|
|
14
|
+
|
|
15
|
+
</p>
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
# swarmauri_certs_azure
|
|
20
|
+
|
|
21
|
+
Community-maintained utilities for working with X.509 certificates via Azure Key Vault.
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
- `AzureKeyVaultCertService` adapter that plugs into Swarmauri's certificate service architecture.
|
|
25
|
+
- RFC-aligned helpers for serial number generation (RFC 5280), PEM formatting (RFC 7468), and PKCS#10 CSR creation (RFC 2986).
|
|
26
|
+
- Native `DefaultAzureCredential` support so you can reuse the same authentication chain across tools.
|
|
27
|
+
- Works with RSA 2048-bit key material—perfect for Key Vault-backed certificate issuance flows.
|
|
28
|
+
|
|
29
|
+
## Prerequisites
|
|
30
|
+
- Python 3.10 or newer.
|
|
31
|
+
- An Azure Key Vault enabled for the Certificates and Keys resource providers.
|
|
32
|
+
- Exportable RSA key material (PEM) or an Azure Key Vault key that can be exported for CSR signing.
|
|
33
|
+
- Azure credentials configured for `DefaultAzureCredential` (e.g., `AZURE_CLIENT_ID`, managed identity, or CLI login).
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# pip
|
|
39
|
+
pip install swarmauri_certs_azure
|
|
40
|
+
|
|
41
|
+
# poetry
|
|
42
|
+
poetry add swarmauri_certs_azure
|
|
43
|
+
|
|
44
|
+
# uv (pyproject-based projects)
|
|
45
|
+
uv add swarmauri_certs_azure
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Quickstart
|
|
49
|
+
|
|
50
|
+
Generate a CSR using `AzureKeyVaultCertService` and store it for downstream issuance:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
import asyncio
|
|
54
|
+
from pathlib import Path
|
|
55
|
+
|
|
56
|
+
from azure.identity import DefaultAzureCredential
|
|
57
|
+
|
|
58
|
+
from swarmauri_certs_azure.certs import AzureKeyVaultCertService
|
|
59
|
+
from swarmauri_core.crypto.types import KeyRef
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
async def main() -> None:
|
|
63
|
+
service = AzureKeyVaultCertService(
|
|
64
|
+
vault_url="https://example-vault.vault.azure.net/",
|
|
65
|
+
credential=DefaultAzureCredential(),
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
key_ref = KeyRef(material=Path("app-private-key.pem").read_bytes())
|
|
69
|
+
csr_bytes = await service.create_csr(
|
|
70
|
+
key=key_ref,
|
|
71
|
+
subject={"CN": "app.example.com"},
|
|
72
|
+
san={"dns": ["app.example.com", "www.app.example.com"]},
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
Path("app.csr").write_bytes(csr_bytes)
|
|
76
|
+
print("CSR written to app.csr")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
if __name__ == "__main__":
|
|
80
|
+
asyncio.run(main())
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Integrate with Azure Certificate Workflows
|
|
84
|
+
|
|
85
|
+
After generating the CSR, import it into Azure Key Vault or an external CA:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from pathlib import Path
|
|
89
|
+
|
|
90
|
+
from azure.identity import DefaultAzureCredential
|
|
91
|
+
from azure.keyvault.certificates import CertificateClient
|
|
92
|
+
|
|
93
|
+
vault_url = "https://example-vault.vault.azure.net/"
|
|
94
|
+
client = CertificateClient(vault_url=vault_url, credential=DefaultAzureCredential())
|
|
95
|
+
|
|
96
|
+
csr_bytes = Path("app.csr").read_bytes()
|
|
97
|
+
|
|
98
|
+
poller = client.begin_create_certificate(
|
|
99
|
+
certificate_name="app-cert",
|
|
100
|
+
policy={
|
|
101
|
+
"contentType": "application/x-pem-file",
|
|
102
|
+
"csr": csr_bytes,
|
|
103
|
+
},
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
certificate = poller.result()
|
|
107
|
+
print("Certificate operation state:", certificate.properties.x509_thumbprint)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
For external issuance, submit `app.csr` to your CA, then store the returned certificate chain back in Key Vault using `set_certificate_contacts` and `import_certificate`.
|
|
111
|
+
|
|
112
|
+
## Testing
|
|
113
|
+
Run tests with:
|
|
114
|
+
```bash
|
|
115
|
+
uv run --package swarmauri_certs_azure --directory community pytest
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Best Practices
|
|
119
|
+
- Prefer managed identities or workload identity federation over client secrets in production.
|
|
120
|
+
- Scope Key Vault permissions tightly (`get`, `sign`, `unwrapKey`) for the keys used by this service.
|
|
121
|
+
- Rotate keys and certificates ahead of expiry; the helper functions simplify CSR generation for renewals.
|
|
122
|
+
- Persist generated CSRs and issued certificates securely to aid in auditing and disaster recovery.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "swarmauri_certs_azure"
|
|
3
|
+
version = "0.3.0"
|
|
4
|
+
description = "Azure Key Vault certificate utilities for the Swarmauri ecosystem"
|
|
5
|
+
license = "Apache-2.0"
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
repository = "http://github.com/swarmauri/swarmauri-sdk"
|
|
8
|
+
requires-python = ">=3.10,<3.13"
|
|
9
|
+
classifiers = [
|
|
10
|
+
"License :: OSI Approved :: Apache Software License",
|
|
11
|
+
"Programming Language :: Python :: 3.10",
|
|
12
|
+
"Programming Language :: Python :: 3.11",
|
|
13
|
+
"Programming Language :: Python :: 3.12",
|
|
14
|
+
"Natural Language :: English",
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Topic :: Software Development :: Libraries :: Application Frameworks",
|
|
18
|
+
]
|
|
19
|
+
authors = [{ name = "Jacob Stewart", email = "jacob@swarmauri.com" }]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"swarmauri_core",
|
|
22
|
+
"swarmauri_base",
|
|
23
|
+
"azure-identity",
|
|
24
|
+
"azure-keyvault-keys",
|
|
25
|
+
"cryptography",
|
|
26
|
+
"asn1crypto",
|
|
27
|
+
]
|
|
28
|
+
keywords = [
|
|
29
|
+
"swarmauri",
|
|
30
|
+
"certs",
|
|
31
|
+
"azure",
|
|
32
|
+
"key",
|
|
33
|
+
"vault",
|
|
34
|
+
"certificate",
|
|
35
|
+
"utilities",
|
|
36
|
+
"ecosystem",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[project.optional-dependencies]
|
|
40
|
+
azure = ["azure-identity", "azure-keyvault-keys"]
|
|
41
|
+
crypto = ["cryptography", "asn1crypto"]
|
|
42
|
+
|
|
43
|
+
[tool.uv.sources]
|
|
44
|
+
swarmauri_core = { workspace = true }
|
|
45
|
+
swarmauri_base = { workspace = true }
|
|
46
|
+
|
|
47
|
+
[tool.pytest.ini_options]
|
|
48
|
+
markers = [
|
|
49
|
+
"test: standard test",
|
|
50
|
+
"unit: Unit tests",
|
|
51
|
+
"i9n: Integration tests",
|
|
52
|
+
"r8n: Regression tests",
|
|
53
|
+
"timeout: mark test to timeout after X seconds",
|
|
54
|
+
"xpass: Expected passes",
|
|
55
|
+
"xfail: Expected failures",
|
|
56
|
+
"acceptance: Acceptance tests",
|
|
57
|
+
"perf: Performance tests that measure execution time and resource usage",
|
|
58
|
+
]
|
|
59
|
+
timeout = 300
|
|
60
|
+
log_cli = true
|
|
61
|
+
log_cli_level = "INFO"
|
|
62
|
+
log_cli_format = "%(asctime)s [%(levelname)s] %(message)s"
|
|
63
|
+
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
|
|
64
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
65
|
+
|
|
66
|
+
[dependency-groups]
|
|
67
|
+
dev = [
|
|
68
|
+
"pytest>=8.0",
|
|
69
|
+
"pytest-asyncio>=0.24.0",
|
|
70
|
+
"pytest-xdist>=3.6.1",
|
|
71
|
+
"pytest-json-report>=1.5.0",
|
|
72
|
+
"python-dotenv",
|
|
73
|
+
"requests>=2.32.3",
|
|
74
|
+
"flake8>=7.0",
|
|
75
|
+
"pytest-timeout>=2.3.1",
|
|
76
|
+
"ruff>=0.9.9",
|
|
77
|
+
"pytest-benchmark>=4.0.0",
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
[build-system]
|
|
81
|
+
requires = ["poetry-core>=1.0.0"]
|
|
82
|
+
build-backend = "poetry.core.masonry.api"
|
|
File without changes
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import datetime as dt
|
|
5
|
+
import random
|
|
6
|
+
from typing import Any, Dict, Iterable, Literal, Mapping, Optional
|
|
7
|
+
|
|
8
|
+
from azure.identity import DefaultAzureCredential
|
|
9
|
+
from azure.keyvault.keys import KeyClient
|
|
10
|
+
from cryptography import x509
|
|
11
|
+
from cryptography.hazmat.primitives import hashes, serialization
|
|
12
|
+
from cryptography.x509.oid import NameOID
|
|
13
|
+
|
|
14
|
+
from swarmauri_base.certs.CertServiceBase import CertServiceBase
|
|
15
|
+
from swarmauri_core.certs.ICertService import AltNameSpec, SubjectSpec
|
|
16
|
+
from swarmauri_core.crypto.types import KeyRef
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# ---------------------------------------------------------------------------
|
|
20
|
+
# Helpers
|
|
21
|
+
# ---------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _now_utc() -> dt.datetime:
|
|
25
|
+
"""Return current UTC time."""
|
|
26
|
+
return dt.datetime.utcnow().replace(tzinfo=dt.timezone.utc)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _serial_or_random(serial: Optional[int]) -> int:
|
|
30
|
+
"""Return provided serial number or a random 128-bit value per RFC 5280."""
|
|
31
|
+
if serial is not None:
|
|
32
|
+
return int(serial)
|
|
33
|
+
return random.SystemRandom().getrandbits(128)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _pem_cert(der_bytes: bytes) -> bytes:
|
|
37
|
+
"""Encode DER certificate bytes into PEM as described in RFC 7468."""
|
|
38
|
+
b64 = base64.encodebytes(der_bytes).decode("ascii").replace("\n", "")
|
|
39
|
+
lines = [b64[i : i + 64] for i in range(0, len(b64), 64)]
|
|
40
|
+
return (
|
|
41
|
+
"-----BEGIN CERTIFICATE-----\n"
|
|
42
|
+
+ "\n".join(lines)
|
|
43
|
+
+ "\n-----END CERTIFICATE-----\n"
|
|
44
|
+
).encode("ascii")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _to_x509_name(spec: SubjectSpec) -> x509.Name:
|
|
48
|
+
rdns = []
|
|
49
|
+
if cn := spec.get("CN"):
|
|
50
|
+
rdns.append(x509.NameAttribute(NameOID.COMMON_NAME, cn))
|
|
51
|
+
return x509.Name(rdns)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# ---------------------------------------------------------------------------
|
|
55
|
+
# Service
|
|
56
|
+
# ---------------------------------------------------------------------------
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class AzureKeyVaultCertService(CertServiceBase):
|
|
60
|
+
"""Minimal Azure Key Vault backed certificate service."""
|
|
61
|
+
|
|
62
|
+
type: Literal["AzureKeyVaultCertService"] = "AzureKeyVaultCertService"
|
|
63
|
+
|
|
64
|
+
def __init__(self, vault_url: str, *, credential: Optional[Any] = None) -> None:
|
|
65
|
+
super().__init__()
|
|
66
|
+
self._cred = credential or DefaultAzureCredential()
|
|
67
|
+
self._keys = KeyClient(vault_url=vault_url, credential=self._cred)
|
|
68
|
+
|
|
69
|
+
def supports(self) -> Mapping[str, Iterable[str]]:
|
|
70
|
+
return {
|
|
71
|
+
"key_algs": ("RSA-2048",),
|
|
72
|
+
"sig_algs": ("RSA-SHA256",),
|
|
73
|
+
"features": ("csr",),
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async def create_csr(
|
|
77
|
+
self,
|
|
78
|
+
key: KeyRef,
|
|
79
|
+
subject: SubjectSpec,
|
|
80
|
+
*,
|
|
81
|
+
san: Optional[AltNameSpec] = None,
|
|
82
|
+
extensions: Optional[Dict[str, Any]] = None,
|
|
83
|
+
sig_alg: Optional[str] = None,
|
|
84
|
+
challenge_password: Optional[str] = None,
|
|
85
|
+
output_der: bool = False,
|
|
86
|
+
opts: Optional[Dict[str, Any]] = None,
|
|
87
|
+
) -> bytes:
|
|
88
|
+
"""Create a PKCS#10 CSR using the supplied key material."""
|
|
89
|
+
pem_priv = key.material
|
|
90
|
+
if not pem_priv:
|
|
91
|
+
raise NotImplementedError(
|
|
92
|
+
"Non-exportable keys are not supported in this simplified service."
|
|
93
|
+
)
|
|
94
|
+
subject_name = _to_x509_name(subject)
|
|
95
|
+
builder = x509.CertificateSigningRequestBuilder().subject_name(subject_name)
|
|
96
|
+
priv = serialization.load_pem_private_key(pem_priv, password=None)
|
|
97
|
+
csr = builder.sign(priv, hashes.SHA256())
|
|
98
|
+
encoding = (
|
|
99
|
+
serialization.Encoding.DER if output_der else serialization.Encoding.PEM
|
|
100
|
+
)
|
|
101
|
+
return csr.public_bytes(encoding)
|
|
File without changes
|