Skip to content

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

First owner account signup

天舟a27bf8dd2ad6
diff --git a/api/actuator.go b/api/actuator.go
index 410a536685..15f956de5d 100644
--- a/api/actuator.go
+++ b/api/actuator.go
@@ -2,10 +2,11 @@ package api
// Actuator concept is similar to the Spring Boot Actuator
type ServerInfo struct {
- Version string `json:"version"`
- Readonly bool `json:"readonly"`
- Demo bool `json:"demo"`
- Host string `json:"host"`
- Port string `json:"port"`
- StartedTs int64 `json:"startedTs"`
+ Version string `json:"version"`
+ Readonly bool `json:"readonly"`
+ Demo bool `json:"demo"`
+ Host string `json:"host"`
+ Port string `json:"port"`
+ NeedAdminSetup bool `json:"needAdminSetup"`
+ StartedTs int64 `json:"startedTs"`
}
diff --git a/api/member.go b/api/member.go
index e69ea890bc..a1fd82012d 100644
--- a/api/member.go
+++ b/api/member.go
@@ -79,6 +79,7 @@ type MemberFind struct {
// Domain specific fields
PrincipalId *int
+ Role *Role
}
func (find *MemberFind) String() string {
diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index 1a3b974ebe..4af70e56d0 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -1,5 +1,14 @@
<template>
- <router-view />
+ <Suspense>
+ <template #default>
+ <ProvideAppContext>
+ <router-view />
+ </ProvideAppContext>
+ </template>
+ <template #fallback>
+ <span class="items-center flex">Loading...</span>
+ </template>
+ </Suspense>
<template v-if="state.notificationList.length > 0">
<BBNotification
:placement="'BOTTOM_RIGHT'"
@@ -13,7 +22,7 @@
import { reactive, watchEffect, onErrorCaptured } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
-import ProvideDashboardContext from "./components/ProvideDashboardContext.vue";
+import ProvideAppContext from "./components/ProvideAppContext.vue";
import { isDev } from "./utils";
import { Notification } from "./types";
import { BBNotificationItem } from "./bbkit/types";
@@ -31,7 +40,7 @@ interface LocalState {
export default {
name: "App",
- components: { ProvideDashboardContext },
+ components: { ProvideAppContext },
setup(props, ctx) {
const store = useStore();
const router = useRouter();
diff --git a/frontend/src/components/ProvideAppContext.vue b/frontend/src/components/ProvideAppContext.vue
new file mode 100644
index 0000000000..aa3891afeb
--- /dev/null
+++ b/frontend/src/components/ProvideAppContext.vue
@@ -0,0 +1,16 @@
+<template>
+ <slot />
+</template>
+
+<script lang="ts">
+import { useStore } from "vuex";
+
+export default {
+ name: "ProvideAppContext",
+ async setup() {
+ const store = useStore();
+
+ await Promise.all([store.dispatch("actuator/info")]);
+ },
+};
+</script>
diff --git a/frontend/src/components/ProvideDashboardContext.vue b/frontend/src/components/ProvideDashboardContext.vue
index 1a54c56046..76e186b08e 100644
--- a/frontend/src/components/ProvideDashboardContext.vue
+++ b/frontend/src/components/ProvideDashboardContext.vue
@@ -19,7 +19,6 @@ export default {
await Promise.all([
store.dispatch("plan/fetchCurrentPlan"),
- store.dispatch("actuator/info"),
// Fetch member and principal list so PrincipalSelect can have the data.
store.dispatch("member/fetchMemberList"),
store.dispatch("principal/fetchPrincipalList"),
diff --git a/frontend/src/store/modules/actuator.ts b/frontend/src/store/modules/actuator.ts
index 3c152d1189..35dd3c2afc 100644
--- a/frontend/src/store/modules/actuator.ts
+++ b/frontend/src/store/modules/actuator.ts
@@ -18,6 +18,9 @@ const getters = {
isReadonly: (state: ActuatorState) => (): boolean => {
return state.serverInfo?.readonly || false;
},
+ needAdminSetup: (state: ActuatorState) => (): boolean => {
+ return state.serverInfo?.needAdminSetup || false;
+ },
};
const actions = {
diff --git a/frontend/src/types/actuator.ts b/frontend/src/types/actuator.ts
index f47c1affb8..33411a5593 100644
--- a/frontend/src/types/actuator.ts
+++ b/frontend/src/types/actuator.ts
@@ -4,5 +4,6 @@ export type ServerInfo = {
demo: boolean;
host: string;
port: string;
+ needAdminSetup: boolean;
startedTs: number;
};
diff --git a/frontend/src/views/auth/Signup.vue b/frontend/src/views/auth/Signup.vue
index a28ad3cf30..b1d20cefa4 100644
--- a/frontend/src/views/auth/Signup.vue
+++ b/frontend/src/views/auth/Signup.vue
@@ -3,7 +3,11 @@
<div>
<img class="h-12 w-auto" src="../../assets/logo.svg" alt="Bytebase" />
<h2 class="mt-6 text-3xl leading-9 font-extrabold text-main">
- Register your account
+ <template v-if="needAdminSetup" class="text-accent font-semnibold">
+ Setup
+ <span class="text-accent font-semnibold">admin account</span>
+ </template>
+ <template v-else> Register your account </template>
</h2>
</div>
@@ -15,7 +19,7 @@
for="email"
class="block text-sm font-medium leading-5 text-control"
>
- Email<span class="text-red-600">*</span>
+ Email <span class="text-red-600">*</span>
</label>
<div class="mt-1 rounded-md shadow-sm">
<input
@@ -36,8 +40,7 @@
focus:outline-none
focus:shadow-outline-blue
focus:border-control-border
- sm:text-sm
- sm:leading-5
+ sm:text-sm sm:leading-5
"
@input="onTextEmail"
/>
@@ -49,14 +52,15 @@
for="password"
class="block text-sm font-medium leading-5 text-control"
>
- Password<span class="text-red-600">*</span>
+ Password <span class="text-red-600">*</span>
</label>
<div class="mt-1 rounded-md shadow-sm">
<input
id="password"
type="password"
- autocomplete="on"
- v-model="state.password"
+ autocomplete="off"
+ :value="state.password"
+ @input="changePassword($event.target.value)"
required
class="
appearance-none
@@ -70,8 +74,44 @@
focus:outline-none
focus:shadow-outline-blue
focus:border-control-border
- sm:text-sm
- sm:leading-5
+ sm:text-sm sm:leading-5
+ "
+ />
+ </div>
+ </div>
+
+ <div>
+ <label
+ for="password-confirm"
+ class="block text-sm font-medium leading-5 text-control"
+ >
+ Confirm Password
+ <span class="text-red-600"
+ >* {{ state.showPasswordMismatchError ? "mismatch" : "" }}</span
+ >
+ </label>
+ <div class="mt-1 rounded-md shadow-sm">
+ <input
+ id="password-confirm"
+ type="password"
+ autocomplete="off"
+ :placeholder="'Confirm password'"
+ :value="state.passwordConfirm"
+ @input="changePasswordConfirm($event.target.value)"
+ required
+ class="
+ appearance-none
+ block
+ w-full
+ px-3
+ py-2
+ border border-control-border
+ rounded-md
+ placeholder-control-placeholder
+ focus:outline-none
+ focus:shadow-outline-blue
+ focus:border-control-border
+ sm:text-sm sm:leading-5
"
/>
</div>
@@ -102,8 +142,7 @@
focus:outline-none
focus:shadow-outline-blue
focus:border-control-border
- sm:text-sm
- sm:leading-5
+ sm:text-sm sm:leading-5
"
@input="onTextName"
/>
@@ -117,7 +156,7 @@
:disabled="!allowSignup"
class="btn-primary w-full flex justify-center py-2 px-4"
>
- Register
+ {{ needAdminSetup ? "Create admin account" : "Register" }}
</button>
</span>
</div>
@@ -125,7 +164,7 @@
</div>
</div>
- <div class="mt-6 relative">
+ <div v-if="!needAdminSetup" class="mt-6 relative">
<div class="absolute inset-0 flex items-center" aria-hidden="true">
<div class="w-full border-t border-control-border"></div>
</div>
@@ -142,15 +181,18 @@
</template>
<script lang="ts">
-import { computed, reactive } from "vue";
+import { computed, onUnmounted, reactive } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
-import { SignupInfo } from "../../types";
+import { SignupInfo, TEXT_VALIDATION_DELAY } from "../../types";
import { isValidEmail } from "../../utils";
interface LocalState {
email: string;
password: string;
+ passwordConfirm: string;
+ passwordValidationTimer?: ReturnType<typeof setTimeout>;
+ showPasswordMismatchError: boolean;
name: string;
nameManuallyEdited: boolean;
}
@@ -164,14 +206,67 @@ export default {
const state = reactive<LocalState>({
email: "",
password: "",
+ passwordConfirm: "",
+ showPasswordMismatchError: false,
name: "",
nameManuallyEdited: false,
});
+ onUnmounted(() => {
+ if (state.passwordValidationTimer) {
+ clearInterval(state.passwordValidationTimer);
+ }
+ });
+
+ const needAdminSetup = computed(() => {
+ return store.getters["actuator/needAdminSetup"]();
+ });
+
const allowSignup = computed(() => {
- return isValidEmail(state.email) && state.password;
+ return (
+ isValidEmail(state.email) &&
+ state.password &&
+ !state.showPasswordMismatchError
+ );
+ });
+
+ const passwordMatch = computed(() => {
+ return state.password == state.passwordConfirm;
});
+ const refreshPasswordValidation = () => {
+ if (state.passwordValidationTimer) {
+ clearInterval(state.passwordValidationTimer);
+ }
+
+ if (passwordMatch.value) {
+ state.showPasswordMismatchError = false;
+ } else {
+ state.passwordValidationTimer = setTimeout(() => {
+ // If error is already displayed, we hide the error only if there is valid input.
+ // Otherwise, we hide the error if input is either empty or valid.
+ if (state.showPasswordMismatchError) {
+ state.showPasswordMismatchError = !passwordMatch.value;
+ } else {
+ state.showPasswordMismatchError =
+ state.password != "" &&
+ state.passwordConfirm != "" &&
+ !passwordMatch.value;
+ }
+ }, TEXT_VALIDATION_DELAY);
+ }
+ };
+
+ const changePassword = (value: string) => {
+ state.password = value;
+ refreshPasswordValidation();
+ };
+
+ const changePasswordConfirm = (value: string) => {
+ state.passwordConfirm = value;
+ refreshPasswordValidation();
+ };
+
const onTextEmail = () => {
const email = state.email.trim();
if (!state.nameManuallyEdited) {
@@ -199,19 +294,26 @@ export default {
};
const trySignup = () => {
- const signupInfo: SignupInfo = {
- email: state.email,
- password: state.password,
- name: state.name,
- };
- store.dispatch("auth/signup", signupInfo).then(() => {
- router.push("/");
- });
+ if (!passwordMatch.value) {
+ state.showPasswordMismatchError = true;
+ } else {
+ const signupInfo: SignupInfo = {
+ email: state.email,
+ password: state.password,
+ name: state.name,
+ };
+ store.dispatch("auth/signup", signupInfo).then(() => {
+ router.push("/");
+ });
+ }
};
return {
state,
+ needAdminSetup,
allowSignup,
+ changePassword,
+ changePasswordConfirm,
onTextEmail,
onTextName,
trySignup,
diff --git a/server/acl.go b/server/acl.go
index e2766c193b..1e5dbe729d 100644
--- a/server/acl.go
+++ b/server/acl.go
@@ -25,8 +25,8 @@ func GetRoleContextKey() string {
func ACLMiddleware(l *zap.Logger, s *Server, ce *casbin.Enforcer, next echo.HandlerFunc, readonly bool) echo.HandlerFunc {
return func(c echo.Context) error {
- // Skips auth and plan
- if strings.HasPrefix(c.Path(), "/api/auth") || strings.HasPrefix(c.Path(), "/api/plan") {
+ // Skips auth, actuator and plan
+ if strings.HasPrefix(c.Path(), "/api/auth") || strings.HasPrefix(c.Path(), "/api/actuator") || strings.HasPrefix(c.Path(), "/api/plan") {
return next(c)
}
diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csv
index bcd11dce5a..a099dd0aaf 100644
--- a/server/acl_casbin_policy_dba.csv
+++ b/server/acl_casbin_policy_dba.csv
@@ -1,4 +1,3 @@
-p, DBA, /actuator/info, GET
p, DBA, /principal, GET
p, DBA, /principal/{id}, GET
p, DBA, /principal/{id}, PATCH_SELF
diff --git a/server/acl_casbin_policy_developer.csv b/server/acl_casbin_policy_developer.csv
index eed327ef2d..102dd23012 100644
--- a/server/acl_casbin_policy_developer.csv
+++ b/server/acl_casbin_policy_developer.csv
@@ -1,4 +1,3 @@
-p, DEVELOPER, /actuator/info, GET
p, DEVELOPER, /principal, GET
p, DEVELOPER, /principal/{id}, GET
p, DEVELOPER, /principal/{id}, PATCH_SELF
diff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csv
index bf2881d16b..b90ee5e887 100644
--- a/server/acl_casbin_policy_owner.csv
+++ b/server/acl_casbin_policy_owner.csv
@@ -1,4 +1,3 @@
-p, OWNER, /actuator/info, GET
p, OWNER, /principal, POST
p, OWNER, /principal, GET
p, OWNER, /principal/{id}, GET
diff --git a/server/actuator.go b/server/actuator.go
index 792fc27b9d..e0116714cd 100644
--- a/server/actuator.go
+++ b/server/actuator.go
@@ -1,6 +1,7 @@
package server
import (
+ "context"
"net/http"
"strconv"
@@ -18,6 +19,17 @@ func (s *Server) registerActuatorRoutes(g *echo.Group) {
Port: strconv.Itoa(s.port),
StartedTs: s.startedTs,
}
+
+ findRole := api.Owner
+ find := &api.MemberFind{
+ Role: &findRole,
+ }
+ list, err := s.MemberService.FindMemberList(context.Background(), find)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch admin setup status").SetInternal(err)
+ }
+ serverInfo.NeedAdminSetup = len(list) == 0
+
return c.JSON(http.StatusOK, serverInfo)
})
}
diff --git a/server/auth.go b/server/auth.go
index a0505e07dc..7c67dcb7c2 100644
--- a/server/auth.go
+++ b/server/auth.go
@@ -75,28 +75,42 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to signup").SetInternal(err)
}
+ findRole := api.Owner
+ find := &api.MemberFind{
+ Role: &findRole,
+ }
+ list, err := s.MemberService.FindMemberList(context.Background(), find)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to signup").SetInternal(err)
+ }
+
+ // Grant the member Owner role if there is no existing Owner member.
+ role := api.Developer
+ if len(list) == 0 {
+ role = api.Owner
+ }
memberCreate := &api.MemberCreate{
CreatorId: api.SYSTEM_BOT_ID,
Status: api.Active,
- Role: api.Developer,
+ Role: role,
PrincipalId: user.ID,
}
_, err = s.MemberService.CreateMember(context.Background(), memberCreate)
if err != nil {
if bytebase.ErrorCode(err) == bytebase.ECONFLICT {
- return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Member already exists: %s.", signup.Email))
+ return echo.NewHTTPError(http.StatusConflict, fmt.Sprintf("Member already exists: %s", signup.Email))
}
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to signup.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to signup").SetInternal(err)
}
if err := GenerateTokensAndSetCookies(c, user, s.mode, s.secret); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate access token.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate access token").SetInternal(err)
}
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
if err := jsonapi.MarshalPayload(c.Response().Writer, user); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal signup response.").SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal signup response").SetInternal(err)
}
return nil
})
diff --git a/server/jwt.go b/server/jwt.go
index d70578f8b2..30fae13dcc 100644
--- a/server/jwt.go
+++ b/server/jwt.go
@@ -144,8 +144,8 @@ func setUserCookie(c echo.Context, user *api.Principal, expiration time.Time) {
// will try to generate new access token and refresh token.
func JWTMiddleware(l *zap.Logger, p api.PrincipalService, next echo.HandlerFunc, mode string, secret string) echo.HandlerFunc {
return func(c echo.Context) error {
- // Skips auth
- if strings.HasPrefix(c.Path(), "/api/auth") {
+ // Skips auth and actuator
+ if strings.HasPrefix(c.Path(), "/api/auth") || strings.HasPrefix(c.Path(), "/api/actuator") {
return next(c)
}
diff --git a/store/member.go b/store/member.go
index f2d049c2cf..5eeb561515 100644
--- a/store/member.go
+++ b/store/member.go
@@ -177,6 +177,9 @@ func findMemberList(ctx context.Context, tx *Tx, find *api.MemberFind) (_ []*api
if v := find.PrincipalId; v != nil {
where, args = append(where, "principal_id = ?"), append(args, *v)
}
+ if v := find.Role; v != nil {
+ where, args = append(where, "role = ?"), append(args, *v)
+ }
rows, err := tx.QueryContext(ctx, `
SELECT