Skip to content
New issue

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

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

Already on GitHub? # to your account

GCP CLI support: tsh and tctl changes #19790

Merged
merged 19 commits into from
Jan 11, 2023
Merged

GCP CLI support: tsh and tctl changes #19790

merged 19 commits into from
Jan 11, 2023

Conversation

Tener
Copy link
Contributor

@Tener Tener commented Jan 3, 2023

tsh and tctl changes for #17257

To be applied on top of #19789.

This is the final PR in the chain. Fixes #17257.


TODO: Testing:


Setup:

  1. Create a service account teleport-service.
  2. Assign Service Account User role to the teleport-service account.
  3. Deploy Teleport instance to GCP VM. Select teleport-service as a VM account to use. If you cannot select the service account, check that it has appropriate role (see step 2).
  4. Create service accounts for users to use, for example:teleport-user-1 and teleport-user-2. Assign desired permissions for these accounts.
  5. Grant access for teleport-user-1, teleport-user-2 etc. to teleport-service at least with Service Account Token Creator role. This will allow teleport-service to issue tokens as teleport-user-XXX. More info: https://cloud.google.com/iam/docs/service-accounts#token-creator-role
  6. Configure GCP as a Teleport application. cloud: GCP is the critical part of the config.
app_service:
  enabled: "yes"
  apps:
  - name: gcpapp
    cloud: GCP
  1. Ensure the user is allowed to use chosen service accounts. If this is a fresh cluster, the access role should contain the snippet below. If not, modify a role the user has access to include a reference to {{internal.gcp_service_accounts}}. You can also hardcode the list of identities in the role itself (not recommended).
spec:
  allow:
    gcp_service_accounts:
    - '{{internal.gcp_service_accounts}}'
  1. When using {{internal.gcp_service_accounts}}, the list of allowed roles is sourced from user-specific traits. To update an existing user use tctl users update <username> --set-gcp-service-accounts ID_1,ID_2,ID_3,..., where ID_N is a full name of service account (e.g. teleport-user-1@my-account-123456.iam.gserviceaccount.com )
  2. Refresh client login with tsh logout; tsh login --proxy=....
  3. Log in to gcpapp app with tsh app login gcpapp [--gcp-service-account ACCOUNT]. The service account is optional if there is just one available. The service account name can be shortened by discarding domain (e.g. teleport-user-1) as long as it doesn't make it ambigous.
  4. Run commands with tsh gcloud ..., e.g. tsh gcloud compute instances list.
  5. Alternatively, start a proxy with tsh proxy gcloud and follow printed instructions.

Notes:

  1. Instead of deploying Teleport to GCP VM, you can download the key for teleport-service service account to your local machine and set the GOOGLE_APPLICATION_CREDENTIALS env variable to its location. This is handful in development, but not recommended for production as the key file can be stolen. Example: GOOGLE_APPLICATION_CREDENTIALS=/path/to/teleport-service-my-account-123456-ee0011223344.json teleport start
  2. The setup above assumes Teleport is running as a privileged account teleport-service which is inaccessible to Teleport users.

@Tener Tener marked this pull request as ready for review January 3, 2023 11:50
@github-actions github-actions bot added size/lg tctl tctl - Teleport admin tool tsh tsh - Teleport's command line tool for logging into nodes running Teleport. labels Jan 3, 2023
@Tener Tener force-pushed the tener/gcloud-cli-handler branch from 54f03a0 to 987e04c Compare January 3, 2023 12:53
@Tener Tener force-pushed the tener/gcloud-cli-tsh-tctl branch from 8321cdb to cbc0c1b Compare January 3, 2023 12:54
@Tener Tener force-pushed the tener/gcloud-cli-handler branch from 987e04c to d92542c Compare January 3, 2023 13:12
@Tener Tener force-pushed the tener/gcloud-cli-tsh-tctl branch from cbc0c1b to 2dae0a0 Compare January 3, 2023 13:12
@Tener Tener force-pushed the tener/gcloud-cli-handler branch from d92542c to 71e3a69 Compare January 4, 2023 10:30
@Tener Tener force-pushed the tener/gcloud-cli-tsh-tctl branch from 49ec4f8 to cb83f86 Compare January 4, 2023 10:32
@smallinsky smallinsky requested a review from greedy52 January 4, 2023 11:00
@Tener Tener force-pushed the tener/gcloud-cli-tsh-tctl branch from cb83f86 to 0e35c38 Compare January 4, 2023 15:05
@Tener Tener force-pushed the tener/gcloud-cli-handler branch from d5a27d2 to f8a2788 Compare January 4, 2023 15:36
@Tener Tener force-pushed the tener/gcloud-cli-tsh-tctl branch from 0e35c38 to a3bd10d Compare January 4, 2023 15:37
Copy link
Contributor

@GavinFrazar GavinFrazar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LGTM, but a few things I'd like to address before approving

