Skip to content

Connecting To a Database via Teleport

Database consoles in gprd, gstg, and other environments are accessed via Teleport. Access to Database consoles by SSH’ing into the console servers will be removed in future (for the majority of use cases).

  • The main reasons for this change are security and compliance: With Teleport we have fully flexible, on-demand, and audited access to our Database consoles and to some other terminal/CLI tools, like kubectl, Rails Console, and more.
  • Teleport’s goal is to provide a Unified Access Plane for all of our infrastructure. Here you can find some of the most popular use cases for Teleport.
  • We evaluated Teleport thoroughly (see this issue) and found it to comply with most of our infrastructure access requirements, unlike some of its competitors (Okta-ASA, Hashicorp Boundary and others).

Before you start using Teleport, you must be assigned the app in Okta. This should be a part of your role’s baseline group assignment. In most cases there should be no additional action required to gain access to the services appropriate to your role. If your onboarding is complete and you still do not have access to the Teleport app in Okta, open an access request and follow the appropriate approval methods.

Note that trying to login to the Teleport Console from Okta dashboard can fail with a message “Login Unsuccessful”, this is totally expected and does not mean you need to open an access request.

You need the Teleport CLI client tsh for connecting to a Database console.

Official packages for macOS and Linux can be found at Teleport’s website.

You also need psql for accessing a Database shell. Please ensure you install it with Homebrew and not other tools such as mise or asdf.

Terminal window
brew install postgresql@14

Follow the guide corresponding to the database instance and access level required:

Non-Production Read/write or Production access requires an access request using Teleport for the appropriate role. The access will be temporary (12 hours from the time of approval) and can be approved by any SRE or Reliability Manager.

The typical workflow for accessing a database which requires an access request is as follows:

  1. Authenticate to the Teleport instance.
  2. Submit an access request for the database role that you need.
  3. After your request is approved, log in to the database with the appropriate database user.
  4. Connect to the database shell.

If the request is urgent, you can ping @sre-oncall, but to spread out the workload, please try to allow some time for others to review first if possible. Access can be extended before or after expiration using the same process.

NOTE: we now have only one Teleport instance available at https://production.teleport.gitlab.net.

The following database instances are available:

DescriptionDatabase Name
Maindb-main-replica-gstg
CIdb-ci-replica-gstg
Registrydb-registry-replica-gstg
Delayed Replica (DR) archive of maindb-main-dr-archive-gstg
Delayed Replica (DR) archive of CIdb-ci-dr-archive-gstg
Delayed Replica (DR) archive of registrydb-registry-dr-archive-gstg

This list is available using tsh db ls environment=gstg.

Replace <Database Name> in the following examples with the desired value from the above table.

Read-only access to non-production (gstg or pre) databases is given to everyone by default via the non-prod-database-ro role. Hence, you do NOT need to submit an access request for read-only access in non-production environments (gstg or pre), and can instead just authenticate and connect to the database using the following process:

  1. Authenticate to the Teleport instance and login to the database. This command opens Okta in a browser window:

    Terminal window
    $ tsh db login --proxy=production.teleport.gitlab.net --db-user=console-ro --db-name=gitlabhq_production <Database Name>
  2. Connect to the database:

    Terminal window
    $ tsh db connect <Database Name>

    See the troubleshooting section if the above command returns ERROR: signal: segmentation fault.

Read/write access to non-production (gstg or pre) databases requires an access request using Teleport for the appropriate role:

  1. Authenticate to the Teleport instance and request approval for the required DB role. This command opens Okta in a browser window:

    DatabaseRoleNote
    main or cinon-prod-database-rw
    registrynon-prod-database-registry-rwFor Package Team members

    Use the following command to request approval:

    Terminal window
    $ tsh login --proxy=production.teleport.gitlab.net --request-roles=<Role> --request-reason="GitLab Issue URL or ZenDesk Ticket URL"

    This command will pause while it waits for the reviewer to approve the request. It may appear to hang, but it is waiting for someone to approve it. The command will return as soon as the request is approved, denied, or expires.

    If the command is stopped or times out, but the request is approved, you do not need to request another approval. Simply login and provide the approved request ID (output by the previous command, or find it in the web interface).

  2. Login with the approved request ID.

    The request ID is shown in the output of tsh login when making the initial request, and can also be found attached to your request notification in #teleport-requests.

    Terminal window
    $ tsh login --request-id=<request-id>
  3. Login to the database.

    Once an approval is issued, the next step is to log in to the database. The database name at the end of the line refers to the database host that Teleport is pointing to (which you can see with tsh db ls).

    Terminal window
    $ tsh db login --db-user=console-rw --db-name=gitlabhq_production <Database Name>
  4. Connect to the database.

    Once logged in, you can connect and disconnect from the console as many times as needed.

    Terminal window
    $ tsh db connect <Database Name>

    See the troubleshooting section if the above command returns ERROR: signal: segmentation fault.

The following database instances are available:

DescriptionDatabase Name
Maindb-main-replica-gprd
CIdb-ci-replica-gprd
Registrydb-registry-replica-gprd
Delayed Replica (DR) archive of maindb-main-dr-archive-gprd
Delayed Replica (DR) archive of CIdb-ci-dr-archive-gprd
Delayed Replica (DR) archive of registrydb-registry-dr-archive-gprd

This list is available using tsh db ls environment=gprd.

