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

kbs: switch to Regorus for resource policy #357

Merged
merged 1 commit into from
Apr 16, 2024

Conversation

fitzthum
Copy link
Member

@fitzthum fitzthum commented Mar 29, 2024

  1. Switch from Go-based policy engine to rust-based Regorus
  2. Switch from Anyhow to thiserror for resource policy
  3. Add several unit tests and test policies

This does not touch the AS policy. I will do that later although the plan may change based on whether/how we use EAR tokens.

@fitzthum fitzthum requested a review from sameo as a code owner March 29, 2024 22:35
@fitzthum
Copy link
Member Author

Unfortunately when I rebase (and pick up az-snp-vtpm 0.5.2) I run into this dependency conflict. Not sure we can fix this ourselves? @mkulke @anakrish

error: failed to select a version for `once_cell`.
    ... required by package `jsonschema v0.17.1`
    ... which satisfies dependency `jsonschema = "^0.17.1"` of package `regorus v0.1.2`
    ... which satisfies dependency `regorus = "^0.1.2"` of package `api-server v0.1.0 (/home/tobin/kbs/kbs/src/api)`
    ... which satisfies path dependency `api-server` of package `kbs v0.1.0 (/home/tobin/kbs/kbs/src/kbs)`
versions that meet the requirements `^1.17` are: 1.19.0, 1.18.0, 1.17.2, 1.17.1, 1.17.0

all possible versions conflict with previously selected packages.

  previously selected package `once_cell v1.0.1`
    ... which satisfies dependency `once_cell = ">=1, <=1.14.0"` of package `mbox v0.6.1`
    ... which satisfies dependency `mbox = "^0.6.0"` of package `tss-esapi v7.4.0`
    ... which satisfies dependency `tss-esapi = "^7.4"` of package `az-cvm-vtpm v0.5.2`
    ... which satisfies dependency `az-cvm-vtpm = "^0.5.2"` of package `az-snp-vtpm v0.5.2`
    ... which satisfies dependency `az-snp-vtpm = "^0.5.2"` of package `attester v0.1.0 (https://github.com/confidential-containers/guest-components.git?rev=21b2c536b4d6c5c1442b53916c908b54dde136e8#21b2c536)`
    ... which satisfies git dependency `attester` of package `kbs_protocol v0.1.0 (https://github.com/confidential-containers/guest-components.git?rev=21b2c536b4d6c5c1442b53916c908b54dde136e8#21b2c536)`
    ... which satisfies git dependency `kbs_protocol` of package `kbs-client v0.1.0 (/home/tobin/kbs/kbs/tools/client)`

failed to select a version for `once_cell` which could resolve this conflict
make: *** [Makefile:21: background-check-kbs] Error 101

Cargo.toml Outdated
@@ -31,6 +31,8 @@ hex = "0.4.3"
kbs-types = "0.5.3"
log = "0.4.17"
prost = "0.11.0"
#regorus = "0.1.2"
regorus = { git = "https://github.com/microsoft/regorus" }

Choose a reason for hiding this comment

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

Any reason for the preference for github repo over crates.io?

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops. That's leftover from development when I was thinking about using the new eval_rule thing.

Copy link

Choose a reason for hiding this comment

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

eva;_rule would be the way to go in future. It avoids having to parse a query, and also returns the value of a rule directly.

Cargo.toml Outdated
@@ -31,6 +31,8 @@ hex = "0.4.3"
kbs-types = "0.5.3"
log = "0.4.17"
prost = "0.11.0"
#regorus = "0.1.2"
regorus = { git = "https://github.com/microsoft/regorus" }

Choose a reason for hiding this comment

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

We could configure regorus to support only those builtins that will be used by the policies. The way it is used currently, we might not even need the arc feature since neither the Engine instance nor the Value instances are shared between threads.

Suggested change
regorus = { git = "https://github.com/microsoft/regorus" }
regorus = { git = "https://github.com/microsoft/regorus", default-features=false, features=["arc"] }

Copy link
Member Author

Choose a reason for hiding this comment

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

This seems to work. Thanks for the advice.

@anakrish
Copy link

