Skip to content

Commit

Permalink
Update RBACs for tier support in Calico OSS (#3474)
Browse files Browse the repository at this point in the history
  • Loading branch information
mazdakn authored Aug 26, 2024
1 parent b76262f commit fa15ace
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 63 deletions.
52 changes: 52 additions & 0 deletions pkg/crds/calico/crd.projectcalico.org_bgpfilters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ spec:
type: string
matchOperator:
type: string
prefixLength:
properties:
max:
format: int32
maximum: 32
minimum: 0
type: integer
min:
format: int32
maximum: 32
minimum: 0
type: integer
type: object
source:
type: string
required:
Expand All @@ -70,6 +83,19 @@ spec:
type: string
matchOperator:
type: string
prefixLength:
properties:
max:
format: int32
maximum: 128
minimum: 0
type: integer
min:
format: int32
maximum: 128
minimum: 0
type: integer
type: object
source:
type: string
required:
Expand All @@ -91,6 +117,19 @@ spec:
type: string
matchOperator:
type: string
prefixLength:
properties:
max:
format: int32
maximum: 32
minimum: 0
type: integer
min:
format: int32
maximum: 32
minimum: 0
type: integer
type: object
source:
type: string
required:
Expand All @@ -112,6 +151,19 @@ spec:
type: string
matchOperator:
type: string
prefixLength:
properties:
max:
format: int32
maximum: 128
minimum: 0
type: integer
min:
format: int32
maximum: 128
minimum: 0
type: integer
type: object
source:
type: string
required:
Expand Down
16 changes: 12 additions & 4 deletions pkg/crds/calico/crd.projectcalico.org_globalnetworkpolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -794,10 +794,10 @@ spec:
order:
description: Order is an optional field that specifies the order in
which the policy is applied. Policies with higher "order" are applied
after those with lower order. If the order is omitted, it may be
considered to be "infinite" - i.e. the policy will be applied last. Policies
with identical order will be applied in alphanumerical order based
on the Policy "Name".
after those with lower order within the same tier. If the order
is omitted, it may be considered to be "infinite" - i.e. the policy
will be applied last. Policies with identical order will be applied
in alphanumerical order based on the Policy "Name" within the tier.
type: number
performanceHints:
description: "PerformanceHints contains a list of hints to Calico's
Expand Down Expand Up @@ -839,6 +839,14 @@ spec:
description: ServiceAccountSelector is an optional field for an expression
used to select a pod based on service accounts.
type: string
tier:
description: The name of the tier that this policy belongs to. If
this is omitted, the default tier (name is "default") is assumed. The
specified tier must exist in order to create security policies within
the tier, the "default" tier is created automatically if it does
not exist, this means for deployments requiring only a single Tier,
the tier name may be omitted on all policy management requests.
type: string
types:
description: "Types indicates whether this policy applies to ingress,
or to egress, or to both. When not explicitly specified (and so
Expand Down
16 changes: 12 additions & 4 deletions pkg/crds/calico/crd.projectcalico.org_networkpolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -779,10 +779,10 @@ spec:
order:
description: Order is an optional field that specifies the order in
which the policy is applied. Policies with higher "order" are applied
after those with lower order. If the order is omitted, it may be
considered to be "infinite" - i.e. the policy will be applied last. Policies
with identical order will be applied in alphanumerical order based
on the Policy "Name".
after those with lower order within the same tier. If the order
is omitted, it may be considered to be "infinite" - i.e. the policy
will be applied last. Policies with identical order will be applied
in alphanumerical order based on the Policy "Name" within the tier.
type: number
performanceHints:
description: "PerformanceHints contains a list of hints to Calico's
Expand Down Expand Up @@ -820,6 +820,14 @@ spec:
description: ServiceAccountSelector is an optional field for an expression
used to select a pod based on service accounts.
type: string
tier:
description: The name of the tier that this policy belongs to. If
this is omitted, the default tier (name is "default") is assumed. The
specified tier must exist in order to create security policies within
the tier, the "default" tier is created automatically if it does
not exist, this means for deployments requiring only a single Tier,
the tier name may be omitted on all policy management requests.
type: string
types:
description: "Types indicates whether this policy applies to ingress,
or to egress, or to both. When not explicitly specified (and so
Expand Down
54 changes: 54 additions & 0 deletions pkg/crds/calico/crd.projectcalico.org_tiers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: (devel)
creationTimestamp: null
name: tiers.crd.projectcalico.org
spec:
group: crd.projectcalico.org
names:
kind: Tier
listKind: TierList
plural: tiers
singular: tier
scope: Cluster
versions:
- name: v1
schema:
openAPIV3Schema:
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: TierSpec contains the specification for a security policy
tier resource.
properties:
order:
description: Order is an optional field that specifies the order in
which the tier is applied. Tiers with higher "order" are applied
after those with lower order. If the order is omitted, it may be
considered to be "infinite" - i.e. the tier will be applied last. Tiers
with identical order will be applied in alphanumerical order based
on the Tier "Name".
type: number
type: object
type: object
served: true
storage: true
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
81 changes: 70 additions & 11 deletions pkg/render/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ func (c *apiServerComponent) Objects() ([]client.Object, []client.Object) {
globalObjects := []client.Object{
c.calicoCustomResourcesClusterRole(),
c.calicoCustomResourcesClusterRoleBinding(),
c.tierGetterClusterRole(),
c.kubeControllerMgrTierGetterClusterRoleBinding(),
c.calicoPolicyPassthruClusterRole(),
c.calicoPolicyPassthruClusterRolebinding(),
}

// These objects are global, and have different names based on Calico or Calico Enterprise.
Expand Down Expand Up @@ -219,8 +223,6 @@ func (c *apiServerComponent) Objects() ([]client.Object, []client.Object) {
CreateNamespace(rmeta.APIServerNamespace(operatorv1.TigeraSecureEnterprise), c.cfg.Installation.KubernetesProvider, PSSPrivileged),
c.tigeraApiServerClusterRole(),
c.tigeraApiServerClusterRoleBinding(),
c.tierGetterClusterRole(),
c.kubeControllerMgrTierGetterClusterRoleBinding(),
c.uisettingsgroupGetterClusterRole(),
c.kubeControllerMgrUisettingsgroupGetterClusterRoleBinding(),
c.tieredPolicyPassthruClusterRole(),
Expand Down Expand Up @@ -565,6 +567,7 @@ func (c *apiServerComponent) calicoCustomResourcesClusterRole() *rbacv1.ClusterR
"ipamblocks",
"blockaffinities",
"ipamconfigs",
"tiers",
},
Verbs: []string{
"get",
Expand Down Expand Up @@ -1229,7 +1232,6 @@ func (c *apiServerComponent) tigeraApiServerClusterRole() *rbacv1.ClusterRole {
"stagedkubernetesnetworkpolicies",
"stagednetworkpolicies",
"stagedglobalnetworkpolicies",
"tiers",
"licensekeys",
"alertexceptions",
"globalalerts",
Expand Down Expand Up @@ -1309,13 +1311,11 @@ func (c *apiServerComponent) tigeraApiServerClusterRoleBinding() *rbacv1.Cluster
}

// tierGetterClusterRole creates a clusterrole that gives permissions to get tiers.
//
// Calico Enterprise only
func (c *apiServerComponent) tierGetterClusterRole() *rbacv1.ClusterRole {
return &rbacv1.ClusterRole{
TypeMeta: metav1.TypeMeta{Kind: "ClusterRole", APIVersion: "rbac.authorization.k8s.io/v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "tigera-tier-getter",
Name: "calico-tier-getter",
},
Rules: []rbacv1.PolicyRule{
{
Expand All @@ -1332,17 +1332,15 @@ func (c *apiServerComponent) tierGetterClusterRole() *rbacv1.ClusterRole {
// kubeControllerMgrTierGetterClusterRoleBinding creates a rolebinding that allows the k8s kube-controller manager to
// get tiers. In k8s 1.15+, cascading resource deletions (for instance pods for a replicaset) failed
// due to k8s kube-controller not having permissions to get tiers.
//
// Calico Enterprise only
func (c *apiServerComponent) kubeControllerMgrTierGetterClusterRoleBinding() *rbacv1.ClusterRoleBinding {
return &rbacv1.ClusterRoleBinding{
TypeMeta: metav1.TypeMeta{Kind: "ClusterRoleBinding", APIVersion: "rbac.authorization.k8s.io/v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "tigera-tier-getter",
Name: "calico-tier-getter",
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: "tigera-tier-getter",
Name: "calico-tier-getter",
APIGroup: "rbac.authorization.k8s.io",
},
Subjects: []rbacv1.Subject{
Expand Down Expand Up @@ -1765,6 +1763,49 @@ func (c *apiServerComponent) tigeraNetworkAdminClusterRole() *rbacv1.ClusterRole
}
}

// calicoPolicyPassthruClusterRole creates a clusterrole that is used to control the RBAC
// mechanism for Calico tiered policy.
func (c *apiServerComponent) calicoPolicyPassthruClusterRole() *rbacv1.ClusterRole {
return &rbacv1.ClusterRole{
TypeMeta: metav1.TypeMeta{Kind: "ClusterRole", APIVersion: "rbac.authorization.k8s.io/v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "calico-tiered-policy-passthrough",
},
// If tiered policy is enabled we allow all authenticated users to access the main tier resource, instead
// restricting access using the tier.xxx resource type. Kubernetes NetworkPolicy and the
// StagedKubernetesNetworkPolicy are handled using normal (non-tiered) RBAC.
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{"projectcalico.org"},
Resources: []string{"networkpolicies", "globalnetworkpolicies"},
Verbs: []string{"*"},
},
},
}
}

// calicoPolicyPassthruClusterRolebinding creates a clusterrolebinding that applies calicoPolicyPassthruClusterRole to all users.
func (c *apiServerComponent) calicoPolicyPassthruClusterRolebinding() *rbacv1.ClusterRoleBinding {
return &rbacv1.ClusterRoleBinding{
TypeMeta: metav1.TypeMeta{Kind: "ClusterRoleBinding", APIVersion: "rbac.authorization.k8s.io/v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "calico-tiered-policy-passthrough",
},
Subjects: []rbacv1.Subject{
{
Kind: "Group",
Name: "system:authenticated",
APIGroup: "rbac.authorization.k8s.io",
},
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: "calico-tiered-policy-passthrough",
APIGroup: "rbac.authorization.k8s.io",
},
}
}

// tieredPolicyPassthruClusterRole creates a clusterrole that is used to control the RBAC
// mechanism for Tigera Secure tiered policy.
//
Expand All @@ -1781,7 +1822,7 @@ func (c *apiServerComponent) tieredPolicyPassthruClusterRole() *rbacv1.ClusterRo
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{"projectcalico.org"},
Resources: []string{"networkpolicies", "globalnetworkpolicies", "stagednetworkpolicies", "stagedglobalnetworkpolicies"},
Resources: []string{"stagednetworkpolicies", "stagedglobalnetworkpolicies"},
Verbs: []string{"*"},
},
},
Expand Down Expand Up @@ -1940,5 +1981,23 @@ func (c *apiServerComponent) getDeprecatedResources() []client.Object {
},
})

// The following resources were not present in Calico OSS, so there is no need to clean up in OSS.
if c.cfg.Installation.Variant == operatorv1.TigeraSecureEnterprise {
// Renamed ClusterRoleBinging tigera-tier-getter to calico-tier-getter since Tier is available in OSS
renamedRscList = append(renamedRscList, &rbacv1.ClusterRoleBinding{
TypeMeta: metav1.TypeMeta{Kind: "ClusterRoleBinding", APIVersion: "rbac.authorization.k8s.io/v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "tigera-tier-getter",
},
})
// Renamed ClusterRole tigera-tier-getter to calico-tier-getter since Tier is available in OSS
renamedRscList = append(renamedRscList, &rbacv1.ClusterRole{
TypeMeta: metav1.TypeMeta{Kind: "ClusterRole", APIVersion: "rbac.authorization.k8s.io/v1"},
ObjectMeta: metav1.ObjectMeta{
Name: "tigera-tier-getter",
},
})
}

return renamedRscList
}
Loading

0 comments on commit fa15ace

Please sign in to comment.