glazed-oxidized 0.1.0
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.
- checksums.yaml +7 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +8 -0
- data/LICENSE.txt +202 -0
- data/README.md +16 -0
- data/Rakefile +12 -0
- data/lib/glazed/oxidized/bridge.rb +148 -0
- data/lib/glazed/oxidized/errors.rb +21 -0
- data/lib/glazed/oxidized/model_box.rb +115 -0
- data/lib/glazed/oxidized/model_dialect.rb +119 -0
- data/lib/glazed/oxidized/node_view.rb +69 -0
- data/lib/glazed/oxidized/prelude.rb +708 -0
- data/lib/glazed/oxidized/secret_substitution.rb +105 -0
- data/lib/glazed/oxidized/version.rb +11 -0
- data/lib/glazed/oxidized.rb +93 -0
- metadata +71 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1f2ea93455706c7b24047955aa5568d5e54275437ba028dfedea25975a06d8cc
|
|
4
|
+
data.tar.gz: 2c16dd38d731d7c326b1fe52f11eb280d1e08f6dcb39304321e473371d9d88cb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 29c083a2ab28aa4413476bed9b18a2982663aa51083c55db244f0e6440459f63f9d65cde4e82ef8b409947148bf135fd6b9b2a84722a7342b2c4c1595a0f571b
|
|
7
|
+
data.tar.gz: 387474290b6096c7d4df7dc24ecff6256c74024a9c10665a73f0c02272ce5322eaa7d77aac817b3fc497e22f22df32f49eab56350c4cc671f391f6cfc24c90f6
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
## [0.1.0] - 2026-10-14
|
|
2
|
+
|
|
3
|
+
- Run Oxidized model files as `oxidized_<model>` dialects, each loaded into its own Ruby::Box.
|
|
4
|
+
- Turn the substitutions in models' `cmd :secret` blocks into sensitive patterns for the `redact` step.
|
|
5
|
+
|
|
6
|
+
## [0.1.0] - 2025-03-29
|
|
7
|
+
|
|
8
|
+
- Initial release
|
data/LICENSE.txt
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# glazed-oxidized
|
|
2
|
+
|
|
3
|
+
Runs [Oxidized](https://github.com/ytti/oxidized) model files as [glazed](https://github.com/sorah/glazed) dialects. A node whose dialect is `oxidized_<model>` runs the model file `<model>.rb`, such as `oxidized_eos` for `eos.rb`. Each model file is loaded into its own [Ruby::Box](https://docs.ruby-lang.org/en/4.0/Ruby/Box.html), so whatever it defines at the top level, patches into core classes or sets as globals stays out of glazed.
|
|
4
|
+
|
|
5
|
+
Requires Ruby 4.0 or later, started with `RUBY_BOX=1`, and Oxidized model files from the `oxidized` gem or a directory.
|
|
6
|
+
|
|
7
|
+
```jsonc
|
|
8
|
+
// glazed.json
|
|
9
|
+
{"requires": ["glazed/oxidized"], ...}
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
RUBY_BOX=1 bundle exec glazed run -c glazed.json
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The substitutions in a model's `cmd :secret` blocks become the dialect's sensitive patterns, so the `redact` step hides the secrets the model knows about. See [docs/oxidized-shim.md](../docs/oxidized-shim.md) for usage, node variables, secrets and limitations.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
|
|
6
|
+
require 'glazed/cli_session'
|
|
7
|
+
require 'glazed/dialect/run'
|
|
8
|
+
require 'glazed/oxidized/errors'
|
|
9
|
+
require 'glazed/oxidized/model_box'
|
|
10
|
+
require 'glazed/oxidized/node_view'
|
|
11
|
+
|
|
12
|
+
module Glazed
|
|
13
|
+
module Oxidized
|
|
14
|
+
# Connects one CLI session to an instance of an Oxidized model, so that the
|
|
15
|
+
# model and its inputs reach the device through glazed.
|
|
16
|
+
#
|
|
17
|
+
# A bridge is created on first use for a session, which may be an
|
|
18
|
+
# interaction during login, and is attached to the run after login.
|
|
19
|
+
class Bridge
|
|
20
|
+
@registry = ObjectSpace::WeakKeyMap.new #: ObjectSpace::WeakKeyMap
|
|
21
|
+
@registry_lock = Mutex.new #: Thread::Mutex
|
|
22
|
+
|
|
23
|
+
# Finds or creates the bridge of a session.
|
|
24
|
+
#
|
|
25
|
+
# @param session [CliSession] session to the device
|
|
26
|
+
# @param box [ModelBox] model the session runs
|
|
27
|
+
# @return [Bridge] the session's bridge
|
|
28
|
+
#: (CliSession session, ModelBox box) -> Bridge
|
|
29
|
+
def self.for(session, box)
|
|
30
|
+
@registry_lock.synchronize { @registry[session] ||= new(session:, box:) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# @param session [CliSession] session to the device
|
|
34
|
+
# @param box [ModelBox] model the session runs
|
|
35
|
+
#: (session: CliSession, box: ModelBox) -> void
|
|
36
|
+
def initialize(session:, box:)
|
|
37
|
+
@session = session #: CliSession
|
|
38
|
+
@run = nil #: Dialect::Run?
|
|
39
|
+
@model = box.new_model #: untyped
|
|
40
|
+
@model.input = box.new_generic_input.glazed_attach(self, nil)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @return [CliSession] session to the device
|
|
44
|
+
#: CliSession
|
|
45
|
+
attr_reader :session
|
|
46
|
+
# @return [Object] the model instance
|
|
47
|
+
#: untyped
|
|
48
|
+
attr_reader :model
|
|
49
|
+
|
|
50
|
+
# Binds the model and the input configured for the transport to a run.
|
|
51
|
+
#
|
|
52
|
+
# @param run [Dialect::Run] the run in progress
|
|
53
|
+
# @param input [Object] the box's input, with the model's cfg blocks evaluated
|
|
54
|
+
# @return [void]
|
|
55
|
+
#: (Dialect::Run run, untyped input) -> void
|
|
56
|
+
def attach(run, input)
|
|
57
|
+
@run = run
|
|
58
|
+
node = NodeView.new(node: run.node, prompt: session.prompt)
|
|
59
|
+
input.glazed_attach(self, node)
|
|
60
|
+
model.node = node
|
|
61
|
+
model.input = input
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Collects the configuration through the model.
|
|
65
|
+
#
|
|
66
|
+
# @return [String] the configuration
|
|
67
|
+
# @raise [Error] when a command of the model returned no output
|
|
68
|
+
#: () -> String
|
|
69
|
+
def get
|
|
70
|
+
model.glazed_get or raise Error, 'the Oxidized model returned no configuration'
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Applies one of the model's expect blocks to the session buffer.
|
|
74
|
+
#
|
|
75
|
+
# @param index [Integer] position of the block among the model's expects
|
|
76
|
+
# @param buffer [String] copy of the session buffer
|
|
77
|
+
# @return [String] the new buffer, as bytes
|
|
78
|
+
#: (Integer index, String buffer) -> String
|
|
79
|
+
def apply_expect(index, buffer) = model.glazed_expect(index, buffer).to_s.b
|
|
80
|
+
|
|
81
|
+
# Sends raw data.
|
|
82
|
+
#
|
|
83
|
+
# @param data [String] data to send
|
|
84
|
+
# @return [void]
|
|
85
|
+
#: (String data) -> void
|
|
86
|
+
def write(data)
|
|
87
|
+
session.write(data)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# @return [String] data received but not consumed yet
|
|
91
|
+
#: () -> String
|
|
92
|
+
def output = session.buffer.dup
|
|
93
|
+
|
|
94
|
+
# Waits for one of the patterns, consuming the buffer up to the match.
|
|
95
|
+
#
|
|
96
|
+
# @param patterns [Array<Regexp>] patterns
|
|
97
|
+
# @return [Regexp] the pattern that matched
|
|
98
|
+
# @raise [TimeoutError] when nothing matches in time
|
|
99
|
+
#: (Array[Regexp] patterns) -> Regexp
|
|
100
|
+
def expect(patterns)
|
|
101
|
+
cli = session #: as untyped
|
|
102
|
+
cli.expect(*patterns).regexp
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Runs a command the way Oxidized inputs do: on a shell, the output
|
|
106
|
+
# includes the echoed command and the prompt, which models cut.
|
|
107
|
+
#
|
|
108
|
+
# @param line [String] command line
|
|
109
|
+
# @param expect [Regexp, Array<Regexp>, nil] patterns ending the output; nil only sends the line
|
|
110
|
+
# @param exec [Boolean] run on an exec channel when the transport supports it
|
|
111
|
+
# @return [String] the output, in UTF-8
|
|
112
|
+
# @raise [TimeoutError] when nothing matches in time
|
|
113
|
+
# @raise [InvalidEncoding] when the output is not valid UTF-8
|
|
114
|
+
#: (String line, Regexp | Array[Regexp] | nil expect, exec: bool) -> String
|
|
115
|
+
def cmd(line, expect, exec:)
|
|
116
|
+
unless expect
|
|
117
|
+
write("#{line}\n")
|
|
118
|
+
return +''
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
transport = session.transport
|
|
122
|
+
raw = if exec && transport.exec?
|
|
123
|
+
transport.exec(line, timeout: session.timeout).b
|
|
124
|
+
else
|
|
125
|
+
cli = session #: as untyped
|
|
126
|
+
cli.discard
|
|
127
|
+
write("#{line}\n")
|
|
128
|
+
match = cli.expect(*Array(expect)) #: MatchData
|
|
129
|
+
match.pre_match + match[0].to_s
|
|
130
|
+
end
|
|
131
|
+
normalize(line, raw)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# @param line [String] command line
|
|
135
|
+
# @param raw [String] output as bytes
|
|
136
|
+
# @return [String] the output after the dialect's output filters, in UTF-8
|
|
137
|
+
# @raise [InvalidEncoding] when the output is not valid UTF-8
|
|
138
|
+
#: (String line, String raw) -> String
|
|
139
|
+
private def normalize(line, raw)
|
|
140
|
+
filters = @run&.dialect&.output_filters || []
|
|
141
|
+
output = filters.inject(raw) { |text, output_filter| output_filter.call(text) }.dup.force_encoding(Encoding::UTF_8)
|
|
142
|
+
raise InvalidEncoding, "output of #{line.inspect} is not valid UTF-8" unless output.valid_encoding?
|
|
143
|
+
|
|
144
|
+
output
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
|
|
6
|
+
require 'glazed'
|
|
7
|
+
|
|
8
|
+
module Glazed
|
|
9
|
+
module Oxidized
|
|
10
|
+
# Base class for errors raised by glazed-oxidized.
|
|
11
|
+
class Error < Glazed::Error; end
|
|
12
|
+
# Raised when Ruby::Box is unavailable.
|
|
13
|
+
class UnavailableError < Error; end
|
|
14
|
+
# Raised when a model file defines no model class by its name.
|
|
15
|
+
class ModelNotFound < Error; end
|
|
16
|
+
# Raised when a model has no cfg block for the transport of a node.
|
|
17
|
+
class UnsupportedTransport < Error; end
|
|
18
|
+
# Raised when a model's `cmd :secret` blocks cannot be turned into sensitive patterns.
|
|
19
|
+
class UnredactableModel < Error; end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
|
|
6
|
+
require 'glazed/must'
|
|
7
|
+
require 'glazed/oxidized/errors'
|
|
8
|
+
require 'glazed/oxidized/secret_substitution'
|
|
9
|
+
|
|
10
|
+
module Glazed
|
|
11
|
+
module Oxidized
|
|
12
|
+
# An Oxidized model file loaded into its own Ruby::Box, together with the
|
|
13
|
+
# emulated Oxidized API it builds on, so that its top-level classes,
|
|
14
|
+
# monkey patches and globals stay out of the main box.
|
|
15
|
+
#
|
|
16
|
+
# @example
|
|
17
|
+
# box = Glazed::Oxidized::ModelBox.new(name: 'ios', path: '/path/to/model/ios.rb')
|
|
18
|
+
# box.model_class.prompt #=> /^([\w.@()-]+#\s?)$/
|
|
19
|
+
class ModelBox
|
|
20
|
+
# File loaded into each box before the model.
|
|
21
|
+
PRELUDE = File.expand_path('prelude.rb', __dir__) #: String
|
|
22
|
+
|
|
23
|
+
# Input class names inside the box, by glazed transport name.
|
|
24
|
+
INPUTS = {ssh: :SSH, telnet: :Telnet}.freeze #: Hash[Symbol, Symbol]
|
|
25
|
+
|
|
26
|
+
# @param name [String] model name, the file name without extension
|
|
27
|
+
# @param path [String] path of the model file
|
|
28
|
+
# @raise [ModelNotFound] when the file defines no model class by that name
|
|
29
|
+
# @raise [UnavailableError] when Ruby::Box is not enabled
|
|
30
|
+
#: (name: String, path: String) -> void
|
|
31
|
+
def initialize(name:, path:)
|
|
32
|
+
ModelBox.ensure_enabled!
|
|
33
|
+
@name = name #: String
|
|
34
|
+
@path = path #: String
|
|
35
|
+
@box = Ruby::Box.new #: Ruby::Box[top]
|
|
36
|
+
@box.require(PRELUDE)
|
|
37
|
+
@box.require(path)
|
|
38
|
+
@model_class = find_model_class #: untyped
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# @return [String] model name
|
|
42
|
+
#: String
|
|
43
|
+
attr_reader :name
|
|
44
|
+
# @return [String] path of the model file
|
|
45
|
+
#: String
|
|
46
|
+
attr_reader :path
|
|
47
|
+
# @return [Class] the model class, a subclass of the box's Oxidized::Model
|
|
48
|
+
#: untyped
|
|
49
|
+
attr_reader :model_class
|
|
50
|
+
|
|
51
|
+
# @raise [UnavailableError] when Ruby::Box is not enabled
|
|
52
|
+
# @return [void]
|
|
53
|
+
#: () -> void
|
|
54
|
+
def self.ensure_enabled!
|
|
55
|
+
return if defined?(Ruby::Box) && Ruby::Box.enabled?
|
|
56
|
+
|
|
57
|
+
raise UnavailableError, 'glazed-oxidized loads models into Ruby::Box; run Ruby 4.0+ with RUBY_BOX=1'
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @return [Object] a new instance of the model
|
|
61
|
+
#: () -> untyped
|
|
62
|
+
def new_model = model_class.new
|
|
63
|
+
|
|
64
|
+
# @return [Array<SecretSubstitution>] substitutions of the model's `cmd :secret` blocks, in order
|
|
65
|
+
# @raise [UnredactableModel] when a block does anything but substitute strings for patterns
|
|
66
|
+
#: () -> Array[SecretSubstitution]
|
|
67
|
+
def secret_substitutions
|
|
68
|
+
recorder = oxidized.const_get(:SecretRecorder)
|
|
69
|
+
begin
|
|
70
|
+
recorder.record(model_class).map { |pattern, replacement| SecretSubstitution.new(pattern:, replacement:) }
|
|
71
|
+
rescue recorder.const_get(:Unrecordable) => e
|
|
72
|
+
raise UnredactableModel, "cmd :secret blocks of Oxidized model #{name} #{e.message}"
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @return [Object] a new instance of the box's Oxidized::Input
|
|
77
|
+
#: () -> untyped
|
|
78
|
+
def new_generic_input = oxidized.const_get(:Input).new
|
|
79
|
+
|
|
80
|
+
# Builds an input with the model's cfg blocks for a transport evaluated on it.
|
|
81
|
+
#
|
|
82
|
+
# @param kind [Symbol] glazed transport name
|
|
83
|
+
# @return [Object, nil] the input, or nil when the model has no cfg block for the transport
|
|
84
|
+
#: (Symbol kind) -> untyped
|
|
85
|
+
def configured_input(kind)
|
|
86
|
+
class_name = INPUTS[kind] or return nil
|
|
87
|
+
blocks = model_class.cfgs.fetch(kind.to_s, [])
|
|
88
|
+
return nil if blocks.empty?
|
|
89
|
+
|
|
90
|
+
input = oxidized.const_get(class_name).new
|
|
91
|
+
blocks.each { input.instance_exec(&it) }
|
|
92
|
+
input
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# @return [Module] the box's Oxidized module
|
|
96
|
+
#: () -> Module[untyped]
|
|
97
|
+
private def oxidized = Must.be_a(@box.const_get(:Oxidized), Module)
|
|
98
|
+
|
|
99
|
+
# Finds the class named after the file, ignoring case, as Oxidized does.
|
|
100
|
+
#
|
|
101
|
+
# @return [Class] the model class
|
|
102
|
+
# @raise [ModelNotFound] when there is none
|
|
103
|
+
#: () -> untyped
|
|
104
|
+
private def find_model_class
|
|
105
|
+
constant = @box.constants.find { it.to_s.casecmp?(name) }
|
|
106
|
+
klass = constant && @box.const_get(constant)
|
|
107
|
+
unless klass.is_a?(Class) && klass < oxidized.const_get(:Model)
|
|
108
|
+
raise ModelNotFound, "#{path} defines no Oxidized model named #{name}"
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
klass
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
|
|
6
|
+
require 'glazed/dialects/base'
|
|
7
|
+
require 'glazed/oxidized/errors'
|
|
8
|
+
require 'glazed/oxidized/bridge'
|
|
9
|
+
require 'glazed/oxidized/model_box'
|
|
10
|
+
|
|
11
|
+
module Glazed
|
|
12
|
+
module Oxidized
|
|
13
|
+
# Base class of dialects that run an Oxidized model.
|
|
14
|
+
#
|
|
15
|
+
# The model's prompt, comment prefix, expect blocks and login prompts map
|
|
16
|
+
# to dialect declarations. Commands, pre and post blocks, and cfg blocks
|
|
17
|
+
# run inside the model's box, as one :get runbook step. The substitutions
|
|
18
|
+
# of the model's `cmd :secret` blocks become sensitive patterns, and the
|
|
19
|
+
# blocks themselves run when the node param "remove_secret" is "true".
|
|
20
|
+
class ModelDialect < Dialects::Base
|
|
21
|
+
# Prompt Oxidized uses for models that declare none.
|
|
22
|
+
DEFAULT_PROMPT = /^([\w.@-]+[#>]\s?)$/ #: Regexp
|
|
23
|
+
|
|
24
|
+
@model_box = nil #: ModelBox?
|
|
25
|
+
@unredactable = nil #: String?
|
|
26
|
+
|
|
27
|
+
class << self
|
|
28
|
+
# Builds a dialect class for a loaded model.
|
|
29
|
+
#
|
|
30
|
+
# @param box [ModelBox] the loaded model
|
|
31
|
+
# @return [Class<ModelDialect>] the dialect
|
|
32
|
+
# @example
|
|
33
|
+
# Glazed::Oxidized::ModelDialect.build(Glazed::Oxidized::ModelBox.new(name: 'ios', path:))
|
|
34
|
+
#: (ModelBox box) -> singleton(ModelDialect)
|
|
35
|
+
def build(box)
|
|
36
|
+
dialect = Class.new(self)
|
|
37
|
+
dialect.declare(box)
|
|
38
|
+
dialect
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# @return [ModelBox] the model this dialect runs
|
|
42
|
+
# @raise [Must::Violation] on {ModelDialect} itself
|
|
43
|
+
#: () -> ModelBox
|
|
44
|
+
def model_box = Must.expect(@model_box)
|
|
45
|
+
|
|
46
|
+
# @return [Array<Glazed::Dialect::SensitivePattern>] patterns from the model's `cmd :secret` blocks
|
|
47
|
+
# @raise [UnredactableModel] when the blocks cannot be turned into patterns
|
|
48
|
+
#: () -> Array[Glazed::Dialect::SensitivePattern]
|
|
49
|
+
def sensitive_patterns
|
|
50
|
+
reason = @unredactable
|
|
51
|
+
raise UnredactableModel, reason if reason
|
|
52
|
+
|
|
53
|
+
super
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @param box [ModelBox] the model to run
|
|
57
|
+
# @return [void]
|
|
58
|
+
#: (ModelBox box) -> void
|
|
59
|
+
protected def declare(box)
|
|
60
|
+
@model_box = box
|
|
61
|
+
model = box.model_class
|
|
62
|
+
prompt(model.prompt || DEFAULT_PROMPT)
|
|
63
|
+
comment_prefix(model.comment)
|
|
64
|
+
model.expects.each_with_index do |(pattern, _block), index|
|
|
65
|
+
interact(pattern) do |cli, buffer, _match|
|
|
66
|
+
Glazed::Dialect::Actions::Replace.new(content: Bridge.for(cli, box).apply_expect(index, buffer))
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
declare_secrets(box)
|
|
70
|
+
ModelBox::INPUTS.each_key do |kind|
|
|
71
|
+
communication(kind) { |config| configure(config, kind) }
|
|
72
|
+
end
|
|
73
|
+
runbook(:get) do |book|
|
|
74
|
+
book.raw("oxidized model #{box.name}") { |run| run.emit(Bridge.for(run.cli, box).get) }
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Declares a sensitive pattern for each substitution of the model's
|
|
79
|
+
# `cmd :secret` blocks. Blocks that do anything else make
|
|
80
|
+
# {sensitive_patterns} raise, so redaction fails rather than leaks.
|
|
81
|
+
#
|
|
82
|
+
# @param box [ModelBox] the loaded model
|
|
83
|
+
# @return [void]
|
|
84
|
+
#: (ModelBox box) -> void
|
|
85
|
+
private def declare_secrets(box)
|
|
86
|
+
box.secret_substitutions.each do |substitution|
|
|
87
|
+
sensitive_pattern(substitution.pattern) { substitution.offsets(it) }
|
|
88
|
+
end
|
|
89
|
+
rescue UnredactableModel => e
|
|
90
|
+
@unredactable = e.message
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Applies the model's cfg blocks for a transport to a communication config.
|
|
94
|
+
#
|
|
95
|
+
# @param config [Glazed::Dialect::CommunicationConfig] config to fill
|
|
96
|
+
# @param kind [Symbol] transport name
|
|
97
|
+
# @return [void]
|
|
98
|
+
#: (Glazed::Dialect::CommunicationConfig config, Symbol kind) -> void
|
|
99
|
+
private def configure(config, kind)
|
|
100
|
+
box = model_box
|
|
101
|
+
input = box.configured_input(kind)
|
|
102
|
+
unless input
|
|
103
|
+
config.post_login { raise UnsupportedTransport, "Oxidized model #{box.name} has no cfg block for #{kind}" }
|
|
104
|
+
return
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
config.username_prompt = input.glazed_username_prompt
|
|
108
|
+
config.password_prompt = input.glazed_password_prompt
|
|
109
|
+
config.exec = input.glazed_exec?
|
|
110
|
+
config.post_login do |run|
|
|
111
|
+
Bridge.for(run.cli, box).attach(run, input)
|
|
112
|
+
input.connect_cli
|
|
113
|
+
end
|
|
114
|
+
config.pre_logout { input.disconnect_cli }
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|