Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a simulator + regopolicyinterpreter. #1558

Merged
merged 9 commits into from
Jan 10, 2023
Merged

Adding a simulator + regopolicyinterpreter. #1558

merged 9 commits into from
Jan 10, 2023

Conversation

matajoh
Copy link
Member

@matajoh matajoh commented Nov 2, 2022

This PR separates all the interaction with Rego into its own extractable package called regopolicyinterpreter. Instead of calling Rego directly, the securitypolicy package now uses this package to implement Rego policies. Separating out the Rego interpreter behavior in this way allows the same code to be used by a new policyenginesimulator tool, which provides the ability to simulate security policy execution on the command line.

regopolicyinterpreter exposes various Rego things like modules and metadata in a typed way to make them easier to work with:

  • RegoPolicyInterpreter is the main interface
  • RegoModule is a standalone Rego module that can be included in the policy execution. There are AddModule and RemoveModule methods for modifying the interpreter to include various modules.
  • RegoQueryResult wraps the results that come from the Rego policy with some useful methods for extracting scalar data types (i.e. bool/int/float/string)
  • EnableLogging provides a way to get multiple levels of policy logging for debugging purposes, ranging from Info, which will output prints that come from the Rego policy itself, to Metadata, which will dump the entire policy metadata structure to the log with each interaction. This is primarily intended for offline use (e.g. by the simulator).

The policyenginesimulator tool uses RegoPolicyInterpreter to simulate policy enforcement. Usage:

  -commands string
        commands JSON
  -data string
        initial data state
  -log string
        log path
  -logLevel string
        None|Info|Results|Metadata (default "Info")
  -policy string
        policy Rego

The commands JSON allows the user to specify the type and order of the commands send by the host to the guest that will interact with the simulated policy, for example:

[
    {
        "name": "load_fragment",
        "input": {
            "issuer": "did:web:contoso.github.io",
            "feed": "contoso.azurecr.io/custom",
            "namespace": "custom",
            "local_path": "custom.rego"
        }
    },
    {
        "name": "mount_device",
        "input": {
            "target": "/mnt/layer0",
            "deviceHash": "16b514057a06ad665f92c02863aca074fd5976c755d26bff16365299169e8415"
        }
    },
    {
        "name": "mount_overlay",
        "input": {
            "target": "/mnt/overlay0",
            "containerID": "container0",
            "layerPaths": [
                "/mnt/layer0"
            ]
        }
    },
    {
        "name": "create_container",
        "input": {
            "containerID": "container0",
            "argList": [
                "/pause"
            ],
            "envList": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "TERM=xterm"
            ],
            "mounts": [],
            "workingDir": "/",
            "sandboxDir": "/sandbox",
            "hugePagesDir": "/hugepages"
        }
    }
]

@matajoh matajoh requested a review from a team as a code owner November 2, 2022 13:59
internal/tools/policyenginesimulator/README.md Outdated Show resolved Hide resolved
pkg/regopolicyinterpreter/regopolicyinterpreter.go Outdated Show resolved Hide resolved
pkg/regopolicyinterpreter/regopolicyinterpreter.go Outdated Show resolved Hide resolved
pkg/regopolicyinterpreter/regopolicyinterpreter.go Outdated Show resolved Hide resolved
pkg/regopolicyinterpreter/regopolicyinterpreter.go Outdated Show resolved Hide resolved
pkg/regopolicyinterpreter/regopolicyinterpreter.go Outdated Show resolved Hide resolved
pkg/regopolicyinterpreter/regopolicyinterpreter.go Outdated Show resolved Hide resolved
@@ -14,6 +15,12 @@ import (
"github.com/pkg/errors"
)

//go:embed framework.rego
var FrameworkCode string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this here? shouldn't this be part of rego enforcer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the policy simulator can run in either windows or Linux, and since it needs this string to simulate policies, it made sense to move it to securitypolicy.go, which is not boxed by a build tag.

This PR separates all the interaction with Rego into its own extractable package
called `regopolicyinterpreter`. Instead of calling Rego directly,
the `securitypolicy` package now uses this package to implement Rego policies.
Separating out the Rego interpreter behavior in this way allows the same
code to be used by a new `policyenginesimulator` tool, which provides the
ability to simulate security policy execution on the command line.

`regopolicyinterpreter` exposes various Rego things like modules and metadata
in a typed way to make them easier to work with:
    - `RegoPolicyInterpreter` is the main interface
    - `RegoModule` is a standalone Rego module that can be included in the
       policy execution. There are `AddModule` and `RemoveModule` methods for
       modifying the interpreter to include various modules.
    - `RegoQueryResult` wraps the results that come from the Rego policy with
       some useful methods for extracting scalar data types
       (i.e. `bool`/`int`/`float`/`string`)
    - `EnableLogging` provides a way to get multiple levels of policy logging
      for debugging purposes, ranging from `Info`, which will output prints that
      come from the Rego policy itself, to `Metadata`, which will dump the
      entire policy metadata structure to the log with each interaction. This is
      primarily intended for offline use (e.g. by the simulator).

