-
Notifications
You must be signed in to change notification settings - Fork 495
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
bake: enable support for entitlements #2666
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,175 @@ | ||
package bake | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/containerd/console" | ||
"github.com/docker/buildx/build" | ||
"github.com/moby/buildkit/util/entitlements" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type EntitlementKey string | ||
|
||
const ( | ||
EntitlementKeyNetworkHost EntitlementKey = "network.host" | ||
EntitlementKeySecurityInsecure EntitlementKey = "security.insecure" | ||
EntitlementKeyFSRead EntitlementKey = "fs.read" | ||
EntitlementKeyFSWrite EntitlementKey = "fs.write" | ||
EntitlementKeyFS EntitlementKey = "fs" | ||
EntitlementKeyImagePush EntitlementKey = "image.push" | ||
EntitlementKeyImageLoad EntitlementKey = "image.load" | ||
EntitlementKeyImage EntitlementKey = "image" | ||
EntitlementKeySSH EntitlementKey = "ssh" | ||
) | ||
|
||
type EntitlementConf struct { | ||
NetworkHost bool | ||
SecurityInsecure bool | ||
FSRead []string | ||
FSWrite []string | ||
ImagePush []string | ||
ImageLoad []string | ||
SSH bool | ||
} | ||
|
||
func ParseEntitlements(in []string) (EntitlementConf, error) { | ||
var conf EntitlementConf | ||
for _, e := range in { | ||
switch e { | ||
case string(EntitlementKeyNetworkHost): | ||
conf.NetworkHost = true | ||
case string(EntitlementKeySecurityInsecure): | ||
conf.SecurityInsecure = true | ||
case string(EntitlementKeySSH): | ||
conf.SSH = true | ||
default: | ||
k, v, _ := strings.Cut(e, "=") | ||
switch k { | ||
case string(EntitlementKeyFSRead): | ||
conf.FSRead = append(conf.FSRead, v) | ||
case string(EntitlementKeyFSWrite): | ||
conf.FSWrite = append(conf.FSWrite, v) | ||
case string(EntitlementKeyFS): | ||
conf.FSRead = append(conf.FSRead, v) | ||
conf.FSWrite = append(conf.FSWrite, v) | ||
case string(EntitlementKeyImagePush): | ||
conf.ImagePush = append(conf.ImagePush, v) | ||
case string(EntitlementKeyImageLoad): | ||
conf.ImageLoad = append(conf.ImageLoad, v) | ||
case string(EntitlementKeyImage): | ||
conf.ImagePush = append(conf.ImagePush, v) | ||
conf.ImageLoad = append(conf.ImageLoad, v) | ||
default: | ||
return conf, errors.Errorf("uknown entitlement key %q", k) | ||
} | ||
|
||
// TODO: dedupe slices and parent paths | ||
} | ||
} | ||
return conf, nil | ||
} | ||
|
||
func (c EntitlementConf) Validate(m map[string]build.Options) (EntitlementConf, error) { | ||
var expected EntitlementConf | ||
|
||
for _, v := range m { | ||
if err := c.check(v, &expected); err != nil { | ||
return EntitlementConf{}, err | ||
} | ||
} | ||
|
||
return expected, nil | ||
} | ||
|
||
func (c EntitlementConf) check(bo build.Options, expected *EntitlementConf) error { | ||
for _, e := range bo.Allow { | ||
switch e { | ||
case entitlements.EntitlementNetworkHost: | ||
if !c.NetworkHost { | ||
expected.NetworkHost = true | ||
} | ||
case entitlements.EntitlementSecurityInsecure: | ||
if !c.SecurityInsecure { | ||
expected.SecurityInsecure = true | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (c EntitlementConf) Prompt(ctx context.Context, out io.Writer) error { | ||
var term bool | ||
if _, err := console.ConsoleFromFile(os.Stdin); err == nil { | ||
term = true | ||
} | ||
|
||
var msgs []string | ||
var flags []string | ||
|
||
if c.NetworkHost { | ||
msgs = append(msgs, " - Running build containers that can access host network") | ||
flags = append(flags, "network.host") | ||
} | ||
if c.SecurityInsecure { | ||
msgs = append(msgs, " - Running privileged containers that can make system changes") | ||
flags = append(flags, "security.insecure") | ||
} | ||
|
||
if len(msgs) == 0 { | ||
return nil | ||
} | ||
|
||
fmt.Fprintf(out, "Your build is requesting privileges for following possibly insecure capabilities:\n\n") | ||
for _, m := range msgs { | ||
fmt.Fprintf(out, "%s\n", m) | ||
} | ||
|
||
for i, f := range flags { | ||
flags[i] = "--allow=" + f | ||
} | ||
|
||
if term { | ||
fmt.Fprintf(out, "\nIn order to not see this message in the future pass %q to grant requested privileges.\n", strings.Join(flags, " ")) | ||
} else { | ||
fmt.Fprintf(out, "\nPass %q to grant requested privileges.\n", strings.Join(flags, " ")) | ||
} | ||
|
||
args := append([]string(nil), os.Args...) | ||
if v, ok := os.LookupEnv("DOCKER_CLI_PLUGIN_ORIGINAL_CLI_COMMAND"); ok && v != "" { | ||
args[0] = v | ||
} | ||
idx := slices.Index(args, "bake") | ||
|
||
if idx != -1 { | ||
fmt.Fprintf(out, "\nYour full command with requested privileges:\n\n") | ||
fmt.Fprintf(out, "%s %s %s\n\n", strings.Join(args[:idx+1], " "), strings.Join(flags, " "), strings.Join(args[idx+1:], " ")) | ||
} | ||
|
||
if term { | ||
fmt.Fprintf(out, "Do you want to grant requested privileges and continue? [y/N] ") | ||
reader := bufio.NewReader(os.Stdin) | ||
answerCh := make(chan string, 1) | ||
go func() { | ||
answer, _, _ := reader.ReadLine() | ||
answerCh <- string(answer) | ||
close(answerCh) | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
case answer := <-answerCh: | ||
if strings.ToLower(string(answer)) == "y" { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
return errors.Errorf("additional privileges requested") | ||
} |
Oops, something went wrong.
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.
Even though these additional entitlements are defined here, the current PR does not perform any validation for them yet.