xref: /aosp_15_r20/external/sandboxed-api/contrib/jsonnet/README.md (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li# Jsonnet Sandboxed API
2*ec63e07aSXin Li
3*ec63e07aSXin LiThis library was sandboxed as part of Google's summer 2020 internship program
4*ec63e07aSXin Li([blog post](https://security.googleblog.com/2020/12/improving-open-source-security-during.html)).
5*ec63e07aSXin Li
6*ec63e07aSXin LiThis directory contains a sandbox for the
7*ec63e07aSXin Li[Jsonnet](https://github.com/google/jsonnet) library.
8*ec63e07aSXin Li
9*ec63e07aSXin Li## How to use from an existing Project
10*ec63e07aSXin Li
11*ec63e07aSXin LiIf your project does not include Sandboxed API as a dependency yet, add the
12*ec63e07aSXin Lifollowing lines to the main `CMakeLists.txt`:
13*ec63e07aSXin Li
14*ec63e07aSXin Li```cmake
15*ec63e07aSXin Liinclude(FetchContent)
16*ec63e07aSXin Li
17*ec63e07aSXin LiFetchContent_Declare(sandboxed-api
18*ec63e07aSXin Li  GIT_REPOSITORY https://github.com/google/sandboxed-api
19*ec63e07aSXin Li  GIT_TAG        main  # Or pin a specific commit/tag
20*ec63e07aSXin Li)
21*ec63e07aSXin LiFetchContent_MakeAvailable(sandboxed-api)  # CMake 3.14 or higher
22*ec63e07aSXin Li
23*ec63e07aSXin Liadd_sapi_subdirectory(contrib/jsonnet)
24*ec63e07aSXin Li```
25*ec63e07aSXin Li
26*ec63e07aSXin LiThe `add_sapi_subdirectory()` macro sets up the source and binary directories
27*ec63e07aSXin Lifor the sandboxed jsonnet targets.
28*ec63e07aSXin Li
29*ec63e07aSXin LiAfterwards your project's code can link to `sapi_contrib::jsonnet` and use the
30*ec63e07aSXin Licorresponding header `contrib/jsonnet/jsonnet_base_sandbox.h`.
31*ec63e07aSXin Li
32*ec63e07aSXin Li## Examples
33*ec63e07aSXin Li
34*ec63e07aSXin LiThe `examples/` directory contains code to produce three command-line tools --
35*ec63e07aSXin Li`jsonnet_sandboxed`, `jsonnet_yaml_stream_sandboxed` and
36*ec63e07aSXin Li`jsonnet_multiple_files_sandboxed` to evaluate jsonnet code. The first one
37*ec63e07aSXin Lienables the user to evaluate jsonnet code held in one file and writing to one
38*ec63e07aSXin Lioutput file. The second evaluates one jsonnet file into one file, which can be
39*ec63e07aSXin Liinterepreted as YAML stream. The third one is for evaluating one jsonnet file
40*ec63e07aSXin Liinto multiple output files. All three tools are based on what can be found
41*ec63e07aSXin Li[here](https://github.com/google/jsonnet/blob/master/cmd/jsonnet.cpp).
42*ec63e07aSXin Li
43*ec63e07aSXin LiApart from these, there is also a file producing `jsonnet_formatter_sandboxed`
44*ec63e07aSXin Liexecutable. It is based on a tool found from
45*ec63e07aSXin Li[here](https://github.com/google/jsonnet/blob/master/cmd/jsonnetfmt.cpp). It is
46*ec63e07aSXin Lia jsonnet code formatter -- it changes poorly written jsonnet files into their
47*ec63e07aSXin Licanonical form.
48*ec63e07aSXin Li
49*ec63e07aSXin Li### Build as part of Sandboxed API
50*ec63e07aSXin Li
51*ec63e07aSXin LiTo build these examples, after cloning the whole Sandbox API project, run this
52*ec63e07aSXin Liin the `contrib/jsonnet` directory:
53*ec63e07aSXin Li
54*ec63e07aSXin Li```
55*ec63e07aSXin Limkdir -p build && cd build
56*ec63e07aSXin Licmake .. -G Ninja -Wno-dev -DSAPI_BUILD_TESTING=ON
57*ec63e07aSXin Lininja
58*ec63e07aSXin Li```
59*ec63e07aSXin Li
60*ec63e07aSXin LiTo run `jsonnet_sandboxed` (or `jsonnet_yaml_stream_sandboxed` or
61*ec63e07aSXin Li`jsonnet_formatter_sandboxed` in a similar way):
62*ec63e07aSXin Li
63*ec63e07aSXin Li```
64*ec63e07aSXin Licd examples
65*ec63e07aSXin Li./jsonnet_sandboxed \
66*ec63e07aSXin Li    absolute/path/to/the/input_file.jsonnet \
67*ec63e07aSXin Li    absolute/path/to/the/output_file
68*ec63e07aSXin Li```
69*ec63e07aSXin Li
70*ec63e07aSXin LiTo run `jsonnet_mutiple_files_sandboxed`:
71*ec63e07aSXin Li
72*ec63e07aSXin Li```
73*ec63e07aSXin Licd examples
74*ec63e07aSXin Li./jsonnet_mutiple_files_sandboxed \
75*ec63e07aSXin Li    absolute/path/to/the/input_file.jsonnet \
76*ec63e07aSXin Li    absolute/path/to/the/output_directory
77*ec63e07aSXin Li```
78*ec63e07aSXin Li
79*ec63e07aSXin LiAll three tools support evaluating one input file (possibly relying on multiple
80*ec63e07aSXin Liother files, e.x. by jsonnet `import` command; the files must be held in the
81*ec63e07aSXin Lisame directory as input file) into one or more output files. Example jsonnet
82*ec63e07aSXin Licodes to evaluate in a one-in-one-out manner can be found
83*ec63e07aSXin Li[here](https://github.com/google/jsonnet/tree/master/examples). Example code
84*ec63e07aSXin Liproducing multiple output files or YAML stream files can be found in the
85*ec63e07aSXin Li`examples/jsonnet_codes` directory (along with some other examples copied with
86*ec63e07aSXin Liminimal changes from the library files), in files called
87*ec63e07aSXin Li`multiple_files_example.jsonnet` and `yaml_stream_example.jsonnet`,
88*ec63e07aSXin Lirespectively. In the `examples/jsonnet_codes_expected_output` directory one can
89*ec63e07aSXin Lifound outputs the mentioned above files' evaluation should produce.
90*ec63e07aSXin Li
91*ec63e07aSXin LiThe formatter reads one input file and produces one output file as a result.
92*ec63e07aSXin LiExample code for this tool can also be found in `examples/jsonnet_codes`
93*ec63e07aSXin Lidirectory, in a file called `formatter_example.jsonnet`.
94*ec63e07aSXin Li
95*ec63e07aSXin Li### Running the tests
96*ec63e07aSXin Li
97*ec63e07aSXin LiA few tests prepared with a use of
98*ec63e07aSXin Li[Google Test](https://github.com/google/googletest) framework are included. To
99*ec63e07aSXin Lirun them type:
100*ec63e07aSXin Li
101*ec63e07aSXin Li```
102*ec63e07aSXin Lictest -R JsonnetTest.
103*ec63e07aSXin Li```
104