2023-02-02 10:58:34 +00:00
|
|
|
package api
|
2022-05-05 21:06:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-02-01 06:38:04 +00:00
|
|
|
"fmt"
|
2022-05-05 21:06:18 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-logr/logr"
|
|
|
|
)
|
|
|
|
|
2023-02-02 10:58:34 +00:00
|
|
|
const ImageVerifyAnnotationKey = "kyverno.io/verify-images"
|
2022-05-05 21:06:18 +00:00
|
|
|
|
|
|
|
type ImageVerificationMetadata struct {
|
|
|
|
Data map[string]bool `json:"data"`
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:58:34 +00:00
|
|
|
func (ivm *ImageVerificationMetadata) Add(image string, verified bool) {
|
2022-05-05 21:06:18 +00:00
|
|
|
if ivm.Data == nil {
|
|
|
|
ivm.Data = make(map[string]bool)
|
|
|
|
}
|
|
|
|
ivm.Data[image] = verified
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:58:34 +00:00
|
|
|
func (ivm *ImageVerificationMetadata) IsVerified(image string) bool {
|
2022-05-05 21:06:18 +00:00
|
|
|
if ivm.Data == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
verified, ok := ivm.Data[image]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return verified
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:58:34 +00:00
|
|
|
func ParseImageMetadata(jsonData string) (*ImageVerificationMetadata, error) {
|
2022-05-05 21:06:18 +00:00
|
|
|
var data map[string]bool
|
|
|
|
if err := json.Unmarshal([]byte(jsonData), &data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ImageVerificationMetadata{
|
|
|
|
Data: data,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ivm *ImageVerificationMetadata) Patches(hasAnnotations bool, log logr.Logger) ([][]byte, error) {
|
|
|
|
var patches [][]byte
|
|
|
|
if !hasAnnotations {
|
2022-05-17 06:19:03 +00:00
|
|
|
addAnnotationsPatch := make(map[string]interface{})
|
2022-05-05 21:06:18 +00:00
|
|
|
addAnnotationsPatch["op"] = "add"
|
|
|
|
addAnnotationsPatch["path"] = "/metadata/annotations"
|
|
|
|
addAnnotationsPatch["value"] = map[string]string{}
|
|
|
|
patchBytes, err := json.Marshal(addAnnotationsPatch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.V(4).Info("adding annotation patch", "patch", string(patchBytes))
|
|
|
|
patches = append(patches, patchBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(ivm.Data)
|
|
|
|
if err != nil {
|
2023-02-01 06:38:04 +00:00
|
|
|
return nil, fmt.Errorf("failed to marshal metadata value: %v: %w", data, err)
|
2022-05-05 21:06:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 06:19:03 +00:00
|
|
|
addKeyPatch := make(map[string]interface{})
|
2022-05-05 21:06:18 +00:00
|
|
|
addKeyPatch["op"] = "add"
|
|
|
|
addKeyPatch["path"] = makeAnnotationKeyForJSONPatch()
|
|
|
|
addKeyPatch["value"] = string(data)
|
|
|
|
|
|
|
|
patchBytes, err := json.Marshal(addKeyPatch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.V(4).Info("adding image verification patch", "patch", string(patchBytes))
|
|
|
|
patches = append(patches, patchBytes)
|
|
|
|
return patches, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ivm *ImageVerificationMetadata) Merge(other *ImageVerificationMetadata) {
|
|
|
|
for k, v := range other.Data {
|
2023-02-02 10:58:34 +00:00
|
|
|
ivm.Add(k, v)
|
2022-05-05 21:06:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ivm *ImageVerificationMetadata) IsEmpty() bool {
|
|
|
|
return len(ivm.Data) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeAnnotationKeyForJSONPatch() string {
|
2023-02-02 10:58:34 +00:00
|
|
|
return "/metadata/annotations/" + strings.ReplaceAll(ImageVerifyAnnotationKey, "/", "~1")
|
2022-05-05 21:06:18 +00:00
|
|
|
}
|