Cross-Platform Libraries with Rust
Our library: Oso
Oso is an authorization framework that runs inside your application. It comes as a library that you call into to make authorization decisions. Within that library, there is an interpreter for a declarative programming language called Polar. Polar is a language used for writing authorization logic, and it has a lot of nice features that make it easy to interact with your application data.
The first version of Oso was a Python library and the first version of Polar was written in Python. Since we were using the Polar language from within Python, it was very natural to pass Python objects as arguments to Polar queries. It was also easy to look up properties on a Python object or call a method on an object right from the policy. This made it feel like the policy was executing “inside” of your app, rather than requiring you to serialize all the data and send it to the policy. It made the Polar language feel like an extension of Python itself.
This was a very natural way of expressing authorization and our early users liked it. But, we wanted it to work for anyone in any language. We knew we didn't want to port the whole codebase to every language we want to support and that we wanted to keep Oso as a library that worked with application data in the same way. Our best option would be to take the core code and port it to something that could be embedded in all the language libraries.
Multi-language support: moving the core to Rust
We pulled out what we call the "Core" which is the Polar language and policy evaluation code. That way we can develop it as a single codebase and embed it into our language-specific libraries.
We chose Rust for this because we like it, but it also is easy to embed in other languages. Rust code can be compiled down to a native library and can expose a C-compatible API. This means anyone that can talk to a C library can talk to our Rust library, and everyone can talk to a C library.
C is the language the operating system uses to expose its API. If your language can’t talk to C, it can’t do anything useful like open files, allocate memory, or print to the console. This means that every language out there has built-in ways to call C code, so our C-compatible API would work for every language we wanted to support.
Designing the API
Another important goal was to keep the intuitive feel of the Python implementation, especially being able to operate on the host language’s objects. We wanted to be able to look up properties on objects, call host language methods, and detect the classes of objects from Polar. This was all easy to implement when everything was Python, but now we needed some way to resolve these questions while the Rust code was evaluating.
Running a Polar query in the Rust code is like running a script in any other language. You pass in a program, the Rust code evaluates it and returns the answer. The evaluation is a stateful process that needs to pull in information from the host language as it processes. There are a few options for how the host language could communicate with this evaluation.
A third option could be to run Polar as a separate process, or on a separate thread and communicate with the host language over a socket or some other kind of channel. This could work, but would make Oso less portable. If we did that, we'd be restricted to environments that had threads or sockets. We want Oso to run everywhere, and a setup like this wouldn’t work in the browser or in embedded scenarios. It’s also actually a lot more complicated than what we decided on.
If Rust can’t easily call back to the host language, then we have to have the host language call Rust. By inverting the control so that everything is driven by the host language, we don’t have to deal with any tricky runtime or callback problems. That way, we can keep our C API as simple functions that take and return values.
This is how it works:
Polar query evaluation is centered around an event loop that is driven by the host. When we run a query we create a new Query object. This is a virtual machine that processes polar queries. The host then calls a next_event function. We run the Polar query until we need something from the host, like the result of a method call. It then pauses the virtual machine and returns an event that says “call this method”. The host does whatever it has to do to process the event, in this case calling the method and sending the result. Then the host calls next_event again and the virtual machine picks up where it left off. When the final Done event comes through, the query is over.
There are events for everything the core needs to talk to the host language about. Method calls, property lookups, results, checking the type of values. These events are JSON strings because nearly every language can easily work with JSON.
Building the Libraries
Now that we have a language that any other language can talk to and an API any language can call we have to figure out how to embed it. The Rust core code also has to be available at runtime so the host language can call into it. We have to build the Rust core into a native library and embed it in the host library somehow. The way we do that depends on the language.
In Java and Ruby, we build the core into a dynamic library and embed it within the package (a Gem for Ruby and a Jar for Java). It’s a dynamic library because we can't link it directly into the Ruby process or the JVM, but we want to be able to call into it at runtime. Dynamic libraries have to be in a different format for each operating system, so we build three different ones: one for macOS, one for Linux, and one for Windows, and include them all in the package. The code then picks the correct dynamic library to talk to at runtime based on the current operating system. This way we have one Oso package that works on any operating system.
In Python, we do something similar. We build Oso into a dynamic library and include it in a wheel package (Python's packaging system for binaries). In Python, however, we can build different packages for each operating system. Pip, the Python package manager, will take care of downloading the correct one when someone installs the package. This is nice because each package only contains the library it needs, which makes it much smaller than the Ruby and Java libraries.
The next language we ported to was Rust. The Rust port was complicated in some ways—for examples of where that got tricky, see the blog posts on the class system we had to build. But using the core from Rust was very simple. The core is written in Rust, we’re calling it from Rust, so we distribute it as a Rust library. No need to build it beforehand and embed it.
Golang works a little bit differently than the others. Go is a compiled language, and generates an executable binary. That binary can be moved around and run without having the source code available. If we did a dynamic library like some of the other languages, then the user would have to copy that dynamic library around with the binary, which would be extremely annoying. Instead, we build a static library that gets linked into the executable with the Go code. We still need to build that static library for each operating system, but only one will get linked into the compiled binary.
This style of embedding works well and lets us make the Oso library very portable, but there are a few downsides. Not only do we have to build code for each operating system, but we also have to build it for each CPU architecture. Currently, we only support x64 because up until a couple of years ago that was what the vast majority of personal and server computers were using. Recently ARM has gotten a lot more popular, and soon we'll support that too. That will mean that we have to double the number of builds we do.
I've glossed over a lot of the complexity in building and distributing native code. There are a lot of subtle and tedious details that come up in building cross-platform libraries. Deciding between dynamic and static linking of the C runtime library, deciding between Musl and GlibC, deciding between Windows MSVC and Windows GNU—each of these is a decision that complicates our implementation. We’ve had to figure out most of this via trial and error. It's worth it to us because we’re committed to making Oso work for everyone. We’ve continued to fix things as they come up, expand our platform support, and add more and more test platforms to our CI job.
All of this is still less work than porting features to six versions of the same codebase. There are alternatives that might give us the same shared code advantages but make some of the library complexity easier. One option we’ve been talking about is building an Oso compiler instead of an interpreter. This would be a tool that compiled your policy to some host language code for evaluation. At runtime, there wouldn't be any more Rust code, just generated evaluation code. We'd only have to support every language instead of every language/OS/toolchain/libc/CPU combo. We'd still have a cross-platform compiler binary, but that's a bit easier to manage than cross-platform libraries and would be a build dependency instead of a runtime one.
We're still in the early stages of thinking about that but for now, our native library approach has worked well so far. What languages or platforms would you like to see Oso come to? Let us know on GitHub!