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"
|
|
|
|
|
|
|
|
jsonpatch "github.com/evanphx/json-patch"
|
|
|
|
"github.com/golang/glog"
|
2020-01-07 10:33:28 -08:00
|
|
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
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-01-24 12:05:53 -08:00
|
|
|
//AddJSON merges the json with context
|
2019-12-30 17:08:50 -08:00
|
|
|
AddJSON(dataRaw []byte) error
|
2020-01-24 12:05:53 -08:00
|
|
|
//AddResource merges resource json under request.object
|
2019-12-30 17:08:50 -08:00
|
|
|
AddResource(dataRaw []byte) error
|
2020-01-24 12:05:53 -08:00
|
|
|
//AddUserInfo merges userInfo json under kyverno.userInfo
|
2020-01-07 10:33:28 -08:00
|
|
|
AddUserInfo(userInfo kyverno.UserInfo) error
|
2020-01-24 12:05:53 -08:00
|
|
|
//AddSA merges serrviceaccount
|
2020-01-07 15:13:57 -08:00
|
|
|
AddSA(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 {
|
2020-01-07 10:33:28 -08:00
|
|
|
mu sync.RWMutex
|
|
|
|
// data map[string]interface{}
|
2019-12-30 17:08:50 -08:00
|
|
|
jsonRaw []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewContext returns a new context
|
|
|
|
func NewContext() *Context {
|
|
|
|
ctx := Context{
|
|
|
|
// data: map[string]interface{}{},
|
|
|
|
jsonRaw: []byte(`{}`), // empty json struct
|
|
|
|
}
|
|
|
|
return &ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddJSON merges json data
|
|
|
|
func (ctx *Context) AddJSON(dataRaw []byte) error {
|
|
|
|
var err error
|
|
|
|
ctx.mu.Lock()
|
|
|
|
defer ctx.mu.Unlock()
|
|
|
|
// merge json
|
|
|
|
ctx.jsonRaw, err = jsonpatch.MergePatch(ctx.jsonRaw, dataRaw)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("failed to merge JSON data: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//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 {
|
|
|
|
glog.V(4).Infof("failed to unmarshall the context data: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
modifiedResource := struct {
|
|
|
|
Request interface{} `json:"request"`
|
|
|
|
}{
|
|
|
|
Request: struct {
|
|
|
|
Object interface{} `json:"object"`
|
|
|
|
}{
|
|
|
|
Object: data,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
objRaw, err := json.Marshal(modifiedResource)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("failed to marshall the updated context data")
|
|
|
|
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 {
|
2019-12-30 17:08:50 -08:00
|
|
|
modifiedResource := struct {
|
|
|
|
Request interface{} `json:"request"`
|
|
|
|
}{
|
2020-01-07 10:33:28 -08:00
|
|
|
Request: userRequestInfo,
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
objRaw, err := json.Marshal(modifiedResource)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("failed to marshall the updated context data")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ctx.AddJSON(objRaw)
|
|
|
|
}
|
2020-01-07 15:13:57 -08:00
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//AddSA removes prefix 'system:serviceaccount:' and namespace, then loads only SA name and SA namespace
|
2020-01-07 15:13:57 -08:00
|
|
|
func (ctx *Context) AddSA(userName string) error {
|
|
|
|
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 {
|
|
|
|
glog.V(4).Infof("serviceAccount namespace: %s", groups[0])
|
|
|
|
glog.V(4).Infof("serviceAccount name: %s", groups[1])
|
|
|
|
saName = groups[1]
|
|
|
|
saNamespace = groups[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.Infof("Loading variable serviceAccountName with value: %s", saName)
|
|
|
|
saNameObj := struct {
|
|
|
|
SA string `json:"serviceAccountName"`
|
|
|
|
}{
|
|
|
|
SA: saName,
|
|
|
|
}
|
|
|
|
saNameRaw, err := json.Marshal(saNameObj)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("failed to marshall the updated context data")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ctx.AddJSON(saNameRaw); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.Infof("Loading variable serviceAccountNamespace with value: %s", saNamespace)
|
|
|
|
saNsObj := struct {
|
|
|
|
SA string `json:"serviceAccountNamespace"`
|
|
|
|
}{
|
|
|
|
SA: saNamespace,
|
|
|
|
}
|
|
|
|
saNsRaw, err := json.Marshal(saNsObj)
|
|
|
|
if err != nil {
|
|
|
|
glog.V(4).Infof("failed to marshall the updated context data")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ctx.AddJSON(saNsRaw); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|