mirror of
https://github.com/khairul169/vaulterm.git
synced 2026-09-15 17:03:30 +07:00
feat: add keychains
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -13,6 +13,7 @@ type PVEServer struct {
|
||||
HostName string
|
||||
Port int
|
||||
Username string
|
||||
Realm string
|
||||
Password string
|
||||
}
|
||||
|
||||
@@ -72,7 +73,7 @@ func (pve *PVEServer) GetAccessTicket() (*PVEAccessTicket, error) {
|
||||
|
||||
// note for myself: don't forget the realm
|
||||
body, err := fetch("POST", url, &PVERequestInit{Body: map[string]string{
|
||||
"username": pve.Username,
|
||||
"username": fmt.Sprintf("%s@%s", pve.Username, pve.Realm),
|
||||
"password": pve.Password,
|
||||
}})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user