2024-06-07 07:29:07 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-11 11:38:30 +03:00
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
|
2024-06-07 07:29:07 +03:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
uriSecretData = "/:secret/data/:path"
|
|
|
|
uriSecretMetadata = "/:secret/metadata/:path"
|
|
|
|
)
|
|
|
|
|
2024-06-11 11:38:30 +03:00
|
|
|
func vaultApiSimpleKv2(c *fiber.Ctx, action string) error {
|
|
|
|
secret := c.Params("secret")
|
|
|
|
path := c.Params("path")
|
|
|
|
if (secret == "") || (path == "") {
|
|
|
|
return fiberNone(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("%v %v %v", action, secret, path)
|
|
|
|
|
|
|
|
return fiberOk(c)
|
|
|
|
}
|
|
|
|
|
2024-06-07 07:29:07 +03:00
|
|
|
func setupVaultApi(router fiber.Router) {
|
|
|
|
// https://developer.hashicorp.com/vault/api-docs/secret/kv/kv-v2
|
|
|
|
|
|
|
|
/* read secret */
|
2024-06-11 11:38:30 +03:00
|
|
|
router.Get(uriSecretData, func(c *fiber.Ctx) error {
|
|
|
|
// return vaultApiSimpleKv2(c, "GET")
|
|
|
|
|
|
|
|
secret := c.Params("secret")
|
|
|
|
path := c.Params("path")
|
|
|
|
if (secret == "") || (path == "") {
|
|
|
|
return fiberNone(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
var version int64 = -1
|
|
|
|
if ver_s := c.Query("version"); ver_s != "" {
|
|
|
|
var err error
|
|
|
|
version, err = strconv.ParseInt(ver_s, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return fiberNone(c)
|
2024-06-07 07:29:07 +03:00
|
|
|
}
|
2024-06-11 11:38:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if version >= 0 {
|
|
|
|
// version is sane
|
|
|
|
log.Printf("GET %v %v v=%v", secret, path, version)
|
|
|
|
} else {
|
|
|
|
log.Printf("GET %v %v", secret, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fiberOk(c)
|
2024-06-07 07:29:07 +03:00
|
|
|
})
|
|
|
|
/* create/update/patch secret */
|
2024-06-11 11:38:30 +03:00
|
|
|
router.Post(uriSecretData, func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "SET")
|
|
|
|
})
|
|
|
|
router.Patch(uriSecretData, func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "SET")
|
|
|
|
})
|
2024-06-07 07:29:07 +03:00
|
|
|
/* delete secret */
|
2024-06-11 11:38:30 +03:00
|
|
|
router.Delete(uriSecretData, func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "DELETE")
|
|
|
|
})
|
2024-06-07 07:29:07 +03:00
|
|
|
/* delete/undelete secret */
|
2024-06-11 11:38:30 +03:00
|
|
|
router.Post("/:secret/delete/:path", func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "DELETE")
|
|
|
|
})
|
|
|
|
router.Post("/:secret/undelete/:path", func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "UNDELETE")
|
|
|
|
})
|
2024-06-07 07:29:07 +03:00
|
|
|
/* destroy secret */
|
2024-06-11 11:38:30 +03:00
|
|
|
router.Post("/:secret/destroy/:path", func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "DESTROY")
|
|
|
|
})
|
2024-06-07 07:29:07 +03:00
|
|
|
|
|
|
|
/* read subkeys */
|
|
|
|
// router.Get("/:secret/subkeys/:path?version=:version&depth=:depth", fiberOk)
|
2024-06-11 11:38:30 +03:00
|
|
|
router.Get("/:secret/subkeys/:path", func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "SUBKEYS")
|
2024-06-07 07:29:07 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
/* read metadata */
|
2024-06-11 11:38:30 +03:00
|
|
|
router.Get(uriSecretMetadata, func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "META-GET")
|
|
|
|
})
|
2024-06-07 07:29:07 +03:00
|
|
|
/* create/update metadata */
|
2024-06-11 11:38:30 +03:00
|
|
|
router.Post(uriSecretMetadata, func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "META-SET")
|
|
|
|
})
|
|
|
|
router.Patch(uriSecretMetadata, func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "META-SET")
|
|
|
|
})
|
2024-06-07 07:29:07 +03:00
|
|
|
/* delete metadata */
|
2024-06-11 11:38:30 +03:00
|
|
|
router.Delete(uriSecretMetadata, func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "META-DELETE")
|
|
|
|
})
|
2024-06-07 07:29:07 +03:00
|
|
|
/* list secrets */
|
2024-06-11 11:38:30 +03:00
|
|
|
router.Add(MethodList, uriSecretMetadata, func(c *fiber.Ctx) error {
|
|
|
|
return vaultApiSimpleKv2(c, "LIST")
|
|
|
|
})
|
2024-06-07 07:29:07 +03:00
|
|
|
}
|