Skip to main content

Deploying Login Rules Using the Teleport Kubernetes Operator

Report an Issue

This guide will explain how to:

  • Use Teleport's Kubernetes Operator to deploy Login Rules to your Teleport cluster
  • Edit deployed Login Rules with kubectl

This guide is applicable if you self-host Teleport in Kubernetes using the teleport-cluster Helm chart.

How it works​

Login Rules are registered with the Teleport Auth Service as resources stored on the Auth Service backend. The Teleport Auth Service exposes a gRPC API that enables clients to create, delete, or modify backend resources, including Login Rules. The Teleport Kubernetes Operator can manage Login Rules by authenticating to the Teleport Auth Service and interacting with its gRPC API.

The Teleport Kubernetes Operator only watches custom resources in the namespace where it is installed. You must create your TeleportLoginRule resources in that same namespace, or the operator will not sync them to Teleport.

Prerequisites​

  • A Teleport Enterprise license

  • A Kubernetes cluster (with or without teleport-cluster Helm chart already deployed)

  • Helm

  • kubectl

  • Validate Kubernetes connectivity by running the following command:

    kubectl cluster-info

    Kubernetes control plane is running at https://127.0.0.1:6443

    CoreDNS is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

    tip

    Users wanting to experiment locally with the Operator can use minikube to start a local Kubernetes cluster:

    minikube start
  • Follow the Teleport operator guides to install the Teleport Operator in your Kubernetes cluster. Make sure to follow the Enterprise instructions if you're deploying the operator as part of the teleport-cluster chart.

    Note the namespace you installed the teleport-cluster chart into. This guide refers to it as teleport-cluster. Every kubectl command in this guide passes --namespace explicitly, so you do not need to change your current kubectl context.

    This guide also assumes you named the Helm release teleport-cluster, as in the operator guide, so the Auth Service runs in a deployment called teleport-cluster-auth. If you used a different release name, replace teleport-cluster-auth with <release-name>-auth in the commands below.

    Confirm that the CRD (Custom Resource Definition) for Login Rules has been installed with the following command:

    kubectl explain TeleportLoginRule.spec
    KIND: TeleportLoginRuleVERSION: resources.teleport.dev/v1
    RESOURCE: spec <Object>
    DESCRIPTION: LoginRule resource definition v1 from Teleport
    FIELDS: priority <integer> Priority is the priority of the login rule relative to other login rules in the same cluster. Login rules with a lower numbered priority will be evaluated first.
    traits_expression <string> TraitsExpression is a predicate expression which should return the desired traits for the user upon login.
    traits_map <> TraitsMap is a map of trait keys to lists of predicate expressions which should evaluate to the desired values for that trait.

    The header lines of this output can vary by kubectl version. The command succeeds if the priority, traits_expression, and traits_map fields are listed. If the command fails, you may not have installed the Teleport Operator, or you may have installed an older version.

Step 1/2. Create a Login Rule using kubectl​

Paste the following into a file called login-rules.yaml that describes two custom Login Rule resources:

# login-rules.yaml
apiVersion: resources.teleport.dev/v1
kind: TeleportLoginRule
metadata:
  name: example-traits-map-rule
  labels:
    example: "true"
spec:
  # The rule with the lowest priority will be evaluated first.
  priority: 0

  # traits_map holds a map of all desired trait keys to lists of expressions
  # that determine the trait values.
  traits_map:

    # The "logins" traits will be set to the external "username" trait converted
    # to lowercase, and any external "logins" trait.
    logins:
      - 'strings.lower(external.username)'
      - 'external.logins'

    # The external "groups" trait will be passed through unchanged, all other
    # traits will be filtered out.
    groups:
      - external.groups
---
apiVersion: resources.teleport.dev/v1
kind: TeleportLoginRule
metadata:
  name: example-traits-expression-rule
  labels:
    example: "true"
spec:
  # This rule has a higher priority value, so it will be evaluated after the
  # "example-traits-map-rule".
  priority: 1

  # traits_expression is an alternative to traits_map, which returns all desired
  # traits in a single expression.
  traits_expression: |
    external.put("groups",
      choose(
        option(external.groups.contains("admins"), external.groups.add("app-admins", "db-admins")),
        option(external.groups.contains("ops"), external.groups.add("k8s-admins")),
        option(true, external.groups)))

This file does not set metadata.namespace. The namespace is set by the --namespace flag in the commands below. If you apply this file another way, such as through a GitOps tool, make sure it lands in the teleport-cluster namespace, or the operator will not sync it.

