2020-03-11 18:14:23 -07:00
package generate
import (
2022-11-29 14:59:40 +01:00
"context"
2020-03-11 18:14:23 -07:00
"fmt"
2023-06-22 22:14:06 +08:00
"strings"
2020-03-11 18:14:23 -07:00
2020-03-17 11:05:20 -07:00
"github.com/go-logr/logr"
2022-05-17 13:12:43 +02:00
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
2022-08-31 14:03:47 +08:00
"github.com/kyverno/kyverno/pkg/clients/dclient"
2023-03-03 19:32:40 +08:00
"github.com/kyverno/kyverno/pkg/engine/variables/regex"
2020-10-07 11:12:31 -07:00
"github.com/kyverno/kyverno/pkg/policy/common"
2022-09-26 13:23:00 +05:30
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
2022-09-28 21:14:38 +05:30
"github.com/kyverno/kyverno/pkg/utils/wildcard"
2020-03-11 18:14:23 -07:00
)
// Generate provides implementation to validate 'generate' rule
type Generate struct {
2023-08-23 14:29:56 +02:00
user string
2020-03-11 18:14:23 -07:00
// rule to hold 'generate' rule specifications
2022-05-17 13:12:43 +02:00
rule kyvernov1 . Generation
2020-03-11 18:14:23 -07:00
// authCheck to check access for operations
authCheck Operations
2022-05-17 08:19:03 +02:00
// logger
2020-03-17 11:05:20 -07:00
log logr . Logger
2020-03-11 18:14:23 -07:00
}
2022-05-17 08:19:03 +02:00
// NewGenerateFactory returns a new instance of Generate validation checker
2023-04-24 18:31:42 +08:00
func NewGenerateFactory ( client dclient . Interface , rule kyvernov1 . Generation , user string , log logr . Logger ) * Generate {
2020-03-11 18:14:23 -07:00
g := Generate {
2023-08-23 14:29:56 +02:00
user : user ,
2020-03-11 18:14:23 -07:00
rule : rule ,
2023-04-24 18:31:42 +08:00
authCheck : NewAuth ( client , user , log ) ,
2020-03-17 11:05:20 -07:00
log : log ,
2020-03-11 18:14:23 -07:00
}
return & g
}
2022-05-17 08:19:03 +02:00
// Validate validates the 'generate' rule
2023-03-22 21:14:57 +08:00
func ( g * Generate ) Validate ( ctx context . Context ) ( string , error ) {
2020-03-11 18:14:23 -07:00
rule := g . rule
2022-05-17 13:12:43 +02:00
if rule . GetData ( ) != nil && rule . Clone != ( kyvernov1 . CloneFrom { } ) {
2020-12-14 02:43:16 -08:00
return "" , fmt . Errorf ( "only one of data or clone can be specified" )
2020-03-11 18:14:23 -07:00
}
2020-12-12 21:19:37 -08:00
2022-09-08 10:17:09 +05:30
if rule . Clone != ( kyvernov1 . CloneFrom { } ) && len ( rule . CloneList . Kinds ) != 0 {
return "" , fmt . Errorf ( "only one of clone or cloneList can be specified" )
}
2023-05-03 18:33:29 +08:00
apiVersion , kind , name , namespace := rule . ResourceSpec . GetAPIVersion ( ) , rule . ResourceSpec . GetKind ( ) , rule . ResourceSpec . GetName ( ) , rule . ResourceSpec . GetNamespace ( )
2020-03-11 18:14:23 -07:00
2022-09-08 10:17:09 +05:30
if len ( rule . CloneList . Kinds ) == 0 {
if name == "" {
return "name" , fmt . Errorf ( "name cannot be empty" )
}
if kind == "" {
return "kind" , fmt . Errorf ( "kind cannot be empty" )
}
2023-05-03 18:33:29 +08:00
if apiVersion == "" {
return "apiVersion" , fmt . Errorf ( "apiVersion cannot be empty" )
}
2022-10-11 23:35:07 +05:30
} else {
if name != "" {
2023-04-13 15:38:47 +08:00
return "name" , fmt . Errorf ( "with cloneList, generate.name. should not be specified" )
2022-10-11 23:35:07 +05:30
}
if kind != "" {
2023-04-13 15:38:47 +08:00
return "kind" , fmt . Errorf ( "with cloneList, generate.kind. should not be specified" )
2022-10-11 23:35:07 +05:30
}
2020-03-11 18:14:23 -07:00
}
2022-09-28 21:14:38 +05:30
if rule . CloneList . Selector != nil {
if wildcard . ContainsWildcard ( rule . CloneList . Selector . String ( ) ) {
return "selector" , fmt . Errorf ( "wildcard characters `*/?` not supported" )
}
}
2020-03-11 18:14:23 -07:00
2022-03-06 20:07:51 +01:00
if target := rule . GetData ( ) ; target != nil {
2022-05-17 08:19:03 +02:00
// TODO: is this required ?? as anchors can only be on pattern and not resource
2020-03-11 18:14:23 -07:00
// we can add this check by not sure if its needed here
2023-01-30 17:47:19 +05:30
if path , err := common . ValidatePattern ( target , "/" , nil ) ; err != nil {
2020-03-11 18:14:23 -07:00
return fmt . Sprintf ( "data.%s" , path ) , fmt . Errorf ( "anchors not supported on generate resources: %v" , err )
}
}
// Kyverno generate-controller create/update/deletes the resources specified in generate rule of policy
2022-12-19 16:26:07 +01:00
// kyverno uses SA 'kyverno' and has default ClusterRoles and ClusterRoleBindings
2021-02-22 12:08:26 -08:00
// instructions to modify the RBAC for kyverno are mentioned at https://github.com/kyverno/kyverno/blob/master/documentation/installation.md
2020-03-11 18:14:23 -07:00
// - operations required: create/update/delete/get
// If kind and namespace contain variables, then we cannot resolve then so we skip the processing
2022-09-26 13:23:00 +05:30
if len ( rule . CloneList . Kinds ) != 0 {
for _ , kind = range rule . CloneList . Kinds {
2023-06-22 22:14:06 +08:00
gvk , sub := parseCloneKind ( kind )
if err := g . canIGenerate ( ctx , gvk , namespace , sub ) ; err != nil {
2022-09-26 13:23:00 +05:30
return "" , err
}
}
} else {
2023-06-22 22:14:06 +08:00
k , sub := kubeutils . SplitSubresource ( kind )
if err := g . canIGenerate ( ctx , strings . Join ( [ ] string { apiVersion , k } , "/" ) , namespace , sub ) ; err != nil {
2022-09-26 13:23:00 +05:30
return "" , err
}
2020-03-11 18:14:23 -07:00
}
return "" , nil
}
2022-05-17 08:19:03 +02:00
// canIGenerate returns a error if kyverno cannot perform operations
2023-06-22 22:14:06 +08:00
func ( g * Generate ) canIGenerate ( ctx context . Context , gvk , namespace , subresource string ) error {
2020-03-11 18:14:23 -07:00
// Skip if there is variable defined
authCheck := g . authCheck
2023-06-22 22:14:06 +08:00
if ! regex . IsVariable ( gvk ) {
ok , err := authCheck . CanICreate ( ctx , gvk , namespace , subresource )
2020-03-11 18:14:23 -07:00
if err != nil {
return err
}
if ! ok {
2023-08-23 14:29:56 +02:00
return fmt . Errorf ( "%s does not have permissions to 'create' resource %s/%s/%s. Grant proper permissions to the background controller" , g . user , gvk , subresource , namespace )
2020-03-11 18:14:23 -07:00
}
2023-06-22 22:14:06 +08:00
ok , err = authCheck . CanIUpdate ( ctx , gvk , namespace , subresource )
2020-03-11 18:14:23 -07:00
if err != nil {
return err
}
if ! ok {
2023-08-23 14:29:56 +02:00
return fmt . Errorf ( "%s does not have permissions to 'update' resource %s/%s/%s. Grant proper permissions to the background controller" , g . user , gvk , subresource , namespace )
2020-03-11 18:14:23 -07:00
}
2023-06-22 22:14:06 +08:00
ok , err = authCheck . CanIGet ( ctx , gvk , namespace , subresource )
2020-03-11 18:14:23 -07:00
if err != nil {
return err
}
if ! ok {
2023-08-23 14:29:56 +02:00
return fmt . Errorf ( "%s does not have permissions to 'get' resource %s/%s/%s. Grant proper permissions to the background controller" , g . user , gvk , subresource , namespace )
2020-03-11 18:14:23 -07:00
}
2023-06-22 22:14:06 +08:00
ok , err = authCheck . CanIDelete ( ctx , gvk , namespace , subresource )
2020-03-11 18:14:23 -07:00
if err != nil {
return err
}
if ! ok {
2023-08-23 14:29:56 +02:00
return fmt . Errorf ( "%s does not have permissions to 'delete' resource %s/%s/%s. Grant proper permissions to the background controller" , g . user , gvk , subresource , namespace )
2020-03-11 18:14:23 -07:00
}
} else {
2023-04-13 15:38:47 +08:00
g . log . V ( 2 ) . Info ( "resource Kind uses variables, so cannot be resolved. Skipping Auth Checks." )
2020-03-11 18:14:23 -07:00
}
return nil
}
2023-06-22 22:14:06 +08:00
func parseCloneKind ( gvks string ) ( gvk , sub string ) {
gv , ks := kubeutils . GetKindFromGVK ( gvks )
k , sub := kubeutils . SplitSubresource ( ks )
if ! strings . Contains ( gv , "*" ) {
k = strings . Join ( [ ] string { gv , k } , "/" )
}
return k , sub
}