Replace <Database Name> in the following examples with the desired value from the above table.

  1. Authenticate to the Teleport instance. This command opens Okta in a browser window:

    Terminal window
    $ tsh login --proxy=production.teleport.gitlab.net
  2. Request approval for the database role that you need.

    DatabaseTypeRole
    main or ciRead-onlyprod-database-ro
    main or ciRead/Writeprod-database-rw
    registryRead-onlyprod-database-registry-ro
    registryRead/Writeprod-database-registry-rw

    Use the following command to request approval:

    Terminal window
    $ tsh login --proxy=production.teleport.gitlab.net --request-roles=<Role> --request-reason="GitLab Issue URL or ZenDesk Ticket URL"

    This command will pause while it waits for the reviewer to approve the request. It may appear to hang, but it is waiting for someone to approve it. The command will return as soon as the request is approved, denied, or expires.

    If the command is stopped or times out, but the request is approved, you do not need to request another approval. Simply login and provide the approved request ID (output by the previous command, or find it in the web interface).

  3. Login with the approved request ID.

    The request ID is shown in the output of tsh login when making the initial request, and can also be found attached to your request notification in #teleport-requests.

    Terminal window
    $ tsh login --request-id=<request-id>
  4. Login to the database.

    Once an approval is issued, the next step is to log in to the database.

    Terminal window
    $ tsh db login --db-user=console-ro --db-name=gitlabhq_production <Database Name>
  5. Connect to the database.

    Once logged in, you can connect and disconnect from the console as many times as needed.

    Terminal window
    tsh db connect <Database Name>

    See the troubleshooting section if the above command returns ERROR: signal: segmentation fault.

The database-admin role gives admin access to any database. This role is only meant to be used by DBREs (and SREs in case of incidents).

Submit an access request as follows.

Terminal window
$ tsh login --proxy=production.teleport.gitlab.net --request-roles=database-admin --request-reason="GitLab Issue URL or ZenDesk Ticket URL"
$ tsh login --request-id=<request-id>

Approvers will get your request via an automated notification in the #teleport-requests Slack channel. If you have additional context, or need to expedite an approval, please comment as a thread under that message. If the request is urgent, you can ping @sre-oncall, but to spread out the workload, please try to allow some time for others to review first if possible. If the approval request does not show up in #teleport-requests, feel free to ask someone in that channel to take a look at your request, and provide the request ID.

Approvers will review the issue URL in the request. If Rails Console access seems like a reasonable step to address that issue, they will approve it.

If you have any issues using Teleport, or the approval process, please ask the Foundations team or in the #infrastructure-lounge Slack channel.

Note: If you need more time, you can renew your role access before or after expiration using the same method as the initial request.

If you have issues connecting, try using the --debug flag to display more verbose information.

psql: error: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port X

Section titled “psql: error: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port X”

This is possibly because the local psql client is overriding the user and database name. It can be resolved by running this more explicit command.

Terminal window
$ tsh db connect --db-user=console-ro --db-name=gitlabhq_production <database_name>

For example:

Terminal window
$ tsh db connect --db-user=console-ro --db-name=gitlabhq_production db-main-replica-gstg

psql: error: SSL SYSCALL error: Undefined error: 0 ERROR: signal: segmentation fault

Section titled “psql: error: SSL SYSCALL error: Undefined error: 0 ERROR: signal: segmentation fault”

If you encounter the error, it is probably because you have postgresql installed via asdf ormise. To solve it:

  1. Install postgresql via homebrew, if not already installed:

    Terminal window
    $ brew install postgresql@14
  2. Run brew --prefix to obtain the path to the psql binary installed via homebrew:

    Terminal window
    $ $(brew --prefix postgresql@14)/bin/psql
  3. Run tsh db config --format=cmd <database_name> to get the full tsh command, for example:

    Terminal window
    $ tsh db config --format=cmd db-customersdot-gstg
    ~/.asdf/shims/psql "postgres://teleport-cloudsql%40gitlab-subscriptions-staging.iam@production.teleport.gitlab.net..."
  4. Replace the ~/.asdf/shims/psql or ~/.local/share/mise/installs/postgres/13.9/bin/psql if using mise path from the output of the full tsh command obtained in step 3 above with the path to the psql binary installed via homebrew:

    Terminal window
    $ $(brew --prefix postgresql@14)/bin/psql "postgres://teleport-cloudsql%40gitlab-subscriptions-staging.iam@production.teleport.gitlab.net:443/CustomersDot_stg?sslrootcert=/Users/<username>/.tsh/keys/production.teleport.gitlab.net/cas/production.teleport.gitlab.net.pem&sslcert=/Users/<username>/.tsh/keys/production.teleport.gitlab.net/<username>@gitlab.com-db/production.teleport.gitlab.net/db-customersdot-gstg-x509.pem&sslkey=/Users/<username>/.tsh/keys/production.teleport.gitlab.net/<username>@gitlab.com&sslmode=verify-full"

psql: error: sslmode value "verify-full" invalid when SSL support is not compiled in

Section titled “psql: error: sslmode value "verify-full" invalid when SSL support is not compiled in”

tsh db is a wrapper over psql and this likely means that your installed psql version was not configured with OpenSSL options. You can consider taking steps like this blog post if psql was installed via asdf. Ideally, use the Homebrew installed psql version.

failed to add one or more keys to the agent

Section titled “failed to add one or more keys to the agent”

If you encounter the error

ERROR: failed to add one or more keys to the agent.
agent: failure, agent: failure

Try running the same command and passing in the flag --add-keys-to-agent=no

Terminal window
$ tsh login --add-keys-to-agent=no --proxy=production.teleport.gitlab.net

There is an open issue about this