2021-04-29 22:39:44 +05:30
|
|
|
package store
|
|
|
|
|
2022-05-11 20:51:13 +05:30
|
|
|
import (
|
|
|
|
"github.com/kyverno/kyverno/pkg/registryclient"
|
|
|
|
)
|
2022-03-16 09:56:47 +05:30
|
|
|
|
2023-01-30 16:30:47 +01:00
|
|
|
type Context struct {
|
|
|
|
Policies []Policy `json:"policies"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Policy struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Rules []Rule `json:"rules"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Rule struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Values map[string]interface{} `json:"values"`
|
|
|
|
ForEachValues map[string][]interface{} `json:"foreachValues"`
|
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
type Store struct {
|
2023-06-26 22:46:30 +02:00
|
|
|
local bool
|
2022-12-07 16:08:37 +01:00
|
|
|
registryClient registryclient.Client
|
2023-01-30 16:30:47 +01:00
|
|
|
allowApiCalls bool
|
|
|
|
policies []Policy
|
|
|
|
foreachElement int
|
2023-12-19 15:45:53 +01:00
|
|
|
}
|
2021-04-29 22:39:44 +05:30
|
|
|
|
2023-06-26 22:46:30 +02:00
|
|
|
// SetLocal sets local (clusterless) execution for the CLI
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) SetLocal(m bool) {
|
|
|
|
s.local = m
|
2021-04-29 22:39:44 +05:30
|
|
|
}
|
|
|
|
|
2023-06-26 22:46:30 +02:00
|
|
|
// IsLocal returns 'true' if the CLI is in local (clusterless) execution
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) IsLocal() bool {
|
|
|
|
return s.local
|
2021-04-29 22:39:44 +05:30
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) SetForEachElement(element int) {
|
|
|
|
s.foreachElement = element
|
2022-04-25 22:06:31 +05:30
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) GetForeachElement() int {
|
|
|
|
return s.foreachElement
|
2022-04-25 22:06:31 +05:30
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) SetRegistryAccess(access bool) {
|
2022-03-16 09:56:47 +05:30
|
|
|
if access {
|
2023-12-19 15:45:53 +01:00
|
|
|
s.registryClient = registryclient.NewOrDie(registryclient.WithLocalKeychain())
|
2022-03-16 09:56:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) GetRegistryAccess() bool {
|
|
|
|
return s.registryClient != nil
|
2022-12-07 16:08:37 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) GetRegistryClient() registryclient.Client {
|
|
|
|
return s.registryClient
|
2022-03-16 09:56:47 +05:30
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) SetPolicies(p ...Policy) {
|
|
|
|
s.policies = p
|
2021-04-29 22:39:44 +05:30
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) HasPolicies() bool {
|
|
|
|
return len(s.policies) != 0
|
2021-04-29 22:39:44 +05:30
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) GetPolicy(policyName string) *Policy {
|
|
|
|
for _, policy := range s.policies {
|
2021-04-29 22:39:44 +05:30
|
|
|
if policy.Name == policyName {
|
|
|
|
return &policy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) GetPolicyRule(policyName string, ruleName string) *Rule {
|
|
|
|
for _, policy := range s.policies {
|
2021-04-29 22:39:44 +05:30
|
|
|
if policy.Name == policyName {
|
|
|
|
for _, rule := range policy.Rules {
|
2023-09-12 15:38:57 +02:00
|
|
|
switch ruleName {
|
|
|
|
case rule.Name, "autogen-" + rule.Name, "autogen-cronjob-" + rule.Name:
|
2021-04-29 22:39:44 +05:30
|
|
|
return &rule
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) AllowApiCall(allow bool) {
|
|
|
|
s.allowApiCalls = allow
|
2022-10-19 22:09:15 +05:30
|
|
|
}
|
|
|
|
|
2023-12-19 15:45:53 +01:00
|
|
|
func (s *Store) IsApiCallAllowed() bool {
|
|
|
|
return s.allowApiCalls
|
2022-10-19 22:09:15 +05:30
|
|
|
}
|