|
| 1 | +--- |
| 2 | +title: Deploying Machine ID on Bitbucket Pipelines |
| 3 | +description: How to install and configure Machine ID on Bitbucket Pipelines |
| 4 | +--- |
| 5 | + |
| 6 | +In this guide, you will configure Machine ID's agent, `tbot`, to run within a |
| 7 | +Bitbucket Pipelines workflow. The bot will be configured to use the `bitbucket` |
| 8 | +delegated joining method to eliminate the need for long-lived secrets. |
| 9 | + |
| 10 | +## How it works |
| 11 | + |
| 12 | +The `bitbucket` join method is a secure way for Machine ID bots to authenticate |
| 13 | +with the Teleport Auth Service without using any shared secrets. Instead, it |
| 14 | +makes use of an OpenID Connect token that Bitbucket Pipelines injects into the |
| 15 | +job environment. |
| 16 | + |
| 17 | +This token is sent to the Teleport Auth Service, and assuming it has been |
| 18 | +configured to trust Bitbucket's identity provider and all identity assertions |
| 19 | +match, the authentication attempt will succeed. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +(!docs/pages/includes/edition-prereqs-tabs.mdx!) |
| 24 | + |
| 25 | +- (!docs/pages/includes/tctl.mdx!) |
| 26 | +- A Bitbucket repository you can push to. |
| 27 | + |
| 28 | +## Step 1/4. Determine Bitbucket configuration |
| 29 | + |
| 30 | +Bitbucket joining requires a number of configuration parameters that can be |
| 31 | +found in your repository settings. From the Bitbucket repository, navigate to |
| 32 | +"Repository settings", then in the sidebar under "Pipelines" select "OpenID |
| 33 | +Connect". |
| 34 | + |
| 35 | +From this page, note the following values: |
| 36 | +- Identity provider URL (<Var name="identity-provider-url" />) |
| 37 | +- Audience (<Var name="audience" />) |
| 38 | +- Workspace UUID, including the braces (<Var name="workspace-uuid" />) |
| 39 | +- Repository UUID, including the braces (<Var name="repository-uuid" />) |
| 40 | + |
| 41 | +## Step 2/4. Create the Machine ID bot |
| 42 | + |
| 43 | +(!docs/pages/includes/machine-id/create-a-bot.mdx!) |
| 44 | + |
| 45 | +## Step 3/4. Create the join token for Bitbucket Pipelines |
| 46 | + |
| 47 | +In order to allow your Pipelines workflow to authenticate with your Teleport |
| 48 | +cluster, you'll first need to create a join token. These tokens set out criteria |
| 49 | +by which the Auth Service decides whether or not to allow a bot or node to join. |
| 50 | + |
| 51 | +Create a file named `bot-token.yaml`, ensuring that you replace the |
| 52 | +`identity_provider_url`, `audience`, `workspace_uuid`, and `repository_uuid` |
| 53 | +with the values from Step 1. |
| 54 | + |
| 55 | +```yaml |
| 56 | +kind: token |
| 57 | +version: v2 |
| 58 | +metadata: |
| 59 | + name: example-bot |
| 60 | +spec: |
| 61 | + roles: [Bot] |
| 62 | + join_method: bitbucket |
| 63 | + bot_name: example |
| 64 | + bitbucket: |
| 65 | + identity_provider_url: <Var name="identity-provider-url" /> |
| 66 | + audience: <Var name="audience" /> |
| 67 | + # allow specifies the rules by which the Auth Service determines if `tbot` |
| 68 | + # should be allowed to join. |
| 69 | + allow: |
| 70 | + - workspace_uuid: <Var name="workspace-uuid" /> |
| 71 | + repository_uuid: <Var name="repository-uuid" /> |
| 72 | +``` |
| 73 | +
|
| 74 | +Let's go over the token resource's fields in more detail: |
| 75 | +
|
| 76 | +- `metadata.name` defines the name of the token. Note that this value will need |
| 77 | + to be used in other parts of the configuration later. |
| 78 | +- `spec.bot_name` is the name of the Machine ID bot that this token will grant |
| 79 | + access to. Note that this value will need to be used in other parts of the |
| 80 | + configuration later. |
| 81 | +- `spec.roles` defines which roles that this token will grant access to. The |
| 82 | + value of `[Bot]` states that this token grants access to a Machine ID bot. |
| 83 | +- `spec.join_method` defines the join method the token is applicable for. Since |
| 84 | + this guide only focuses on Bitbucket Pipelines, you will set this to to |
| 85 | + `bitbucket`. |
| 86 | +- `spec.bitbucket.identity_provider_url` is the identity provider URL shown in |
| 87 | + the Bitbucket repository settings, under Pipelines and OpenID Connect. |
| 88 | +- `spec.bitbucket.audience` is the audience value shown in the Bitbucket |
| 89 | + repository settings, under Pipelines and OpenID connect. |
| 90 | +- `spec.bitbucket.allow` is used to set rules for what Bitbucket Pipelines runs |
| 91 | + will be able to authenticate by using the token. |
| 92 | + |
| 93 | +Refer to the [token reference](../../../reference/join-methods.mdx#bitbucket-pipelines-bitbucket) |
| 94 | +for a full list of valid fields. |
| 95 | + |
| 96 | +Apply this to your Teleport cluster using `tctl`: |
| 97 | + |
| 98 | +```code |
| 99 | +$ tctl create -f bot-token.yaml |
| 100 | +``` |
| 101 | + |
| 102 | +## Step 4/4. Configure a Bitbucket Pipelines workflow |
| 103 | + |
| 104 | +With the bot and join token created, you can now configure a workflow that can |
| 105 | +authenticate to Teleport. |
| 106 | + |
| 107 | +This example workflow defines a "custom" pipeline that can be triggered manually |
| 108 | +from "Pipelines" or "Branches" views: |
| 109 | + |
| 110 | +```yaml |
| 111 | +image: atlassian/default-image:3 |
| 112 | +
|
| 113 | +pipelines: |
| 114 | + custom: |
| 115 | + run-tbot: |
| 116 | + - step: |
| 117 | + oidc: true |
| 118 | + script: |
| 119 | + # Download and extract Teleport |
| 120 | + - wget https://cdn.teleport.dev/teleport-v(=teleport.version=)-linux-amd64-bin.tar.gz |
| 121 | + - tar -xvf teleport-v(=teleport.version=)-linux-amd64-bin.tar.gz |
| 122 | +
|
| 123 | + # Run `tbot` in identity mode for SSH access |
| 124 | + - ./teleport/tbot start identity --destination=./tbot-user --join-method=bitbucket --proxy-server=example.teleport.sh:443 --token=bot-bitbucket --oneshot |
| 125 | + |
| 126 | + # Make use of the generated SSH credentials |
| 127 | + - ssh -F ./tbot-user/ssh_config user@node.example.teleport.sh echo "hello world" |
| 128 | +``` |
| 129 | +
|
| 130 | +This example will start `tbot` in identity mode to generate SSH credentials. |
| 131 | +Refer to the [`tbot start` documentation](../../../reference/cli/tbot.mdx#tbot-start) |
| 132 | +for details on using other modes such as database, application, and Kubernetes |
| 133 | +access. |
| 134 | + |
| 135 | +If you're adapting an existing workflow, note these steps: |
| 136 | +1. Set `oidc: true` on the step properties so that step will be issued a token |
| 137 | +1. Download and extract a `.tar.gz` Teleport build |
| 138 | +1. Run `tbot` with `--join-method=bitbucket`, `--token=example-bot` (or |
| 139 | + whichever name was configured in Step 3), and `--oneshot` |
| 140 | + |
| 141 | +<Admonition type="warning" title="Sharing credentials between steps"> |
| 142 | +Note that in Bitbucket Pipelines, outputs cannot be securely shared between |
| 143 | +steps as anything stored using `artifacts` will remain downloadable once the CI |
| 144 | +run has completed. |
| 145 | + |
| 146 | +Due to this limitation, all operations making use of Teleport credentials should |
| 147 | +be performed as part of the same step. If necessary, you can duplicate the |
| 148 | +script shown here to download and run `tbot` multiple times in a given run if |
| 149 | +credentials are needed in multiple steps. |
| 150 | +</Admonition> |
| 151 | + |
| 152 | +## Further steps |
| 153 | + |
| 154 | +- Follow the [access guides](../access-guides/access-guides.mdx) to finish configuring `tbot` for |
| 155 | + your environment. |
| 156 | +- Read the [configuration reference](../../../reference/machine-id/configuration.mdx) to explore |
| 157 | + all the available configuration options. |
| 158 | +- For more information about Bitbucket Pipelines itself, read |
| 159 | + [their documentation](https://support.atlassian.com/bitbucket-cloud/docs/get-started-with-bitbucket-pipelines/). |
| 160 | + |
0 commit comments