-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Frabit - The next-generation database automatic operation platform | ||
// Copyright © 2022-2024 Frabit Team | ||
// | ||
// Licensed under the GNU General Public License, Version 3.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.gnu.org/licenses/gpl-3.0.txt | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api | ||
|
||
import ( | ||
"github.com/frabits/frabit/pkg/services/settings" | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
func (hs *HttpServer) applyAdmin(group *gin.RouterGroup) { | ||
subRouter := group.Group("/admin") | ||
subRouter.POST("/settings", hs.CreateSetting) | ||
subRouter.GET("/settings", hs.GetAllSetting) | ||
} | ||
|
||
func (hs *HttpServer) CreateSetting(c *gin.Context) { | ||
var settingsInfo settings.CreateSettingsCmd | ||
if err := c.ShouldBindJSON(&settingsInfo); err != nil { | ||
hs.Logger.Error("Bind agent info failed", "Error", err.Error()) | ||
} | ||
if err := hs.settings.CreateSettings(hs.ctx, &settingsInfo); err != nil { | ||
hs.Logger.Error("create sso setting failed", "Name", settingsInfo.Name) | ||
c.IndentedJSON(http.StatusBadGateway, "register agent failed") | ||
} | ||
c.IndentedJSON(http.StatusOK, "register an agent") | ||
} | ||
|
||
func (hs *HttpServer) GetAllSetting(c *gin.Context) { | ||
results, err := hs.settings.GetSettings(hs.ctx) | ||
if err != nil { | ||
hs.Logger.Error("create sso setting failed") | ||
c.IndentedJSON(http.StatusBadGateway, "register agent failed") | ||
} | ||
c.IndentedJSON(http.StatusOK, results) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Frabit - The next-generation database automatic operation platform | ||
// Copyright © 2022-2024 Frabit Team | ||
// | ||
// Licensed under the GNU General Public License, Version 3.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.gnu.org/licenses/gpl-3.0.txt | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api | ||
|
||
import ( | ||
"github.com/frabits/frabit/pkg/satoken" | ||
"net/http" | ||
|
||
"github.com/frabits/frabit/pkg/services/apikey" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func (hs *HttpServer) applyApiKey(group *gin.RouterGroup) { | ||
subRouter := group.Group("/apikey") | ||
subRouter.POST("", hs.CreateApiKey) | ||
} | ||
|
||
func (hs *HttpServer) CreateApiKey(c *gin.Context) { | ||
var keyInfo apikey.CreateAPIKeyCmd | ||
if err := c.ShouldBindJSON(&keyInfo); err != nil { | ||
hs.Logger.Error("Bind agent info failed", "Error", err.Error()) | ||
} | ||
|
||
newKey := satoken.New() | ||
keyInfo.HashKey = newKey.HashedKey | ||
metaKey, err := hs.apiKey.AddAPIKey(hs.ctx, &keyInfo) | ||
if err != nil { | ||
hs.Logger.Error("create apikey failed", "Error", err.Error()) | ||
c.IndentedJSON(http.StatusBadGateway, "create apikey failed") | ||
} | ||
result := apikey.APIKeyDTO{ | ||
Name: metaKey.Name, | ||
ClientSecret: newKey.ClientSecret, | ||
} | ||
c.IndentedJSON(http.StatusOK, result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Frabit - The next-generation database automatic operation platform | ||
// Copyright © 2022-2024 Frabit Team | ||
// | ||
// Licensed under the GNU General Public License, Version 3.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.gnu.org/licenses/gpl-3.0.txt | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package settings | ||
|
||
type SettingsSSO struct { | ||
Id uint32 `gorm:"primary_key;AUTO_INCREMENT" json:"id"` | ||
Name string `gorm:"type:varchar(30);not null;unique:uniq_login" json:"name"` | ||
Settings string `gorm:"type:text;not null" json:"settings"` | ||
CreatedAt string `gorm:"type:varchar(50);not null" json:"Created_at"` | ||
UpdatedAt string `gorm:"type:varchar(50);not null" json:"updated_at"` | ||
} | ||
|
||
func (u SettingsSSO) TableName() string { | ||
return "settings_sso" | ||
} | ||
|
||
type CreateSettingsCmd struct { | ||
Name string `json:"name"` | ||
Settings string `json:"settings"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Frabit - The next-generation database automatic operation platform | ||
// Copyright © 2022-2024 Frabit Team | ||
// | ||
// Licensed under the GNU General Public License, Version 3.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.gnu.org/licenses/gpl-3.0.txt | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package settings | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"log/slog" | ||
|
||
"github.com/frabits/frabit/pkg/common/utils" | ||
"github.com/frabits/frabit/pkg/infra/db" | ||
"github.com/frabits/frabit/pkg/infra/log" | ||
"github.com/frabits/frabit/pkg/services/secrets" | ||
) | ||
|
||
type service struct { | ||
secrets secrets.Service | ||
log *slog.Logger | ||
store Store | ||
} | ||
|
||
func ProviderService(secrets secrets.Service, meta *db.MetaStore) Service { | ||
metaStore := newStoreImpl(meta.Gdb) | ||
return &service{ | ||
secrets: secrets, | ||
log: log.New("settings"), | ||
store: metaStore, | ||
} | ||
} | ||
|
||
func (s *service) CreateSettings(ctx context.Context, cmd *CreateSettingsCmd) error { | ||
encSettings, err := s.secrets.Encrypt([]byte(cmd.Settings)) | ||
if err != nil { | ||
s.log.Error("create sso settings failed", "Error", err.Error()) | ||
return err | ||
} | ||
s.log.Info("encrypted data", "data", encSettings) | ||
settings := &SettingsSSO{ | ||
Name: cmd.Name, | ||
Settings: base64.RawStdEncoding.EncodeToString(encSettings), | ||
CreatedAt: utils.NowDatetime(), | ||
UpdatedAt: utils.NowDatetime(), | ||
} | ||
|
||
return s.store.CreateSettings(ctx, settings) | ||
} | ||
|
||
func (s *service) GetSettings(ctx context.Context) ([]SettingsSSO, error) { | ||
results := make([]SettingsSSO, 0) | ||
settings, err := s.store.QuerySettings(ctx) | ||
if err != nil { | ||
s.log.Error("query settings failed", "Error", err.Error()) | ||
} | ||
for _, setting := range settings { | ||
result := SettingsSSO{ | ||
Id: setting.Id, | ||
Name: setting.Name, | ||
CreatedAt: setting.CreatedAt, | ||
UpdatedAt: setting.UpdatedAt, | ||
} | ||
toDecrypt, _ := base64.RawStdEncoding.DecodeString(setting.Settings) | ||
decryptSetting, err := s.secrets.Decrypt(toDecrypt) | ||
if err != nil { | ||
s.log.Error("decrypt settings failed", "Error", err.Error()) | ||
} | ||
result.Settings = string(decryptSetting) | ||
|
||
results = append(results, result) | ||
} | ||
return results, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Frabit - The next-generation database automatic operation platform | ||
// Copyright © 2022-2024 Frabit Team | ||
// | ||
// Licensed under the GNU General Public License, Version 3.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.gnu.org/licenses/gpl-3.0.txt | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package settings | ||
|
||
import ( | ||
"context" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type Store interface { | ||
CreateSettings(ctx context.Context, settings *SettingsSSO) error | ||
QuerySettings(ctx context.Context) ([]SettingsSSO, error) | ||
} | ||
|
||
type storeImpl struct { | ||
DB *gorm.DB | ||
} | ||
|
||
func newStoreImpl(db *gorm.DB) Store { | ||
return &storeImpl{DB: db} | ||
} | ||
|
||
func (s *storeImpl) CreateSettings(ctx context.Context, settings *SettingsSSO) error { | ||
s.DB.Create(settings) | ||
return nil | ||
} | ||
|
||
func (s *storeImpl) QuerySettings(ctx context.Context) ([]SettingsSSO, error) { | ||
var settings []SettingsSSO | ||
s.DB.Model(SettingsSSO{}).Find(&settings) | ||
return settings, nil | ||
} |