feat: add keychains

This commit is contained in:
2024-11-09 18:57:36 +00:00
parent b50abccae0
commit 0a788b05e5
30 changed files with 699 additions and 102 deletions
+2 -2
View File
@@ -49,6 +49,6 @@ func (r *Hosts) Create(item *models.Host) error {
return r.db.Create(item).Error
}
func (r *Hosts) Update(item *models.Host) error {
return r.db.Save(item).Error
func (r *Hosts) Update(id string, item *models.Host) error {
return r.db.Where("id = ?", id).Updates(item).Error
}
+1 -1
View File
@@ -93,7 +93,7 @@ func update(c *fiber.Ctx) error {
}
item.OS = osName
if err := repo.Update(item); err != nil {
if err := repo.Update(id, item); err != nil {
return utils.ResponseError(c, err, 500)
}
+10
View File
@@ -32,6 +32,12 @@ func (r *Keychains) Get(id string) (*models.Keychain, error) {
return &keychain, nil
}
func (r *Keychains) Exists(id string) (bool, error) {
var count int64
ret := r.db.Model(&models.Keychain{}).Where("id = ?", id).Count(&count)
return count > 0, ret.Error
}
type KeychainDecrypted struct {
models.Keychain
Data map[string]interface{}
@@ -50,3 +56,7 @@ func (r *Keychains) GetDecrypted(id string) (*KeychainDecrypted, error) {
return &KeychainDecrypted{Keychain: *keychain, Data: data}, nil
}
func (r *Keychains) Update(id string, item *models.Keychain) error {
return r.db.Where("id = ?", id).Updates(item).Error
}
+62 -3
View File
@@ -1,6 +1,7 @@
package keychains
import (
"fmt"
"net/http"
"github.com/gofiber/fiber/v2"
@@ -13,18 +14,46 @@ func Router(app *fiber.App) {
router.Get("/", getAll)
router.Post("/", create)
router.Put("/:id", update)
}
type GetAllResult struct {
*models.Keychain
Data map[string]interface{} `json:"data"`
}
func getAll(c *fiber.Ctx) error {
withData := c.Query("withData")
repo := NewRepository()
rows, err := repo.GetAll()
if err != nil {
return utils.ResponseError(c, err, 500)
}
return c.JSON(fiber.Map{
"rows": rows,
})
if withData != "true" {
return c.JSON(fiber.Map{"rows": rows})
}
res := make([]*GetAllResult, len(rows))
doneCh := make(chan struct{})
// Decrypt data
for i, item := range rows {
go func(i int, item *models.Keychain) {
var data map[string]interface{}
item.DecryptData(&data)
res[i] = &GetAllResult{item, data}
doneCh <- struct{}{}
}(i, item)
}
for range rows {
<-doneCh
}
return c.JSON(fiber.Map{"rows": res})
}
func create(c *fiber.Ctx) error {
@@ -50,3 +79,33 @@ func create(c *fiber.Ctx) error {
return c.Status(http.StatusCreated).JSON(item)
}
func update(c *fiber.Ctx) error {
var body CreateKeychainSchema
if err := c.BodyParser(&body); err != nil {
return utils.ResponseError(c, err, 500)
}
repo := NewRepository()
id := c.Params("id")
exist, _ := repo.Exists(id)
if !exist {
return utils.ResponseError(c, fmt.Errorf("key %s not found", id), 404)
}
item := &models.Keychain{
Type: body.Type,
Label: body.Label,
}
if err := item.EncryptData(body.Data); err != nil {
return utils.ResponseError(c, err, 500)
}
if err := repo.Update(id, item); err != nil {
return utils.ResponseError(c, err, 500)
}
return c.JSON(item)
}
+2
View File
@@ -50,12 +50,14 @@ func sshHandler(c *websocket.Conn, data *models.HostDecrypted) {
func pveHandler(c *websocket.Conn, data *models.HostDecrypted) {
client := c.Query("client")
username, _ := data.Key["username"].(string)
realm, _ := data.Key["realm"].(string)
password, _ := data.Key["password"].(string)
pve := &lib.PVEServer{
HostName: data.Host.Host,
Port: data.Port,
Username: username,
Realm: realm,
Password: password,
}