Skip to content

Commit

Permalink
driver: set network.host entitlement by default for container drivers
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Feb 23, 2024
1 parent d8e9c7f commit 2d6ae5c
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 14 deletions.
55 changes: 44 additions & 11 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/google/shlex"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -429,12 +430,14 @@ func Create(ctx context.Context, txn *store.Txn, dockerCli command.Cli, opts Cre
}
}

var buildkitdFlags []string
if opts.BuildkitdFlags != "" {
buildkitdFlags, err = shlex.Split(opts.BuildkitdFlags)
if err != nil {
return nil, errors.Wrap(err, "failed to parse BuildKit daemon flags")
}
driverOpts, err := csvToMap(opts.DriverOpts)
if err != nil {
return nil, err
}

buildkitdFlags, err := parseBuildkitdFlags(opts.BuildkitdFlags, driverName, driverOpts)
if err != nil {
return nil, err
}

var ep string
Expand Down Expand Up @@ -493,11 +496,6 @@ func Create(ctx context.Context, txn *store.Txn, dockerCli command.Cli, opts Cre
setEp = false
}

driverOpts, err := csvToMap(opts.DriverOpts)
if err != nil {
return nil, err
}

buildkitdConfigFile := opts.BuildkitdConfigFile
if buildkitdConfigFile == "" {
// if buildkit daemon config is not provided, check if the default one
Expand Down Expand Up @@ -642,3 +640,38 @@ func validateBuildkitEndpoint(ep string) (string, error) {
}
return ep, nil
}

// parseBuildkitdFlags parses buildkit flags
func parseBuildkitdFlags(inp string, driver string, driverOpts map[string]string) (res []string, err error) {
if inp != "" {
res, err = shlex.Split(inp)
if err != nil {
return nil, errors.Wrap(err, "failed to parse buildkit flags")
}
}

var allowInsecureEntitlements []string
flags := pflag.NewFlagSet("buildkitd", pflag.ContinueOnError)
flags.Usage = func() {}
flags.StringArrayVar(&allowInsecureEntitlements, "allow-insecure-entitlement", nil, "")
_ = flags.Parse(res)

var hasNetworkHostEntitlement bool
for _, e := range allowInsecureEntitlements {
if e == "network.host" {
hasNetworkHostEntitlement = true
break
}
}

if v, ok := driverOpts["network"]; ok && v == "host" && !hasNetworkHostEntitlement && driver == "docker-container" {
// always set network.host entitlement if user has set network=host
res = append(res, "--allow-insecure-entitlement=network.host")
} else if len(allowInsecureEntitlements) == 0 && (driver == "kubernetes" || driver == "docker-container") {
// set network.host entitlement if user does not provide any as
// network is isolated for container drivers.
res = append(res, "--allow-insecure-entitlement=network.host")
}

return res, nil
}
113 changes: 113 additions & 0 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package builder
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -24,3 +25,115 @@ func TestCsvToMap(t *testing.T) {
require.Contains(t, r, "namespace")
require.Equal(t, r["namespace"], "default")
}

func TestParseBuildkitdFlags(t *testing.T) {
testCases := []struct {
name string
flags string
driver string
driverOpts map[string]string
expected []string
wantErr bool
}{
{
"docker-container no flags",
"",
"docker-container",
nil,
[]string{
"--allow-insecure-entitlement=network.host",
},
false,
},
{
"kubernetes no flags",
"",
"kubernetes",
nil,
[]string{
"--allow-insecure-entitlement=network.host",
},
false,
},
{
"remote no flags",
"",
"remote",
nil,
nil,
false,
},
{
"docker-container with insecure flag",
"--allow-insecure-entitlement=security.insecure",
"docker-container",
nil,
[]string{
"--allow-insecure-entitlement=security.insecure",
},
false,
},
{
"docker-container with insecure and host flag",
"--allow-insecure-entitlement=network.host --allow-insecure-entitlement=security.insecure",
"docker-container",
nil,
[]string{
"--allow-insecure-entitlement=network.host",
"--allow-insecure-entitlement=security.insecure",
},
false,
},
{
"docker-container with network host opt",
"",
"docker-container",
map[string]string{"network": "host"},
[]string{
"--allow-insecure-entitlement=network.host",
},
false,
},
{
"docker-container with host flag and network host opt",
"--allow-insecure-entitlement=network.host",
"docker-container",
map[string]string{"network": "host"},
[]string{
"--allow-insecure-entitlement=network.host",
},
false,
},
{
"docker-container with insecure, host flag and network host opt",
"--allow-insecure-entitlement=network.host --allow-insecure-entitlement=security.insecure",
"docker-container",
map[string]string{"network": "host"},
[]string{
"--allow-insecure-entitlement=network.host",
"--allow-insecure-entitlement=security.insecure",
},
false,
},
{
"error parsing flags",
"foo'",
"docker-container",
nil,
nil,
true,
},
}
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
flags, err := parseBuildkitdFlags(tt.flags, tt.driver, tt.driverOpts)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.expected, flags)
})
}
}
3 changes: 0 additions & 3 deletions driver/docker-container/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver
switch {
case k == "network":
d.netMode = v
if v == "host" {
d.InitConfig.BuildkitdFlags = append(d.InitConfig.BuildkitdFlags, "--allow-insecure-entitlement=network.host")
}
case k == "image":
d.image = v
case k == "memory":
Expand Down
31 changes: 31 additions & 0 deletions tests/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func inspectCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) {

var inspectTests = []func(t *testing.T, sb integration.Sandbox){
testInspect,
testInspectNetworkHostEntitlement,
}

func testInspect(t *testing.T, sb integration.Sandbox) {
Expand Down Expand Up @@ -47,3 +48,33 @@ func testInspect(t *testing.T, sb integration.Sandbox) {
require.Empty(t, hostGatewayIP, "host-gateway-ip worker label should not be set with non-docker driver")
}
}

func testInspectNetworkHostEntitlement(t *testing.T, sb integration.Sandbox) {
if sb.Name() != "docker-container" {
t.Skip("only testing for docker-container driver")
}

var builderName string
t.Cleanup(func() {
if builderName == "" {
return
}
out, err := rmCmd(sb, withArgs(builderName))
require.NoError(t, err, out)
})

out, err := createCmd(sb, withArgs("--driver", "docker-container"))
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)

out, err = inspectCmd(sb, withArgs(builderName))
require.NoError(t, err, out)

for _, line := range strings.Split(out, "\n") {
if v, ok := strings.CutPrefix(line, "Flags:"); ok {
require.Contains(t, v, "--allow-insecure-entitlement=network.host")
return
}
}
require.Fail(t, "network.host insecure entitlement not found in inspect output")

Check failure on line 79 in tests/inspect.go

View workflow job for this annotation

GitHub Actions / test-integration (docker-container, ./tests)

Failed: tests/TestIntegration/TestInspectNetworkHostEntitlement/worker=docker-container

=== RUN TestIntegration/TestInspectNetworkHostEntitlement/worker=docker-container === PAUSE TestIntegration/TestInspectNetworkHostEntitlement/worker=docker-container === CONT TestIntegration/TestInspectNetworkHostEntitlement/worker=docker-container inspect.go:79: Error Trace: /src/tests/inspect.go:79 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:93 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:207 Error: network.host insecure entitlement not found in inspect output Test: TestIntegration/TestInspectNetworkHostEntitlement/worker=docker-container --- FAIL: TestIntegration/TestInspectNetworkHostEntitlement/worker=docker-container (0.67s)
}

0 comments on commit 2d6ae5c

Please sign in to comment.