2019-12-30 17:08:50 -08:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-01-07 15:13:57 -08:00
|
|
|
"strings"
|
2019-12-30 17:08:50 -08:00
|
|
|
"sync"
|
|
|
|
|
2020-04-15 21:17:14 +05:30
|
|
|
"k8s.io/api/admission/v1beta1"
|
|
|
|
|
2019-12-30 17:08:50 -08:00
|
|
|
jsonpatch "github.com/evanphx/json-patch"
|
2020-03-17 11:05:20 -07:00
|
|
|
"github.com/go-logr/logr"
|
2020-10-07 11:12:31 -07:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
2020-03-17 16:25:34 -07:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2019-12-30 17:08:50 -08:00
|
|
|
)
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//Interface to manage context operations
|
2019-12-30 17:08:50 -08:00
|
|
|
type Interface interface {
|
2020-12-23 15:10:07 -08:00
|
|
|
|
|
|
|
// AddJSON merges the json with context
|
2019-12-30 17:08:50 -08:00
|
|
|
AddJSON(dataRaw []byte) error
|
2020-12-23 15:10:07 -08:00
|
|
|
|
|
|
|
// AddResource merges resource json under request.object
|
2019-12-30 17:08:50 -08:00
|
|
|
AddResource(dataRaw []byte) error
|
2020-12-23 15:10:07 -08:00
|
|
|
|
|
|
|
// AddUserInfo merges userInfo json under kyverno.userInfo
|
2020-01-07 10:33:28 -08:00
|
|
|
AddUserInfo(userInfo kyverno.UserInfo) error
|
2020-12-23 15:10:07 -08:00
|
|
|
|
|
|
|
// AddServiceAccount merges ServiceAccount types
|
|
|
|
AddServiceAccount(userName string) error
|
|
|
|
|
2019-12-30 17:08:50 -08:00
|
|
|
EvalInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
//EvalInterface ... to evaluate
|
|
|
|
type EvalInterface interface {
|
|
|
|
Query(query string) (interface{}, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Context stores the data resources as JSON
|
|
|
|
type Context struct {
|
2021-02-01 23:22:19 -08:00
|
|
|
mutex sync.RWMutex
|
|
|
|
jsonRaw []byte
|
|
|
|
jsonRawCheckpoint []byte
|
|
|
|
builtInVars []string
|
|
|
|
log logr.Logger
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//NewContext returns a new context
|
2020-11-24 17:48:54 -08:00
|
|
|
// builtInVars is the list of known variables (e.g. serviceAccountName)
|
|
|
|
func NewContext(builtInVars ...string) *Context {
|
2019-12-30 17:08:50 -08:00
|
|
|
ctx := Context{
|
2020-11-24 17:48:54 -08:00
|
|
|
jsonRaw: []byte(`{}`), // empty json struct
|
|
|
|
builtInVars: builtInVars,
|
|
|
|
log: log.Log.WithName("context"),
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
2020-11-24 17:48:54 -08:00
|
|
|
|
2019-12-30 17:08:50 -08:00
|
|
|
return &ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddJSON merges json data
|
|
|
|
func (ctx *Context) AddJSON(dataRaw []byte) error {
|
|
|
|
var err error
|
2021-02-01 23:22:19 -08:00
|
|
|
ctx.mutex.Lock()
|
|
|
|
defer ctx.mutex.Unlock()
|
2019-12-30 17:08:50 -08:00
|
|
|
// merge json
|
|
|
|
ctx.jsonRaw, err = jsonpatch.MergePatch(ctx.jsonRaw, dataRaw)
|
|
|
|
if err != nil {
|
2020-03-17 11:05:20 -07:00
|
|
|
ctx.log.Error(err, "failed to merge JSON data")
|
2019-12-30 17:08:50 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:07:30 -08:00
|
|
|
// AddRequest addes an admission request to context
|
2020-04-15 21:17:14 +05:30
|
|
|
func (ctx *Context) AddRequest(request *v1beta1.AdmissionRequest) error {
|
|
|
|
modifiedResource := struct {
|
|
|
|
Request interface{} `json:"request"`
|
|
|
|
}{
|
|
|
|
Request: request,
|
|
|
|
}
|
|
|
|
|
|
|
|
objRaw, err := json.Marshal(modifiedResource)
|
|
|
|
if err != nil {
|
2020-05-06 01:02:39 +05:30
|
|
|
ctx.log.Error(err, "failed to marshal the request")
|
2020-04-15 21:17:14 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ctx.AddJSON(objRaw)
|
|
|
|
}
|
|
|
|
|
|
|
|
//AddResource data at path: request.object
|
2019-12-30 17:08:50 -08:00
|
|
|
func (ctx *Context) AddResource(dataRaw []byte) error {
|
|
|
|
|
|
|
|
// unmarshall the resource struct
|
|
|
|
var data interface{}
|
|
|
|
if err := json.Unmarshal(dataRaw, &data); err != nil {
|
2020-03-17 11:05:20 -07:00
|
|
|
ctx.log.Error(err, "failed to unmarshall the resource")
|
2019-12-30 17:08:50 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
modifiedResource := struct {
|
|
|
|
Request interface{} `json:"request"`
|
|
|
|
}{
|
|
|
|
Request: struct {
|
|
|
|
Object interface{} `json:"object"`
|
|
|
|
}{
|
|
|
|
Object: data,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
objRaw, err := json.Marshal(modifiedResource)
|
|
|
|
if err != nil {
|
2020-03-17 11:05:20 -07:00
|
|
|
ctx.log.Error(err, "failed to marshal the resource")
|
2019-12-30 17:08:50 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ctx.AddJSON(objRaw)
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//AddUserInfo adds userInfo at path request.userInfo
|
2020-01-07 10:33:28 -08:00
|
|
|
func (ctx *Context) AddUserInfo(userRequestInfo kyverno.RequestInfo) error {
|
2020-05-05 19:19:47 +05:30
|
|
|
modifiedResource := struct {
|
|
|
|
Request interface{} `json:"request"`
|
|
|
|
}{
|
|
|
|
Request: userRequestInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
objRaw, err := json.Marshal(modifiedResource)
|
2019-12-30 17:08:50 -08:00
|
|
|
if err != nil {
|
2020-03-17 11:05:20 -07:00
|
|
|
ctx.log.Error(err, "failed to marshal the UserInfo")
|
2019-12-30 17:08:50 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ctx.AddJSON(objRaw)
|
|
|
|
}
|
2020-01-07 15:13:57 -08:00
|
|
|
|
2020-12-23 15:10:07 -08:00
|
|
|
//AddServiceAccount removes prefix 'system:serviceaccount:' and namespace, then loads only SA name and SA namespace
|
|
|
|
func (ctx *Context) AddServiceAccount(userName string) error {
|
2020-01-07 15:13:57 -08:00
|
|
|
saPrefix := "system:serviceaccount:"
|
|
|
|
var sa string
|
|
|
|
saName := ""
|
|
|
|
saNamespace := ""
|
|
|
|
if len(userName) <= len(saPrefix) {
|
|
|
|
sa = ""
|
|
|
|
} else {
|
|
|
|
sa = userName[len(saPrefix):]
|
|
|
|
}
|
|
|
|
// filter namespace
|
|
|
|
groups := strings.Split(sa, ":")
|
|
|
|
if len(groups) >= 2 {
|
|
|
|
saName = groups[1]
|
|
|
|
saNamespace = groups[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
saNameObj := struct {
|
|
|
|
SA string `json:"serviceAccountName"`
|
|
|
|
}{
|
|
|
|
SA: saName,
|
|
|
|
}
|
|
|
|
saNameRaw, err := json.Marshal(saNameObj)
|
|
|
|
if err != nil {
|
2020-03-17 11:05:20 -07:00
|
|
|
ctx.log.Error(err, "failed to marshal the SA")
|
2020-01-07 15:13:57 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ctx.AddJSON(saNameRaw); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
saNsObj := struct {
|
|
|
|
SA string `json:"serviceAccountNamespace"`
|
|
|
|
}{
|
|
|
|
SA: saNamespace,
|
|
|
|
}
|
|
|
|
saNsRaw, err := json.Marshal(saNsObj)
|
|
|
|
if err != nil {
|
2020-03-17 11:05:20 -07:00
|
|
|
ctx.log.Error(err, "failed to marshal the SA namespace")
|
2020-01-07 15:13:57 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ctx.AddJSON(saNsRaw); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-01 23:22:19 -08:00
|
|
|
|
|
|
|
// Checkpoint creates a copy of the internal state.
|
|
|
|
// Prior checkpoints will be overridden.
|
|
|
|
func (ctx *Context) Checkpoint() {
|
|
|
|
ctx.mutex.Lock()
|
|
|
|
defer ctx.mutex.Unlock()
|
|
|
|
|
|
|
|
ctx.jsonRawCheckpoint = make([]byte, len(ctx.jsonRaw))
|
|
|
|
copy(ctx.jsonRawCheckpoint, ctx.jsonRaw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore restores internal state from a prior checkpoint, if one exists.
|
|
|
|
// If a prior checkpoint does not exist, the state will not be changed.
|
|
|
|
func (ctx *Context) Restore() {
|
|
|
|
ctx.mutex.Lock()
|
|
|
|
defer ctx.mutex.Unlock()
|
|
|
|
|
|
|
|
if ctx.jsonRawCheckpoint == nil || len(ctx.jsonRawCheckpoint) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.jsonRaw = make([]byte, len(ctx.jsonRawCheckpoint))
|
|
|
|
copy(ctx.jsonRaw, ctx.jsonRawCheckpoint)
|
2021-02-07 20:26:56 -08:00
|
|
|
}
|