TExcelTAD
Home › Platform Engineering: Internal Developer Platforms › RBAC & Network Isolation for Tenants

RBAC & Network Isolation for Tenants

The tenant kit bound teams to the built-in admin and edit roles and put a default-deny NetworkPolicy in every namespace. This lesson looks at both more closely: what those roles really allow, how to give automation less, and how to reason about which Pods can reach which.

What the built-in roles allow

Kubernetes ships user-facing ClusterRoles meant to be bound per namespace with a RoleBinding:

ClusterRole Allows in the namespace Notable
view read most objects not Secrets (reading them would be an escalation)
edit create/update/delete workloads, Services, ConfigMaps, Secrets can't change Roles or RoleBindings; can run Pods as any service account in the namespace
admin everything edit can, plus Roles and RoleBindings in the namespace can't change the namespace itself or its ResourceQuota
cluster-admin everything everywhere never for tenants

Two consequences platform teams often miss: anyone with edit can read every Secret in the namespace (directly, or by starting a Pod that mounts it), and can act as any service account there. So a namespace is the real security boundary, and secrets that a team's developers shouldn't see don't belong in the team's namespace.

Least privilege for automation

A CI job or an Argo CD instance that deploys into a team's namespace doesn't need edit. A namespaced Role listing exactly the kinds the golden path's chart creates is enough. The session builds the service account and binding with kubectl create … --dry-run=client (it works without a cluster for these kinds), writes the Role by hand, and validates all three:

bash · try it yourself
kubectl create serviceaccount ci-deployer -n team-growth --dry-run=client -o yaml > sa.yaml
cat > role.yaml <<'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: {name: deployer, namespace: team-growth}
rules:
  - apiGroups: [apps]
    resources: [deployments]
    verbs: [get, list, watch, create, update, patch]
  - apiGroups: [""]
    resources: [services, configmaps]
    verbs: [get, list, watch, create, update, patch]
  - apiGroups: [gateway.networking.k8s.io]
    resources: [httproutes]
    verbs: [get, list, watch, create, update, patch]
EOF
kubectl create rolebinding deployer -n team-growth --role=deployer \
  --serviceaccount=team-growth:ci-deployer --dry-run=client -o yaml > rolebinding.yaml
cat rolebinding.yaml
for f in sa.yaml role.yaml rolebinding.yaml; do echo "---"; cat "$f"; done | kubeconform -n 1 -strict -summary
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: deployer
  namespace: team-growth
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: deployer
subjects:
- kind: ServiceAccount
  name: ci-deployer
  namespace: team-growth
Summary: 3 resources found parsing stdin - Valid: 3, Invalid: 0, Errors: 0, Skipped: 0

No delete (a deploy never needs it, except for pruning, which Argo CD can do with its own permissions), no Secrets (they come from External Secrets, see Secrets & Policy), no RBAC objects. (kubectl create role would also generate this, but it asks the API server for the resource list, so it needs a cluster.)

On a live cluster you check the result by impersonation (sample session):

bash
$ kubectl auth can-i create deployments -n team-growth --as=system:serviceaccount:team-growth:ci-deployer
yes
$ kubectl auth can-i get secrets -n team-growth --as=system:serviceaccount:team-growth:ci-deployer
no
$ kubectl auth can-i create deployments -n team-links --as=system:serviceaccount:team-growth:ci-deployer
no

How RBAC decides, modelled

RBAC is purely additive: a request is allowed if any binding grants it, and there is no deny rule. A model of the evaluation, with the built-in roles reduced to the rules that matter here:

python · try it yourself
CLUSTER_ROLES = {
    "view":  [({"", "apps"}, {"pods", "services", "deployments", "configmaps"}, {"get", "list", "watch"})],
    "edit":  [({"", "apps"}, {"pods", "services", "deployments", "configmaps", "secrets"},
               {"get", "list", "watch", "create", "update", "patch", "delete"})],
}
ROLES = {("team-growth", "deployer"): [({"apps"}, {"deployments"}, {"get", "list", "watch", "create", "update", "patch"}),
                                       ({""}, {"services", "configmaps"}, {"get", "list", "watch", "create", "update", "patch"})]}
BINDINGS = [   # namespace, role kind, role name, subject
    ("team-growth", "ClusterRole", "edit", "group:growth-devs"),
    ("team-growth", "ClusterRole", "view", "group:links-devs"),
    ("team-links",  "ClusterRole", "edit", "group:links-devs"),
    ("team-growth", "Role", "deployer", "sa:team-growth:ci-deployer"),
]

def can(subject, verb, group, resource, ns):
    for b_ns, kind, name, subj in BINDINGS:
        if subj != subject or b_ns != ns:
            continue
        rules = CLUSTER_ROLES[name] if kind == "ClusterRole" else ROLES[(b_ns, name)]
        for groups, resources, verbs in rules:
            if group in groups and resource in resources and verb in verbs:
                return f"yes (via {kind}/{name})"
    return "no"

