/
rbac.go
153 lines (129 loc) · 4.85 KB
/
rbac.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package user
import (
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"go.uber.org/multierr"
rbacv1 "k8s.io/api/rbac/v1"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/endpoints/filters"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/klog/v2"
"k8s.io/utils/strings"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/appuio/control-api/apiserver/secretstorage"
)
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterroles;clusterrolebindings,verbs=get;list;watch;create;delete;patch;update;edit
// +kubebuilder:rbac:groups=rbac.appuio.io;user.appuio.io,resources=invitations,verbs=get;edit;update;patch;delete
// rbacCreatorIsOwner is a wrapper around the Invitation storage that creates a ClusterRole and ClusterRoleBinding
// to make the creator of the Invitation the owner of the Invitation.
type rbacCreatorIsOwner struct {
secretstorage.ScopedStandardStorage
client client.Client
}
// Create passes the object to the wrapped storage and creates a ClusterRole and ClusterRoleBinding for the creator of the object using the returned object's name.
func (c *rbacCreatorIsOwner) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, opts *metav1.CreateOptions) (runtime.Object, error) {
attr, err := filters.GetAuthorizerAttributes(ctx)
if err != nil {
return nil, err
}
user := attr.GetUser()
createdObj, err := c.ScopedStandardStorage.Create(ctx, obj, createValidation, opts)
if err != nil {
return createdObj, err
}
ac := apimeta.NewAccessor()
objName, err := ac.Name(createdObj)
if err != nil {
return createdObj, fmt.Errorf("could not get name of created object: %w", err)
}
rolename := roleName(objName)
role := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: rolename,
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{"rbac.appuio.io", "user.appuio.io"},
Resources: []string{"invitations"},
Verbs: []string{"get", "edit", "update", "patch", "delete"},
ResourceNames: []string{objName},
},
},
}
rolebinding := &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: rolename,
},
Subjects: []rbacv1.Subject{
{
Kind: "User",
APIGroup: "rbac.authorization.k8s.io",
Name: user.GetName(),
},
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
APIGroup: "rbac.authorization.k8s.io",
Name: rolename,
},
}
rollback := func() error {
_, _, err := c.ScopedStandardStorage.Delete(ctx, objName, nil, &metav1.DeleteOptions{DryRun: opts.DryRun})
return err
}
err = c.client.Create(ctx, role, &client.CreateOptions{DryRun: opts.DryRun})
if err != nil {
rollbackErr := rollback()
return createdObj, multierr.Append(err, rollbackErr)
}
err = c.client.Create(ctx, rolebinding, &client.CreateOptions{DryRun: opts.DryRun})
if err != nil {
rollbackErr := rollback()
roleRollbackErr := c.client.Delete(ctx, role, &client.DeleteOptions{DryRun: opts.DryRun})
return createdObj, multierr.Combine(err, rollbackErr, roleRollbackErr)
}
return createdObj, nil
}
// Delete passes the object to the wrapped storage and deletes the ClusterRole and ClusterRoleBinding associated with the object.
func (c *rbacCreatorIsOwner) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, opts *metav1.DeleteOptions) (runtime.Object, bool, error) {
deletedObj, im, err := c.ScopedStandardStorage.Delete(ctx, name, deleteValidation, opts)
if err != nil {
return deletedObj, im, err
}
rolename := roleName(name)
err1 := c.client.Delete(ctx, &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: rolename,
},
}, &client.DeleteOptions{DryRun: opts.DryRun})
err2 := c.client.Delete(ctx, &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: rolename,
},
}, &client.DeleteOptions{DryRun: opts.DryRun})
if err := multierr.Combine(err1, err2); err != nil {
klog.FromContext(ctx).Error(err, "failed to clean up RBAC resources")
}
return deletedObj, im, nil
}
func (s *rbacCreatorIsOwner) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
return nil, fmt.Errorf("not implemented")
}
func roleName(objName string) string {
prefix := "invitations-"
suffix := "-owner"
if len(prefix)+len(suffix)+len(objName) <= 63 {
return fmt.Sprintf("%s%s%s", prefix, objName, suffix)
}
h := sha1.New()
h.Write([]byte(objName))
hsh := strings.ShortenString(hex.EncodeToString(h.Sum(nil)), 7)
maxLength := 63 - len(prefix) - len(suffix) - len(hsh) - 1
maxSafe := strings.ShortenString(objName, maxLength)
return fmt.Sprintf("%s%s-%s%s", prefix, maxSafe, hsh, suffix)
}