|
| 1 | +--- |
| 2 | +title: 'Just-in-Time Database Access - Part 2' |
| 3 | +author: Ningjing |
| 4 | +tags: Tutorial |
| 5 | +updated_at: 2024/12/11 18:15 |
| 6 | +integrations: General |
| 7 | +level: Advanced |
| 8 | +estimated_time: '30 mins' |
| 9 | +description: 'In this tutorial, we will demonstrate how to automate JIT access via Slack, utilizing Bytebase webhooks and API' |
| 10 | +--- |
| 11 | + |
| 12 | +In the [previous tutorial](/docs/tutorials/just-in-time-part1), we demonstrated how to set up JIT access using the Bytebase GUI. In this tutorial, we will cover how to automate JIT access via Slack, utilizing Bytebase webhooks and API. |
| 13 | + |
| 14 | +When developers need urgent production database access during incidents but lack permissions, they can request Just-in-Time (JIT) access. By triggering the Bytebase webhook, the DBA will receive a notification in Slack immediately and can approve there. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +This is Part 2 of our tutorial series on implementing Just-in-Time (JIT) access: |
| 19 | + |
| 20 | +- Part 1: [Grant JIT database access via Bytebase GUI](/docs/tutorials/just-in-time-part1) |
| 21 | +- Part 2: Approve JIT database access via Slack (this one) |
| 22 | +- Part 3: Request JIT database access via Slack |
| 23 | + |
| 24 | +## Overview |
| 25 | + |
| 26 | +In this tutorial, you'll learn how to approve JIT access via Slack with the help of Bytebase webhooks and APIs. |
| 27 | + |
| 28 | +<HintBlock type="info"> |
| 29 | + |
| 30 | +The complete code for this tutorial is available at: [slack-example](https://github.com/bytebase/slack-example) |
| 31 | + |
| 32 | +</HintBlock> |
| 33 | + |
| 34 | +This tutorial skips the Bytebase setup part, if you haven't set up the Bytebase and registered admin and developer users, please follow instructions in the [previous tutorial](/docs/tutorials/just-in-time-part1). |
| 35 | + |
| 36 | +## Prerequisites |
| 37 | + |
| 38 | +Before you begin, make sure you have: |
| 39 | + |
| 40 | +- Docker installed |
| 41 | +- Slack workspace |
| 42 | +- VS Code for port forwarding |
| 43 | + |
| 44 | +## Step 1 - Finished the previous tutorial |
| 45 | + |
| 46 | +Make sure you finished the [previous tutorial](/docs/tutorials/just-in-time-part1) and have the Bytebase instance running. Particularly, pay attention to **Step 4**, which is to request JIT access via Bytebase GUI. |
| 47 | + |
| 48 | +The `Request role` feature is supported by **Enterprise Plan** which will be needed for this tutorial, other plans only allow the `Assign role` feature which is not enough. You may request a trial from [here](https://www.bytebase.com/contact-us/). |
| 49 | + |
| 50 | +## Step 2 - Register a service account in Bytebase |
| 51 | + |
| 52 | +<IncludeBlock url="/docs/share/tutorials/create-service-account"></IncludeBlock> |
| 53 | + |
| 54 | +3. Go to `Sample Project`, click **Manage > Members**, and assign the service account as `Project Owner` which can fit the custom approval set in the previous tutorial. |
| 55 | + |
| 56 | +## Step 3 - Download `slack-example` code and run it |
| 57 | + |
| 58 | +1. Download the [slack-example code](https://github.com/bytebase/slack-example). |
| 59 | +1. Go to the `approve-issue` folder and copy the `env-template.local` file to `.env.local`. |
| 60 | +1. Paste the registered service account information into the `.env.local` file. |
| 61 | +1. By using VS Code's [Port forwarding](https://code.visualstudio.com/docs/editor/port-forwarding), you can forward the local server's ports: |
| 62 | + - `3000` for the `slack-example` app |
| 63 | + - `8080` for the Bytebase instance |
| 64 | +  |
| 65 | +1. Copy the 8080 port forwarded address to the `.env.local` file as `BB_HOST`. |
| 66 | +1. Also, go to Bytebase, click **Settings > General** to set the address as **External URL**. |
| 67 | + |
| 68 | +## Step 4 - Create Bytebase Webhook |
| 69 | + |
| 70 | +1. Go to Bytebase and select the `Sample Project`. |
| 71 | +1. Click **Integration > Webhooks** and click **Add Webhook**. |
| 72 | +1. Set the **Name** as `Slack` webhook, **URL** as `YOUR_3000_FORWARDED_URL/api/bytebase/webhook`. |
| 73 | +1. Select `Issue approval needed` as **Triggering activities**. |
| 74 | +1. Click **Test webhook** and if it's successful, then click **Create**. |
| 75 | +  |
| 76 | + |
| 77 | +## Step 5 - Create and invite a Slack bot |
| 78 | + |
| 79 | +1. Go to [Slack apps](https://api.slack.com/apps) and click **Create New App**. |
| 80 | +1. Choose **From scratch**, enter the **App name**, and select your **Workspace**. |
| 81 | +1. Go to **OAuth & Permissions** and add the following permissions under **Scopes**: |
| 82 | + - `chat:write` |
| 83 | + - `chat:write.public` |
| 84 | +1. Scroll up to **OAuth Tokens**, click **Install to YOUR_WORKSPACE**, and authorize the app. |
| 85 | +1. Copy the **Bot User OAuth Token** and paste it into the `.env.local` file as **SLACK_BOT_TOKEN**. |
| 86 | +1. Choose a channel and invite the bot to the channel by typing `/invite @YOUR_BOT_NAME`. |
| 87 | +1. Get the **Channel ID** via copying the channel link and extracting the ID from the URL. Copy and paste it into the `.env.local` file as **SLACK_CHANNEL_ID**. |
| 88 | +1. Go to **Interactivity & Shortcuts** in app settings, turn on **Interactivity** and add the **Request URL**: `YOUR_3000_FORWARDED_URL/api/slack/interact`. Click **Save Changes**. |
| 89 | + |
| 90 | +## Step 6 - Verify the workflow |
| 91 | + |
| 92 | +Now, everything is ready, let's verify the workflow: |
| 93 | + |
| 94 | +1. Go to Bytebase, log in as the developer and go into the `Sample Project`. |
| 95 | +1. By default, the developer has no permission to access the database. Click **Manage > Members** and you'll see the devloper only has **Project Developer** role. If you go to **SQL Editor**, you'll see the `hr_prod` database is not accessible. |
| 96 | +1. Click **Database > databases**, select the `hr_prod` database, and click **Request Querier role**. |
| 97 | +  |
| 98 | +1. Choose the database or table you want to access, and click **OK**. |
| 99 | +  |
| 100 | +1. A request issue is created, the configured custom approval flow will be matched. |
| 101 | +  |
| 102 | +1. Go to Slack, the bot already sent a message to the channel, which is triggered by the webhook. |
| 103 | +  |
| 104 | +1. Click **Approve** and the Slack Bot will trigger the interact API, which calls Bytebase API to approve the issue. |
| 105 | +  |
| 106 | +1. Go back to Bytebase, the issue is approved. The developer can access the database now. |
| 107 | +  |
| 108 | + |
| 109 | +## Code structure |
| 110 | + |
| 111 | +If digging into the code is your interest, here is a brief explanation of the code structure: |
| 112 | + |
| 113 | +- `src/app/api/bytebase/webhook/route.ts`: handle the webhook from Bytebase. |
| 114 | +- `src/app/api/slack/interact/route.ts`: handle the interaction from Slack. |
| 115 | +- `src/lib/slack.ts`: send the message to Slack via using the its [web API](https://tools.slack.dev/node-slack-sdk/web-api/). |
| 116 | + |
| 117 | +## Conclusion |
| 118 | + |
| 119 | +In this tutorial, you learned how to set up JIT access via Slack with the help of Bytebase webhooks and APIs. In the next part, we will cover how to request JIT access via Slack. |
0 commit comments