codeimprint 0.5.0a11__py3-none-any.whl
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.
- codeimprint-0.5.0a11.dist-info/METADATA +92 -0
- codeimprint-0.5.0a11.dist-info/RECORD +23 -0
- codeimprint-0.5.0a11.dist-info/WHEEL +4 -0
- codeimprint-0.5.0a11.dist-info/entry_points.txt +2 -0
- codeimprint-0.5.0a11.dist-info/licenses/LICENSE +177 -0
- codeimprint-0.5.0a11.dist-info/licenses/NOTICE +11 -0
- codeimprint_cli/__init__.py +7 -0
- codeimprint_cli/__main__.py +5 -0
- codeimprint_cli/api.py +315 -0
- codeimprint_cli/chainseal.py +175 -0
- codeimprint_cli/cli.py +2128 -0
- codeimprint_cli/config.py +181 -0
- codeimprint_cli/gitrepo.py +148 -0
- codeimprint_cli/imprint_file.py +128 -0
- codeimprint_cli/linecount.py +340 -0
- codeimprint_cli/ownership.py +341 -0
- codeimprint_cli/project.py +129 -0
- codeimprint_cli/receipt.py +679 -0
- codeimprint_cli/records.py +59 -0
- codeimprint_cli/timestamp.py +187 -0
- codeimprint_cli/transparency.py +71 -0
- codeimprint_cli/trust.py +190 -0
- codeimprint_cli/tsa_roots.py +137 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: codeimprint
|
|
3
|
+
Version: 0.5.0a11
|
|
4
|
+
Summary: CodeImprint by Mudrika: seal your code with a CodeImprint server (tamper-evident, logged and timestamped) and verify seals offline.
|
|
5
|
+
Project-URL: Homepage, https://codeimprint.ai
|
|
6
|
+
Project-URL: Hub, https://app.codeimprint.ai
|
|
7
|
+
Author: Mudrika
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
License-File: NOTICE
|
|
11
|
+
Keywords: code provenance,code sealing,github actions,software birthmark,transparency log
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Topic :: Security
|
|
23
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
24
|
+
Classifier: Topic :: Software Development :: Version Control :: Git
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Requires-Dist: asn1crypto>=1.5
|
|
27
|
+
Requires-Dist: cryptography>=42
|
|
28
|
+
Requires-Dist: pathspec>=0.12
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
31
|
+
Requires-Dist: ruff==0.17.2; extra == 'dev'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# CodeImprint by Mudrika — command-line client
|
|
35
|
+
|
|
36
|
+
**Code provenance that survives AI.** CodeImprint seals your code: every seal is an
|
|
37
|
+
**Imprint**, a compact signature of the code that is tamper-evident, recorded in a public
|
|
38
|
+
transparency log and timestamped, so it cannot be altered or backdated without detection. An
|
|
39
|
+
Imprint still matches when the code is renamed, reformatted, reordered or rewritten, and it
|
|
40
|
+
does not contain your source.
|
|
41
|
+
|
|
42
|
+
This package is the command-line client, `imprint`. It talks to a CodeImprint server, which
|
|
43
|
+
does the analysis; the client sends your files, checks what the server returns and verifies
|
|
44
|
+
seals offline. Learn more at [codeimprint.ai](https://codeimprint.ai).
|
|
45
|
+
|
|
46
|
+
> **Pre-release (alpha).** Install with `--pre`. Python 3.10 or newer.
|
|
47
|
+
|
|
48
|
+
## Install
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install --pre codeimprint
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
(`pip install --pre code-imprint` installs the same package.)
|
|
55
|
+
|
|
56
|
+
## Get started
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
imprint login --url https://api.codeimprint.ai # sign in (browser or e-mail link)
|
|
60
|
+
imprint init # your developer key, the project and its first seal
|
|
61
|
+
imprint seal . # seal the current commit
|
|
62
|
+
imprint verify # check the seal offline
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
- **`imprint login`** saves the server and your sign-in in your user config folder, readable
|
|
66
|
+
by you only. In GitHub Actions, use an API token: `imprint login --token-stdin`, or the
|
|
67
|
+
`CODEIMPRINT_URL` and `CODEIMPRINT_TOKEN` environment variables.
|
|
68
|
+
- **`imprint init`** creates your Ed25519 developer key in the user config folder (it is
|
|
69
|
+
never uploaded and never written to the repository), writes `.imprint/config.json` and a
|
|
70
|
+
GitHub Actions workflow, and creates the project, named `owner/repo` from the `origin`
|
|
71
|
+
remote.
|
|
72
|
+
- **`imprint seal .`** shows what it is about to send and asks first (`--yes` skips the
|
|
73
|
+
question). Only the files that changed since the last seal are sent. It checks the server's
|
|
74
|
+
signature and log proof, signs the result with your developer key, and prints the Imprint
|
|
75
|
+
ID and a link to its public page on [app.codeimprint.ai](https://app.codeimprint.ai).
|
|
76
|
+
- **`imprint verify`** checks a seal offline: digest, server attestation, your signature, log
|
|
77
|
+
inclusion and the RFC 3161 timestamp. `--online` also checks the live log.
|
|
78
|
+
|
|
79
|
+
Other commands: `imprint check` (compare a project with a protected Imprint; similarity is
|
|
80
|
+
evidence, not proof), `imprint chain`, `imprint hook install`, `imprint identity`,
|
|
81
|
+
`imprint claim`, `imprint github link` and more. Run `imprint --help`.
|
|
82
|
+
|
|
83
|
+
## What leaves your machine
|
|
84
|
+
|
|
85
|
+
`imprint seal` and `imprint check` upload the project's files to the CodeImprint server you
|
|
86
|
+
signed in to, which fingerprints them and deletes them at once; only the Imprint is kept.
|
|
87
|
+
Your developer key never leaves your machine: only its public half is sent.
|
|
88
|
+
|
|
89
|
+
## Licence
|
|
90
|
+
|
|
91
|
+
Apache-2.0. The client contains no part of the CodeImprint engine, which runs on the server.
|
|
92
|
+
"Mudrika" and "CodeImprint" are trademarks of their owner (see NOTICE).
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
codeimprint_cli/__init__.py,sha256=VPYTP4Z6WgDkls1nDK8dO3Dm8TnyK-p0OgG97GXFubs,250
|
|
2
|
+
codeimprint_cli/__main__.py,sha256=E6Gls0DNz8GQK2K-kOUIx8cYhgANW_CH54VKrfCfs14,52
|
|
3
|
+
codeimprint_cli/api.py,sha256=wvsEQlJ05Nos3OwnjRFi4UL9mRmPYdFEfCu_drEb9Cw,16474
|
|
4
|
+
codeimprint_cli/chainseal.py,sha256=A6eQAkDb3iwMmdRXgMAlaHnww2VEwJ_wgQL8fCs-gfQ,7980
|
|
5
|
+
codeimprint_cli/cli.py,sha256=LwDSKb97CgUuzT5w-jTqTWIQeZOA8Tp8Oyy1hlIZlI4,106778
|
|
6
|
+
codeimprint_cli/config.py,sha256=nuTNPAwBK2u40PkgOVp3mTydkDTNrNtEMzTCdTje1Qs,7038
|
|
7
|
+
codeimprint_cli/gitrepo.py,sha256=BA8Lzl6l61j4lp5LVKR4OheStDNTvsu3yGV00xtklNE,5398
|
|
8
|
+
codeimprint_cli/imprint_file.py,sha256=jQBLbupQsLbtiKLL7nt9Fd2aLOHB0lqupT_DOlzNe9M,5305
|
|
9
|
+
codeimprint_cli/linecount.py,sha256=YNSITLtQZWc1fZ0-xgR78Vne58tFBNmR6sm3jadNF2Y,14854
|
|
10
|
+
codeimprint_cli/ownership.py,sha256=ZfJA4VLosPfi1mt1z9E39Iu1ozMJq8UyG_Y06NqBuus,16584
|
|
11
|
+
codeimprint_cli/project.py,sha256=0ASy8SLg5MMAceTjn5w8t_53LnqeO1syqq4CM3xPg8o,4620
|
|
12
|
+
codeimprint_cli/receipt.py,sha256=-co0b1NE38HaLgioA21wz65P3-72uJbH-1UoK5z924A,35989
|
|
13
|
+
codeimprint_cli/records.py,sha256=68xtLuxHzuDdE58fME50xL4lPETNHdOe_ZQyyPPG_GI,2805
|
|
14
|
+
codeimprint_cli/timestamp.py,sha256=tFKheQqZYqTPVDtISKdVu_-SLpG8fKEO8tGzfzyNi8k,8108
|
|
15
|
+
codeimprint_cli/transparency.py,sha256=ivkmIIma9svAc_XgAdI_RSqQ8BA4kezheJ0-STiqPHM,2274
|
|
16
|
+
codeimprint_cli/trust.py,sha256=YUMpO-aJM0hS7sMXjJjDY9FP1AaMjbJwvaEYo6zp_nY,7540
|
|
17
|
+
codeimprint_cli/tsa_roots.py,sha256=sAwqiN-bLlh9c6YMAlWW3FxlzMLxftRU8D1DS9u7TZU,7529
|
|
18
|
+
codeimprint-0.5.0a11.dist-info/METADATA,sha256=xsEANgaIqsncS2RRGV1AT3SfiXgsAqfAfX8ZeeyMzNw,4403
|
|
19
|
+
codeimprint-0.5.0a11.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
20
|
+
codeimprint-0.5.0a11.dist-info/entry_points.txt,sha256=G06DyvtqD6z-F1wGp5V7wPrVasEfOA-OPzMYgmq6YdU,53
|
|
21
|
+
codeimprint-0.5.0a11.dist-info/licenses/LICENSE,sha256=fVRQyy0UJlG4r6MVtfI478gF2tgn2RujZ9hRa8nUnno,10175
|
|
22
|
+
codeimprint-0.5.0a11.dist-info/licenses/NOTICE,sha256=ht4ZZ0ykUICENum0UdkZ71PAjaNLpYRJhm2oCKCi4Wk,480
|
|
23
|
+
codeimprint-0.5.0a11.dist-info/RECORD,,
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
https://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
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
CodeImprint client by Mudrika
|
|
2
|
+
Copyright 2026 Mudrika
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
|
|
6
|
+
This client calls a CodeImprint server. It contains no part of the IMP-1 engine, which is
|
|
7
|
+
proprietary to Mudrika and is not covered by this licence.
|
|
8
|
+
|
|
9
|
+
"Mudrika" and "CodeImprint" are trademarks of their owner. The Apache License does not grant
|
|
10
|
+
permission to use these names except as required for reasonable and customary use in
|
|
11
|
+
describing the origin of the work.
|
codeimprint_cli/api.py
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
"""HTTP calls to a CodeImprint server (standard library only)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import urllib.error
|
|
7
|
+
import urllib.request
|
|
8
|
+
import uuid
|
|
9
|
+
from urllib.parse import quote
|
|
10
|
+
|
|
11
|
+
from . import __version__, config
|
|
12
|
+
|
|
13
|
+
USER_AGENT = f"codeimprint/{__version__}"
|
|
14
|
+
ACCESS_HINT = ("the server is behind Cloudflare Access: set CODEIMPRINT_CF_ACCESS_CLIENT_ID and "
|
|
15
|
+
"CODEIMPRINT_CF_ACCESS_CLIENT_SECRET (a service token), or run `imprint login "
|
|
16
|
+
"--cf-access`")
|
|
17
|
+
ACCESS_REFUSED = ("Cloudflare Access refused the service token: check "
|
|
18
|
+
"CODEIMPRINT_CF_ACCESS_CLIENT_ID and CODEIMPRINT_CF_ACCESS_CLIENT_SECRET (or "
|
|
19
|
+
"`imprint login --cf-access`) and that the token is in the Access policy")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class _NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
23
|
+
"""The API never redirects JSON calls; following one would copy the request's headers
|
|
24
|
+
(the token, an Access secret) to wherever it points. Redirects come back as errors."""
|
|
25
|
+
|
|
26
|
+
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
_OPENER = urllib.request.build_opener(_NoRedirect)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ApiError(Exception):
|
|
34
|
+
"""The server answered with an error."""
|
|
35
|
+
|
|
36
|
+
def __init__(self, status: int, code: str, message: str) -> None:
|
|
37
|
+
super().__init__(f"{message} ({status} {code})")
|
|
38
|
+
self.status, self.code, self.message = status, code, message
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class NetworkError(Exception):
|
|
42
|
+
"""The server could not be reached."""
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _multipart(fields: dict[str, str], file_name: str, file_bytes: bytes) -> tuple[bytes, str]:
|
|
46
|
+
boundary = f"codeimprint-{uuid.uuid4().hex}"
|
|
47
|
+
parts = []
|
|
48
|
+
for name, value in fields.items():
|
|
49
|
+
parts.append(f'--{boundary}\r\nContent-Disposition: form-data; name="{name}"\r\n\r\n'
|
|
50
|
+
f"{value}\r\n".encode())
|
|
51
|
+
parts.append(f'--{boundary}\r\nContent-Disposition: form-data; name="file"; '
|
|
52
|
+
f'filename="{file_name}"\r\nContent-Type: application/zip\r\n\r\n'.encode())
|
|
53
|
+
parts.append(file_bytes)
|
|
54
|
+
parts.append(f"\r\n--{boundary}--\r\n".encode())
|
|
55
|
+
return b"".join(parts), f"multipart/form-data; boundary={boundary}"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _multipart_parts(parts: list[tuple[str, bytes]]) -> tuple[bytes, str]:
|
|
59
|
+
"""One binary part per (field name, bytes): for blob uploads, the name is the blob ID."""
|
|
60
|
+
boundary = f"codeimprint-{uuid.uuid4().hex}"
|
|
61
|
+
out = []
|
|
62
|
+
for name, data in parts:
|
|
63
|
+
out.append(f'--{boundary}\r\nContent-Disposition: form-data; name="{name}"; '
|
|
64
|
+
f'filename="{name}"\r\nContent-Type: application/octet-stream\r\n\r\n'
|
|
65
|
+
.encode())
|
|
66
|
+
out.append(data)
|
|
67
|
+
out.append(b"\r\n")
|
|
68
|
+
out.append(f"--{boundary}--\r\n".encode())
|
|
69
|
+
return b"".join(out), f"multipart/form-data; boundary={boundary}"
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class Client:
|
|
73
|
+
def __init__(self, url: str, token: str | None = None, timeout: float = 300) -> None:
|
|
74
|
+
self.url, self.token, self.timeout = url.rstrip("/"), token, timeout
|
|
75
|
+
self.access = config.cf_access_for(self.url) # this server's host only
|
|
76
|
+
|
|
77
|
+
def _request(self, method: str, path: str, body: bytes | None = None,
|
|
78
|
+
content_type: str | None = None) -> dict:
|
|
79
|
+
headers = {"Accept": "application/json", "User-Agent": USER_AGENT}
|
|
80
|
+
if content_type:
|
|
81
|
+
headers["Content-Type"] = content_type
|
|
82
|
+
if self.token:
|
|
83
|
+
headers["Authorization"] = f"Bearer {self.token}"
|
|
84
|
+
if self.access:
|
|
85
|
+
headers["CF-Access-Client-Id"], headers["CF-Access-Client-Secret"] = self.access
|
|
86
|
+
req = urllib.request.Request(self.url + path, data=body, headers=headers, method=method)
|
|
87
|
+
try:
|
|
88
|
+
with _OPENER.open(req, timeout=self.timeout) as res:
|
|
89
|
+
return json.loads(res.read().decode("utf-8"))
|
|
90
|
+
except urllib.error.HTTPError as exc:
|
|
91
|
+
self._access_problem(exc)
|
|
92
|
+
code, message = "http_error", exc.reason or "Request failed"
|
|
93
|
+
try:
|
|
94
|
+
err = json.loads(exc.read().decode("utf-8"))["error"]
|
|
95
|
+
code, message = err["code"], err["message"]
|
|
96
|
+
except (ValueError, KeyError, TypeError):
|
|
97
|
+
pass
|
|
98
|
+
raise ApiError(exc.code, code, str(message)) from exc
|
|
99
|
+
except (urllib.error.URLError, TimeoutError, ConnectionError) as exc:
|
|
100
|
+
reason = getattr(exc, "reason", exc)
|
|
101
|
+
raise NetworkError(f"could not reach {self.url}: {reason}") from exc
|
|
102
|
+
except ValueError as exc:
|
|
103
|
+
raise NetworkError(f"{self.url} did not answer with JSON") from exc
|
|
104
|
+
|
|
105
|
+
def _access_problem(self, exc: urllib.error.HTTPError) -> None:
|
|
106
|
+
"""Turn Cloudflare Access's answers (a redirect to its login, or its own 401/403
|
|
107
|
+
page) into a clear message; the engine's own errors are JSON and pass through."""
|
|
108
|
+
location = exc.headers.get("Location") or ""
|
|
109
|
+
if 300 <= exc.code < 400:
|
|
110
|
+
if "cloudflareaccess.com" in location:
|
|
111
|
+
raise NetworkError(ACCESS_REFUSED if self.access else ACCESS_HINT) from exc
|
|
112
|
+
raise NetworkError(f"{self.url} answered with a redirect to {location or '?'}; "
|
|
113
|
+
"use the server's final https:// URL") from exc
|
|
114
|
+
servers = [s.lower() for s in exc.headers.get_all("Server") or []]
|
|
115
|
+
if exc.code in (401, 403) and "json" not in (exc.headers.get("Content-Type") or "") \
|
|
116
|
+
and "cloudflare" in servers:
|
|
117
|
+
raise NetworkError(ACCESS_REFUSED if self.access else ACCESS_HINT) from exc
|
|
118
|
+
|
|
119
|
+
def health(self) -> dict:
|
|
120
|
+
return self._request("GET", "/health")
|
|
121
|
+
|
|
122
|
+
def seal(self, zip_bytes: bytes, name: str | None = None, lines: dict | None = None,
|
|
123
|
+
project_id: str | None = None) -> dict:
|
|
124
|
+
fields = {k: v for k, v in (("name", name), ("project_id", project_id)) if v}
|
|
125
|
+
if lines is not None:
|
|
126
|
+
fields["lines"] = json.dumps(lines, sort_keys=True, separators=(",", ":"))
|
|
127
|
+
body, ctype = _multipart(fields, "project.zip", zip_bytes)
|
|
128
|
+
return self._request("POST", "/api/v1/seal", body, ctype)
|
|
129
|
+
|
|
130
|
+
def _json(self, method: str, path: str, payload: dict | None = None) -> dict:
|
|
131
|
+
body = json.dumps(payload).encode() if payload is not None else None
|
|
132
|
+
return self._request(method, path, body, "application/json" if body else None)
|
|
133
|
+
|
|
134
|
+
# Git-aware sealing: projects, plan -> blobs -> prepare -> finalise ------------------
|
|
135
|
+
def create_project(self, public_key: str, signature: str, name: str | None = None) -> dict:
|
|
136
|
+
return self._json("POST", "/api/v1/projects",
|
|
137
|
+
{"public_key": public_key, "signature": signature,
|
|
138
|
+
**({"name": name} if name else {})})
|
|
139
|
+
|
|
140
|
+
def rename_project(self, project: str, name: str, public: bool, public_key: str,
|
|
141
|
+
signature: str) -> dict:
|
|
142
|
+
return self._json("POST", f"/api/v1/projects/{quote(project)}/name",
|
|
143
|
+
{"name": name, "public": public, "public_key": public_key,
|
|
144
|
+
"signature": signature})
|
|
145
|
+
|
|
146
|
+
# the GitHub App: the owner's key for a repository's project ------------------------
|
|
147
|
+
def github_link_prepare(self, repository: str, public_key: str,
|
|
148
|
+
project: str | None = None) -> dict:
|
|
149
|
+
return self._json("POST", "/api/v1/integrations/github/link/prepare",
|
|
150
|
+
{"repository": repository, "public_key": public_key,
|
|
151
|
+
**({"project_id": project} if project else {})})
|
|
152
|
+
|
|
153
|
+
def github_link_finalise(self, repository: str, public_key: str, project: str | None,
|
|
154
|
+
signature: str, key_signature: str | None = None) -> dict:
|
|
155
|
+
return self._json("POST", "/api/v1/integrations/github/link/finalise",
|
|
156
|
+
{"repository": repository, "public_key": public_key,
|
|
157
|
+
**({"project_id": project} if project else {}),
|
|
158
|
+
"signature": signature,
|
|
159
|
+
**({"key_signature": key_signature} if key_signature else {})})
|
|
160
|
+
|
|
161
|
+
def plan(self, project: str, payload: dict) -> dict:
|
|
162
|
+
return self._json("POST", f"/api/v1/projects/{quote(project)}/seal/plan", payload)
|
|
163
|
+
|
|
164
|
+
def upload_blobs(self, project: str, blobs: list[tuple[str, bytes]]) -> dict:
|
|
165
|
+
body, ctype = _multipart_parts(blobs)
|
|
166
|
+
return self._request("POST", f"/api/v1/projects/{quote(project)}/seal/blobs", body,
|
|
167
|
+
ctype)
|
|
168
|
+
|
|
169
|
+
def prepare(self, project: str, plan_id: str) -> dict:
|
|
170
|
+
return self._json("POST", f"/api/v1/projects/{quote(project)}/seal/prepare",
|
|
171
|
+
{"plan_id": plan_id})
|
|
172
|
+
|
|
173
|
+
def job(self, job_id: str) -> dict:
|
|
174
|
+
return self._json("GET", f"/api/v1/jobs/{quote(job_id)}")
|
|
175
|
+
|
|
176
|
+
def finalise(self, project: str, pending_id: str, public_key: str, signature: str) -> dict:
|
|
177
|
+
return self._json("POST", f"/api/v1/projects/{quote(project)}/seal/finalise",
|
|
178
|
+
{"pending_id": pending_id, "public_key": public_key,
|
|
179
|
+
"signature": signature})
|
|
180
|
+
|
|
181
|
+
def export(self, imprint_id: str) -> dict:
|
|
182
|
+
return self._json("GET", f"/api/v1/imprints/{quote(imprint_id)}/export")
|
|
183
|
+
|
|
184
|
+
def public_project(self, project: str) -> dict:
|
|
185
|
+
return self._json("GET", f"/api/v1/public/projects/{quote(project)}")
|
|
186
|
+
|
|
187
|
+
def public_chain(self, project: str, after: int | None = None) -> dict:
|
|
188
|
+
query = f"?after={int(after)}" if after is not None else ""
|
|
189
|
+
return self._json("GET", f"/api/v1/public/projects/{quote(project)}/chain{query}")
|
|
190
|
+
|
|
191
|
+
def check(self, zip_bytes: bytes, reference: str) -> dict:
|
|
192
|
+
body, ctype = _multipart({"reference": reference}, "project.zip", zip_bytes)
|
|
193
|
+
return self._request("POST", "/api/v1/check", body, ctype)
|
|
194
|
+
|
|
195
|
+
def add_signature(self, imprint_id: str, signature: dict) -> dict:
|
|
196
|
+
body = json.dumps(signature).encode()
|
|
197
|
+
return self._request("POST", f"/api/v1/imprints/{quote(imprint_id)}/signatures", body,
|
|
198
|
+
"application/json")
|
|
199
|
+
|
|
200
|
+
# public, no token needed -----------------------------------------------------------
|
|
201
|
+
def published_keys(self) -> dict:
|
|
202
|
+
return self._request("GET", "/.well-known/codeimprint-keys.json")
|
|
203
|
+
|
|
204
|
+
def log_checkpoint(self) -> dict:
|
|
205
|
+
return self._request("GET", "/api/v1/public/log/checkpoint")
|
|
206
|
+
|
|
207
|
+
def log_consistency(self, first: int, second: int) -> dict:
|
|
208
|
+
return self._request("GET", f"/api/v1/public/log/consistency?from={int(first)}"
|
|
209
|
+
f"&to={int(second)}")
|
|
210
|
+
|
|
211
|
+
# sign-in and verified identity (engine 0.7.0a2) ---------------------------------------
|
|
212
|
+
def login_providers(self) -> dict:
|
|
213
|
+
return self._request("GET", "/api/v1/auth/providers")
|
|
214
|
+
|
|
215
|
+
def login_start(self, provider: str, email: str | None = None) -> dict:
|
|
216
|
+
return self._json("POST", "/api/v1/auth/cli/start",
|
|
217
|
+
{"provider": provider, **({"email": email} if email else {})})
|
|
218
|
+
|
|
219
|
+
def login_poll(self, login_id: str, device_secret: str) -> dict:
|
|
220
|
+
return self._json("POST", "/api/v1/auth/cli/poll",
|
|
221
|
+
{"login_id": login_id, "device_secret": device_secret})
|
|
222
|
+
|
|
223
|
+
def session_info(self) -> dict:
|
|
224
|
+
return self._request("GET", "/api/v1/auth/session")
|
|
225
|
+
|
|
226
|
+
def me(self) -> dict:
|
|
227
|
+
"""The signed-in account (engine 0.7.0a8): new_account, terms_required, ..."""
|
|
228
|
+
return self._request("GET", "/api/v1/me")
|
|
229
|
+
|
|
230
|
+
def identity_challenge(self, public_key: str, level: str,
|
|
231
|
+
show_email_domain: bool = False) -> dict:
|
|
232
|
+
return self._json("POST", "/api/v1/identity/challenges",
|
|
233
|
+
{"public_key": public_key, "level": level,
|
|
234
|
+
"show_email_domain": show_email_domain})
|
|
235
|
+
|
|
236
|
+
def identity_attest(self, challenge_id: str, signature: str) -> dict:
|
|
237
|
+
return self._json("POST", "/api/v1/identity/attestations",
|
|
238
|
+
{"challenge_id": challenge_id, "signature": signature})
|
|
239
|
+
|
|
240
|
+
def revoke(self, document: dict, signatures: list[dict]) -> dict:
|
|
241
|
+
return self._json("POST", "/api/v1/revocations",
|
|
242
|
+
{"document": document, "signatures": signatures})
|
|
243
|
+
|
|
244
|
+
def key_identity(self, fingerprint: str) -> dict:
|
|
245
|
+
return self._request("GET", f"/api/v1/public/keys/{quote(fingerprint)}/identity")
|
|
246
|
+
|
|
247
|
+
def public_record(self, record_id: str) -> dict:
|
|
248
|
+
return self._request("GET", f"/api/v1/public/records/{quote(record_id)}")
|
|
249
|
+
|
|
250
|
+
def public_imprint(self, imprint_id: str) -> dict:
|
|
251
|
+
return self._request("GET", f"/api/v1/public/imprints/{quote(imprint_id)}")
|
|
252
|
+
|
|
253
|
+
# ownership claims and coverage (engine 0.7.0a4) --------------------------------------
|
|
254
|
+
def claim_prepare(self, project: str, document: dict) -> dict:
|
|
255
|
+
return self._json("POST", f"/api/v1/projects/{quote(project)}/claims/prepare",
|
|
256
|
+
{"document": document})
|
|
257
|
+
|
|
258
|
+
def claim_finalise(self, project: str, document: dict, signatures: list[dict]) -> dict:
|
|
259
|
+
return self._json("POST", f"/api/v1/projects/{quote(project)}/claims/finalise",
|
|
260
|
+
{"document": document, "signatures": signatures})
|
|
261
|
+
|
|
262
|
+
def claim_key(self, project: str, claim: str, document: dict,
|
|
263
|
+
signatures: list[dict]) -> dict:
|
|
264
|
+
return self._json("POST", f"/api/v1/projects/{quote(project)}/claims/{quote(claim)}"
|
|
265
|
+
"/keys", {"document": document, "signatures": signatures})
|
|
266
|
+
|
|
267
|
+
def assignment(self, project: str, document: dict, signatures: list[dict]) -> dict:
|
|
268
|
+
return self._json("POST", f"/api/v1/projects/{quote(project)}/assignments",
|
|
269
|
+
{"document": document, "signatures": signatures})
|
|
270
|
+
|
|
271
|
+
def public_ownership(self, project: str, after: int | None = None) -> dict:
|
|
272
|
+
query = f"?after={int(after)}" if after is not None else ""
|
|
273
|
+
return self._json("GET", f"/api/v1/public/projects/{quote(project)}/ownership{query}")
|
|
274
|
+
|
|
275
|
+
def public_org(self, org: str) -> dict:
|
|
276
|
+
return self._request("GET", f"/api/v1/public/orgs/{quote(org)}")
|
|
277
|
+
|
|
278
|
+
# organisations, domains, memberships, disclosure, contributors (engine 0.7.0a3+) -----
|
|
279
|
+
def org_create(self, public_key: str, signature: str) -> dict:
|
|
280
|
+
return self._json("POST", "/api/v1/orgs", {"public_key": public_key,
|
|
281
|
+
"signature": signature})
|
|
282
|
+
|
|
283
|
+
def org_domain_challenge(self, org: str, domain: str, public_key: str) -> dict:
|
|
284
|
+
return self._json("POST", f"/api/v1/orgs/{quote(org)}/domains",
|
|
285
|
+
{"domain": domain, "public_key": public_key})
|
|
286
|
+
|
|
287
|
+
def org_domain_verify(self, org: str, domain: str, signature: str) -> dict:
|
|
288
|
+
return self._json("POST", f"/api/v1/orgs/{quote(org)}/domains/verify",
|
|
289
|
+
{"domain": domain, "signature": signature})
|
|
290
|
+
|
|
291
|
+
def org_disclosure_document(self, org: str, domain: str) -> dict:
|
|
292
|
+
return self._json("GET", f"/api/v1/orgs/{quote(org)}/disclosures/document?domain="
|
|
293
|
+
f"{quote(domain)}")
|
|
294
|
+
|
|
295
|
+
def org_disclose(self, org: str, domain: str, public_key: str, signature: str) -> dict:
|
|
296
|
+
return self._json("POST", f"/api/v1/orgs/{quote(org)}/disclosures",
|
|
297
|
+
{"domain": domain, "public_key": public_key, "signature": signature})
|
|
298
|
+
|
|
299
|
+
def org_member_prepare(self, org: str, member_public_key: str, basis: str) -> dict:
|
|
300
|
+
return self._json("POST", f"/api/v1/orgs/{quote(org)}/members/prepare",
|
|
301
|
+
{"member_public_key": member_public_key, "basis": basis})
|
|
302
|
+
|
|
303
|
+
def org_member_finalise(self, org: str, pending_id: str, signatures: list[dict]) -> dict:
|
|
304
|
+
return self._json("POST", f"/api/v1/orgs/{quote(org)}/members/finalise",
|
|
305
|
+
{"pending_id": pending_id, "signatures": signatures})
|
|
306
|
+
|
|
307
|
+
def org_member_end(self, org: str, membership: str, valid_until: str,
|
|
308
|
+
signatures: list[dict]) -> dict:
|
|
309
|
+
return self._json("POST", f"/api/v1/orgs/{quote(org)}/members/end",
|
|
310
|
+
{"membership": membership, "valid_until": valid_until,
|
|
311
|
+
"signatures": signatures})
|
|
312
|
+
|
|
313
|
+
def contributor(self, project: str, document: dict, signatures: list[dict]) -> dict:
|
|
314
|
+
return self._json("POST", f"/api/v1/projects/{quote(project)}/contributors",
|
|
315
|
+
{"document": document, "signatures": signatures})
|