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

add flag to disable networking in build environment #145

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type Context struct {
DependencyLog string
BinShOverlay string
ignorePatterns []*xignore.Pattern
DisableNetwork bool
}

type Dependencies struct {
Expand Down Expand Up @@ -374,6 +375,12 @@ func WithBinShOverlay(binShOverlay string) Option {
}
}

// WithDisableNetwork disables networking in the build environment.
var WithDisableNetwork Option = func(ctx *Context) error {
ctx.DisableNetwork = true
return nil
}

// Load the configuration data from the build context configuration file.
func (cfg *Configuration) Load(configFile, template string) error {
data, err := os.ReadFile(configFile)
Expand Down Expand Up @@ -792,6 +799,10 @@ func (ctx *Context) WorkspaceCmd(args ...string) (*exec.Cmd, error) {
"--setenv", "SOURCE_DATE_EPOCH", fmt.Sprintf("%d", ctx.SourceDateEpoch.Unix()),
}

if ctx.DisableNetwork {
baseargs = append(baseargs, "--unshare-net")
}

// Add any user-provided env vars
for k, v := range ctx.Configuration.Environment.Environment {
baseargs = append(baseargs, "--setenv", k, v)
Expand Down
5 changes: 5 additions & 0 deletions pkg/cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Build() *cobra.Command {
var template string
var dependencyLog string
var overlayBinSh string
var disableNet bool

cmd := &cobra.Command{
Use: "build",
Expand All @@ -67,6 +68,9 @@ func Build() *cobra.Command {
build.WithDependencyLog(dependencyLog),
build.WithBinShOverlay(overlayBinSh),
}
if disableNet {
options = append(options, build.WithDisableNetwork)
}

if len(args) > 0 {
options = append(options, build.WithConfig(args[0]))
Expand Down Expand Up @@ -104,6 +108,7 @@ func Build() *cobra.Command {
cmd.Flags().StringSliceVar(&archstrs, "arch", nil, "architectures to build for (e.g., x86_64,ppc64le,arm64) -- default is all, unless specified in config.")
cmd.Flags().StringSliceVarP(&extraKeys, "keyring-append", "k", []string{}, "path to extra keys to include in the build environment keyring")
cmd.Flags().StringSliceVarP(&extraRepos, "repository-append", "r", []string{}, "path to extra repositories to include in the build environment")
cmd.Flags().BoolVar(&disableNet, "disable-network", false, "if true, networking is disabled in the build environment")

return cmd
}
Expand Down