2018-08-20 13:59:40 -04:00
|
|
|
package entitlements
|
|
|
|
|
2019-04-03 02:23:23 -04:00
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
2018-08-20 13:59:40 -04:00
|
|
|
|
|
|
|
type Entitlement string
|
|
|
|
|
|
|
|
const (
|
2019-04-03 02:23:23 -04:00
|
|
|
EntitlementSecurityInsecure Entitlement = "security.insecure"
|
|
|
|
EntitlementNetworkHost Entitlement = "network.host"
|
2018-08-20 13:59:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var all = map[Entitlement]struct{}{
|
2019-04-03 02:23:23 -04:00
|
|
|
EntitlementSecurityInsecure: {},
|
|
|
|
EntitlementNetworkHost: {},
|
2018-08-20 13:59:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(s string) (Entitlement, error) {
|
|
|
|
_, ok := all[Entitlement(s)]
|
|
|
|
if !ok {
|
|
|
|
return "", errors.Errorf("unknown entitlement %s", s)
|
|
|
|
}
|
|
|
|
return Entitlement(s), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func WhiteList(allowed, supported []Entitlement) (Set, error) {
|
|
|
|
m := map[Entitlement]struct{}{}
|
|
|
|
|
|
|
|
var supm Set
|
|
|
|
if supported != nil {
|
|
|
|
var err error
|
|
|
|
supm, err = WhiteList(supported, nil)
|
|
|
|
if err != nil { // should not happen
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range allowed {
|
|
|
|
e, err := Parse(string(e))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if supported != nil {
|
|
|
|
if !supm.Allowed(e) {
|
2020-07-20 09:49:37 -04:00
|
|
|
return nil, errors.Errorf("granting entitlement %s is not allowed by build daemon configuration", e)
|
2018-08-20 13:59:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m[e] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Set(m), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Set map[Entitlement]struct{}
|
|
|
|
|
|
|
|
func (s Set) Allowed(e Entitlement) bool {
|
|
|
|
_, ok := s[e]
|
|
|
|
return ok
|
|
|
|
}
|