Create the Kubernetes resources in the same namespace as the Teleport Operator:

kubectl apply -f login-rules.yaml --namespace teleport-cluster

List the created Kubernetes resources:

kubectl get loginrules --namespace teleport-cluster
NAME AGEexample-traits-expression-rule 8m8sexample-traits-map-rule 8m8s

The values in the AGE column will differ in your output.

Confirm that the operator synced each Login Rule to Teleport. Listing the resources only shows that they exist in Kubernetes, not that Teleport accepted them. The operator syncs resources in the background, so wait for it to finish:

kubectl wait --for=condition=SuccessfullyReconciled TeleportLoginRule/example-traits-map-rule --namespace teleport-cluster --timeout=30s
kubectl wait --for=condition=SuccessfullyReconciled TeleportLoginRule/example-traits-expression-rule --namespace teleport-cluster --timeout=30s

Each command prints condition met when Teleport has accepted the rule. If a command times out, find out why with kubectl describe:

kubectl describe TeleportLoginRule example-traits-map-rule --namespace teleport-cluster

In the Status section of the output, find the condition with Type: SuccessfullyReconciled. If it shows Status: False, the Message field explains why Teleport rejected the rule. Fix the rule in login-rules.yaml and apply it again before you continue.

Check that the Login Rules have been created in Teleport:

kubectl exec -i deployment/teleport-cluster-auth --namespace teleport-cluster -c teleport -- tctl get login_rules
kind: login_rulemetadata: id: 1680225062340767900 labels: example: "true" teleport.dev/origin: kubernetes name: example-traits-expression-rulespec: priority: 1 traits_expression: | external.put("groups", choose( option(external.groups.contains("admins"),external.groups.add("app-admins", "db-admins")), option(external.groups.contains("ops"),external.groups.add("k8s-admins")), option(true, external.groups)))version: v1---kind: login_rulemetadata: id: 1680225067068319000 labels: example: "true" teleport.dev/origin: kubernetes name: example-traits-map-rulespec: priority: 0 traits_map: groups: - external.groups logins: - strings.lower(external.username) - external.loginsversion: v1

If tctl get login_rules returns [], the operator has not synced your rules. Confirm that you applied login-rules.yaml to the teleport-cluster namespace.

Test the Login Rules by sending some example input traits to the standard input of the tctl login_rule test command and having it load all Login Rules from the cluster.

echo '{"groups": ["admins", "ops"], "username": ["Alice"], "logins": ["user", "root"]}' | \ kubectl exec -i deployment/teleport-cluster-auth --namespace teleport-cluster -c teleport -- tctl login_rule test --load-from-cluster
groups:- admins- ops- app-admins- db-adminslogins:- alice- user- root

Trait values are sets, so the order of the values in your output may differ. The test succeeds if your output contains the same values under each trait.

If the output matches your input unchanged (for example, Alice is not lowercased and no app-admins group appears), no Login Rules were loaded from the cluster. Recheck the kubectl describe and tctl get login_rules steps above.

Step 2/2. Edit the Login Rules with kubectl​

Edit the example-traits-map-rule to add an extra example login.

Each entry in a traits_map list is a predicate expression, not a literal string. To add a fixed value, wrap it in the set() function:

--- a/login-rules.yaml
+++ b/login-rules.yaml
@@ -18,6 +18,7 @@ spec:
     logins:
       - 'strings.lower(external.username)'
       - 'external.logins'
+      - 'set("example")'

     # The external "groups" trait will be passed through unchanged, all other
     # traits will be filtered out.

Apply the update to the Kubernetes resource:

kubectl apply -f login-rules.yaml --namespace teleport-cluster

Confirm that the operator accepted the updated rule:

kubectl describe TeleportLoginRule example-traits-map-rule --namespace teleport-cluster

The SuccessfullyReconciled condition should show Status: True and Reason: NoError. If it shows Status: False, Teleport kept the previous version of the rule. Check the Message field for the expression error.

The condition already showed True before your edit, so a True status alone doesn't prove the operator has processed the update. If the test below still shows the old result, wait a few seconds and run kubectl describe again.

Test the Login Rules again to see the extra example login:

echo '{"groups": ["admins", "ops"], "username": ["Alice"], "logins": ["user", "root"]}' | \ kubectl exec -i deployment/teleport-cluster-auth --namespace teleport-cluster -c teleport -- tctl login_rule test --load-from-cluster
groups:- ops- app-admins- db-admins- adminslogins:- root- user- example- alice

As before, the order of values may differ. The test succeeds if logins now contains example in addition to alice, user, and root.

Next Steps​