-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implement frontend support for RUN --security=insecure #1081
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
11 changes: 11 additions & 0 deletions
11
frontend/dockerfile/dockerfile2llb/convert_norunsecurity.go
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,11 @@ | ||
// +build !dfrunsecurity | ||
|
||
package dockerfile2llb | ||
|
||
import ( | ||
"github.com/moby/buildkit/frontend/dockerfile/instructions" | ||
) | ||
|
||
func dispatchRunSecurity(d *dispatchState, c *instructions.RunCommand) error { | ||
return nil | ||
} |
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,27 @@ | ||
// +build dfrunsecurity | ||
|
||
package dockerfile2llb | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
|
||
"github.com/moby/buildkit/frontend/dockerfile/instructions" | ||
"github.com/moby/buildkit/solver/pb" | ||
) | ||
|
||
func dispatchRunSecurity(d *dispatchState, c *instructions.RunCommand) error { | ||
security := instructions.GetSecurity(c) | ||
|
||
for _, sec := range security { | ||
switch sec { | ||
case instructions.SecurityInsecure: | ||
d.state = d.state.Security(pb.SecurityMode_INSECURE) | ||
case instructions.SecuritySandbox: | ||
d.state = d.state.Security(pb.SecurityMode_SANDBOX) | ||
default: | ||
return errors.Errorf("unsupported security mode %q", sec) | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,131 @@ | ||
// +build dfrunsecurity | ||
|
||
package dockerfile | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/containerd/continuity/fs/fstest" | ||
"github.com/moby/buildkit/client" | ||
"github.com/moby/buildkit/frontend/dockerfile/builder" | ||
"github.com/moby/buildkit/util/entitlements" | ||
"github.com/moby/buildkit/util/testutil/integration" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var runSecurityTests = []integration.Test{ | ||
testRunSecurityInsecure, | ||
testRunSecuritySandbox, | ||
testRunSecurityDefault, | ||
} | ||
|
||
func init() { | ||
securityTests = append(securityTests, runSecurityTests...) | ||
|
||
} | ||
|
||
func testRunSecurityInsecure(t *testing.T, sb integration.Sandbox) { | ||
f := getFrontend(t, sb) | ||
|
||
dockerfile := []byte(` | ||
FROM busybox | ||
RUN --security=insecure [ "$(cat /proc/self/status | grep CapBnd)" == "CapBnd: 0000003fffffffff" ] | ||
`) | ||
|
||
dir, err := tmpdir( | ||
fstest.CreateFile("Dockerfile", dockerfile, 0600), | ||
) | ||
require.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
|
||
c, err := client.New(context.TODO(), sb.Address()) | ||
require.NoError(t, err) | ||
defer c.Close() | ||
|
||
_, err = f.Solve(context.TODO(), c, client.SolveOpt{ | ||
LocalDirs: map[string]string{ | ||
builder.DefaultLocalNameDockerfile: dir, | ||
builder.DefaultLocalNameContext: dir, | ||
}, | ||
AllowedEntitlements: []entitlements.Entitlement{entitlements.EntitlementSecurityInsecure}, | ||
}, nil) | ||
|
||
secMode := sb.Value("secmode") | ||
switch secMode { | ||
case securitySandbox: | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "entitlement security.insecure is not allowed") | ||
case securityInsecure: | ||
require.NoError(t, err) | ||
default: | ||
require.Fail(t, "unexpected secmode") | ||
} | ||
} | ||
|
||
func testRunSecuritySandbox(t *testing.T, sb integration.Sandbox) { | ||
f := getFrontend(t, sb) | ||
|
||
dockerfile := []byte(` | ||
FROM busybox | ||
RUN --security=sandbox [ "$(cat /proc/self/status | grep CapBnd)" == "CapBnd: 00000000a80425fb" ] | ||
`) | ||
|
||
dir, err := tmpdir( | ||
fstest.CreateFile("Dockerfile", dockerfile, 0600), | ||
) | ||
require.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
|
||
c, err := client.New(context.TODO(), sb.Address()) | ||
require.NoError(t, err) | ||
defer c.Close() | ||
|
||
_, err = f.Solve(context.TODO(), c, client.SolveOpt{ | ||
LocalDirs: map[string]string{ | ||
builder.DefaultLocalNameDockerfile: dir, | ||
builder.DefaultLocalNameContext: dir, | ||
}, | ||
}, nil) | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
func testRunSecurityDefault(t *testing.T, sb integration.Sandbox) { | ||
f := getFrontend(t, sb) | ||
|
||
dockerfile := []byte(` | ||
FROM busybox | ||
RUN [ "$(cat /proc/self/status | grep CapBnd)" == "CapBnd: 00000000a80425fb" ] | ||
`) | ||
|
||
dir, err := tmpdir( | ||
fstest.CreateFile("Dockerfile", dockerfile, 0600), | ||
) | ||
require.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
|
||
c, err := client.New(context.TODO(), sb.Address()) | ||
require.NoError(t, err) | ||
defer c.Close() | ||
|
||
_, err = f.Solve(context.TODO(), c, client.SolveOpt{ | ||
LocalDirs: map[string]string{ | ||
builder.DefaultLocalNameDockerfile: dir, | ||
builder.DefaultLocalNameContext: dir, | ||
}, | ||
AllowedEntitlements: []entitlements.Entitlement{entitlements.EntitlementSecurityInsecure}, | ||
}, nil) | ||
|
||
secMode := sb.Value("secmode") | ||
switch secMode { | ||
case securitySandbox: | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "entitlement security.insecure is not allowed") | ||
case securityInsecure: | ||
require.NoError(t, err) | ||
default: | ||
require.Fail(t, "unexpected secmode") | ||
} | ||
} |
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
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,83 @@ | ||
// +build dfrunsecurity | ||
|
||
package instructions | ||
|
||
import ( | ||
"encoding/csv" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
SecurityInsecure = "insecure" | ||
SecuritySandbox = "sandbox" | ||
) | ||
|
||
var allowedSecurity = map[string]struct{}{ | ||
SecurityInsecure: {}, | ||
SecuritySandbox: {}, | ||
} | ||
|
||
func isValidSecurity(value string) bool { | ||
_, ok := allowedSecurity[value] | ||
return ok | ||
} | ||
|
||
type securityKeyT string | ||
|
||
var securityKey = securityKeyT("dockerfile/run/security") | ||
|
||
func init() { | ||
parseRunPreHooks = append(parseRunPreHooks, runSecurityPreHook) | ||
parseRunPostHooks = append(parseRunPostHooks, runSecurityPostHook) | ||
} | ||
|
||
func runSecurityPreHook(cmd *RunCommand, req parseRequest) error { | ||
st := &securityState{} | ||
st.flag = req.flags.AddStrings("security") | ||
cmd.setExternalValue(securityKey, st) | ||
return nil | ||
} | ||
|
||
func runSecurityPostHook(cmd *RunCommand, req parseRequest) error { | ||
st := getSecurityState(cmd) | ||
if st == nil { | ||
return errors.Errorf("no security state") | ||
} | ||
|
||
for _, value := range st.flag.StringValues { | ||
csvReader := csv.NewReader(strings.NewReader(value)) | ||
fields, err := csvReader.Read() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to parse csv security") | ||
} | ||
|
||
for _, field := range fields { | ||
if !isValidSecurity(field) { | ||
return errors.Errorf("security %q is not valid", field) | ||
} | ||
|
||
st.security = append(st.security, field) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getSecurityState(cmd *RunCommand) *securityState { | ||
v := cmd.getExternalValue(securityKey) | ||
if v == nil { | ||
return nil | ||
} | ||
return v.(*securityState) | ||
} | ||
|
||
func GetSecurity(cmd *RunCommand) []string { | ||
return getSecurityState(cmd).security | ||
} | ||
|
||
type securityState struct { | ||
flag *Flag | ||
security []string | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
dfrunmount dfsecrets dfssh | ||
dfrunmount dfrunsecurity dfsecrets dfssh | ||
AkihiroSuda marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add: In order to build Dockerfiles with
--security=insecure
user needs to enablesecurity.insecure
entitlement when starting the buildkit daemon and allow it for a specific build request.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated!