-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement frontend support for RUN --ents=...
This follows syntax suggested in #950. Example: RUN --ents=security.insecure cat /proc/self/status | grep CapEff #84 0.093 CapEff: 0000003fffffffff Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
- Loading branch information
Showing
9 changed files
with
192 additions
and
2 deletions.
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
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 !dfrunents | ||
|
||
package dockerfile2llb | ||
|
||
import ( | ||
"github.com/moby/buildkit/frontend/dockerfile/instructions" | ||
) | ||
|
||
func dispatchRunEnts(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,25 @@ | ||
// +build dfrunents | ||
|
||
package dockerfile2llb | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
|
||
"github.com/moby/buildkit/frontend/dockerfile/instructions" | ||
"github.com/moby/buildkit/solver/pb" | ||
) | ||
|
||
func dispatchRunEnts(d *dispatchState, c *instructions.RunCommand) error { | ||
ents := instructions.GetEnts(c) | ||
|
||
for _, ent := range ents { | ||
switch ent { | ||
case instructions.EntitlementSecurityInsecure: | ||
d.state = d.state.Security(pb.SecurityMode_INSECURE) | ||
default: | ||
return errors.Errorf("unsupported entitlement %q", ent) | ||
} | ||
} | ||
|
||
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,53 @@ | ||
// +build dfrunents | ||
|
||
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 entsTests = []integration.Test{ | ||
testEntsSecurityInsecure, | ||
} | ||
|
||
func init() { | ||
allTests = append(allTests, entsTests...) | ||
|
||
} | ||
|
||
func testEntsSecurityInsecure(t *testing.T, sb integration.Sandbox) { | ||
f := getFrontend(t, sb) | ||
|
||
dockerfile := []byte(` | ||
FROM busybox | ||
RUN --ents=security.insecure [ "$(cat /proc/self/status | grep CapEff)" == "CapEff: 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) | ||
require.NoError(t, err) | ||
} |
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,79 @@ | ||
// +build dfrunents | ||
|
||
package instructions | ||
|
||
import ( | ||
"encoding/csv" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const EntitlementSecurityInsecure = "security.insecure" | ||
|
||
var allowedEntitlements = map[string]struct{}{ | ||
EntitlementSecurityInsecure: {}, | ||
} | ||
|
||
func isValidEntitlement(value string) bool { | ||
_, ok := allowedEntitlements[value] | ||
return ok | ||
} | ||
|
||
type entsKeyT string | ||
|
||
var entsKey = entsKeyT("dockerfile/run/mounts") | ||
|
||
func init() { | ||
parseRunPreHooks = append(parseRunPreHooks, runEntsPreHook) | ||
parseRunPostHooks = append(parseRunPostHooks, runEntsPostHook) | ||
} | ||
|
||
func runEntsPreHook(cmd *RunCommand, req parseRequest) error { | ||
st := &entState{} | ||
st.flag = req.flags.AddStrings("ents") | ||
cmd.setExternalValue(entsKey, st) | ||
return nil | ||
} | ||
|
||
func runEntsPostHook(cmd *RunCommand, req parseRequest) error { | ||
st := getEntsState(cmd) | ||
if st == nil { | ||
return errors.Errorf("no entitlements 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 mounts") | ||
} | ||
|
||
for _, field := range fields { | ||
if !isValidEntitlement(field) { | ||
return errors.Errorf("entitlement %q is not valid", field) | ||
} | ||
|
||
st.entitlements = append(st.entitlements, field) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getEntsState(cmd *RunCommand) *entState { | ||
v := cmd.getExternalValue(entsKey) | ||
if v == nil { | ||
return nil | ||
} | ||
return v.(*entState) | ||
} | ||
|
||
func GetEnts(cmd *RunCommand) []string { | ||
return getEntsState(cmd).entitlements | ||
} | ||
|
||
type entState struct { | ||
flag *Flag | ||
entitlements []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 | ||
dfrunents dfrunmount dfsecrets dfssh |
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