forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_globaldns.go
108 lines (98 loc) · 3.72 KB
/
validator_globaldns.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
100
101
102
103
104
105
106
107
108
package globaldns
import (
"fmt"
"net/http"
"strings"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
gaccess "github.com/rancher/rancher/pkg/api/customization/globalnamespaceaccess"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Wrapper struct {
GlobalDNSLister v3.GlobalDNSLister
GlobalDNSes v3.GlobalDNSInterface
PrtbLister v3.ProjectRoleTemplateBindingLister
MultiClusterAppLister v3.MultiClusterAppLister
Users v3.UserInterface
GrbLister v3.GlobalRoleBindingLister
GrLister v3.GlobalRoleLister
}
const (
creatorIDAnn = "field.cattle.io/creatorId"
)
func (w Wrapper) Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
if request.Method != http.MethodPut && request.Method != http.MethodPost {
return nil
}
var targetProjects []string
var accessType string
ma := gaccess.MemberAccess{
Users: w.Users,
GrLister: w.GrLister,
GrbLister: w.GrbLister,
}
callerID := request.Request.Header.Get(gaccess.ImpersonateUserHeader)
if request.Method == http.MethodPost {
// create request, caller is owner/creator
accessType = gaccess.OwnerAccess
// Request is POST, hence global DNS is being created.
// if multiclusterapp ID is provided check access to its projects
mcappID := convert.ToString(data[client.GlobalDNSFieldMultiClusterAppID])
if mcappID != "" {
split := strings.SplitN(mcappID, ":", 2)
if len(split) != 2 {
return fmt.Errorf("incorrect multiclusterapp id %v provided for global dns %v", mcappID, request.ID)
}
mcapp, err := w.MultiClusterAppLister.Get(split[0], split[1])
if err != nil {
return err
}
for _, t := range mcapp.Spec.Targets {
targetProjects = append(targetProjects, t.ProjectName)
}
} else {
// if not, check access to all projects provided in the projects list
targetProjects = convert.ToStringSlice(data[client.GlobalDNSFieldProjectIDs])
}
return ma.CheckCallerAccessToTargets(request, targetProjects, client.ProjectType, &client.Project{})
}
// edit request, check access type caller has
split := strings.SplitN(request.ID, ":", 2)
if len(split) != 2 {
return fmt.Errorf("incorrect global DNS ID %v", request.ID)
}
gDNS, err := w.GlobalDNSes.GetNamespaced(split[0], split[1], v1.GetOptions{})
if err != nil {
return err
}
metaAccessor, err := meta.Accessor(gDNS)
if err != nil {
return err
}
creatorID, ok := metaAccessor.GetAnnotations()[creatorIDAnn]
if !ok {
return fmt.Errorf("GlobalDNS %v has no creatorId annotation", metaAccessor.GetName())
}
accessType, err = ma.GetAccessTypeOfCaller(callerID, creatorID, gDNS.Name, gDNS.Spec.Members)
if err != nil {
return err
}
if accessType != gaccess.OwnerAccess {
return fmt.Errorf("invalid access type %v for globaldns member", accessType)
}
// only members list, FQDN and multiclusterappID can be edited through PUT, for updating projects, we need to use actions only
// that's why projects and multiclusterappID field have been made non updatable in rancher/types
if err := gaccess.CheckAccessToUpdateMembers(gDNS.Spec.Members, data, accessType == gaccess.OwnerAccess); err != nil {
return err
}
originalMultiClusterApp := gDNS.Spec.MultiClusterAppName
newMultiClusterApp := convert.ToString(data[client.GlobalDNSFieldMultiClusterAppID])
if newMultiClusterApp != "" && originalMultiClusterApp != newMultiClusterApp {
// check access to new multiclusterapp
return ma.CheckCallerAccessToTargets(request, []string{newMultiClusterApp}, client.MultiClusterAppType, &client.MultiClusterApp{})
}
return nil
}