Skip to content

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

Rework how to store the access token/refresh token for the VCS integration

天舟143efc88a584
diff --git a/api/repository.go b/api/repository.go
index 17230e14a7..e10b894b0a 100644
--- a/api/repository.go
+++ b/api/repository.go
@@ -33,6 +33,10 @@ type Repository struct {
WebhookURLHost string
WebhookEndpointId string
WebhookSecretToken string
+ // These will be exclusively used on the server side and we don't return it to the client.
+ AccessToken string
+ ExpiresTs int64
+ RefreshToken string
}
type RepositoryCreate struct {
@@ -51,9 +55,12 @@ type RepositoryCreate struct {
BaseDirectory string `jsonapi:"attr,baseDirectory"`
BranchFilter string `jsonapi:"attr,branchFilter"`
ExternalId string `jsonapi:"attr,externalId"`
- // Token belonged by the end user creating the webhook. We are accessing the end user's repository
- // so we should use her access token instead of the access token stored in VCS.
+ // Token belonged by the user linking the project to the VCS repository. We store this token together
+ // with the refresh token in the new repository record so we can use it to call VCS API on
+ // behalf of that user to perform tasks like webhook CRUD later.
AccessToken string `jsonapi:"attr,accessToken"`
+ ExpiresTs int64 `jsonapi:"attr,expiresTs"`
+ RefreshToken string `jsonapi:"attr,refreshToken"`
ExternalWebhookId string
WebhookURLHost string
WebhookEndpointId string
diff --git a/api/vcs.go b/api/vcs.go
index 1ccf1b369a..858dc71b2e 100644
--- a/api/vcs.go
+++ b/api/vcs.go
@@ -37,10 +37,6 @@ type VCS struct {
ApiURL string `jsonapi:"attr,apiURL"`
ApplicationId string `jsonapi:"attr,applicationId"`
Secret string `jsonapi:"attr,secret"`
- // These will be exclusively used on the server side (e.g. clean up orphaned webhooks) and we don't return it to the client.
- AccessToken string
- ExpiresTs int64
- RefreshToken string
}
type VCSCreate struct {
@@ -56,9 +52,6 @@ type VCSCreate struct {
ApiURL string
ApplicationId string `jsonapi:"attr,applicationId"`
Secret string `jsonapi:"attr,secret"`
- AccessToken string `jsonapi:"attr,accessToken"`
- ExpiresTs int64 `jsonapi:"attr,expiresTs"`
- RefreshToken string `jsonapi:"attr,refreshToken"`
}
type VCSFind struct {
@@ -84,9 +77,6 @@ type VCSPatch struct {
Name *string `jsonapi:"attr,name"`
ApplicationId *string `jsonapi:"attr,applicationId"`
Secret *string `jsonapi:"attr,secret"`
- AccessToken *string `jsonapi:"attr,accessToken"`
- ExpiresTs *int64 `jsonapi:"attr,expiresTs"`
- RefreshToken *string `jsonapi:"attr,refreshToken"`
}
type VCSDelete struct {
diff --git a/frontend/src/components/RepositorySetupWizard.vue b/frontend/src/components/RepositorySetupWizard.vue
index c615b53a69..6dc0b17e90 100644
--- a/frontend/src/components/RepositorySetupWizard.vue
+++ b/frontend/src/components/RepositorySetupWizard.vue
@@ -37,7 +37,6 @@ import {
RepositoryCreate,
unknown,
VCS,
- WebhookInfo,
} from "../types";
import { projectSlug, randomString } from "../utils";
@@ -129,6 +128,8 @@ export default {
branchFilter: state.config.repositoryConfig.branchFilter,
externalId: state.config.repositoryInfo.externalId,
accessToken: state.config.token.accessToken,
+ expiresTs: state.config.token.expiresTs,
+ refreshToken: state.config.token.refreshToken,
};
store
.dispatch("repository/createRepository", repositoryCreate)
diff --git a/frontend/src/components/VCSSetupWizard.vue b/frontend/src/components/VCSSetupWizard.vue
index 96c0a2c4d4..befb619470 100644
--- a/frontend/src/components/VCSSetupWizard.vue
+++ b/frontend/src/components/VCSSetupWizard.vue
@@ -73,8 +73,6 @@ export default {
const store = useStore();
const router = useRouter();
- const currentUser = computed(() => store.getters["auth/currentUser"]());
-
const state = reactive<LocalState>({
config: {
type: "GITLAB_SELF_HOST",
@@ -82,9 +80,6 @@ export default {
instanceURL: "",
applicationId: "",
secret: "",
- accessToken: "",
- expiresTs: 0,
- refreshToken: "",
},
currentStep: 0,
});
@@ -159,9 +154,6 @@ export default {
state.oAuthResultCallback = (token: OAuthToken | undefined) => {
if (token) {
state.currentStep = newStep;
- state.config.accessToken = token.accessToken;
- state.config.expiresTs = token.expiresTs;
- state.config.refreshToken = token.refreshToken;
allowChangeCallback();
store.dispatch("notification/pushNotification", {
module: "bytebase",
diff --git a/frontend/src/types/repository.ts b/frontend/src/types/repository.ts
index 01e9a041e0..40ec410b28 100644
--- a/frontend/src/types/repository.ts
+++ b/frontend/src/types/repository.ts
@@ -43,6 +43,8 @@ export type RepositoryCreate = {
branchFilter: string;
externalId: string;
accessToken: string;
+ expiresTs: number;
+ refreshToken: string;
};
export type RepositoryPatch = {
diff --git a/frontend/src/types/vcs.ts b/frontend/src/types/vcs.ts
index 477451da1e..18ea659bd2 100644
--- a/frontend/src/types/vcs.ts
+++ b/frontend/src/types/vcs.ts
@@ -10,9 +10,6 @@ export interface VCSConfig {
instanceURL: string;
applicationId: string;
secret: string;
- accessToken: string;
- expiresTs: number;
- refreshToken: string;
}
export type VCS = {
@@ -40,9 +37,6 @@ export type VCSCreate = {
instanceURL: string;
applicationId: string;
secret: string;
- accessToken: string;
- expiresTs: number;
- refreshToken: string;
};
export type VCSPatch = {
@@ -50,9 +44,6 @@ export type VCSPatch = {
name?: string;
applicationId?: string;
secret?: string;
- accessToken?: string;
- expiresTs?: number;
- refreshToken?: string;
};
export type VCSTokenCreate = {
diff --git a/frontend/src/views/SettingWorkspaceVCSDetail.vue b/frontend/src/views/SettingWorkspaceVCSDetail.vue
index 9773b0ce2f..b93c0fd757 100644
--- a/frontend/src/views/SettingWorkspaceVCSDetail.vue
+++ b/frontend/src/views/SettingWorkspaceVCSDetail.vue
@@ -251,9 +251,6 @@ export default {
if (!isEmpty(state.secret)) {
vcsPatch.secret = state.secret;
}
- vcsPatch.accessToken = token.accessToken;
- vcsPatch.expiresTs = token.expiresTs;
- vcsPatch.refreshToken = token.refreshToken;
store
.dispatch("vcs/patchVCS", {
vcsId: vcs.value.id,
diff --git a/server/project.go b/server/project.go
index 8f14c0707c..7d5782447a 100644
--- a/server/project.go
+++ b/server/project.go
@@ -319,7 +319,7 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal put request for updating webhook %s for project ID: %v", repository.ExternalWebhookId, projectId)).SetInternal(err)
}
resourcePath := fmt.Sprintf("projects/%s/hooks/%s", repository.ExternalId, repository.ExternalWebhookId)
- resp, err := gitlab.PUT(vcs.InstanceURL, resourcePath, vcs.AccessToken, bytes.NewBuffer(json))
+ resp, err := gitlab.PUT(vcs.InstanceURL, resourcePath, repository.AccessToken, bytes.NewBuffer(json))
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update webhook ID %s for project ID: %v", repository.ExternalWebhookId, projectId)).SetInternal(err)
}
@@ -394,7 +394,7 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
// If we delete it before we delete the repository, then if the repository deletion fails, we will have a broken repository with no webhook.
switch vcs.Type {
case "GITLAB_SELF_HOST":
- resp, err := gitlab.DELETE(vcs.InstanceURL, fmt.Sprintf("projects/%s/hooks/%s", repository.ExternalId, repository.ExternalWebhookId), vcs.AccessToken)
+ resp, err := gitlab.DELETE(vcs.InstanceURL, fmt.Sprintf("projects/%s/hooks/%s", repository.ExternalId, repository.ExternalWebhookId), repository.AccessToken)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete webhook ID %s for project ID: %v", repository.ExternalWebhookId, projectId)).SetInternal(err)
}
diff --git a/server/webhook.go b/server/webhook.go
index 3e0c480d30..232ce48b5a 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -80,7 +80,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
resp, err := gitlab.GET(
repository.VCS.InstanceURL,
fmt.Sprintf("projects/%s/repository/files/%s/raw?ref=%s", repository.ExternalId, url.QueryEscape(added), commit.ID),
- repository.VCS.AccessToken,
+ repository.AccessToken,
)
if err != nil {
s.l.Warn("Failed to read added repository file. Skip", zap.String("file", added), zap.Error(err))
diff --git a/store/migration/10001__init_schema.sql b/store/migration/10001__init_schema.sql
index de5074f489..03e50ed164 100644
--- a/store/migration/10001__init_schema.sql
+++ b/store/migration/10001__init_schema.sql
@@ -715,10 +715,7 @@ CREATE TABLE vcs (
AND api_url = rtrim(api_url, '/')
),
application_id TEXT NOT NULL,
- secret TEXT NOT NULL,
- access_token TEXT NOT NULL,
- expires_ts BIGINT NOT NULL,
- refresh_token TEXT NOT NULL
+ secret TEXT NOT NULL
);
INSERT INTO
@@ -778,7 +775,11 @@ CREATE TABLE repo (
-- Identify the target repository receiving the webhook event. This is a random string.
webhook_endpoint_id TEXT NOT NULL UNIQUE,
-- For GitLab, webhook request contains this in the 'X-Gitlab-Token" header and we compare it with the one stored in db to validate it sends to the expected endpoint.
- webhook_secret_token TEXT NOT NULL
+ webhook_secret_token TEXT NOT NULL,
+ -- access_token, expires_ts, refresh_token belongs to the user linking the project to the VCS repository.
+ access_token TEXT NOT NULL,
+ expires_ts BIGINT NOT NULL,
+ refresh_token TEXT NOT NULL
);
INSERT INTO
diff --git a/store/repository.go b/store/repository.go
index f03ff57abc..a9be18d2d1 100644
--- a/store/repository.go
+++ b/store/repository.go
@@ -155,10 +155,13 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
external_webhook_id,
webhook_url_host,
webhook_endpoint_id,
- webhook_secret_token
+ webhook_secret_token,
+ access_token,
+ expires_ts,
+ refresh_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, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_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, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token, access_token, expires_ts, refresh_token
`,
create.CreatorId,
create.CreatorId,
@@ -174,6 +177,9 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
create.WebhookURLHost,
create.WebhookEndpointId,
create.WebhookSecretToken,
+ create.AccessToken,
+ create.ExpiresTs,
+ create.RefreshToken,
)
if err != nil {
@@ -201,6 +207,9 @@ func (s *RepositoryService) createRepository(ctx context.Context, tx *Tx, create
&repository.WebhookURLHost,
&repository.WebhookEndpointId,
&repository.WebhookSecretToken,
+ &repository.AccessToken,
+ &repository.ExpiresTs,
+ &repository.RefreshToken,
); err != nil {
return nil, FormatError(err)
}
@@ -242,7 +251,10 @@ func findRepositoryList(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
external_webhook_id,
webhook_url_host,
webhook_endpoint_id,
- webhook_secret_token
+ webhook_secret_token,
+ access_token,
+ expires_ts,
+ refresh_token
FROM repo
WHERE `+strings.Join(where, " AND "),
args...,
@@ -274,6 +286,9 @@ func findRepositoryList(ctx context.Context, tx *Tx, find *api.RepositoryFind) (
&repository.WebhookURLHost,
&repository.WebhookEndpointId,
&repository.WebhookSecretToken,
+ &repository.AccessToken,
+ &repository.ExpiresTs,
+ &repository.RefreshToken,
); err != nil {
return nil, FormatError(err)
}
@@ -305,7 +320,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, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token
+ 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, external_webhook_id, webhook_url_host, webhook_endpoint_id, webhook_secret_token, access_token, expires_ts, refresh_token
`,
args...,
)
@@ -334,6 +349,9 @@ func patchRepository(ctx context.Context, tx *Tx, patch *api.RepositoryPatch) (*
&repository.WebhookURLHost,
&repository.WebhookEndpointId,
&repository.WebhookSecretToken,
+ &repository.AccessToken,
+ &repository.ExpiresTs,
+ &repository.RefreshToken,
); err != nil {
return nil, FormatError(err)
}
diff --git a/store/seed/test/10160__vcs.sql b/store/seed/test/10160__vcs.sql
index 8f663cb2ae..cfeba8a4bf 100644
--- a/store/seed/test/10160__vcs.sql
+++ b/store/seed/test/10160__vcs.sql
@@ -9,10 +9,7 @@ INSERT INTO
instance_url,
api_url,
application_id,
- secret,
- access_token,
- expires_ts,
- refresh_token
+ secret
)
VALUES
(
@@ -23,9 +20,6 @@ VALUES
'GITLAB_SELF_HOST',
'https://gitlab.bytebase.com',
'https://gitlab.bytebase.com/api/v4',
- '0d1f706e68e3bf22be6712752726a3eec2a9684ec098a9695015f484165ed922',
- 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
- 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
- 0,
- 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
+ 'fda62e44b5388b1ca6e72d5a7028a3c2c47157fc13fd98328e2bd446fae98fd8',
+ '4ac5cf6f2e400398e34f753a5222432838e84349e8d5120c4adaa6a65278b765'
);
\ No newline at end of file
diff --git a/store/seed/test/10170__repo.sql b/store/seed/test/10170__repo.sql
index 7143f39795..07e9df5eb1 100644
--- a/store/seed/test/10170__repo.sql
+++ b/store/seed/test/10170__repo.sql
@@ -15,7 +15,10 @@ INSERT INTO
external_webhook_id,
webhook_url_host,
webhook_endpoint_id,
- webhook_secret_token
+ webhook_secret_token,
+ access_token,
+ expires_ts,
+ refresh_token
)
VALUES
(
@@ -35,7 +38,10 @@ VALUES
"60",
"https://demo.bytebase.com",
"e99bf622-7f58-4d6b-a5be-b97af313d7ea",
- "xxxxxxxxxxxxxxxx"
+ "xxxxxxxxxxxxxxxx",
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
+ 0,
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
);
INSERT INTO
@@ -54,7 +60,10 @@ INSERT INTO
external_webhook_id,
webhook_url_host,
webhook_endpoint_id,
- webhook_secret_token
+ webhook_secret_token,
+ access_token,
+ expires_ts,
+ refresh_token
)
VALUES
(
@@ -74,5 +83,8 @@ VALUES
"61",
"https://demo.bytebase.com",
"c5e30130-7322-4f84-953a-d08168c047d1",
- "xxxxxxxxxxxxxxxx"
+ "xxxxxxxxxxxxxxxx",
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
+ 0,
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
);
\ No newline at end of file
diff --git a/store/vcs.go b/store/vcs.go
index 249f3f2398..6d305726ac 100644
--- a/store/vcs.go
+++ b/store/vcs.go
@@ -136,13 +136,10 @@ func createVCS(ctx context.Context, tx *Tx, create *api.VCSCreate) (*api.VCS, er
instance_url,
api_url,
application_id,
- secret,
- access_token,
- expires_ts,
- refresh_token
+ secret
)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, name, `+"`type`, instance_url, api_url, application_id, secret, access_token, expires_ts, refresh_token"+`
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, name, `+"`type`, instance_url, api_url, application_id, secret"+`
`,
create.CreatorId,
create.CreatorId,
@@ -152,9 +149,6 @@ func createVCS(ctx context.Context, tx *Tx, create *api.VCSCreate) (*api.VCS, er
create.ApiURL,
create.ApplicationId,
create.Secret,
- create.AccessToken,
- create.ExpiresTs,
- create.RefreshToken,
)
if err != nil {
@@ -176,9 +170,6 @@ func createVCS(ctx context.Context, tx *Tx, create *api.VCSCreate) (*api.VCS, er
&vcs.ApiURL,
&vcs.ApplicationId,
&vcs.Secret,
- &vcs.AccessToken,
- &vcs.ExpiresTs,
- &vcs.RefreshToken,
); err != nil {
return nil, FormatError(err)
}
@@ -205,10 +196,7 @@ func findVCSList(ctx context.Context, tx *Tx, find *api.VCSFind) (_ []*api.VCS,
instance_url,
api_url,
application_id,
- secret,
- access_token,
- expires_ts,
- refresh_token
+ secret
FROM vcs
WHERE `+strings.Join(where, " AND "),
args...,
@@ -234,9 +222,6 @@ func findVCSList(ctx context.Context, tx *Tx, find *api.VCSFind) (_ []*api.VCS,
&vcs.ApiURL,
&vcs.ApplicationId,
&vcs.Secret,
- &vcs.AccessToken,
- &vcs.ExpiresTs,
- &vcs.RefreshToken,
); err != nil {
return nil, FormatError(err)
}
@@ -263,15 +248,6 @@ func patchVCS(ctx context.Context, tx *Tx, patch *api.VCSPatch) (*api.VCS, error
if v := patch.Secret; v != nil {
set, args = append(set, "secret = ?"), append(args, *v)
}
- if v := patch.AccessToken; v != nil {
- set, args = append(set, "access_token = ?"), append(args, *v)
- }
- if v := patch.ExpiresTs; v != nil {
- set, args = append(set, "expires_ts = ?"), append(args, *v)
- }
- if v := patch.RefreshToken; v != nil {
- set, args = append(set, "refresh_token = ?"), append(args, *v)
- }
args = append(args, patch.ID)
// Execute update query with RETURNING.
@@ -279,7 +255,7 @@ func patchVCS(ctx context.Context, tx *Tx, patch *api.VCSPatch) (*api.VCS, error
UPDATE vcs
SET `+strings.Join(set, ", ")+`
WHERE id = ?
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, name, `+"`type`, instance_url, api_url, application_id, secret, access_token, expires_ts, refresh_token"+`
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, name, `+"`type`, instance_url, api_url, application_id, secret"+`
`,
args...,
)
@@ -302,9 +278,6 @@ func patchVCS(ctx context.Context, tx *Tx, patch *api.VCSPatch) (*api.VCS, error
&vcs.ApiURL,
&vcs.ApplicationId,
&vcs.Secret,
- &vcs.AccessToken,
- &vcs.ExpiresTs,
- &vcs.RefreshToken,
); err != nil {
return nil, FormatError(err)
}