The `policyenginesimulator` tool uses `RegoPolicyInterpreter` to simulate
policy enforcement. Usage:

```
  -commands string
        commands JSON
  -data string
        initial data state
  -log string
        log path
  -logLevel string
        None|Info|Results|Metadata (default "Info")
  -policy string
        policy Rego
```

The commands JSON allows the user to specify the type and order of the commands
send by the host to the guest that will interact with the simulated policy, for
example:

``` json
[
    {
        "name": "load_fragment",
        "input": {
            "issuer": "did:web:contoso.github.io",
            "feed": "contoso.azurecr.io/custom",
            "namespace": "custom",
            "local_path": "custom.rego"
        }
    },
    {
        "name": "mount_device",
        "input": {
            "target": "/mnt/layer0",
            "deviceHash": "16b514057a06ad665f92c02863aca074fd5976c755d26bff16365299169e8415"
        }
    },
    {
        "name": "mount_overlay",
        "input": {
            "target": "/mnt/overlay0",
            "containerID": "container0",
            "layerPaths": [
                "/mnt/layer0"
            ]
        }
    },
    {
        "name": "create_container",
        "input": {
            "containerID": "container0",
            "argList": [
                "/pause"
            ],
            "envList": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "TERM=xterm"
            ],
            "mounts": [],
            "workingDir": "/",
            "sandboxDir": "/sandbox",
            "hugePagesDir": "/hugepages"
        }
    }
]
```

Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
@anmaxvl anmaxvl merged commit 939de61 into microsoft:main Jan 10, 2023
@anmaxvl anmaxvl deleted the simulator branch January 10, 2023 23:47
princepereira pushed a commit to princepereira/hcsshim that referenced this pull request Aug 29, 2024
* Adding a simulator + regopolicyinterpreter.

This PR separates all the interaction with Rego into its own extractable package
called `regopolicyinterpreter`. Instead of calling Rego directly,
the `securitypolicy` package now uses this package to implement Rego policies.
Separating out the Rego interpreter behavior in this way allows the same
code to be used by a new `policyenginesimulator` tool, which provides the
ability to simulate security policy execution on the command line.

`regopolicyinterpreter` exposes various Rego things like modules and metadata
in a typed way to make them easier to work with:
    - `RegoPolicyInterpreter` is the main interface
    - `RegoModule` is a standalone Rego module that can be included in the
       policy execution. There are `AddModule` and `RemoveModule` methods for
       modifying the interpreter to include various modules.
    - `RegoQueryResult` wraps the results that come from the Rego policy with
       some useful methods for extracting scalar data types
       (i.e. `bool`/`int`/`float`/`string`)
    - `EnableLogging` provides a way to get multiple levels of policy logging
      for debugging purposes, ranging from `Info`, which will output prints that
      come from the Rego policy itself, to `Metadata`, which will dump the
      entire policy metadata structure to the log with each interaction. This is
      primarily intended for offline use (e.g. by the simulator).

The `policyenginesimulator` tool uses `RegoPolicyInterpreter` to simulate
policy enforcement. Usage:

```
  -commands string
        commands JSON
  -data string
        initial data state
  -log string
        log path
  -logLevel string
        None|Info|Results|Metadata (default "Info")
  -policy string
        policy Rego
```

The commands JSON allows the user to specify the type and order of the commands
send by the host to the guest that will interact with the simulated policy, for
example:

``` json
[
    {
        "name": "load_fragment",
        "input": {
            "issuer": "did:web:contoso.github.io",
            "feed": "contoso.azurecr.io/custom",
            "namespace": "custom",
            "local_path": "custom.rego"
        }
    },
    {
        "name": "mount_device",
        "input": {
            "target": "/mnt/layer0",
            "deviceHash": "16b514057a06ad665f92c02863aca074fd5976c755d26bff16365299169e8415"
        }
    },
    {
        "name": "mount_overlay",
        "input": {
            "target": "/mnt/overlay0",
            "containerID": "container0",
            "layerPaths": [
                "/mnt/layer0"
            ]
        }
    },
    {
        "name": "create_container",
        "input": {
            "containerID": "container0",
            "argList": [
                "/pause"
            ],
            "envList": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "TERM=xterm"
            ],
            "mounts": [],
            "workingDir": "/",
            "sandboxDir": "/sandbox",
            "hugePagesDir": "/hugepages"
        }
    }
]
```

Signed-off-by: Matthew A Johnson <matjoh@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants