Skip to content

Adding a simulator + regopolicyinterpreter. - #1558

Merged
Maksim An (anmaxvl) merged 9 commits into
microsoft:mainfrom
SeanTAllen:simulator
Jan 10, 2023
Merged

Adding a simulator + regopolicyinterpreter.#1558
Maksim An (anmaxvl) merged 9 commits into
microsoft:mainfrom
SeanTAllen:simulator

Conversation

@matajoh

@matajohMatthew A Johnson (matajoh) commented Nov 2, 2022

Copy link
Copy Markdown
Contributor

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"
}
}
]

Comment threadinternal/tools/policyenginesimulator/README.md
Comment threadinternal/tools/policyenginesimulator/README.md
Comment threadinternal/tools/policyenginesimulator/README.md Outdated
Comment threadpkg/regopolicyinterpreter/regopolicyinterpreter.go Outdated
Comment threadinternal/tools/policyenginesimulator/README.md Outdated
Comment threadpkg/regopolicyinterpreter/regopolicyinterpreter.go Outdated
Comment threadpkg/regopolicyinterpreter/regopolicyinterpreter.go Outdated
Comment threadpkg/regopolicyinterpreter/regopolicyinterpreter.go Outdated
Comment threadpkg/regopolicyinterpreter/regopolicyinterpreter.go
Comment threadpkg/regopolicyinterpreter/regopolicyinterpreter.go
Comment threadpkg/regopolicyinterpreter/regopolicyinterpreter.go Outdated
)

//go:embed framework.rego
var FrameworkCode string

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

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.

Matthew A Johnson added 6 commits December 20, 2022 23:22
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>
Comment threadinternal/tools/policyenginesimulator/main.go Outdated
Comment threadinternal/tools/policyenginesimulator/README.md
Matthew A Johnson added 3 commits January 4, 2023 09:36
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>
Comment threadpkg/regopolicyinterpreter/regopolicyinterpreter.go
@anmaxvl
Maksim An (anmaxvl) merged commit 939de61 into microsoft:mainJan 10, 2023
@anmaxvl
Maksim An (anmaxvl) deleted the simulator branch January 10, 2023 23:47
Prince Pereira (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 freeto 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

@matajoh@anmaxvl@hgarvison@helsaawy