Skip to content

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

Only returns username/password if query parameter 'secret=true'

天舟b168852c29a6
diff --git a/backend/api/instance.go b/backend/api/instance.go
index 72a457bc39..e6854e9c04 100644
--- a/backend/api/instance.go
+++ b/backend/api/instance.go
@@ -25,6 +25,9 @@ type Instance struct {
ExternalLink string `jsonapi:"attr,externalLink"`
Host string `jsonapi:"attr,host"`
Port string `jsonapi:"attr,port"`
+ // Only returns username/password if query parameter 'secret=true'
+ Username string `jsonapi:"attr,username"`
+ Password string `jsonapi:"attr,password"`
}
type InstanceCreate struct {
diff --git a/backend/server/database.go b/backend/server/database.go
index ef23eea7f0..d90073ae7b 100644
--- a/backend/server/database.go
+++ b/backend/server/database.go
@@ -172,7 +172,7 @@ func (s *Server) ComposeDatabaseRelationship(ctx context.Context, database *api.
}
if sort.SearchStrings(includeList, "instance") >= 0 {
- database.Instance, err = s.ComposeInstanceById(context.Background(), database.InstanceId, includeList)
+ database.Instance, err = s.ComposeInstanceById(context.Background(), database.InstanceId, false /*includeSecret*/)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance for database: %v", database.Name)).SetInternal(err)
}
diff --git a/backend/server/instance.go b/backend/server/instance.go
index 2fc93391f4..4a54bd07bf 100644
--- a/backend/server/instance.go
+++ b/backend/server/instance.go
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
- "sort"
"strconv"
"github.com/bytebase/bytebase"
@@ -30,7 +29,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create instance").SetInternal(err)
}
- if err := s.ComposeInstanceRelationship(context.Background(), instance, c.Get(getIncludeKey()).([]string)); err != nil {
+ if err := s.ComposeInstanceRelationship(context.Background(), instance, true /*includeSecret*/); err != nil {
return err
}
@@ -56,7 +55,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
}
for _, instance := range list {
- if err := s.ComposeInstanceRelationship(context.Background(), instance, c.Get(getIncludeKey()).([]string)); err != nil {
+ if err := s.ComposeInstanceRelationship(context.Background(), instance, false /*includeSecret*/); err != nil {
return err
}
}
@@ -74,7 +73,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err)
}
- instance, err := s.ComposeInstanceById(context.Background(), id, c.Get(getIncludeKey()).([]string))
+ instance, err := s.ComposeInstanceById(context.Background(), id, true /*includeSecret*/)
if err != nil {
return err
}
@@ -145,7 +144,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
}
}
- if err := s.ComposeInstanceRelationship(context.Background(), instance, c.Get(getIncludeKey()).([]string)); err != nil {
+ if err := s.ComposeInstanceRelationship(context.Background(), instance, true /*includeSecret*/); err != nil {
return err
}
@@ -157,7 +156,7 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
})
}
-func (s *Server) ComposeInstanceById(ctx context.Context, id int, includeList []string) (*api.Instance, error) {
+func (s *Server) ComposeInstanceById(ctx context.Context, id int, includeSecret bool) (*api.Instance, error) {
instanceFind := &api.InstanceFind{
ID: &id,
}
@@ -169,32 +168,32 @@ func (s *Server) ComposeInstanceById(ctx context.Context, id int, includeList []
return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", id)).SetInternal(err)
}
- if err := s.ComposeInstanceRelationship(ctx, instance, includeList); err != nil {
+ if err := s.ComposeInstanceRelationship(ctx, instance, includeSecret); err != nil {
return nil, err
}
return instance, nil
}
-func (s *Server) ComposeInstanceRelationship(ctx context.Context, instance *api.Instance, includeList []string) error {
+func (s *Server) ComposeInstanceRelationship(ctx context.Context, instance *api.Instance, includeSecret bool) error {
var err error
- instance.Creator, err = s.ComposePrincipalById(context.Background(), instance.CreatorId, includeList)
+ instance.Creator, err = s.ComposePrincipalById(context.Background(), instance.CreatorId, []string{})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch creator for instance: %v", instance.Name)).SetInternal(err)
}
- instance.Updater, err = s.ComposePrincipalById(context.Background(), instance.UpdaterId, includeList)
+ instance.Updater, err = s.ComposePrincipalById(context.Background(), instance.UpdaterId, []string{})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch updater for instance: %v", instance.Name)).SetInternal(err)
}
- instance.Environment, err = s.ComposeEnvironmentById(context.Background(), instance.EnvironmentId, includeList)
+ instance.Environment, err = s.ComposeEnvironmentById(context.Background(), instance.EnvironmentId, []string{})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch environment for instance: %v", instance.Name)).SetInternal(err)
}
- if sort.SearchStrings(includeList, "dataSource") >= 0 {
+ if includeSecret {
dataSourceFind := &api.DataSourceFind{
InstanceId: &instance.ID,
}
@@ -202,6 +201,13 @@ func (s *Server) ComposeInstanceRelationship(ctx context.Context, instance *api.
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch data source for instance: %v", instance.Name)).SetInternal(err)
}
+ for _, dataSource := range instance.DataSourceList {
+ if dataSource.Type == api.Admin {
+ instance.Username = dataSource.Username
+ instance.Password = dataSource.Password
+ break
+ }
+ }
}
return nil
diff --git a/frontend/src/store/modules/instance.ts b/frontend/src/store/modules/instance.ts
index b8cb2b9ef5..8e119421e7 100644
--- a/frontend/src/store/modules/instance.ts
+++ b/frontend/src/store/modules/instance.ts
@@ -30,18 +30,7 @@ function convert(
let environment: Environment = unknown("ENVIRONMENT") as Environment;
environment.id = parseInt(environmentId);
- let username = undefined;
- let password = undefined;
for (const item of includedList || []) {
- if (
- item.type == "dataSource" &&
- item.attributes.type == "ADMIN" &&
- item.attributes.instanceId == instance.id
- ) {
- username = item.attributes.username as string;
- password = item.attributes.password as string;
- }
-
if (
item.type == "environment" &&
(instance.relationships!.environment.data as ResourceIdentifier).id ==
@@ -60,8 +49,6 @@ function convert(
creator,
updater,
environment,
- username,
- password,
};
}
@@ -137,11 +124,9 @@ const actions = {
{ commit, rootGetters }: any,
instanceId: InstanceId
) {
- // Data source contains sensitive info such as username, password so
- // we only fetch if requesting the specific instance id
- const data = (
- await axios.get(`/api/instance/${instanceId}?include=dataSource`)
- ).data;
+ // secret=true to return username/password when requesting the specific instance id
+ const data = (await axios.get(`/api/instance/${instanceId}?secret=true`))
+ .data;
const instance = convert(data.data, data.included, rootGetters);
commit("setInstanceById", {
@@ -156,7 +141,7 @@ const actions = {
newInstance: InstanceCreate
) {
const data = (
- await axios.post(`/api/instance?include=dataSource`, {
+ await axios.post(`/api/instance?secret=true`, {
data: {
type: "InstanceCreate",
attributes: newInstance,