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

topdown+wasm: Verifying host based on allow_net allowlist in built-in functions #4152

Conversation

johanfylling
Copy link
Contributor

@johanfylling johanfylling commented Dec 16, 2021

Adding host allow-listing based on the allow_net capability in the http.send()- and
net.lookup_ip_addr() built-in functions when running the eval command.

Fixes: #3665

Signed-off-by: Johan Fylling johan.dev@fylling.se

@johanfylling
Copy link
Contributor Author

This implementation only applies to the eval command, but could be extended: #4153

@johanfylling johanfylling force-pushed the feature/3665/http_send_allow_net branch from 5f22d52 to 161cdbe Compare December 16, 2021 16:22
… functions

Adding host allow-listing based on the allow_net capability in the http.send()- and
net.lookup_ip_addr() built-in functions when running the eval command.

Fixes: open-policy-agent#3665

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
@johanfylling johanfylling force-pushed the feature/3665/http_send_allow_net branch from 161cdbe to de8875c Compare December 16, 2021 16:35
@johanfylling johanfylling changed the title topdown+wasm: Verifying host based on allow_net white-list in built-in functions topdown+wasm: Verifying host based on allow_net allowlist in built-in functions Dec 16, 2021
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

some nitpicks, questions; but this looks good!

ast/compile.go Outdated
@@ -86,6 +86,9 @@ type Compiler struct {
// with the key being the generated name and value being the original.
RewrittenVars map[Var]Var

// Capabilities is the user-supplied capabilities or features allowed for OPA.
Capabilities *Capabilities
Copy link
Contributor

Choose a reason for hiding this comment

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

Exposing this technically means it could be messed with. However, WithCapabilities() already allows that, so I guess we're OK. Alternatively, we could do

func (c *Compiler) Capabilities() *Capabilities {
    return c.capabilities
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There already is precedence for direct access for other fields, but if a function is preferred, I can add one. The only drawback would be that it might look haphazard to a user.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've been thinking about this, too, it's the only part of this PR that worries me. Once we expose this as a field, we can never go back or change or structures or something.

Having a getter function would leave us done wiggle room. I'd prefer we go with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright! Will fix.


serverUrl, err := url.Parse(ts.URL)
if err != nil {
panic(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit]

Suggested change
panic(err)
t.Fatal(err)


resultObj, err := ast.InterfaceToValue(expectedResult)
if err != nil {
panic(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] Let's either do t.Fatal(err) here, or use resultObj := ast.MustInterfaceToValue(expectedResult) above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, will fix.

"allow_net match + additional host": {addr, "example.com"},
} {
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] Do we somehow need the cancel here? We night just do ctx := context.Background(); but I might be missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I thing you're right. This is a copy/paste I didn't clean up properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, all other test cases use cancel. I'll keep this as-is, just to be in line with the rest of them. However, this might be unnecessary in all cases.

@@ -305,7 +335,7 @@ func createHTTPRequest(bctx BuiltinContext, obj ast.Object) (*http.Request, *htt
var strVal string

if s, ok := obj.Get(val).Value.(ast.String); ok {
strVal = string(s)
strVal = strings.Trim(string(s), "\"")
Copy link
Contributor

Choose a reason for hiding this comment

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

It's OK given the existing code, but something here doesn't seem correct to me. Why are there extra "?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, I don't get this either; but I didn't want to change existing functionality.

rego/rego_wasmtarget_test.go Show resolved Hide resolved
Signed-off-by: Johan Fylling <johan.dev@fylling.se>
srenatus
srenatus previously approved these changes Dec 17, 2021
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

Looks like something worth documenting. (Even more so if it'll become available beyond opa eval)

Changes look good to me, thanks!

@johanfylling
Copy link
Contributor Author

johanfylling commented Dec 17, 2021

Looks like something worth documenting.

Good point! Will find a suitable place to add to the docs.

@srenatus
Copy link
Contributor

https://github.com/open-policy-agent/opa/blob/main/docs/content/deployments.md#network seems like a good spot?

Adding documentation.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
Copy link
Contributor

@srenatus srenatus 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!

@johanfylling johanfylling merged commit 7b64270 into open-policy-agent:main Dec 17, 2021
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.

Add support for AllowNet capability in http.send
2 participants