Git Product home page Git Product logo

opal's Introduction

Opal - the OPA not quite standard library

Opal is a collection of common functions for OPA/Rego as found in the standard library of many popular programming languages. While OPA itself includes many useful built-ins and constructs for policy authoring, the provided "stdlib" is kept quite minimal. Opal aims to complement that with a batteries included package of functions recognizable from languages such as Python, Go or Java. While the library is ready to be used as it, it is just as much meant as an educational resource answering the occasional "how would I write function X in rego?" that may come up while authoring Rego policies. Copy-pasting highly encouraged!

If you are rather looking for examples, policies and rules for working with systems like Kubernetes, Terraform or AWS, checkout the Open Policy Agent Library.

Using Opal

  1. git clone the repository and copy the lib directory to wherver you keep your Rego policies. Import and use.

or

  1. Simply copy and paste the functions you may want to use into your policies and use them directly.

Functions

Strings

Function Description
strings.capitalize(word) Capitalize provided word
strings.repeat(str, x) Returns str repeated x number of times
strings.lines(str) Returns a list of all lines in str
strings.char_at(str, x) Get character from str at position x
strings.pad_left(str, length, char) Pad str to the left with char up to length
strings.pad_right(str, length, char) Pad str to the right with char up to length
strings.is_blank(str) True if str is empty or contains only whitespace characters
strings.startswith_any(str, list) True if str starts with any item in list
strings.endswith_any(str, list) True if str ends with any item in list

Lists / Arrays

Function Description
lists.append(l, x) Returns list l with value x appended (added last)
lists.insert(l, x, pos) Return list l with value x inserted at position pos
lists.index(l, x) Provides the position of first occurrence of value x in list, or -1 if not found
lists.index_last Provides the position of last occurrence of value x in list, or -1 if not found
lists.reverse(l) Reverse provided list
lists.count_value(l, v) Count the number of occurrences of provided value v in list l

Objects

Function Description
objects.keys(o) Returns a list of (top level) keys in object o 
objects.values(o) Returns a list of (top level) values in object o 
objects.put(o, k, v) Return a new object with the provided key/value added (overwritten if exists)
objects.put_if_absent(o, k, v) Return a new object with the provided key/value added (if not exists)
objects.select(o, path) Return the value at provided path (separated by '.') in nested object o

Math

Function Description
math.ceil(x) Returns x rounded up to the closest integer 
math.floor(x) Returns x rounded down to the closest integer 

Contributing

If you have a function you'd like to add, that's great! Just remember to submit a unit test or two along with your PR.

opal's People

Contributors

anderseknert avatar srenatus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

srenatus syllogy

opal's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.