Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Separate RBAC declarations by component for clearer access requirements #1606

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 58 additions & 46 deletions internal/resources/otelcollector/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,46 @@ func MakeMetricGatewayRBAC(name types.NamespacedName) Rbac {
}

func makeTraceGatewayClusterRole(name types.NamespacedName) *rbacv1.ClusterRole {
k8sAttributeRules := []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"namespaces", "pods"},
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{"apps"},
Resources: []string{"replicasets"},
Verbs: []string{"get", "list", "watch"},
},
}
clusterRoleRules := append([]rbacv1.PolicyRule{}, k8sAttributeRules...)

return &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: name.Name,
Namespace: name.Namespace,
Labels: labels.MakeDefaultLabel(name.Name),
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"namespaces", "pods"},
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{"apps"},
Resources: []string{"replicasets"},
Verbs: []string{"get", "list", "watch"},
},
},
Rules: clusterRoleRules,
}
}

func makeMetricAgentClusterRole(name types.NamespacedName) *rbacv1.ClusterRole {
clusterRole := &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: name.Name,
Namespace: name.Namespace,
Labels: labels.MakeDefaultLabel(name.Name),
kubeletStatsRules := []rbacv1.PolicyRule{{
APIGroups: []string{""},
Resources: []string{"nodes", "nodes/stats", "nodes/proxy"},
Verbs: []string{"get", "list", "watch"},
}}

prometheusRules := []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"nodes", "nodes/metrics", "services", "endpoints", "pods"},
Verbs: []string{"get", "list", "watch"},
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"nodes", "nodes/metrics", "nodes/stats", "nodes/proxy", "services", "endpoints", "pods"},
Verbs: []string{"get", "list", "watch"},
},
{
NonResourceURLs: []string{"/metrics", "/metrics/cadvisor"},
Verbs: []string{"get"},
},
{
NonResourceURLs: []string{"/metrics", "/metrics/cadvisor"},
Verbs: []string{"get"},
},
}

Expand All @@ -106,29 +108,31 @@ func makeMetricAgentClusterRole(name types.NamespacedName) *rbacv1.ClusterRole {
Verbs: []string{"get", "list", "watch"},
}}

clusterRole.Rules = append(clusterRole.Rules, k8sClusterRules...)

return clusterRole
}
clusterRoleRules := append([]rbacv1.PolicyRule{}, kubeletStatsRules...)
clusterRoleRules = append(clusterRoleRules, prometheusRules...)
clusterRoleRules = append(clusterRoleRules, k8sClusterRules...)

func makeMetricGatewayClusterRole(name types.NamespacedName) *rbacv1.ClusterRole {
clusterRole := rbacv1.ClusterRole{
return &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: name.Name,
Namespace: name.Namespace,
Labels: labels.MakeDefaultLabel(name.Name),
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"namespaces", "pods"},
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{"apps"},
Resources: []string{"replicasets"},
Verbs: []string{"get", "list", "watch"},
},
Rules: clusterRoleRules,
}
}

func makeMetricGatewayClusterRole(name types.NamespacedName) *rbacv1.ClusterRole {
k8sAttributeRules := []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"namespaces", "pods"},
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{"apps"},
Resources: []string{"replicasets"},
Verbs: []string{"get", "list", "watch"},
},
}

Expand All @@ -150,9 +154,17 @@ func makeMetricGatewayClusterRole(name types.NamespacedName) *rbacv1.ClusterRole
Verbs: []string{"get", "list", "watch"},
}}

clusterRole.Rules = append(clusterRole.Rules, kymaStatsRules...)
clusterRoleRules := append([]rbacv1.PolicyRule{}, k8sAttributeRules...)
clusterRoleRules = append(clusterRoleRules, kymaStatsRules...)

return &clusterRole
return &rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{
Name: name.Name,
Namespace: name.Namespace,
Labels: labels.MakeDefaultLabel(name.Name),
},
Rules: clusterRoleRules,
}
}

func makeClusterRoleBinding(name types.NamespacedName) *rbacv1.ClusterRoleBinding {
Expand Down
7 changes: 6 additions & 1 deletion internal/resources/otelcollector/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ func TestMakeMetricAgentRBAC(t *testing.T) {
expectedRules := []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"nodes", "nodes/metrics", "nodes/stats", "nodes/proxy", "services", "endpoints", "pods"},
Resources: []string{"nodes", "nodes/stats", "nodes/proxy"},
Verbs: []string{"get", "list", "watch"},
},
{
APIGroups: []string{""},
Resources: []string{"nodes", "nodes/metrics", "services", "endpoints", "pods"},
Verbs: []string{"get", "list", "watch"},
},
{
Expand Down
Loading