checks = [
    ("group:growth-devs", "get", "", "secrets", "team-growth"),
    ("group:links-devs", "get", "", "secrets", "team-growth"),
    ("group:links-devs", "list", "apps", "deployments", "team-growth"),
    ("group:links-devs", "delete", "apps", "deployments", "team-growth"),
    ("sa:team-growth:ci-deployer", "patch", "apps", "deployments", "team-growth"),
    ("sa:team-growth:ci-deployer", "delete", "apps", "deployments", "team-growth"),
    ("sa:team-growth:ci-deployer", "get", "", "secrets", "team-growth"),
]
for subject, verb, group, resource, ns in checks:
    print(f"{subject:28} {verb:6} {resource:11} in {ns:11}: {can(subject, verb, group, resource, ns)}")
group:growth-devs            get    secrets     in team-growth: yes (via ClusterRole/edit)
group:links-devs             get    secrets     in team-growth: no
group:links-devs             list   deployments in team-growth: yes (via ClusterRole/view)
group:links-devs             delete deployments in team-growth: no
sa:team-growth:ci-deployer   patch  deployments in team-growth: yes (via Role/deployer)
sa:team-growth:ci-deployer   delete deployments in team-growth: no
sa:team-growth:ci-deployer   get    secrets     in team-growth: no

Granting links-devs read access to growth's namespace (useful for debugging a shared dependency) doesn't leak growth's Secrets, because view excludes them. Granting edit would.

Which Pods can reach which, modelled

NetworkPolicies are also additive, per direction: once any policy selects a Pod for ingress, only traffic that some policy allows gets in (the same for egress). The kit's default-deny selects every Pod in both directions, so everything is closed until allow-baseline and app policies open it. A connection needs both the sender's egress and the receiver's ingress to allow it:

python · try it yourself
PODS = {   # pod: (namespace, labels)
    "linkly":   ("team-links", {"app": "linkly"}),
    "postgres": ("team-links", {"app": "postgres"}),
    "analytics": ("team-growth", {"app": "analytics"}),
    "gateway":  ("gateway-system", {"app": "gateway"}),
    "prometheus": ("monitoring", {"app": "prometheus"}),
}
# per namespace: the kit's policies (+ one app policy); peers are ("same-ns",) or ("ns", name) or ("pod", ns, label)
INGRESS = {
    "team-links":  [("same-ns",), ("ns", "gateway-system"), ("ns", "monitoring")],
    "team-growth": [("same-ns",), ("ns", "gateway-system"), ("ns", "monitoring")],
}
EGRESS = {
    "team-links":  [("same-ns",)],
    "team-growth": [("same-ns",), ("pod", "team-links", "linkly")],   # app policy: analytics may call linkly
}

def peer_ok(rules, own_ns, other):
    o_ns, o_labels = PODS[other]
    for r in rules:
        if r[0] == "same-ns" and o_ns == own_ns: return True
        if r[0] == "ns" and o_ns == r[1]: return True
        if r[0] == "pod" and o_ns == r[1] and o_labels.get("app") == r[2]: return True
    return False

def allowed(src, dst):
    s_ns, d_ns = PODS[src][0], PODS[dst][0]
    egress_ok = s_ns not in EGRESS or peer_ok(EGRESS[s_ns], s_ns, dst)     # namespaces without policies allow all
    ingress_ok = d_ns not in INGRESS or peer_ok(INGRESS[d_ns], d_ns, src)
    return egress_ok, ingress_ok

for src, dst in [("linkly", "postgres"), ("gateway", "linkly"), ("prometheus", "analytics"),
                 ("analytics", "linkly"), ("analytics", "postgres"), ("linkly", "analytics")]:
    e, i = allowed(src, dst)
    verdict = "ALLOW" if e and i else "deny "
    print(f"{src:10} -> {dst:9} {verdict}  (egress {'ok' if e else 'blocked'}, ingress {'ok' if i else 'blocked'})")
linkly     -> postgres  ALLOW  (egress ok, ingress ok)
gateway    -> linkly    ALLOW  (egress ok, ingress ok)
prometheus -> analytics ALLOW  (egress ok, ingress ok)
analytics  -> linkly    deny   (egress ok, ingress blocked)
analytics  -> postgres  deny   (egress blocked, ingress blocked)
linkly     -> analytics deny   (egress blocked, ingress blocked)

analytics -> linkly is the interesting row: growth's egress policy allows it, but links' ingress rules only accept their own namespace, the Gateway and Prometheus, so the call is dropped. Cross-team traffic needs both teams to agree, which is exactly right: team-links decides who may call its service. In practice, cross-team calls go through the Gateway or a service mesh with identity-based authorisation, and each team's chart carries its own ingress policy.

Note: NetworkPolicies need a CNI that enforces them (Cilium, Calico, and most managed-cluster network options). On a CNI without support, they are accepted and silently ignored: test isolation after setting up a cluster.

Test yourself

Why can a user with the built-in edit role in a namespace effectively read every Secret there?

  1. They can't
  2. edit allows reading Secrets and creating Pods that mount them or run as any service account in the namespace
  3. Secrets are public
  4. Through the dashboard only

Which statement about RBAC evaluation is true?

  1. A deny rule overrides allows
  2. Permissions are additive: any binding that grants the request allows it; there are no deny rules
  3. Only the first binding counts
  4. ClusterRoles can't be bound per namespace

Pod A (namespace x) may send to Pod B (namespace y) by x's egress policies, but y's ingress policies don't allow x. What happens?

  1. Allowed
  2. Denied: both the sender's egress and the receiver's ingress must allow the connection
  3. Allowed only for DNS
  4. Depends on the Service type