2021-05-20 13:59:28 +00:00
|
|
|
package secrets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-07-21 18:52:45 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2021-05-20 13:59:28 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/config"
|
2024-02-27 19:29:54 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-05-20 13:59:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SecretsPlugin struct {
|
|
|
|
b bot.Bot
|
|
|
|
c *config.Config
|
|
|
|
db *sqlx.DB
|
|
|
|
}
|
|
|
|
|
2024-02-27 22:16:45 +00:00
|
|
|
func New(b bot.Bot) bot.Plugin {
|
2021-05-20 13:59:28 +00:00
|
|
|
p := &SecretsPlugin{
|
|
|
|
b: b,
|
|
|
|
c: b.Config(),
|
|
|
|
db: b.DB(),
|
|
|
|
}
|
|
|
|
p.registerWeb()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SecretsPlugin) registerWeb() {
|
2021-07-21 18:52:45 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
r.HandleFunc("/add", p.handleRegister)
|
|
|
|
r.HandleFunc("/remove", p.handleRemove)
|
|
|
|
r.HandleFunc("/", p.handleIndex)
|
2024-02-27 19:29:54 +00:00
|
|
|
p.b.GetWeb().RegisterWebName(r, "/secrets", "Secrets")
|
2021-05-20 13:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func mkCheckError(w http.ResponseWriter) func(error) bool {
|
|
|
|
return func(err error) bool {
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Stack().Err(err).Msgf("secret failed")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
e, _ := json.Marshal(err)
|
|
|
|
w.Write(e)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkMethod(method string, w http.ResponseWriter, r *http.Request) bool {
|
|
|
|
if r.Method != method {
|
|
|
|
w.WriteHeader(405)
|
|
|
|
fmt.Fprintf(w, "Incorrect HTTP method")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-27 19:29:54 +00:00
|
|
|
func (p *SecretsPlugin) keys() []string {
|
|
|
|
return p.c.SecretKeys()
|
|
|
|
}
|
|
|
|
|
2021-05-20 13:59:28 +00:00
|
|
|
func (p *SecretsPlugin) sendKeys(w http.ResponseWriter, r *http.Request) {
|
2024-02-27 19:29:54 +00:00
|
|
|
p.keysList().Render(r.Context(), w)
|
2021-05-20 13:59:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SecretsPlugin) handleAll(w http.ResponseWriter, r *http.Request) {
|
|
|
|
p.sendKeys(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SecretsPlugin) handleRegister(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if checkMethod(http.MethodPost, w, r) {
|
|
|
|
log.Debug().Msgf("failed post %s", r.Method)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
checkError := mkCheckError(w)
|
2024-02-27 19:29:54 +00:00
|
|
|
key, value := r.FormValue("key"), r.FormValue("value")
|
|
|
|
err := p.c.RegisterSecret(key, value)
|
2021-05-20 13:59:28 +00:00
|
|
|
if checkError(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.sendKeys(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SecretsPlugin) handleRemove(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if checkMethod(http.MethodDelete, w, r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
checkError := mkCheckError(w)
|
2024-02-27 19:29:54 +00:00
|
|
|
b, err := io.ReadAll(r.Body)
|
|
|
|
if checkError(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
q, err := url.ParseQuery(string(b))
|
2021-05-20 13:59:28 +00:00
|
|
|
if checkError(err) {
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 19:29:54 +00:00
|
|
|
secret := q.Get("key")
|
|
|
|
err = p.c.RemoveSecret(secret)
|
2021-05-20 13:59:28 +00:00
|
|
|
if checkError(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.sendKeys(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *SecretsPlugin) handleIndex(w http.ResponseWriter, r *http.Request) {
|
2024-02-27 19:29:54 +00:00
|
|
|
p.b.GetWeb().Index("Secrets", p.index()).Render(r.Context(), w)
|
2021-05-20 13:59:28 +00:00
|
|
|
}
|