Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Support secret token for gitlab webhook

天舟e391314e3bff
diff --git a/backend/api/repository.go b/backend/api/repository.go
index 98c2f788ff..dcc34ced8e 100644
--- a/backend/api/repository.go
+++ b/backend/api/repository.go
@@ -31,6 +31,7 @@ type Repository struct {
ExternalId string `jsonapi:"attr,externalId"`
WebhookId string
WebhookURL string
+ SecretToken string
}
type RepositoryCreate struct {
@@ -51,6 +52,7 @@ type RepositoryCreate struct {
ExternalId string `jsonapi:"attr,externalId"`
WebhookId string `jsonapi:"attr,webhookId"`
WebhookURL string `jsonapi:"attr,webhookURL"`
+ SecretToken string `jsonapi:"attr,secretToken"`
}
type RepositoryFind struct {
diff --git a/backend/store/migration/0001_init_schema.up.sql b/backend/store/migration/0001_init_schema.up.sql
index 1870f40725..7bd9853977 100644
--- a/backend/store/migration/0001_init_schema.up.sql
+++ b/backend/store/migration/0001_init_schema.up.sql
@@ -720,7 +720,9 @@ CREATE TABLE repo (
-- For GitLab, this is the project webhook id. e.g. 123
webhook_id TEXT NOT NULL,
-- Webhook URL, we use this to identify the target repository receiving the webhook event
- webhook_url TEXT NOT NULL UNIQUE
+ webhook_url TEXT NOT NULL UNIQUE,
+ -- For GitLab, webhook request contains this in the 'X-Gitlab-Token" header and we can compare it with the one stored in db to validate it sends to the expected endpoint.
+ secret_token TEXT NOT NULL
);
CREATE INDEX idx_repo_webhook_url ON repo(webhook_url);
diff --git a/backend/store/repository.go b/backend/store/repository.go
index d607fe2e62..76c9880dbb 100644
--- a/backend/store/repository.go
+++ b/backend/store/repository.go
@@ -153,10 +153,11 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
branch_filter,
external_id,
webhook_id,
- webhook_url
+ webhook_url,
+ secret_token
)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, base_directory, branch_filter, external_id, webhook_id, webhook_url
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, base_directory, branch_filter, external_id, webhook_id, webhook_url, secret_token
`,
create.CreatorId,
create.CreatorId,
@@ -170,6 +171,7 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
create.ExternalId,
create.WebhookId,
create.WebhookURL,
+ create.SecretToken,
)
if err != nil {
@@ -195,6 +197,7 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
&repository.ExternalId,
&repository.WebhookId,
&repository.WebhookURL,
+ &repository.SecretToken,
); err != nil {
return nil, FormatError(err)
}
@@ -231,7 +234,8 @@ func findRepositoryList(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
branch_filter,
external_id,
webhook_id,
- webhook_url
+ webhook_url,
+ secret_token
FROM repo
WHERE `+strings.Join(where, " AND "),
args...,
@@ -261,6 +265,7 @@ func findRepositoryList(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
&repository.ExternalId,
&repository.WebhookId,
&repository.WebhookURL,
+ &repository.SecretToken,
); err != nil {
return nil, FormatError(err)
}
@@ -292,7 +297,7 @@ func patchRepository(ctx context.Context, tx *Tx, patch *api.RepositoryPatch) (*
UPDATE repo
SET `+strings.Join(set, ", ")+`
WHERE id = ?
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, base_directory, branch_filter, external_id, webhook_id, webhook_url
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, vcs_id, project_id, name, full_path, web_url, base_directory, branch_filter, external_id, webhook_id, webhook_url, secret_token
`,
args...,
)
@@ -319,6 +324,7 @@ func patchRepository(ctx context.Context, tx *Tx, patch *api.RepositoryPatch) (*
&repository.ExternalId,
&repository.WebhookId,
&repository.WebhookURL,
+ &repository.SecretToken,
); err != nil {
return nil, FormatError(err)
}
diff --git a/backend/store/seed/0170_repo.sql b/backend/store/seed/0170_repo.sql
index dc274f4489..f2fcb72ca2 100644
--- a/backend/store/seed/0170_repo.sql
+++ b/backend/store/seed/0170_repo.sql
@@ -12,7 +12,8 @@ INSERT INTO
branch_filter,
external_id,
webhook_id,
- webhook_url
+ webhook_url,
+ secret_token
)
VALUES
(
@@ -30,5 +31,6 @@ VALUES
"7",
-- Refers to the webhook in bytebase-test/project1
"5",
- "http://gitlab.bytebase.com/hook/gitlab/8368dde4-e352-4696-9fef-26bd3af6ec40"
+ "http://gitlab.bytebase.com/hook/gitlab/8368dde4-e352-4696-9fef-26bd3af6ec40",
+ "VFN2lgKDRLWjJ25B"
);
\ No newline at end of file
diff --git a/frontend/src/components/RepositorySetupWizard.vue b/frontend/src/components/RepositorySetupWizard.vue
index 0f864994b4..a2253df09e 100644
--- a/frontend/src/components/RepositorySetupWizard.vue
+++ b/frontend/src/components/RepositorySetupWizard.vue
@@ -38,7 +38,7 @@ import {
VCS,
WebhookInfo,
} from "../types";
-import { projectSlug } from "../utils";
+import { projectSlug, randomString } from "../utils";
const CHOOSE_PROVIDER_STEP = 0;
const CHOOSE_REPOSITORY_STEP = 1;
@@ -117,13 +117,16 @@ export default {
};
const tryFinishSetup = (allowFinishCallback: () => void) => {
+ // The secret token to validate the gitlab webhook sender.
+ const secretToken = randomString(16);
const createFunc = () => {
store
.dispatch("gitlab/createWebhook", {
vcs: state.config.vcs,
projectId: state.config.repositoryInfo.externalId,
branchFilter: state.config.repositoryConfig.branchFilter,
- token: state.config.token.accessToken,
+ secretToken,
+ accessToken: state.config.token.accessToken,
})
.then((webhook: WebhookInfo) => {
const repositoryCreate: RepositoryCreate = {
@@ -137,6 +140,7 @@ export default {
externalId: state.config.repositoryInfo.externalId,
webhookId: webhook.id,
webhookURL: webhook.url,
+ secretToken,
};
store
.dispatch("repository/createRepository", repositoryCreate)
diff --git a/frontend/src/store/modules/gitlab.ts b/frontend/src/store/modules/gitlab.ts
index 489ad3e31d..5386cb4c2e 100644
--- a/frontend/src/store/modules/gitlab.ts
+++ b/frontend/src/store/modules/gitlab.ts
@@ -84,8 +84,15 @@ const actions = {
vcs,
projectId,
branchFilter,
- token,
- }: { vcs: VCS; projectId: string; branchFilter: string; token: string }
+ secretToken,
+ accessToken,
+ }: {
+ vcs: VCS;
+ projectId: string;
+ branchFilter: string;
+ secretToken: string;
+ accessToken: string;
+ }
): Promise<WebhookInfo> {
const url = `${vcs.instanceURL}/${GITLAB_WEBHOOK_PATH}/${uuidv4()}`;
const data = (
@@ -93,6 +100,7 @@ const actions = {
`${vcs.instanceURL}/${GITLAB_API_PATH}/projects/${projectId}/hooks`,
{
url,
+ token: secretToken,
push_events: true,
// For now, there is no native dry run DDL support in mysql/postgres. One may wonder if we could wrap the DDL
// in a transaction and just not commit at the end, unfortunately there are side effects which are hard to control.
@@ -106,7 +114,7 @@ const actions = {
},
{
headers: {
- Authorization: "Bearer " + token,
+ Authorization: "Bearer " + accessToken,
},
}
)
diff --git a/frontend/src/types/repository.ts b/frontend/src/types/repository.ts
index b56d334ca6..a20f3e8124 100644
--- a/frontend/src/types/repository.ts
+++ b/frontend/src/types/repository.ts
@@ -44,6 +44,7 @@ export type RepositoryCreate = {
externalId: string;
webhookId: string;
webhookURL: string;
+ secretToken: string;
};
export type RepositoryPatch = {