-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,8 @@ allow { | |
nodeImage { | ||
images[_] = "node" | ||
} { | ||
true | ||
} | ||
## Get all FROM lines ## | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package opa | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
var ( | ||
_, b, _, _ = runtime.Caller(0) | ||
basepath = filepath.Dir(b) | ||
) | ||
|
||
func TestCreateNetworkDenied(t *testing.T) { | ||
|
||
req, _ := http.NewRequest("GET", "/v1.39/networks/create?", nil) | ||
|
||
opaHandler := &DockerOpaHandler{ basepath+"/../../sample_policies/deny_network_create.rego", "build.rego"} | ||
res, _ := opaHandler.ValidateRequest(req); | ||
|
||
assert.False(t, res, "Network creation should be rejected"); | ||
|
||
} | ||
|
||
func TestDenyPost(t *testing.T) { | ||
|
||
postReq, _ := http.NewRequest("POST", "/test", nil) | ||
|
||
opaHandler := &DockerOpaHandler{ basepath+"/../../sample_policies/deny_post.rego", "build.rego"} | ||
res, _ := opaHandler.ValidateRequest(postReq); | ||
|
||
assert.False(t, res, "POST request should be rejected"); | ||
|
||
getReq, _ := http.NewRequest("GET", "/test", nil) | ||
|
||
res, _ = opaHandler.ValidateRequest(getReq); | ||
|
||
assert.True(t, res, "GET request should be allowed"); | ||
|
||
} | ||
|
||
func TestDenyBasedOnHeader(t *testing.T) { | ||
reqWithHeader, _ := http.NewRequest("POST", "/test", nil) | ||
|
||
reqWithHeader.Header.Set("X-foo", "bar") | ||
|
||
opaHandler := &DockerOpaHandler{ basepath+"/../../sample_policies/deny_header.rego", "build.rego"} | ||
res, _ := opaHandler.ValidateRequest(reqWithHeader); | ||
|
||
assert.True(t, res, "request with X-foo header should be allowed"); | ||
|
||
reqWithoutHeader, _ := http.NewRequest("POST", "/test", nil) | ||
|
||
res, _ = opaHandler.ValidateRequest(reqWithoutHeader); | ||
|
||
assert.False(t, res, "request without X-foo header should be rejected"); | ||
|
||
} | ||
|
||
func TestBuildDockerfileFromFoo(t *testing.T) { | ||
|
||
req, _ := http.NewRequest("GET", "/test", nil) | ||
|
||
opaHandler := &DockerOpaHandler{ "", basepath+"/../../sample_policies/deny_build_foo.rego"} | ||
res, _ := opaHandler.ValidateDockerFile(req, "FROM foo"); | ||
|
||
assert.False(t, res, "FROM foo should be rejected"); | ||
|
||
res, _ = opaHandler.ValidateDockerFile(req, "FROM bar"); | ||
|
||
assert.False(t, res, "FROM bar should be allowed"); | ||
|
||
} | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package docker.build | ||
|
||
allow { | ||
not hasFooImage | ||
} | ||
|
||
hasFooImage { | ||
images[_] = "foo" | ||
} { | ||
true | ||
} | ||
|
||
## Get all FROM lines ## | ||
images[output] { | ||
line := input.Dockerfile[_] | ||
startswith(line, "FROM ") | ||
output = substring(line, 5, -1) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package docker.authz | ||
|
||
allow { | ||
hasXfooHeader | ||
} | ||
|
||
hasXfooHeader { | ||
input.Headers["X-Foo"] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package docker.authz | ||
|
||
allow { | ||
not createNetwork | ||
} | ||
|
||
createNetwork { | ||
startswith(path, "/networks/create") | ||
} | ||
|
||
## Parsing Path, Allowing for versioned and non versioned | ||
versioned = output { | ||
output = re_match("/v\\d+.*", input.Path) | ||
} | ||
|
||
path = output { | ||
not versioned | ||
index := indexof(input.Path, "?") | ||
output = substring(input.Path, 0, index) | ||
} | ||
|
||
path = output { | ||
versioned | ||
path := substring(input.Path, 2, -1) | ||
end := indexof(path, "?") | ||
start := indexof(path, "/") | ||
output = substring(path, start, end) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package docker.authz | ||
|
||
allow { | ||
not input.Method = "POST" | ||
} |