Skip to content

Commit

Permalink
add dump secret feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ybizeul committed Oct 13, 2024
1 parent d0315d8 commit dddad58
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
22 changes: 22 additions & 0 deletions internal/feed/feedmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package feed

import (
"fmt"
"os"
"path"
)

Expand Down Expand Up @@ -60,3 +61,24 @@ func (m *FeedManager) GetFeedWithAuth(feedName string, secret string) (*Feed, er

return result, nil
}

func (m *FeedManager) DumpSecrets() {
d, err := os.ReadDir(m.path)
if err != nil {
// TODO: log error
return
}
for _, entry := range d {
if !entry.IsDir() {
continue
}
feedPath := path.Join(m.path, entry.Name())

result, err := GetFeed(feedPath)
if err != nil {
// TODO Log error
return
}
fmt.Printf("Feed %s: %s\n", result.Name(), result.Config.Secret)
}
}
18 changes: 10 additions & 8 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func (api *ApiHandler) GetServer() *chi.Mux {
}
})

r.Post("/api/secrets", api.postSecretsHandler)
r.Route("/api/feeds", func(r chi.Router) {
r.Get("/{feedName}", api.feedGetFunc)
r.Post("/{feedName}", api.feedPostFunc)
Expand Down Expand Up @@ -224,23 +225,20 @@ func (api *ApiHandler) feedWSHandler(w http.ResponseWriter, r *http.Request) {
_, err := api.FeedManager.GetFeedWithAuth(feedName, secret)

if err != nil {
// A web socket doesn't have a standard http status code, so we need
// to open it and close it with a relevant code
var upgrader = ws.Upgrader{}
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
// utils.CloseWithCodeAndMessage(w, 500, "Unable to upgrade WebSocket")
}
switch {
case errors.Is(err, feed.FeedErrorNotFound):
//WriteError(w, http.StatusNotFound, "feed '%s' not found", feedName)
c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusNotFound+4000, ""), time.Now().Add(time.Second))

_ = c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusNotFound+4000, ""), time.Now().Add(time.Second))
case errors.Is(err, feed.FeedErrorInvalidSecret):
//WriteError(w, http.StatusUnauthorized, "Invalid secret")
c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusUnauthorized+4000, ""), time.Now().Add(time.Second))
_ = c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusUnauthorized+4000, ""), time.Now().Add(time.Second))
default:
//WriteError(w, http.StatusInternalServerError, "Error while getting feed: %s", err.Error())
c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusInternalServerError+4000, ""), time.Now().Add(time.Second))
_ = c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusInternalServerError+4000, ""), time.Now().Add(time.Second))
}
c.Close()
return
Expand Down Expand Up @@ -624,3 +622,7 @@ func (api *ApiHandler) subscriptionDeleteFunc(w http.ResponseWriter, r *http.Req
return
}
}

func (api *ApiHandler) postSecretsHandler(w http.ResponseWriter, r *http.Request) {
api.FeedManager.DumpSecrets()
}

0 comments on commit dddad58

Please sign in to comment.