@libo11y/spec 0.0.0-stage → 0.1.2
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.
- package/LICENSE +202 -0
- package/README.md +84 -2
- package/dist/appregistry.d.ts +61 -0
- package/dist/appregistry.js +152 -0
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +95 -0
- package/dist/generated/goprint.d.ts +5 -0
- package/dist/generated/goprint.js +108 -0
- package/dist/generated/spec.d.ts +218 -0
- package/dist/generated/spec.js +191 -0
- package/dist/generated/tables.d.ts +41 -0
- package/dist/generated/tables.js +176 -0
- package/dist/goquote.d.ts +2 -0
- package/dist/goquote.js +59 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/yaml.d.ts +19 -0
- package/dist/yaml.js +491 -0
- package/dist/yamlv3.d.ts +36 -0
- package/dist/yamlv3.js +335 -0
- package/package.json +36 -4
package/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 [yyyy] [name of copyright owner]
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,85 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @libo11y/spec
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The libo11y telemetry specification for TypeScript. It holds the attribute
|
|
4
|
+
keys, enums, event and metric names, and sensitivity tables generated from
|
|
5
|
+
the libo11y spec registry, and a validator for application attribute
|
|
6
|
+
registries. It runs on Node, Bun and in browsers. The root entry point has
|
|
7
|
+
no dependencies.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @libo11y/spec
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
| Entry point | What it holds |
|
|
14
|
+
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
15
|
+
| `@libo11y/spec` | the generated constants and tables, `SPEC_VERSION`, `SEMCONV_VERSION`, `SCHEMA_URL`, `validateAppRegistry` and `compileAppRegistry`. It imports no other module |
|
|
16
|
+
| `@libo11y/spec/yaml` | `parseAppRegistry` and `parseAppRegistryBytes`. It is the only entry point that imports the `yaml` package, which the install brings in |
|
|
17
|
+
| the `libo11y` bin | `libo11y registry check FILE...` |
|
|
18
|
+
|
|
19
|
+
## Spec constants
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { ATTR_OUTCOME, SCHEMA_URL, SENSITIVITY_OF } from "@libo11y/spec";
|
|
23
|
+
|
|
24
|
+
SENSITIVITY_OF.get("o11y.tenant.id"); // "internal"
|
|
25
|
+
SENSITIVITY_OF.get(ATTR_OUTCOME); // "public", for o11y.outcome
|
|
26
|
+
SCHEMA_URL; // "https://schemas.o11y.ing/libo11y/1.2.0"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Application registries
|
|
30
|
+
|
|
31
|
+
An application registry declares an application's own namespaced
|
|
32
|
+
attributes with their type, sensitivity and cardinality:
|
|
33
|
+
|
|
34
|
+
```yaml
|
|
35
|
+
schema: libo11y.app-registry/v1
|
|
36
|
+
namespace: shop
|
|
37
|
+
attributes:
|
|
38
|
+
shop.order.id: { type: string, sensitivity: internal, cardinality: unbounded }
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`validateAppRegistry` applies the libo11y policy rules with the same rule
|
|
42
|
+
IDs and messages as the Go implementation. `compileAppRegistry` turns a
|
|
43
|
+
valid registry into a frozen table of sensitivity classes, which a browser
|
|
44
|
+
build can embed as JSON:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { compileAppRegistry, validateAppRegistry } from "@libo11y/spec";
|
|
48
|
+
|
|
49
|
+
const result = validateAppRegistry(registry);
|
|
50
|
+
if (!result.ok) for (const v of result.violations) console.error(v.toString());
|
|
51
|
+
const compiled = compileAppRegistry(registry); // throws AppRegistryError when invalid
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The `@libo11y/spec/yaml` subpath reads a registry file. It decodes the
|
|
55
|
+
YAML exactly as the Go implementation does, and it is the only module that
|
|
56
|
+
depends on a YAML parser:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { parseAppRegistryBytes } from "@libo11y/spec/yaml";
|
|
60
|
+
import { readFileSync } from "node:fs";
|
|
61
|
+
|
|
62
|
+
const registry = parseAppRegistryBytes(readFileSync("o11y-registry.yaml"));
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
A file that does not decode throws `AppRegistryParseError`, whose `kind`
|
|
66
|
+
names the problem: `empty`, `second-document`, `duplicate-key`,
|
|
67
|
+
`unknown-key`, `type` or `syntax`.
|
|
68
|
+
|
|
69
|
+
## The libo11y command
|
|
70
|
+
|
|
71
|
+
`libo11y registry check FILE...` validates registry files, for use in CI.
|
|
72
|
+
It prints the same lines and exits with the same status as the Go binary:
|
|
73
|
+
0 when every file is valid, 1 when a file breaks a policy rule, and 2 on a
|
|
74
|
+
usage, read or parse error.
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
npx libo11y registry check o11y-registry.yaml
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Reference
|
|
81
|
+
|
|
82
|
+
The specification itself is
|
|
83
|
+
[docs/SPEC.md](https://github.com/jamesainslie/libo11y/blob/main/docs/SPEC.md).
|
|
84
|
+
[docs/PARITY.md](https://github.com/jamesainslie/libo11y/blob/main/docs/PARITY.md)
|
|
85
|
+
lists where the TypeScript packages match Go and where they differ.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Application attribute registries: the file in which a libo11y consumer declares its own namespaced
|
|
3
|
+
* attributes with their type, sensitivity and cardinality. validateAppRegistry applies the same rules as
|
|
4
|
+
* Go's appregistry.Validate, in the same order and with the same messages. Parsing the YAML file lives
|
|
5
|
+
* in the `@libo11y/spec/yaml` subpath, so this module has no dependencies and runs in a browser.
|
|
6
|
+
*/
|
|
7
|
+
import { type Sensitivity } from "./generated/tables.ts";
|
|
8
|
+
/** One declared application attribute. */
|
|
9
|
+
export interface AppRegistryAttribute {
|
|
10
|
+
readonly type: string;
|
|
11
|
+
readonly sensitivity: string;
|
|
12
|
+
readonly cardinality: string;
|
|
13
|
+
readonly values?: readonly string[];
|
|
14
|
+
readonly brief?: string;
|
|
15
|
+
}
|
|
16
|
+
/** A parsed application registry. */
|
|
17
|
+
export interface AppRegistry {
|
|
18
|
+
readonly schema: string;
|
|
19
|
+
readonly namespace: string;
|
|
20
|
+
readonly attributes: Readonly<Record<string, AppRegistryAttribute>>;
|
|
21
|
+
}
|
|
22
|
+
/** One rule a registry breaks. attribute is undefined for a rule about the registry as a whole. */
|
|
23
|
+
export declare class AppRegistryViolation {
|
|
24
|
+
readonly ruleId: string;
|
|
25
|
+
readonly attribute: string | undefined;
|
|
26
|
+
readonly detail: string;
|
|
27
|
+
constructor(ruleId: string, attribute: string | undefined, detail: string);
|
|
28
|
+
/** Renders the violation as Go's Violation.String does. */
|
|
29
|
+
toString(): string;
|
|
30
|
+
}
|
|
31
|
+
/** The result of validating a registry: the registry itself, or every violation in rule order. */
|
|
32
|
+
export type AppRegistryResult = {
|
|
33
|
+
readonly ok: true;
|
|
34
|
+
readonly registry: AppRegistry;
|
|
35
|
+
} | {
|
|
36
|
+
readonly ok: false;
|
|
37
|
+
readonly violations: readonly AppRegistryViolation[];
|
|
38
|
+
};
|
|
39
|
+
/** The build-time form of a valid registry: each attribute's sensitivity class. JSON-serializable. */
|
|
40
|
+
export interface CompiledAppRegistry {
|
|
41
|
+
readonly namespace: string;
|
|
42
|
+
readonly classes: Readonly<Record<string, Sensitivity>>;
|
|
43
|
+
/** Where the registry came from, such as its file path, for error messages. */
|
|
44
|
+
readonly source?: string;
|
|
45
|
+
}
|
|
46
|
+
/** Thrown by compileAppRegistry for a registry that breaks a rule. */
|
|
47
|
+
export declare class AppRegistryError extends Error {
|
|
48
|
+
readonly violations: readonly AppRegistryViolation[];
|
|
49
|
+
constructor(violations: readonly AppRegistryViolation[]);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Applies the policy rules and returns every violation: registry-level rules first, then each attribute
|
|
53
|
+
* in name order, each attribute's rules in a fixed order.
|
|
54
|
+
*/
|
|
55
|
+
export declare function validateAppRegistry(reg: AppRegistry): AppRegistryResult;
|
|
56
|
+
/**
|
|
57
|
+
* Validates reg and returns each attribute's sensitivity class, the form a browser build embeds. The
|
|
58
|
+
* classes object has no prototype, so a lookup never reaches Object.prototype, and both objects are
|
|
59
|
+
* frozen. Throws AppRegistryError when reg breaks a rule.
|
|
60
|
+
*/
|
|
61
|
+
export declare function compileAppRegistry(reg: AppRegistry): CompiledAppRegistry;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Application attribute registries: the file in which a libo11y consumer declares its own namespaced
|
|
3
|
+
* attributes with their type, sensitivity and cardinality. validateAppRegistry applies the same rules as
|
|
4
|
+
* Go's appregistry.Validate, in the same order and with the same messages. Parsing the YAML file lives
|
|
5
|
+
* in the `@libo11y/spec/yaml` subpath, so this module has no dependencies and runs in a browser.
|
|
6
|
+
*/
|
|
7
|
+
import { APP_REGISTRY_SCHEMA, APP_RESERVED_NAMESPACES } from "./generated/tables.js";
|
|
8
|
+
import { goQuote } from "./goquote.js";
|
|
9
|
+
/** One rule a registry breaks. attribute is undefined for a rule about the registry as a whole. */
|
|
10
|
+
export class AppRegistryViolation {
|
|
11
|
+
ruleId;
|
|
12
|
+
attribute;
|
|
13
|
+
detail;
|
|
14
|
+
constructor(ruleId, attribute, detail) {
|
|
15
|
+
this.ruleId = ruleId;
|
|
16
|
+
this.attribute = attribute;
|
|
17
|
+
this.detail = detail;
|
|
18
|
+
}
|
|
19
|
+
/** Renders the violation as Go's Violation.String does. */
|
|
20
|
+
toString() {
|
|
21
|
+
return this.attribute === undefined
|
|
22
|
+
? `${this.ruleId}: ${this.detail}`
|
|
23
|
+
: `${this.ruleId}: attribute ${this.attribute}: ${this.detail}`;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Thrown by compileAppRegistry for a registry that breaks a rule. */
|
|
27
|
+
export class AppRegistryError extends Error {
|
|
28
|
+
violations;
|
|
29
|
+
constructor(violations) {
|
|
30
|
+
super(`invalid application registry: ${violations.map((v) => v.toString()).join("; ")}`);
|
|
31
|
+
this.name = "AppRegistryError";
|
|
32
|
+
this.violations = violations;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// One snake_case segment.
|
|
36
|
+
const NAMESPACE_PATTERN = /^[a-z][a-z0-9]*(_[a-z0-9]+)*$/;
|
|
37
|
+
// A dotted snake_case name with at least two segments (Go specrules.ValidName).
|
|
38
|
+
const NAME_PATTERN = /^[a-z][a-z0-9]*(_[a-z0-9]+)*(\.[a-z][a-z0-9]*(_[a-z0-9]+)*)+$/;
|
|
39
|
+
const ATTRIBUTE_TYPES = new Set([
|
|
40
|
+
"string",
|
|
41
|
+
"boolean",
|
|
42
|
+
"int",
|
|
43
|
+
"double",
|
|
44
|
+
"string[]",
|
|
45
|
+
"boolean[]",
|
|
46
|
+
"int[]",
|
|
47
|
+
"double[]",
|
|
48
|
+
]);
|
|
49
|
+
const SENSITIVITIES = new Set(["public", "internal", "pii", "secret"]);
|
|
50
|
+
const CARDINALITIES = new Set(["enum", "bounded", "unbounded"]);
|
|
51
|
+
// Go's strings.TrimSpace: the Unicode White_Space property, which includes U+0085 and excludes U+FEFF.
|
|
52
|
+
function trimSpace(s) {
|
|
53
|
+
return s.replace(/^\p{White_Space}+|\p{White_Space}+$/gu, "");
|
|
54
|
+
}
|
|
55
|
+
// Orders strings by code point, which is UTF-8 byte order. The default sort compares UTF-16 code units.
|
|
56
|
+
function compareCodePoints(a, b) {
|
|
57
|
+
const ai = a[Symbol.iterator]();
|
|
58
|
+
const bi = b[Symbol.iterator]();
|
|
59
|
+
for (;;) {
|
|
60
|
+
const x = ai.next();
|
|
61
|
+
const y = bi.next();
|
|
62
|
+
if (x.done === true)
|
|
63
|
+
return y.done === true ? 0 : -1;
|
|
64
|
+
if (y.done === true)
|
|
65
|
+
return 1;
|
|
66
|
+
const d = (x.value.codePointAt(0) ?? 0) - (y.value.codePointAt(0) ?? 0);
|
|
67
|
+
if (d !== 0)
|
|
68
|
+
return d;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Applies the policy rules and returns every violation: registry-level rules first, then each attribute
|
|
73
|
+
* in name order, each attribute's rules in a fixed order.
|
|
74
|
+
*/
|
|
75
|
+
export function validateAppRegistry(reg) {
|
|
76
|
+
const violations = [];
|
|
77
|
+
const fail = (rule, attr, detail) => {
|
|
78
|
+
violations.push(new AppRegistryViolation(rule, attr, detail));
|
|
79
|
+
};
|
|
80
|
+
if (reg.schema !== APP_REGISTRY_SCHEMA) {
|
|
81
|
+
fail("libo11y_app_schema_unknown", undefined, `schema ${goQuote(reg.schema)}, want ${goQuote(APP_REGISTRY_SCHEMA)}`);
|
|
82
|
+
}
|
|
83
|
+
if (!NAMESPACE_PATTERN.test(reg.namespace)) {
|
|
84
|
+
fail("libo11y_app_namespace_invalid", undefined, `namespace ${goQuote(reg.namespace)} is not one snake_case segment`);
|
|
85
|
+
}
|
|
86
|
+
else if (APP_RESERVED_NAMESPACES.has(reg.namespace)) {
|
|
87
|
+
fail("libo11y_app_namespace_reserved", undefined, `namespace ${goQuote(reg.namespace)} belongs to libo11y, OpenTelemetry, or semconv`);
|
|
88
|
+
}
|
|
89
|
+
const names = Object.keys(reg.attributes).sort(compareCodePoints);
|
|
90
|
+
if (names.length === 0) {
|
|
91
|
+
fail("libo11y_app_attributes_empty", undefined, "the registry declares no attributes");
|
|
92
|
+
}
|
|
93
|
+
for (const name of names) {
|
|
94
|
+
const a = reg.attributes[name];
|
|
95
|
+
const values = a.values ?? [];
|
|
96
|
+
if (!name.startsWith(`${reg.namespace}.`)) {
|
|
97
|
+
fail("libo11y_app_attr_outside_namespace", name, `not under the ${reg.namespace} namespace`);
|
|
98
|
+
}
|
|
99
|
+
if (!NAME_PATTERN.test(name)) {
|
|
100
|
+
fail("libo11y_attr_naming", name, "not a dotted snake_case name with at least two segments");
|
|
101
|
+
}
|
|
102
|
+
if (!ATTRIBUTE_TYPES.has(a.type)) {
|
|
103
|
+
fail("libo11y_app_type_unknown", name, `type ${goQuote(a.type)}`);
|
|
104
|
+
}
|
|
105
|
+
if (a.sensitivity === "") {
|
|
106
|
+
fail("libo11y_sensitivity_missing", name, "no sensitivity class");
|
|
107
|
+
}
|
|
108
|
+
else if (!SENSITIVITIES.has(a.sensitivity)) {
|
|
109
|
+
fail("libo11y_sensitivity_unknown", name, `sensitivity ${goQuote(a.sensitivity)}`);
|
|
110
|
+
}
|
|
111
|
+
if (a.cardinality === "") {
|
|
112
|
+
fail("libo11y_cardinality_missing", name, "no cardinality budget");
|
|
113
|
+
}
|
|
114
|
+
else if (!CARDINALITIES.has(a.cardinality)) {
|
|
115
|
+
fail("libo11y_cardinality_unknown", name, `cardinality ${goQuote(a.cardinality)}`);
|
|
116
|
+
}
|
|
117
|
+
if (a.cardinality === "enum" && values.length === 0) {
|
|
118
|
+
fail("libo11y_app_enum_values_missing", name, "an enum declares its values");
|
|
119
|
+
}
|
|
120
|
+
else if (a.cardinality !== "enum" && values.length > 0) {
|
|
121
|
+
fail("libo11y_app_values_without_enum", name, "values need cardinality enum");
|
|
122
|
+
}
|
|
123
|
+
const seen = new Set();
|
|
124
|
+
for (const v of values) {
|
|
125
|
+
const trimmed = trimSpace(v);
|
|
126
|
+
if (v !== trimmed) {
|
|
127
|
+
fail("libo11y_enum_value_whitespace", name, `value ${goQuote(v)}`);
|
|
128
|
+
}
|
|
129
|
+
if (seen.has(trimmed)) {
|
|
130
|
+
fail("libo11y_enum_duplicate_value", name, `value ${goQuote(v)}`);
|
|
131
|
+
}
|
|
132
|
+
seen.add(trimmed);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return violations.length === 0 ? { ok: true, registry: reg } : { ok: false, violations };
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Validates reg and returns each attribute's sensitivity class, the form a browser build embeds. The
|
|
139
|
+
* classes object has no prototype, so a lookup never reaches Object.prototype, and both objects are
|
|
140
|
+
* frozen. Throws AppRegistryError when reg breaks a rule.
|
|
141
|
+
*/
|
|
142
|
+
export function compileAppRegistry(reg) {
|
|
143
|
+
const result = validateAppRegistry(reg);
|
|
144
|
+
if (!result.ok)
|
|
145
|
+
throw new AppRegistryError(result.violations);
|
|
146
|
+
const classes = Object.create(null);
|
|
147
|
+
for (const name of Object.keys(reg.attributes).sort(compareCodePoints)) {
|
|
148
|
+
// Validation guarantees every class is a known sensitivity.
|
|
149
|
+
classes[name] = reg.attributes[name].sensitivity;
|
|
150
|
+
}
|
|
151
|
+
return Object.freeze({ namespace: reg.namespace, classes: Object.freeze(classes) });
|
|
152
|
+
}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Runs the command line and returns the exit status. out and err each receive one line at a time,
|
|
4
|
+
* without its newline. readFile returns a registry file's bytes and throws a Node file system error.
|
|
5
|
+
*/
|
|
6
|
+
export declare function run(args: readonly string[], out: (line: string) => void, err: (line: string) => void, readFile: (path: string) => Uint8Array): number;
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
/**
|
|
4
|
+
* The libo11y command-line tool. `libo11y registry check FILE...` validates application attribute
|
|
5
|
+
* registries against the libo11y policy rules, for use in a consumer's CI. It matches the Go binary
|
|
6
|
+
* (go/cmd/libo11y).
|
|
7
|
+
*
|
|
8
|
+
* Exit status: 0 when every file is valid, 1 when a file breaks a policy rule, 2 on a usage, read, or
|
|
9
|
+
* parse error. The highest status wins.
|
|
10
|
+
*/
|
|
11
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { validateAppRegistry } from "./appregistry.js";
|
|
14
|
+
import { parseAppRegistryBytes } from "./yaml.js";
|
|
15
|
+
const USAGE = "usage: libo11y registry check FILE...";
|
|
16
|
+
function message(err) {
|
|
17
|
+
return err instanceof Error ? err.message : String(err);
|
|
18
|
+
}
|
|
19
|
+
// GO_READ_ERRORS maps a Node file system error code to Go's os.ReadFile error: the operation that
|
|
20
|
+
// failed and the reason Go prints.
|
|
21
|
+
const GO_READ_ERRORS = {
|
|
22
|
+
ENOENT: ["open", "no such file or directory"],
|
|
23
|
+
EACCES: ["open", "permission denied"],
|
|
24
|
+
EPERM: ["open", "operation not permitted"],
|
|
25
|
+
EISDIR: ["read", "is a directory"],
|
|
26
|
+
};
|
|
27
|
+
// readError renders a failed read the way the Go binary prints os.ReadFile's error.
|
|
28
|
+
function readError(path, err) {
|
|
29
|
+
const code = typeof err === "object" && err !== null && "code" in err ? String(err.code) : "";
|
|
30
|
+
const known = GO_READ_ERRORS[code];
|
|
31
|
+
return known === undefined ? `open ${path}: ${message(err)}` : `${known[0]} ${path}: ${known[1]}`;
|
|
32
|
+
}
|
|
33
|
+
// checkFile validates one registry file and returns its exit status.
|
|
34
|
+
function checkFile(path, out, err, readFile) {
|
|
35
|
+
let bytes;
|
|
36
|
+
try {
|
|
37
|
+
bytes = readFile(path);
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
err(readError(path, e));
|
|
41
|
+
return 2;
|
|
42
|
+
}
|
|
43
|
+
let reg;
|
|
44
|
+
try {
|
|
45
|
+
reg = parseAppRegistryBytes(bytes);
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
err(`${path}: ${message(e)}`);
|
|
49
|
+
return 2;
|
|
50
|
+
}
|
|
51
|
+
const result = validateAppRegistry(reg);
|
|
52
|
+
if (!result.ok) {
|
|
53
|
+
for (const v of result.violations)
|
|
54
|
+
out(`${path}: ${v.toString()}`);
|
|
55
|
+
return 1;
|
|
56
|
+
}
|
|
57
|
+
const n = Object.keys(reg.attributes).length;
|
|
58
|
+
out(`${path}: ok (${n} ${n === 1 ? "attribute" : "attributes"} in namespace ${reg.namespace})`);
|
|
59
|
+
return 0;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Runs the command line and returns the exit status. out and err each receive one line at a time,
|
|
63
|
+
* without its newline. readFile returns a registry file's bytes and throws a Node file system error.
|
|
64
|
+
*/
|
|
65
|
+
export function run(args, out, err, readFile) {
|
|
66
|
+
if (args.length === 1 && (args[0] === "-h" || args[0] === "--help" || args[0] === "help")) {
|
|
67
|
+
out(USAGE);
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
if (args.length < 3 || args[0] !== "registry" || args[1] !== "check") {
|
|
71
|
+
err(USAGE);
|
|
72
|
+
return 2;
|
|
73
|
+
}
|
|
74
|
+
let status = 0;
|
|
75
|
+
for (const path of args.slice(2)) {
|
|
76
|
+
status = Math.max(status, checkFile(path, out, err, readFile));
|
|
77
|
+
}
|
|
78
|
+
return status;
|
|
79
|
+
}
|
|
80
|
+
// isEntry reports whether this module is the program being run. It compares the resolved script path,
|
|
81
|
+
// which follows the bin symlink npm installs, because import.meta.main needs Node 24.2 or later.
|
|
82
|
+
function isEntry() {
|
|
83
|
+
const script = process.argv[1];
|
|
84
|
+
if (script === undefined)
|
|
85
|
+
return false;
|
|
86
|
+
try {
|
|
87
|
+
return realpathSync(script) === fileURLToPath(import.meta.url);
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (isEntry()) {
|
|
94
|
+
process.exit(run(process.argv.slice(2), (line) => process.stdout.write(`${line}\n`), (line) => process.stderr.write(`${line}\n`), (path) => readFileSync(path)));
|
|
95
|
+
}
|