avrel 0.1.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.
- avrel-0.1.0/LICENSE +202 -0
- avrel-0.1.0/NOTICE +3 -0
- avrel-0.1.0/PKG-INFO +69 -0
- avrel-0.1.0/README.md +48 -0
- avrel-0.1.0/avrel/__init__.py +22 -0
- avrel-0.1.0/avrel/__main__.py +5 -0
- avrel-0.1.0/avrel/approvals.py +272 -0
- avrel-0.1.0/avrel/cli.py +309 -0
- avrel-0.1.0/avrel/client.py +359 -0
- avrel-0.1.0/avrel/errors.py +28 -0
- avrel-0.1.0/avrel/py.typed +0 -0
- avrel-0.1.0/avrel/readings.py +105 -0
- avrel-0.1.0/avrel.egg-info/PKG-INFO +69 -0
- avrel-0.1.0/avrel.egg-info/SOURCES.txt +18 -0
- avrel-0.1.0/avrel.egg-info/dependency_links.txt +1 -0
- avrel-0.1.0/avrel.egg-info/entry_points.txt +2 -0
- avrel-0.1.0/avrel.egg-info/requires.txt +3 -0
- avrel-0.1.0/avrel.egg-info/top_level.txt +1 -0
- avrel-0.1.0/pyproject.toml +40 -0
- avrel-0.1.0/setup.cfg +4 -0
avrel-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 The Avrel Authors
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
avrel-0.1.0/NOTICE
ADDED
avrel-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: avrel
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for Avrel, the release authority for autonomous hardware
|
|
5
|
+
Author: Avrel
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://getavrel.com
|
|
8
|
+
Project-URL: Documentation, https://docs.getavrel.com/python
|
|
9
|
+
Keywords: avrel,release gate,hardware,telemetry,verification,mcp
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Topic :: Software Development :: Testing
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering
|
|
14
|
+
Requires-Python: >=3.9
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
License-File: NOTICE
|
|
18
|
+
Provides-Extra: pandas
|
|
19
|
+
Requires-Dist: pandas>=1.3; extra == "pandas"
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# avrel
|
|
23
|
+
|
|
24
|
+
Python client for [Avrel](https://getavrel.com), the test-data platform for hardware teams, with release authority built in.
|
|
25
|
+
|
|
26
|
+
It reaches everything an AI assistant can reach in Avrel, through the same agent key and the same permission checks: read telemetry, check a release in CI, record releases and test results, import archived readings, and propose hardware, evidence and safety rules for a person to accept. Every call is written to your organization's audit log.
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
pip install avrel
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Python 3.9 or newer. No dependencies; `pip install "avrel[pandas]"` adds DataFrames.
|
|
33
|
+
|
|
34
|
+
## Use it
|
|
35
|
+
|
|
36
|
+
Create an agent key in Avrel under **Settings > AI assistants**, then:
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
import avrel
|
|
40
|
+
|
|
41
|
+
client = avrel.Client(url="https://app.getavrel.com", key="ava_...") # or AVREL_URL and AVREL_KEY
|
|
42
|
+
|
|
43
|
+
site = client.get_site()
|
|
44
|
+
gate = client.gate(release="v1.4.2", sha="3f2c9e1")
|
|
45
|
+
if gate.blocked:
|
|
46
|
+
raise SystemExit(gate.summary())
|
|
47
|
+
|
|
48
|
+
readings = client.telemetry("RIG-01")
|
|
49
|
+
frame = avrel.to_dataframe(readings) # needs pandas
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Every tool the key may use is a method (`client.get_release(...)`, `client.record_test_result(...)`). `client.tools()` lists them; a tool added to Avrel appears without a new version of this package.
|
|
53
|
+
|
|
54
|
+
## From a terminal or CI
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
export AVREL_URL=https://app.getavrel.com
|
|
58
|
+
export AVREL_KEY=ava_...
|
|
59
|
+
|
|
60
|
+
avrel gate --release v1.4.2 --sha "$GIT_SHA" # exit 1 when the gate blocks the release
|
|
61
|
+
avrel record-release --release v1.4.2 --sha "$GIT_SHA"
|
|
62
|
+
avrel import readings.csv.gz --unit-id RIG-01
|
|
63
|
+
avrel tools
|
|
64
|
+
avrel call get_telemetry unit_id=RIG-01 from:=1790000000
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Full guide: https://docs.getavrel.com/python
|
|
68
|
+
|
|
69
|
+
Licensed under the Apache License 2.0. See the `LICENSE` and `NOTICE` files in the package.
|
avrel-0.1.0/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# avrel
|
|
2
|
+
|
|
3
|
+
Python client for [Avrel](https://getavrel.com), the test-data platform for hardware teams, with release authority built in.
|
|
4
|
+
|
|
5
|
+
It reaches everything an AI assistant can reach in Avrel, through the same agent key and the same permission checks: read telemetry, check a release in CI, record releases and test results, import archived readings, and propose hardware, evidence and safety rules for a person to accept. Every call is written to your organization's audit log.
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
pip install avrel
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Python 3.9 or newer. No dependencies; `pip install "avrel[pandas]"` adds DataFrames.
|
|
12
|
+
|
|
13
|
+
## Use it
|
|
14
|
+
|
|
15
|
+
Create an agent key in Avrel under **Settings > AI assistants**, then:
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
import avrel
|
|
19
|
+
|
|
20
|
+
client = avrel.Client(url="https://app.getavrel.com", key="ava_...") # or AVREL_URL and AVREL_KEY
|
|
21
|
+
|
|
22
|
+
site = client.get_site()
|
|
23
|
+
gate = client.gate(release="v1.4.2", sha="3f2c9e1")
|
|
24
|
+
if gate.blocked:
|
|
25
|
+
raise SystemExit(gate.summary())
|
|
26
|
+
|
|
27
|
+
readings = client.telemetry("RIG-01")
|
|
28
|
+
frame = avrel.to_dataframe(readings) # needs pandas
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Every tool the key may use is a method (`client.get_release(...)`, `client.record_test_result(...)`). `client.tools()` lists them; a tool added to Avrel appears without a new version of this package.
|
|
32
|
+
|
|
33
|
+
## From a terminal or CI
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
export AVREL_URL=https://app.getavrel.com
|
|
37
|
+
export AVREL_KEY=ava_...
|
|
38
|
+
|
|
39
|
+
avrel gate --release v1.4.2 --sha "$GIT_SHA" # exit 1 when the gate blocks the release
|
|
40
|
+
avrel record-release --release v1.4.2 --sha "$GIT_SHA"
|
|
41
|
+
avrel import readings.csv.gz --unit-id RIG-01
|
|
42
|
+
avrel tools
|
|
43
|
+
avrel call get_telemetry unit_id=RIG-01 from:=1790000000
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Full guide: https://docs.getavrel.com/python
|
|
47
|
+
|
|
48
|
+
Licensed under the Apache License 2.0. See the `LICENSE` and `NOTICE` files in the package.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Avrel from Python: the test-data platform for hardware teams, with release authority built in.
|
|
2
|
+
|
|
3
|
+
import avrel
|
|
4
|
+
|
|
5
|
+
client = avrel.Client() # AVREL_URL and AVREL_KEY from the environment
|
|
6
|
+
client.get_site() # any tool your key can use, as a method
|
|
7
|
+
gate = client.gate(release="v1.4.2", sha="3f2c9e1")
|
|
8
|
+
if gate.blocked:
|
|
9
|
+
raise SystemExit(gate.summary())
|
|
10
|
+
|
|
11
|
+
Every call goes through Avrel's control plane with an agent key, so it is checked against the
|
|
12
|
+
key's access and the role of the person who made the key, and it is written to the audit log.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from .approvals import Approval, ApprovalRefused, verify_approval
|
|
16
|
+
from .client import Client, Gate, ToolInfo, to_dataframe
|
|
17
|
+
from .errors import AuthError, AvrelError, ConnectionFailed, Refused, UnknownTool
|
|
18
|
+
from .readings import read_file
|
|
19
|
+
|
|
20
|
+
__version__ = "0.1.0"
|
|
21
|
+
__all__ = ["Approval", "ApprovalRefused", "verify_approval", "Client", "Gate", "ToolInfo", "to_dataframe", "read_file", "AvrelError", "AuthError", "ConnectionFailed",
|
|
22
|
+
"Refused", "UnknownTool", "__version__"]
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"""Check a signed release approval on the device, before installing a build. No network needed.
|
|
2
|
+
|
|
3
|
+
from avrel.approvals import verify_approval, ApprovalRefused
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
approval = verify_approval("avrel-approval.json",
|
|
7
|
+
organization="acme-robotics", # your organization's ID in Avrel
|
|
8
|
+
public_keys=["MCowBQYDK2VwAyEA..."], # pinned: GET /api/release-approvals/keys
|
|
9
|
+
artifact="firmware.bin") # the file you are about to install
|
|
10
|
+
except ApprovalRefused as exc:
|
|
11
|
+
sys.exit(f"Not installing: {exc}")
|
|
12
|
+
|
|
13
|
+
An approval file is ``{format, algorithm, key_id, payload, signature}``: payload is base64 of the exact JSON
|
|
14
|
+
bytes Avrel signed with Ed25519, so any language can check it the same way (decode the payload, verify the
|
|
15
|
+
signature over those bytes with the pinned public key, then read the fields). Avrel never sees what the
|
|
16
|
+
device decides; the check runs only here.
|
|
17
|
+
|
|
18
|
+
Uses the ``cryptography`` package when it is installed, otherwise a small pure-Python Ed25519 check
|
|
19
|
+
(RFC 8032), so the updater needs nothing beyond the standard library.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import base64
|
|
25
|
+
from dataclasses import dataclass, field
|
|
26
|
+
import hashlib
|
|
27
|
+
import json
|
|
28
|
+
import os
|
|
29
|
+
import time
|
|
30
|
+
from typing import Any, Iterable, Mapping, Union
|
|
31
|
+
import urllib.parse
|
|
32
|
+
import urllib.request
|
|
33
|
+
|
|
34
|
+
from .errors import AvrelError
|
|
35
|
+
|
|
36
|
+
FORMAT = "avrel.release-approval.v1"
|
|
37
|
+
_SPKI_PREFIX = bytes.fromhex("302a300506032b6570032100") # Ed25519 SubjectPublicKeyInfo header (RFC 8410)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class ApprovalRefused(AvrelError):
|
|
41
|
+
"""The build must not be installed: the approval is missing, forged, for something else, or withdrawn."""
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True)
|
|
45
|
+
class Approval:
|
|
46
|
+
"""A signed release approval that checked out."""
|
|
47
|
+
|
|
48
|
+
id: str
|
|
49
|
+
organization: str
|
|
50
|
+
release: str
|
|
51
|
+
build: str
|
|
52
|
+
artifacts: list = field(default_factory=list)
|
|
53
|
+
issued_by: str = ""
|
|
54
|
+
issued_at: float = 0.0
|
|
55
|
+
expires_at: Union[float, None] = None
|
|
56
|
+
key_id: str = ""
|
|
57
|
+
status_url: str = ""
|
|
58
|
+
payload: dict = field(default_factory=dict)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# ── Ed25519 (RFC 8032, verification only) ────────────────────────────────
|
|
62
|
+
_P = 2 ** 255 - 19
|
|
63
|
+
_L = 2 ** 252 + 27742317777372353535851937790883648493
|
|
64
|
+
_D = -121665 * pow(121666, _P - 2, _P) % _P
|
|
65
|
+
_I = pow(2, (_P - 1) // 4, _P)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _add(a, b):
|
|
69
|
+
x1, y1, z1, t1 = a
|
|
70
|
+
x2, y2, z2, t2 = b
|
|
71
|
+
A = (y1 - x1) * (y2 - x2) % _P
|
|
72
|
+
B = (y1 + x1) * (y2 + x2) % _P
|
|
73
|
+
C = 2 * t1 * t2 * _D % _P
|
|
74
|
+
D = 2 * z1 * z2 % _P
|
|
75
|
+
E, F, G, H = B - A, D - C, D + C, B + A
|
|
76
|
+
return (E * F % _P, G * H % _P, F * G % _P, E * H % _P)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _mul(s, pt):
|
|
80
|
+
q = (0, 1, 1, 0)
|
|
81
|
+
while s:
|
|
82
|
+
if s & 1:
|
|
83
|
+
q = _add(q, pt)
|
|
84
|
+
pt = _add(pt, pt)
|
|
85
|
+
s >>= 1
|
|
86
|
+
return q
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _equal(a, b):
|
|
90
|
+
return (a[0] * b[2] - b[0] * a[2]) % _P == 0 and (a[1] * b[2] - b[1] * a[2]) % _P == 0
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _decompress(s: bytes):
|
|
94
|
+
if len(s) != 32:
|
|
95
|
+
return None
|
|
96
|
+
y = int.from_bytes(s, "little")
|
|
97
|
+
sign = y >> 255
|
|
98
|
+
y &= (1 << 255) - 1
|
|
99
|
+
if y >= _P:
|
|
100
|
+
return None
|
|
101
|
+
x2 = (y * y - 1) * pow(_D * y * y + 1, _P - 2, _P) % _P
|
|
102
|
+
if x2 == 0:
|
|
103
|
+
if sign:
|
|
104
|
+
return None
|
|
105
|
+
x = 0
|
|
106
|
+
else:
|
|
107
|
+
x = pow(x2, (_P + 3) // 8, _P)
|
|
108
|
+
if (x * x - x2) % _P:
|
|
109
|
+
x = x * _I % _P
|
|
110
|
+
if (x * x - x2) % _P:
|
|
111
|
+
return None
|
|
112
|
+
if (x & 1) != sign:
|
|
113
|
+
x = _P - x
|
|
114
|
+
return (x, y, 1, x * y % _P)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
_GY = 4 * pow(5, _P - 2, _P) % _P
|
|
118
|
+
_G = _decompress(_GY.to_bytes(32, "little"))
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _pure_verify(public: bytes, signature: bytes, message: bytes) -> bool:
|
|
122
|
+
if len(signature) != 64:
|
|
123
|
+
return False
|
|
124
|
+
a = _decompress(public)
|
|
125
|
+
r = _decompress(signature[:32])
|
|
126
|
+
s = int.from_bytes(signature[32:], "little")
|
|
127
|
+
if a is None or r is None or s >= _L:
|
|
128
|
+
return False
|
|
129
|
+
h = int.from_bytes(hashlib.sha512(signature[:32] + public + message).digest(), "little") % _L
|
|
130
|
+
return _equal(_mul(s, _G), _add(r, _mul(h, a)))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def ed25519_verify(public: bytes, signature: bytes, message: bytes) -> bool:
|
|
134
|
+
"""True when signature is a valid Ed25519 signature of message by the 32-byte public key."""
|
|
135
|
+
try:
|
|
136
|
+
from cryptography.exceptions import InvalidSignature
|
|
137
|
+
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
|
|
138
|
+
except ImportError:
|
|
139
|
+
return _pure_verify(public, signature, message)
|
|
140
|
+
try:
|
|
141
|
+
Ed25519PublicKey.from_public_bytes(public).verify(signature, message)
|
|
142
|
+
return True
|
|
143
|
+
except (InvalidSignature, ValueError):
|
|
144
|
+
return False
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
# ── Keys ─────────────────────────────────────────────────────────────────
|
|
148
|
+
def _raw_key(key: Union[str, bytes, Mapping[str, Any]]) -> bytes:
|
|
149
|
+
"""A public key as 32 raw bytes, from base64 of the raw key, a PEM block, or a {public_key} entry."""
|
|
150
|
+
if isinstance(key, Mapping):
|
|
151
|
+
key = key.get("public_key") or key.get("pem") or ""
|
|
152
|
+
if isinstance(key, bytes):
|
|
153
|
+
key = key.decode("ascii", "replace") if len(key) != 32 else key
|
|
154
|
+
if isinstance(key, bytes):
|
|
155
|
+
return key
|
|
156
|
+
text = str(key).strip()
|
|
157
|
+
if "BEGIN PUBLIC KEY" in text:
|
|
158
|
+
text = "".join(line for line in text.splitlines() if "-----" not in line)
|
|
159
|
+
raw = base64.b64decode(text)
|
|
160
|
+
if len(raw) == 44 and raw.startswith(_SPKI_PREFIX):
|
|
161
|
+
raw = raw[len(_SPKI_PREFIX):]
|
|
162
|
+
if len(raw) != 32:
|
|
163
|
+
raise ApprovalRefused("A pinned public key is not an Ed25519 key")
|
|
164
|
+
return raw
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def key_id(raw: bytes) -> str:
|
|
168
|
+
return hashlib.sha256(raw).hexdigest()[:16]
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def fetch_public_keys(url: str = "", timeout: float = 10) -> list:
|
|
172
|
+
"""The public keys from an Avrel server (default $AVREL_URL), to pin when the updater is built, not at install."""
|
|
173
|
+
base = (url or os.environ.get("AVREL_URL") or "https://app.getavrel.com").rstrip("/")
|
|
174
|
+
with urllib.request.urlopen(f"{base}/api/release-approvals/keys", timeout=timeout) as r: # noqa: S310
|
|
175
|
+
return json.loads(r.read().decode("utf-8")).get("keys") or []
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
# ── The check ────────────────────────────────────────────────────────────
|
|
179
|
+
def _load(document: Any) -> dict:
|
|
180
|
+
if isinstance(document, Mapping):
|
|
181
|
+
return dict(document)
|
|
182
|
+
if isinstance(document, bytes):
|
|
183
|
+
return json.loads(document.decode("utf-8"))
|
|
184
|
+
text = str(document)
|
|
185
|
+
if text.lstrip().startswith("{"):
|
|
186
|
+
return json.loads(text)
|
|
187
|
+
with open(text, encoding="utf-8") as fh:
|
|
188
|
+
return json.load(fh)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def sha256_file(path: str) -> str:
|
|
192
|
+
h = hashlib.sha256()
|
|
193
|
+
with open(path, "rb") as fh:
|
|
194
|
+
for chunk in iter(lambda: fh.read(1 << 20), b""):
|
|
195
|
+
h.update(chunk)
|
|
196
|
+
return h.hexdigest()
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def check_status(approval: Approval, timeout: float = 10) -> str:
|
|
200
|
+
"""Ask Avrel whether the approval still stands: valid, expired, revoked or unknown. Needs the network."""
|
|
201
|
+
url = approval.status_url
|
|
202
|
+
if not url:
|
|
203
|
+
raise ApprovalRefused("The approval has no status address to check")
|
|
204
|
+
if urllib.parse.urlparse(url).scheme != "https":
|
|
205
|
+
raise ApprovalRefused("The approval's status address is not https")
|
|
206
|
+
with urllib.request.urlopen(url, timeout=timeout) as r: # noqa: S310 - https only, checked above
|
|
207
|
+
return str(json.loads(r.read().decode("utf-8")).get("state") or "unknown")
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def verify_approval(document: Any, *, organization: str, public_keys: Iterable[Any], build: str = "",
|
|
211
|
+
artifact: Union[str, None] = None, artifact_sha256: str = "", now: Union[float, None] = None,
|
|
212
|
+
online: bool = False) -> Approval:
|
|
213
|
+
"""Check a signed release approval and return it, or raise ApprovalRefused saying why not to install.
|
|
214
|
+
|
|
215
|
+
document the approval file: a path, its JSON text or bytes, or the parsed dict
|
|
216
|
+
organization your organization's ID in Avrel; an approval for anyone else is refused
|
|
217
|
+
public_keys the pinned keys (base64, PEM, or entries from /api/release-approvals/keys)
|
|
218
|
+
build the build ID you are about to install, when you know it
|
|
219
|
+
artifact path of the file you are about to install: its SHA-256 must be the build ID or a listed digest
|
|
220
|
+
online also ask Avrel whether the approval was revoked since (refused if Avrel cannot be reached)
|
|
221
|
+
"""
|
|
222
|
+
try:
|
|
223
|
+
doc = _load(document)
|
|
224
|
+
except (OSError, ValueError) as exc:
|
|
225
|
+
raise ApprovalRefused(f"No readable approval file ({exc})") from None
|
|
226
|
+
if doc.get("format") != FORMAT or doc.get("algorithm") != "Ed25519":
|
|
227
|
+
raise ApprovalRefused(f"Not an Avrel release approval (format must be {FORMAT})")
|
|
228
|
+
try:
|
|
229
|
+
raw = base64.b64decode(str(doc.get("payload") or ""), validate=True)
|
|
230
|
+
signature = base64.b64decode(str(doc.get("signature") or ""), validate=True)
|
|
231
|
+
payload = json.loads(raw.decode("utf-8"))
|
|
232
|
+
except ValueError:
|
|
233
|
+
raise ApprovalRefused("The approval file is damaged") from None
|
|
234
|
+
keys = {key_id(k): k for k in (_raw_key(x) for x in public_keys)}
|
|
235
|
+
if not keys:
|
|
236
|
+
raise ApprovalRefused("No public key is pinned")
|
|
237
|
+
pub = keys.get(str(doc.get("key_id") or ""))
|
|
238
|
+
if pub is None:
|
|
239
|
+
raise ApprovalRefused("It was signed with a key that is not pinned here")
|
|
240
|
+
if not ed25519_verify(pub, signature, raw):
|
|
241
|
+
raise ApprovalRefused("The signature does not verify: the file was changed or Avrel did not sign it")
|
|
242
|
+
if payload.get("format") != FORMAT or payload.get("key_id") != doc.get("key_id"):
|
|
243
|
+
raise ApprovalRefused("The signed payload does not match its envelope")
|
|
244
|
+
if payload.get("organization") != organization:
|
|
245
|
+
raise ApprovalRefused(f"It approves a build for organization {payload.get('organization')!r}, not {organization!r}")
|
|
246
|
+
if build and payload.get("build") != build:
|
|
247
|
+
raise ApprovalRefused(f"It approves build {payload.get('build')}, not {build}")
|
|
248
|
+
if artifact or artifact_sha256:
|
|
249
|
+
digest = (artifact_sha256 or sha256_file(str(artifact))).lower()
|
|
250
|
+
allowed = {str(a.get("sha256") or "").lower() for a in payload.get("artifacts") or []}
|
|
251
|
+
allowed.add(str(payload.get("build") or "").lower())
|
|
252
|
+
if digest not in allowed:
|
|
253
|
+
raise ApprovalRefused(f"The file's SHA-256 {digest[:16]}... is not one this approval names")
|
|
254
|
+
t = time.time() if now is None else now
|
|
255
|
+
expires = payload.get("expires_at")
|
|
256
|
+
if expires is not None and t >= float(expires):
|
|
257
|
+
raise ApprovalRefused("The approval has expired")
|
|
258
|
+
approval = Approval(id=str(payload.get("id") or ""), organization=organization, release=str(payload.get("release") or ""),
|
|
259
|
+
build=str(payload.get("build") or ""), artifacts=list(payload.get("artifacts") or []),
|
|
260
|
+
issued_by=str(payload.get("issued_by") or ""), issued_at=float(payload.get("issued_at") or 0),
|
|
261
|
+
expires_at=expires, key_id=str(doc.get("key_id")), status_url=str(payload.get("status_url") or ""),
|
|
262
|
+
payload=payload)
|
|
263
|
+
if online:
|
|
264
|
+
try:
|
|
265
|
+
state = check_status(approval)
|
|
266
|
+
except ApprovalRefused:
|
|
267
|
+
raise
|
|
268
|
+
except Exception as exc: # noqa: BLE001 - online was asked for, so not knowing is a refusal
|
|
269
|
+
raise ApprovalRefused(f"Could not ask Avrel whether the approval still stands ({type(exc).__name__})") from None
|
|
270
|
+
if state != "valid":
|
|
271
|
+
raise ApprovalRefused(f"Avrel says the approval is {state}")
|
|
272
|
+
return approval
|