@@ -20,13 +20,15 @@ import (
2020 "github.com/moby/buildkit/util/entitlements"
2121 "github.com/pkg/errors"
2222 "github.com/sirupsen/logrus"
23+ "github.com/tonistiigi/go-csvvalue"
2324)
2425
2526type EntitlementKey string
2627
2728const (
2829 EntitlementKeyNetworkHost EntitlementKey = "network.host"
2930 EntitlementKeySecurityInsecure EntitlementKey = "security.insecure"
31+ EntitlementKeyDevice EntitlementKey = "device"
3032 EntitlementKeyFSRead EntitlementKey = "fs.read"
3133 EntitlementKeyFSWrite EntitlementKey = "fs.write"
3234 EntitlementKeyFS EntitlementKey = "fs"
@@ -39,13 +41,19 @@ const (
3941type EntitlementConf struct {
4042 NetworkHost bool
4143 SecurityInsecure bool
44+ Devices * EntitlementsDevicesConf
4245 FSRead []string
4346 FSWrite []string
4447 ImagePush []string
4548 ImageLoad []string
4649 SSH bool
4750}
4851
52+ type EntitlementsDevicesConf struct {
53+ All bool
54+ Devices map [string ]struct {}
55+ }
56+
4957func ParseEntitlements (in []string ) (EntitlementConf , error ) {
5058 var conf EntitlementConf
5159 for _ , e := range in {
@@ -59,6 +67,22 @@ func ParseEntitlements(in []string) (EntitlementConf, error) {
5967 default :
6068 k , v , _ := strings .Cut (e , "=" )
6169 switch k {
70+ case string (EntitlementKeyDevice ):
71+ if v == "" {
72+ conf .Devices = & EntitlementsDevicesConf {All : true }
73+ continue
74+ }
75+ fields , err := csvvalue .Fields (v , nil )
76+ if err != nil {
77+ return EntitlementConf {}, errors .Wrapf (err , "failed to parse device entitlement %q" , v )
78+ }
79+ if conf .Devices == nil {
80+ conf .Devices = & EntitlementsDevicesConf {}
81+ }
82+ if conf .Devices .Devices == nil {
83+ conf .Devices .Devices = make (map [string ]struct {}, 0 )
84+ }
85+ conf .Devices .Devices [fields [0 ]] = struct {}{}
6286 case string (EntitlementKeyFSRead ):
6387 conf .FSRead = append (conf .FSRead , v )
6488 case string (EntitlementKeyFSWrite ):
@@ -95,12 +119,34 @@ func (c EntitlementConf) Validate(m map[string]build.Options) (EntitlementConf,
95119
96120func (c EntitlementConf ) check (bo build.Options , expected * EntitlementConf ) error {
97121 for _ , e := range bo .Allow {
122+ k , rest , _ := strings .Cut (e , "=" )
123+ switch k {
124+ case entitlements .EntitlementDevice .String ():
125+ if rest == "" {
126+ if c .Devices == nil || ! c .Devices .All {
127+ expected .Devices = & EntitlementsDevicesConf {All : true }
128+ }
129+ continue
130+ }
131+ fields , err := csvvalue .Fields (rest , nil )
132+ if err != nil {
133+ return errors .Wrapf (err , "failed to parse device entitlement %q" , rest )
134+ }
135+ if expected .Devices == nil {
136+ expected .Devices = & EntitlementsDevicesConf {}
137+ }
138+ if expected .Devices .Devices == nil {
139+ expected .Devices .Devices = make (map [string ]struct {}, 0 )
140+ }
141+ expected .Devices .Devices [fields [0 ]] = struct {}{}
142+ }
143+
98144 switch e {
99- case entitlements .EntitlementNetworkHost :
145+ case entitlements .EntitlementNetworkHost . String () :
100146 if ! c .NetworkHost {
101147 expected .NetworkHost = true
102148 }
103- case entitlements .EntitlementSecurityInsecure :
149+ case entitlements .EntitlementSecurityInsecure . String () :
104150 if ! c .SecurityInsecure {
105151 expected .SecurityInsecure = true
106152 }
@@ -187,6 +233,18 @@ func (c EntitlementConf) Prompt(ctx context.Context, isRemote bool, out io.Write
187233 flags = append (flags , string (EntitlementKeySecurityInsecure ))
188234 }
189235
236+ if c .Devices != nil {
237+ if c .Devices .All {
238+ msgs = append (msgs , " - Access to CDI devices" )
239+ flags = append (flags , string (EntitlementKeyDevice ))
240+ } else {
241+ for d := range c .Devices .Devices {
242+ msgs = append (msgs , fmt .Sprintf (" - Access to device %s" , d ))
243+ flags = append (flags , string (EntitlementKeyDevice )+ "=" + d )
244+ }
245+ }
246+ }
247+
190248 if c .SSH {
191249 msgsFS = append (msgsFS , " - Forwarding default SSH agent socket" )
192250 flagsFS = append (flagsFS , string (EntitlementKeySSH ))
0 commit comments