@Tener Tener force-pushed the tener/gcloud-cli-handler branch from f8a2788 to aa7754b Compare January 5, 2023 10:59
@Tener Tener force-pushed the tener/gcloud-cli-tsh-tctl branch from a3bd10d to 0f0ddab Compare January 5, 2023 11:04
@Tener Tener requested a review from GavinFrazar January 5, 2023 11:05
@Tener Tener force-pushed the tener/gcloud-cli-handler branch from aa7754b to adf88aa Compare January 5, 2023 11:32
@Tener Tener force-pushed the tener/gcloud-cli-tsh-tctl branch from 0f0ddab to 877d3ce Compare January 5, 2023 11:33
@Tener Tener force-pushed the tener/gcloud-cli-handler branch from adf88aa to 54ccb28 Compare January 5, 2023 12:21
@Tener Tener force-pushed the tener/gcloud-cli-tsh-tctl branch from 877d3ce to c792f8a Compare January 5, 2023 12:22
@Tener Tener requested a review from smallinsky January 5, 2023 13:58
Tener added 15 commits January 11, 2023 12:22
- make debug log entry in appropriate place
- document the default scope list
- replace `context.WithTimeout` with `s.Clock.After` to avoid `time.Sleep` in tests.
- cloud client is now being mocked, making `generateAccessToken` testeable.
- update tests
- replace `context.WithTimeout` with `s.Clock.After` to avoid `time.Sleep` in tests.
- make `tsh gcloud` and `tsh proxy gcloud` base commands so they are shown in `--help`
- `tsh gcp` and `tsh proxy gcp` are aliases now
- switch `REQUESTS_CA_BUNDLE` to official `CLOUDSDK_CORE_CUSTOM_CA_CERTS_FILE`
- add doc links for `CLOUDSDK_AUTH_ACCESS_TOKEN` and `CLOUDSDK_CORE_CUSTOM_CA_CERTS_FILE`
- remove commented out code
- add docs and tests for `SortedGCPServiceAccounts`
- move `TELEPORT_GCLOUD_SECRET` to constant
- typos
@Tener Tener force-pushed the tener/gcloud-cli-handler branch from 54ccb28 to 46d1ddc Compare January 11, 2023 11:40
@Tener Tener force-pushed the tener/gcloud-cli-tsh-tctl branch from c2852fd to 9f4f453 Compare January 11, 2023 11:40
Base automatically changed from tener/gcloud-cli-handler to master January 11, 2023 16:22
@Tener Tener enabled auto-merge (squash) January 11, 2023 16:23
@Tener Tener merged commit e053cba into master Jan 11, 2023
@Tener Tener deleted the tener/gcloud-cli-tsh-tctl branch January 11, 2023 16:34
@Tener
Copy link
Contributor Author

Tener commented Jan 16, 2023

@smallinsky

Regarding point:

  • Terraform GCP provider.

This works out of the box, but Terraform needs to be configured correctly.

Assuming we are running tsh proxy gcp like so:

TELEPORT_GCLOUD_SECRET=my_gcloud_secret tsh proxy gcp --port 3500
Started GCP proxy on http://127.0.0.1:3500.

Use the following credentials and HTTPS proxy setting to connect to the proxy:

  export BOTO_CONFIG=/Users/myuser/.tsh/gcp/teleport.example.com/gcp/bd460f17_boto.cfg
  export CLOUDSDK_AUTH_ACCESS_TOKEN=my_gcloud_secret
  export CLOUDSDK_CONFIG=/Users/myuser/.tsh/gcp/teleport.example.com/gcp/gcloud
  export CLOUDSDK_CORE_CUSTOM_CA_CERTS_FILE=/Users/myuser/.tsh/keys/teleport.example.com/myuser-app/teleport.example.com/gcp-localca.pem
  export CLOUDSDK_CORE_PROJECT=myproject-123456
  export HTTPS_PROXY=http://127.0.0.1:3500

Then we must run Terraform with the following env variables exported:

  export HTTPS_PROXY=http://127.0.0.1:3500
  export GOOGLE_OAUTH_ACCESS_TOKEN=my_gcloud_secret

Instead of using GOOGLE_OAUTH_ACCESS_TOKEN the token can be saved in a file as per https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#access_token.

Unlike some other apps, Terraform does not provide explicit support for custom CA certs: hashicorp/terraform#28551.

To make it trust /Users/myuser/.tsh/keys/teleport.example.com/myuser-app/teleport.example.com/gcp-localca.pem we must use platform-specific mechanisms available to Go-based programs. On Linux that can be SSL_CERT_FILE env var, on MacOS security add-trusted-cert. I'm not sure what the preferred mechanism is on Windows.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
size/lg tctl tctl - Teleport admin tool tsh tsh - Teleport's command line tool for logging into nodes running Teleport.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GCP CLI access
4 participants