Add Labels to Resources
Teleport allows you to add arbitrary key-value pairs to applications, servers, databases, and other resources in your cluster. You can use labels to do the following:
- Filter the resources returned when running
tctlandtshcommands. - Define roles that limit the resources Teleport users can access.
This guide demonstrates how to add labels to enrolled server resources. However, you can follow similar steps to add labels to other types of resources.
Static, dynamic, and resource-based labels
The labels you assign to resources can be static labels, dynamic labels, or resource-based labels.
- Static labels are hardcoded in the Teleport configuration file and don't change
while the
teleportprocess is running. For example, you might use a static label to identify the resources in astagingorproductionenvironment. - Dynamic labels—also known as commands-based labels—allow you to generate labels at runtime.
With a dynamic label, the
teleportprocess executes an external command on its host at a configurable frequency and the output of the command becomes the label value. - Resource-based labels allow you to add labels to an instance without restarting the
teleportprocess or editing the configuration file.
You can add multiple static, dynamic, and resource-based labels for the same resource. However, you can't add static labels that use the same key with different values or use a static label to define multiple potential values.
Dynamic labels are especially useful for decoupling a label value from the Teleport configuration.
For example, if you start Teleport on an Amazon EC2 instance, you can use a dynamic label to set the
region value based on the result from a command sent to the EC2 instance
metadata API.
The dynamic label enables you to use the same configuration for each server in an Amazon Machine Image
but filter and limit access to the servers based on their AWS region.
Prerequisites
-
A running Teleport cluster version 17.4.8 or above. If you want to get started with Teleport, sign up for a free trial or set up a demo environment.
-
The
tctladmin tool andtshclient tool.Visit Installation for instructions on downloading
tctlandtsh.
- A Linux host where you will run a Teleport Agent. This guide shows you how to apply labels to an instance of the Teleport SSH Service. You can use the technique shown in the guide to label any Teleport-protected resource.
To check that you can connect to your Teleport cluster, sign in with tsh login, then
verify that you can run tctl commands using your current credentials.
For example, run the following command, assigning teleport.example.com to the domain name of the Teleport Proxy Service in your cluster and email@example.com to your Teleport username:
tsh login --proxy=teleport.example.com --user=email@example.comtctl statusCluster teleport.example.com
Version 17.4.8
CA pin sha256:abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678
If you can connect to the cluster and run the tctl status command, you can use your
current credentials to run subsequent tctl commands from your workstation.
If you host your own Teleport cluster, you can also run tctl commands on the computer that
hosts the Teleport Auth Service for full permissions.
Step 1/2. Install Teleport
-
Select a Linux server where you will run a Teleport Agent.
-
To install a Teleport Agent on your Linux server:
The easiest installation method, for Teleport versions 17.3 and above, is the cluster install script. It will use the best version, edition, and installation mode for your cluster.
-
Assign teleport.example.com:443 to your Teleport cluster hostname and port, but not the scheme (https://).
-
Run your cluster's install script:
curl "https://teleport.example.com:443/scripts/install.sh" | sudo bash
On older Teleport versions:
-
Assign edition to one of the following, depending on your Teleport edition:
Edition Value Teleport Enterprise Cloud cloudTeleport Enterprise (Self-Hosted) enterpriseTeleport Community Edition oss -
Get the version of Teleport to install. If you have automatic agent updates enabled in your cluster, query the latest Teleport version that is compatible with the updater:
TELEPORT_DOMAIN=example.teleport.com:443TELEPORT_VERSION="$(curl https://$TELEPORT_DOMAIN/v1/webapi/automaticupgrades/channel/default/version | sed 's/v//')"Otherwise, get the version of your Teleport cluster:
TELEPORT_DOMAIN=example.teleport.com:443TELEPORT_VERSION="$(curl https://$TELEPORT_DOMAIN/v1/webapi/ping | jq -r '.server_version')" -
Install Teleport on your Linux server:
curl https://cdn.teleport.dev/install.sh | bash -s ${TELEPORT_VERSION} editionThe installation script detects the package manager on your Linux server and uses it to install Teleport binaries. To customize your installation, learn about the Teleport package repositories in the installation guide.
-
-
Generate an invitation token for the host.
The invitation token is required for the local computer to join the Teleport cluster.
The following example generates a new token that is valid for five minutes and can be used to enroll a server:tctl tokens add --ttl=5m --type=nodeThe invite token: abcd123-insecure-do-not-use-this
-
List all generated non-expired tokens by running the following command:
tctl tokens lsToken Type Labels Expiry Time (UTC)
-------------------------------- ----------- ------ -------------------------------
abcd123-insecure-do-not-use-this Node,Db,App 10 Aug 23 19:49 UTC (4m11s)
-
Write the join token to a file on the host at
/tmp/token:echo abcd123-insecure-do-not-use-this | sudo tee /tmp/token -
On the host where you plan to run the Agent, generate a configuration file that enables the Teleport SSH Service. Replace
teleport.example.comwith the host and port of your Teleport Proxy Service or Teleport Enterprise (Cloud) account:sudo teleport configure \ --token="/tmp/token" \ --roles=node \ --proxy=example.teleport.sh:443 \ -o file
Step 2/2. Apply labels
Follow any or all of the sections below to add different types of labels to your resource.
Apply a static label
You can configure static labels by editing the Teleport configuration file, then starting Teleport.
To add a static label:
-
Open the Teleport configuration file,
/etc/teleport.yaml, in an editor on the computer where you installed the Teleport Agent. -
Locate the
labelsconfiguration under thessh_servicesection. -
Add the static label key and value. For example, add
environmentas the label key anddevas the value:ssh_service: enabled: true labels: environment: devThe preceding example illustrates a simple value setting. However, you can also use static labels to define more complex string values that include white space or punctuation marks. For example:
ssh_service: enabled: true labels: location: San Francisco Bldg 301 4th floor -
Save your changes and close the file.
-
Start Teleport on the Linux host:
Configure your Teleport instance to start automatically when the host boots up by creating a systemd service for it. The instructions depend on how you installed your Teleport instance.
- Package Manager
- TAR Archive
On the host where you will run your Teleport instance, enable and start Teleport:
sudo systemctl enable teleportsudo systemctl start teleportOn the host where you will run your Teleport instance, create a systemd service configuration for Teleport, enable the Teleport service, and start Teleport:
sudo teleport install systemd -o /etc/systemd/system/teleport.servicesudo systemctl enable teleportsudo systemctl start teleportYou can check the status of your Teleport instance with
systemctl status teleportand view its logs withjournalctl -fu teleport. -
Verify that you have added the label by running the following command on your local computer.
tsh ls --query 'labels["environment"]=="dev"'You should see output similar to the following:
Node Name Address Labels ---------------- ---------- ------------------------------------------ip-192-168-13-57 ⟵ Tunnel environment=dev,hostname=ip-192-168-13-57Checking the status of your server
If you don't see your server listed when you query for the label you added, you should verify that the SSH Service is running on the server. Check the log for the server to verify that there are messages similar to the following:
2023-08-07T22:22:21Z INFO [NODE:1] Service is starting in tunnel mode. pid:149932.1 service/service.go:2630 2023-08-07T22:22:21Z INFO [UPLOAD:1] starting upload completer service pid:149932.1 service/service.go:2723 2023-08-07T22:22:21Z INFO [PROC:1] The new service has started successfully. Starting syncing rotation status...Checking your user profile
If the SSH Service is running on the server, verify that your current Teleport user has a login on the local host. You can check the status of your user account by running the following command:
tsh statusYou should see output similar to the following with at least one login listed for your current user:
> Profile URL: https://ajuba-aws.teledocs.click:443 Logged in as: teleport-admin Cluster: teleport-aws.example.com Roles: access, editor Logins: root, ubuntu, ec2-user Kubernetes: enabled Valid until: 2023-08-08 10:08:46 +0000 UTC [valid for 10h36m0s] Extensions: login-ip, permit-agent-forwarding, permit-port-forwarding, permit-pty, private-key-policyIf no valid logins have been assigned to the user, you should update your current user profile to include at least one valid login.
You can add logins to a user by running a command similar to the following:
tctl users update myuser --set-logins=rootThis example adds the
rootlogin to themyuserTeleport user. For more information about managing logins for Teleport users, see Local Users.
Using hidden static labels
If you want to use labels for role-based access control but don't want to
display the labels in command output or the Teleport Web UI, you can define them
in a hidden namespace by prefixing the label key with teleport.hidden/.
For example:
ssh_service:
enabled: true
labels:
teleport.hidden/team-id: ai-lab-01
Apply dynamic labels using commands
As with static labels, you can apply dynamic labels by editing the Teleport configuration file, then restarting the Teleport service on your server.
To add a command to generate a dynamic label:
-
Stop the Teleport service running on your server.
-
Open the Teleport configuration file—by default,
/etc/teleport.yaml—in a text editor. -
Locate the
commandsconfiguration under thessh_servicesection. -
Add a
commandarray that runs theunamecommand with the-pargument to return the architecture of the host server every one hour.For example, add the
name,command, andperiodfields as follows:... ssh_service: enabled: true labels: teleport.internal/resource-id: 1f2cdcc5-cde3-41fa-b390-bc872087821a environment: dev commands: - name: hostname command: [hostname] period: 1m0s - name: arch command: [uname, -p] period: 1h0m0sIn the
commandsetting, the first element is a valid executable. Each subsequent element is an argument. The following syntax is valid:command: ["/bin/uname", "-m"]The following syntax is not valid:
command: ["/bin/uname -m"]For more complex commands, you can use single (') and double (") quotation marks interchangeably to create nested expressions. For example:
command: ["/bin/sh", "-c", "uname -a | egrep -o '[0-9]+\\.[0-9]+\\.[0-9]+'"]In configuring commands, keep the following in mind:
- The executable must be discoverable in the
$PATHor specified using an absolute path. - You must set the executable permission bit on any file you use as a command.
- Shell scripts must have a shebang line.
The
periodsetting specifies how frequently Teleport executes each command. In this example, theuname -pcommand is executed every one hour (1h), zero minutes (0m), and zero seconds (0s). This value can't be less than one minute. - The executable must be discoverable in the
-
Save your changes and close the file.
-
Start Teleport with the invitation token you saved in the
INVITE_TOKENenvironment variable:sudo teleport start --token=${INVITE_TOKEN?} -
Verify that you have added the label by running the following command on your local computer. Your Teleport user must be authorized to access the server.
tsh lsYou should see output similar to the following with both the
archandenvironmentlabels displayed:Node Name Address Labels ---------------- -------------- ------------------------------------------------------ip-192-168-13-57 ⟵ Tunnel arch=x86_64,environment=dev,hostname=ip-192-168-13-57
Apply resource-based labels
Applying resource-based labels is only supported for servers.
You can apply resource-based labels to a Teleport instance by creating a server_info
resource for the instance. For a server with name <name>, its matching server_info
should be named si-<name>.
To add resource-based labels:
-
Run
tctl get node/NODE_NAMEto get the name of the node resource to apply labels to. You should get output similar to the following:kind: node metadata: expires: "2024-01-12T00:41:17.355013266Z" id: <id> name: <name-uuid> revision: <revision-uuid> spec: # ...Save the value of
metadata.namefor the next step. -
Create the file
server_info.yamland paste the following into it:# server_info.yaml kind: server_info metadata: name: si-<node-name> spec: new_labels: "foo": "bar"Replace
<node-name>with the resource name you saved in the previous step. Run the following to create theserver_inforesource:tctl create server_info.yaml -
Verify that you have added the label by running the following command on your local computer. Your Teleport user must be authorized to access the server. Teleport applies labels from
server_inforesources gradually to prevent strain on the Auth Service in larger clusters, so it may take several minutes for the new labels to appear.tsh lsYou should see output similar to the following with the
dynamic/foolabel displayed:Node Name Address Labels ---------------- -------------- ------------------------------------------------------ip-192-168-13-57 ⟵ Tunnel dynamic/foo=bar,hostname=ip-192-168-13-57
All resource-based labels created with tctl will have the
dynamic/ prefix. This prefix forbids the label from being used in a
role's deny rules.
To update resource-based labels, recreate the server_info resource with updated labels.
Next steps
After you have labeled your resources, you can use the labels when running
tsh and tctl commands to filter the resources that the commands return.
For more information, see Resource filtering.
You can also use labels to limit the access that users in different roles have to
specific classes of resources. For more information, see Teleport Role Templates.