Skip to content

Commit

Permalink
add some rpc logging
Browse files Browse the repository at this point in the history
  • Loading branch information
maddsua committed Jan 25, 2025
1 parent 29b7057 commit e043b4d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 14 additions & 0 deletions service/rpc/rest/procedures.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"errors"
"log/slog"
"net/http"

"github.com/google/uuid"
Expand Down Expand Up @@ -76,6 +77,10 @@ func (this *RPCProcedures) StreamsDelete(writer http.ResponseWriter, req *http.R
return
}

slog.Info("RPC: Removed stream",
slog.String("remote_addr", req.RemoteAddr),
slog.String("stream_id", id.String()))

writer.WriteHeader(http.StatusOK)
}

Expand Down Expand Up @@ -105,6 +110,11 @@ func (this *RPCProcedures) StreamsAdd(writer http.ResponseWriter, req *http.Requ
return
}

slog.Info("RPC: Added stream",
slog.String("remote_addr", req.RemoteAddr),
slog.String("stream_id", entry.ID.String()),
slog.String("stream_name", entry.Name))

writeJsonData(writer, result)
}

Expand Down Expand Up @@ -144,6 +154,10 @@ func (this *RPCProcedures) StreamsSetLabels(writer http.ResponseWriter, req *htt
return
}

slog.Info("RPC: Updated stream labels",
slog.String("remote_addr", req.RemoteAddr),
slog.String("stream_id", id.String()))

var result Stream
if err := result.ScanRow(entry); err != nil {
writeJsonError(writer, err, http.StatusInternalServerError)
Expand Down
14 changes: 12 additions & 2 deletions service/rpc/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rest
import (
"encoding/json"
"errors"
"log/slog"
"net/http"
"strings"
"sync"
Expand All @@ -29,7 +30,14 @@ func (this *RPCHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request)
writeJsonError(writer, errors.New("rpc token required"), http.StatusUnauthorized)
return
} else if strings.ToLower(strings.TrimSpace(token)) != this.Token {
this.AuthAttempts.Increment(req.RemoteAddr)

if !this.AuthAttempts.Increment(req.RemoteAddr) {
slog.Warn("RPC: Client locked out with too many auth attempts",
slog.String("remote_addr", req.RemoteAddr),
slog.Duration("hold_time", this.AuthAttempts.Period),
slog.Int("max_attempts", this.AuthAttempts.Attempts))
}

writeJsonError(writer, errors.New("invalid rpc token"), http.StatusForbidden)
return
}
Expand Down Expand Up @@ -116,7 +124,7 @@ func (this *AuthAttempts) Locked(id string) (bool, *time.Time) {
return entry.Value > this.Attempts, &entry.Expires
}

func (this *AuthAttempts) Increment(id string) {
func (this *AuthAttempts) Increment(id string) bool {

this.mtx.Lock()
defer this.mtx.Unlock()
Expand Down Expand Up @@ -147,4 +155,6 @@ func (this *AuthAttempts) Increment(id string) {

this.writesAfterCleanup = 0
}

return entry.Value <= this.Attempts
}

0 comments on commit e043b4d

Please sign in to comment.