Debian Codegraph @

doc/ref/bindings.html

raw
1 ---
2 layout: default
3 title: Bindings
4 ---
5
6 <div class="hgroup">
7 <div class="hgroup-inline">
8 <div class="panel">
9 <h1 id="bindings">Bindings</h1>
10 </div>
11 <div style="clear: both"></div>
12 </div>
13 </div>
14
15 <div class="hgroup">
16 <div class="hgroup-inline">
17 <div class="panel">
18 <p>
19 If you don't want to invoke the <tt>jsonnet</tt> commandline utility to evaluate Jsonnet,
20 there are also official libraries for Python, Go, and C. The C library wraps the C++
21 implementation, hiding all the C++ to make it easy to use Jsonnet from C. In turn, there is
22 a C++ wrapper around the C library to make it easy to use from client C++ code (surprisingly
23 enough, this actually makes sense)! There are also various unofficial bindings built on the
24 C API.
25 </p>
26 </div>
27 <div style="clear: both"></div>
28 </div>
29 </div>
30
31
32 <div class="hgroup">
33 <div class="hgroup-inline">
34 <div class="panel">
35 <h2 id="server_side">A Warning On Server-Side Evaluation</h2>
36 </div>
37 <div style="clear: both"></div>
38 </div>
39 </div>
40
41
42 <div class="hgroup">
43 <div class="hgroup-inline">
44 <div class="panel">
45 <h3>Data exfiltration risk</h3>
46 <p>
47 The normal behaviour of the <tt>import</tt>, <tt>importstr</tt>, and <tt>importbin</tt>
48 keywords is to import files from the filesystem. These accesses are unrestricted; any file
49 accessible to the Jsonnet process can be read this way, including potentially sensitive files
50 such as `/etc/passwd`, `/proc/self/environ`, etc. If you evaluate untrusted Jsonnet code and
51 expose the output of that in any way (for example by reporting error messages back to the user),
52 then you may be exposed to this data exfiltration risk.
53 <p>
54 <p>
55 <b>Mitigation:</b> Using Jsonnet as a library allows you to replace the import lookup logic
56 entirely, in which case you can restrict imports to files (or other data sources) to mitigate
57 data exfiltration risk. As a broader mitigation, executing Jsonnet inside a strong sandbox
58 (for example, gvisor container, firecracker VM, etc) can limit access to sensitive data
59 as long as the sandbox is properly configured.
60 </p>
61 <h3>Denial of Service risk</h3>
62 <p>
63 If you are creating an API server that will evaluate untrusted Jsonnet code in the same
64 process that handles other requests, be aware of your exposure to denial of service attacks.
65 Since Jsonnet allows expressing programs that take unreasonable amounts of CPU time and RAM,
66 it is possible for a malicious request to cause its host API server to exceed its OS
67 resources and crash. Such a crash would affect concurrently running handlers for other
68 users, and decrease your capacity until the server is restarted. A simple remedy for this
69 situation is to <tt>exec</tt> the Jsonnet commandline binary in a <tt>ulimit</tt> child
70 process where the CPU and RAM can be tightly bounded for the specific request.
71 Communication to and from the commandline binary could be via temporary files or pipes.
72 Thus, only the malicious request fails.
73 </p>
74 <p>
75 Linking Jsonnet as a library is more appropriate for client-side applications or situations
76 where the input configuration can be trusted.
77 </p>
78 </div>
79 <div style="clear: both"></div>
80 </div>
81 </div>
82
83
84 <div class="hgroup">
85 <div class="hgroup-inline">
86 <div class="panel">
87 <h2 id="c_api">C API</h2>
88 </div>
89 <div style="clear: both"></div>
90 </div>
91 </div>
92
93
94 <div class="hgroup">
95 <div class="hgroup-inline">
96 <div class="panel">
97 <p>
98 The C API simply wraps the C++ implementation to hide C++ language features like templates,
99 classes, overloading, etc. This makes it easier to bind to other languages, as C is
100 typically the lowest common denominator across all systems.
101 </p>
102
103 <p>
104 The API is documented in <a
105 href="https://github.com/google/jsonnet/blob/master/include/libjsonnet.h">libjsonnet.h</a>.
106 It is built with 'make libjsonnet.so'. It is used by the python bindings, the Jsonnet
107 commandline tool, as well as a couple of simpler tests. Search for #include "libjsonnet.h".
108 </p>
109
110 <p>
111 To use the API, create a JsonnetVM object, set various options, then tell it to evaluate a
112 filename or snippet. To avoid leaking memory, the result of execution (JSON or error
113 message) and the JsonnetVM object itself must be cleaned up using the provided
114 <tt>jsonnet_realloc</tt> and the <tt>jsonnet_destroy</tt> functions, respectively.
115 </p>
116 <div style="clear: both"></div>
117 </div>
118 </div>
119
120 <div class="hgroup">
121 <div class="hgroup-inline">
122 <div class="panel">
123 <h2 id="python_api">Python API</h2>
124 </div>
125 <div style="clear: both"></div>
126 </div>
127 </div>
128
129
130 <div class="hgroup">
131 <div class="hgroup-inline">
132 <div class="panel">
133 <p>
134 The Python API wraps the C API in a straightforward way. It can be installed with <tt>pip
135 install jsonnet</tt> or built directly with <tt>setup.py</tt>. It supports Python 2.7 and
136 Python 3.
137 </p>
138 <p>
139 The Python module provides two functions, <tt>evaluate_file(filename)</tt> and
140 <tt>evaluate_snippet(filename, expr)</tt>. In the latter case, the parameter
141 <tt>filename</tt> is used in stack traces, because all errors are given with the "filename"
142 containing the code.
143 </p>
144 <p>
145 Keyword arguments to these functions are used to control the virtual machine. They are:
146 </p>
147 <ul>
148 <li><tt>jpathdir</tt>&nbsp;&nbsp; (string or list of strings)</li>
149 <li><tt>max_stack</tt>&nbsp;&nbsp; (number)</li>
150 <li><tt>gc_min_objects</tt>&nbsp;&nbsp; (number)</li>
151 <li><tt>gc_growth_trigger</tt>&nbsp;&nbsp; (number)</li>
152 <li><tt>ext_vars</tt>&nbsp;&nbsp; (dict: string to string)</li>
153 <li><tt>ext_codes</tt>&nbsp;&nbsp; (dict string to string)</li>
154 <li><tt>tla_vars</tt>&nbsp;&nbsp; (dict string to string)</li>
155 <li><tt>tla_codes</tt>&nbsp;&nbsp; (dict string to string)</li>
156 <li><tt>max_trace</tt>&nbsp;&nbsp; (number)</li>
157 <li><tt>import_callback</tt>&nbsp;&nbsp; (see example in python/)</li>
158 <li><tt>native_callbacks</tt>&nbsp;&nbsp; (see example in python/)</li>
159 </ul>
160 <p>
161 The argument <tt>import_callback</tt> can be used to pass a callable, to trap the Jsonnet
162 <code>import</code>, <code>importstr</code>, and <code>importbin</code> constructs. This allows, e.g., reading files
163 out of archives or implementing library search paths. The argument <tt>native_callback</tt>
164 is used to allow execution of arbitrary Python code via <code>std.native(...)</code>. This
165 is useful so Jsonnet code can access pure functions in the Python ecosystem, such as
166 compression, encryption, encoding, etc.
167 </p>
168 <p>
169 If an error is raised during the evaluation of the Jsonnet code, it is formed into a stack
170 trace and thrown as a python RuntimeError. Otherwise, the JSON string is returned. To
171 convert this into objects for easy interpretation in Python, use the <a
172 href="https://docs.python.org/2/library/json.html">json</a> module. An example:
173 </p>
174 <pre>import json
175 import _jsonnet
176
177 jsonnet_str = '''
178 {
179 person1: {
180 name: "Alice",
181 welcome: "Hello " + self.name + "!",
182 },
183 person2: self.person1 {
184 name: std.extVar("OTHER_NAME"),
185 },
186 }
187 '''
188
189 json_str = _jsonnet.evaluate_snippet(
190 "snippet", jsonnet_str,
191 ext_vars={'OTHER_NAME': 'Bob'})
192
193 json_obj = json.loads(json_str)
194 for person_id, person in json_obj.items():
195 print('%s is %s, greeted by "%s"' % (
196 person_id,
197 person['name'],
198 person['welcome']))</pre>
199 <div style="clear: both"></div>
200 </div>
201 </div>
202
203
204 <div class="hgroup">
205 <div class="hgroup-inline">
206 <div class="panel">
207 <h2 id="third_party_apis">Unofficial Third Party APIs</h2>
208 </div>
209 <div style="clear: both"></div>
210 </div>
211 </div>
212
213
214 <div class="hgroup">
215 <div class="hgroup-inline">
216 <div class="panel">
217 <p>
218 There are unofficial bindings available for other languages. These are not supported by
219 Google and may be some versions behind the latest release.
220 </p>
221
222 <ul>
223 <li>
224 <a href="https://github.com/strickyak/jsonnet_cgo">Go</a> but please first consider the
225 <a href="https://github.com/google/go-jsonnet">native port</a>
226 </li>
227 <li>
228 <a href="https://github.com/yuduanchen/luajit-jsonnet">LuaJIT (FFI)</a>
229 </li>
230 <li>
231 <a href="https://github.com/yosuke-furukawa/node-jsonnet">Node.js node-jsonnet</a>: C++ version compiled to JS code
232 </li>
233 <li>
234 <a href="https://github.com/olpa/templating-for-api/tree/master/jsonnet-js-ts">Node.js tplfa-jsonnet</a>: Golang version compiled to WASM code
235 </li>
236 <li>
237 <a href="https://github.com/Neeke/Jsonnet-PHP">PHP</a>
238 </li>
239 <li>
240 <a href="https://github.com/yugui/ruby-jsonnet">Ruby</a>
241 </li>
242 <li>
243 <a href="https://github.com/anguslees/rust-jsonnet">Rust</a> but please first consider
244 one of the native ports (<a href="https://github.com/CertainLach/jrsonnet">jrsonnet</a>
245 or <a href="https://github.com/eduardosm/rsjsonnet">rsjsonnet</a>)
246 </li>
247 <li>
248 <a href="https://github.com/moleike/haskell-jsonnet">Haskell native port</a>
249 </li>
250 </ul>
251 <div style="clear: both"></div>
252 </div>
253 </div>