Unfortunately when I rebase (and pick up az-snp-vtpm 0.5.2) I run into this dependency conflict. Not sure we can fix this ourselves? @mkulke @anakrish

error: failed to select a version for `once_cell`.
    ... required by package `jsonschema v0.17.1`
    ... which satisfies dependency `jsonschema = "^0.17.1"` of package `regorus v0.1.2`
    ... which satisfies dependency `regorus = "^0.1.2"` of package `api-server v0.1.0 (/home/tobin/kbs/kbs/src/api)`
    ... which satisfies path dependency `api-server` of package `kbs v0.1.0 (/home/tobin/kbs/kbs/src/kbs)`
versions that meet the requirements `^1.17` are: 1.19.0, 1.18.0, 1.17.2, 1.17.1, 1.17.0

all possible versions conflict with previously selected packages.

  previously selected package `once_cell v1.0.1`
    ... which satisfies dependency `once_cell = ">=1, <=1.14.0"` of package `mbox v0.6.1`
    ... which satisfies dependency `mbox = "^0.6.0"` of package `tss-esapi v7.4.0`
    ... which satisfies dependency `tss-esapi = "^7.4"` of package `az-cvm-vtpm v0.5.2`
    ... which satisfies dependency `az-cvm-vtpm = "^0.5.2"` of package `az-snp-vtpm v0.5.2`
    ... which satisfies dependency `az-snp-vtpm = "^0.5.2"` of package `attester v0.1.0 (https://github.com/confidential-containers/guest-components.git?rev=21b2c536b4d6c5c1442b53916c908b54dde136e8#21b2c536)`
    ... which satisfies git dependency `attester` of package `kbs_protocol v0.1.0 (https://github.com/confidential-containers/guest-components.git?rev=21b2c536b4d6c5c1442b53916c908b54dde136e8#21b2c536)`
    ... which satisfies git dependency `kbs_protocol` of package `kbs-client v0.1.0 (/home/tobin/kbs/kbs/tools/client)`

failed to select a version for `once_cell` which could resolve this conflict
make: *** [Makefile:21: background-check-kbs] Error 101

You could just not use the jsonschema feature in Regorus that bring in the dependency.
https://github.com/microsoft/regorus/blob/3d98c3b12ecbdfdfabdcfd6947c2019413883a9f/Cargo.toml#L38

The following table lists various Rego builtins and which feature each one depends on.
https://github.com/microsoft/regorus/blob/main/docs/builtins.md

For OPA compatibility, all features are enabled by default. But most applications won't need or use all the OPA builtins.

Copy link
Member

@Xynnn007 Xynnn007 left a comment

Choose a reason for hiding this comment

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

Nice PR and good unit tests.

|[deny_all.rego](./deny_all.rego)|Deny all resources release|
Copy link
Member

Choose a reason for hiding this comment

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

If we split docs and code, it would be better. But it looks good now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I think we should refactor a few things in this repo including the docs.

use tokio::sync::Mutex;

#[cfg(feature = "opa")]
mod opa;

const DEFAULT_POLICY_PATH: &str = "/opa/confidential-containers/kbs/policy.rego";

#[derive(Error, Debug, PartialEq)]
pub enum ResourcePolicyError {
Copy link
Member

Choose a reason for hiding this comment

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

btw I like this definition of error types.

kbs/src/api/src/policy_engine/mod.rs Outdated Show resolved Hide resolved
kbs/src/api/src/policy_engine/mod.rs Outdated Show resolved Hide resolved
@@ -0,0 +1,31 @@

Copy link
Member

Choose a reason for hiding this comment

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

maybe we should delete the empty lines

Copy link
Member Author

@fitzthum fitzthum Apr 2, 2024

Choose a reason for hiding this comment

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

The GO OPA engine is very picky about the format of the policy and would often fail if there were any extra lines. This test is supposed to make sure Regorus is more resilient. I added a comment to the policy file because it does look odd.

@fitzthum fitzthum force-pushed the regorus branch 4 times, most recently from 1421883 to a9b51a3 Compare April 2, 2024 20:04
@fitzthum
Copy link
Member Author

fitzthum commented Apr 2, 2024

Hmm. Looks like there is some issue with building Regorus inside of our docker container

60.32 error: failed to run custom build command for `regorus v0.1.2`
60.32 
60.32 Caused by:
60.32   process didn't exit successfully: `/usr/src/kbs/target/release/build/regorus-d111907e1736880c/build-script-build` (exit status: 101)
60.32   --- stderr
60.32   thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/regorus-0.1.2/build.rs:20:10:
60.32   called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }
60.32   note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
60.32 warning: build failed, waiting for other jobs to finish...
81.04 error: failed to compile `kbs v0.1.0 (/usr/src/kbs/kbs/src/kbs)`, intermediate artifacts can be found at `/usr/src/kbs/target`.
81.04 To reuse those artifacts with a future compilation, set the environment variable `CARGO_TARGET_DIR` to that path.

