forked from IBM/portieris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.go
99 lines (85 loc) · 3.58 KB
/
policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2018, 2021 Portieris Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package policy
import (
"context"
"fmt"
policyClientSet "github.com/SimonBaeumer/portieris/pkg/apis/portieris.cloud.ibm.com/client/clientset/versioned"
policyV1 "github.com/SimonBaeumer/portieris/pkg/apis/portieris.cloud.ibm.com/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Interface defines the interface needed to work out which policy should be enforced
type Interface interface {
GetPolicyToEnforce(namespace, image string) (*policyV1.Policy, error)
}
// Client is responsible for working out which policy should be enforced
type Client struct {
// policyClientSet is a clientset for the policy CRDs
policyClientSet policyClientSet.Interface
}
// NewClient creates a new policy client using the Security Enforcement client set it is passed
func NewClient(policyClientSet policyClientSet.Interface) *Client {
return &Client{
policyClientSet: policyClientSet,
}
}
// getImagePolicyList retrieves the list of image policies in the specified namespace
func (c *Client) getImagePolicyList(namespace string) (*policyV1.ImagePolicyList, error) {
policies, err := c.policyClientSet.PortierisV1().ImagePolicies(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
return policies, nil
}
// getClusterPolicySpec retrieves the lost of clusterwide image policies
func (c *Client) getClusterImagePolicyList() (*policyV1.ClusterImagePolicyList, error) {
policies, err := c.policyClientSet.PortierisV1().ClusterImagePolicies().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
return policies, nil
}
// GetPolicyToEnforce retrieves the policy that should be enforced for the specified image in the given namespace
func (c *Client) GetPolicyToEnforce(namespace, image string) (*policyV1.Policy, error) {
policyList, err := c.getImagePolicyList(namespace)
if err != nil {
return nil, err
}
if len((*policyList).Items) == 0 {
// We don't have any image policies in the current namespace, get the list of cluster policies
clusterPolicyList, err := c.getClusterImagePolicyList()
if err != nil {
return nil, err
}
if len((*clusterPolicyList).Items) == 0 {
// We also don't have any cluster image policies, deny the request
return nil, fmt.Errorf("Deny %q, no image policies or cluster polices", image)
}
// See if there is a match for the image
clusterPolicy := clusterPolicyList.FindClusterImagePolicy(image)
if clusterPolicy == nil {
// We also don't have any cluster image policies, deny the request
return nil, fmt.Errorf("Deny %q, no matching repositories in ClusterImagePolicy and no ImagePolicies in the %q namespace", image, namespace)
}
return clusterPolicy, nil
}
// For this image, see if there is an ImagePolicy repository that matches.
// Get the policy if it does
policy := policyList.FindImagePolicy(image)
if policy == nil {
// We also don't have any cluster image policies, deny the request
return nil, fmt.Errorf("Deny %q, no matching repositories in the ImagePolicies", image)
}
return policy, nil
}