Adding a simulator + regopolicyinterpreter.#1558
Merged
anmaxvl merged 9 commits intomicrosoft:mainfrom Jan 10, 2023
SeanTAllen:simulator
Merged
Adding a simulator + regopolicyinterpreter.#1558anmaxvl merged 9 commits intomicrosoft:mainfrom SeanTAllen:simulator
anmaxvl merged 9 commits intomicrosoft:mainfrom
SeanTAllen:simulator
Conversation
hgarvison
reviewed
Nov 17, 2022
hgarvison
reviewed
Nov 17, 2022
hgarvison
reviewed
Nov 17, 2022
hgarvison
reviewed
Nov 17, 2022
hgarvison
reviewed
Nov 17, 2022
anmaxvl
reviewed
Dec 13, 2022
| ) | ||
|
|
||
| //go:embed framework.rego | ||
| var FrameworkCode string |
Contributor
There was a problem hiding this comment.
why is this here? shouldn't this be part of rego enforcer?
Member
Author
There was a problem hiding this comment.
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>
helsaawy
reviewed
Jan 3, 2023
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>
helsaawy
approved these changes
Jan 4, 2023
anmaxvl
approved these changes
Jan 10, 2023
katiewasnothere
pushed a commit
to katiewasnothere/hcsshim
that referenced
this pull request
Feb 10, 2023
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR separates all the interaction with Rego into its own extractable package called
regopolicyinterpreter. Instead of calling Rego directly, thesecuritypolicypackage 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 newpolicyenginesimulatortool, which provides the ability to simulate security policy execution on the command line.regopolicyinterpreterexposes various Rego things like modules and metadata in a typed way to make them easier to work with:RegoPolicyInterpreteris the main interfaceRegoModuleis a standalone Rego module that can be included in the policy execution. There areAddModuleandRemoveModulemethods for modifying the interpreter to include various modules.RegoQueryResultwraps the results that come from the Rego policy with some useful methods for extracting scalar data types (i.e.bool/int/float/string)EnableLoggingprovides a way to get multiple levels of policy logging for debugging purposes, ranging fromInfo, which will output prints that come from the Rego policy itself, toMetadata, 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
policyenginesimulatortool usesRegoPolicyInterpreterto simulate policy enforcement. Usage: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" } } ]