I don't see a similar issue building locally. I have also tried pinning to v0.1.0 and v0.1.1 and the upstream head. Any ideas @anakrish

@anakrish
Copy link

anakrish commented Apr 3, 2024

Hmm. Looks like there is some issue with building Regorus inside of our docker container

60.32 error: failed to run custom build command for `regorus v0.1.2`
60.32 
60.32 Caused by:
60.32   process didn't exit successfully: `/usr/src/kbs/target/release/build/regorus-d111907e1736880c/build-script-build` (exit status: 101)
60.32   --- stderr
60.32   thread 'main' panicked at /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/regorus-0.1.2/build.rs:20:10:
60.32   called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }
60.32   note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
60.32 warning: build failed, waiting for other jobs to finish...
81.04 error: failed to compile `kbs v0.1.0 (/usr/src/kbs/kbs/src/kbs)`, intermediate artifacts can be found at `/usr/src/kbs/target`.
81.04 To reuse those artifacts with a future compilation, set the environment variable `CARGO_TARGET_DIR` to that path.

I don't see a similar issue building locally. I have also tried pinning to v0.1.0 and v0.1.1 and the upstream head. Any ideas @anakrish

Hmm. I haven't seen such an issue before. The failing build command is trying to do this:
https://github.com/microsoft/regorus/blob/0ebcb568cc577602c9ac64ae491ca1e45683deaa/build.rs#L17-L21

    let output = Command::new("git")
        .args(["rev-parse", "HEAD"])
        .output()
        .unwrap();
    let git_hash = String::from_utf8(output.stdout).unwrap();

It is likely that git is not present in the container.

Can you point me to the container image so that I can try it out locally?
By renaming the git executable temporarily, I could reproduce the issue, confirming that the error is due to git not being present in the docker image.
In the Regorus repo, I have changed it so that unless opa.runtime feature is enabled (that built in function requires the commit hash for which git is needed) there is no need to have git while building.

Copy link
Member

@Xynnn007 Xynnn007 left a comment

Choose a reason for hiding this comment

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

lgtm. cc @jialez0

@fitzthum
Copy link
Member Author

fitzthum commented Apr 3, 2024

Ok, thanks @anakrish. I added git to our container image so we don't have to pick up a new version.

@jialez0 I would be curious about your feedback since you've worked on the policy stuff previously.

Copy link
Member

@jialez0 jialez0 left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @fitzthum

Copy link
Contributor

@mkulke mkulke left a comment

Choose a reason for hiding this comment

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

I have some concerns about dropping error sources, but otherwise lg!

kbs/src/api/src/policy_engine/opa/mod.rs Outdated Show resolved Hide resolved
kbs/src/api/src/policy_engine/opa/mod.rs Outdated Show resolved Hide resolved
kbs/src/api/src/policy_engine/opa/mod.rs Show resolved Hide resolved
kbs/src/api/src/policy_engine/opa/mod.rs Show resolved Hide resolved
Switch from Go-based policy engine to rust-based Regorus
Switch from Anyhow to thiserror for resource policy
Add several unit tests and test policies

Signed-off-by: Tobin Feldman-Fitzthum <[email protected]>
@fitzthum fitzthum merged commit 3d0b11d into confidential-containers:main Apr 16, 2024
17 checks passed
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.

5 participants