Skip to content

Commit 59beb57

Browse files
Merge pull request #181 from Tschonti/feature/hide-token-in-logs
Hide studentIds and tokens in logs
2 parents bacce46 + 2aee408 commit 59beb57

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ To run the service locally, follow these steps:
2020

2121
- Clone this repository
2222
```sh
23-
git clone https://github.com/tum-calendar-proxy/tum-calendar-proxy.git
23+
git clone https://github.com/TUM-Dev/CalendarProxy.git
2424
```
25-
- Navigate to the project directory:
25+
- Navigate to the project directory:
2626
```sh
27-
cd tum-calendar-proxy
27+
cd CalendarProxy
2828
```
2929
- Run the proxy server:
3030
```sh

internal/app.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"net/url"
910
"regexp"
1011
"sort"
1112
"strings"
@@ -71,6 +72,44 @@ func newApp() (*App, error) {
7172
return &a, nil
7273
}
7374

75+
func customLogFormatter(params gin.LogFormatterParams) string {
76+
return fmt.Sprintf("[GIN] %v |%s %3d %s | %13v | %15s |%s %-7s%s %#v\n%s",
77+
params.TimeStamp.Format("2006/01/02 - 15:04:05"),
78+
params.StatusCodeColor(),
79+
params.StatusCode,
80+
params.ResetColor(),
81+
params.Latency,
82+
params.ClientIP,
83+
params.MethodColor(),
84+
params.Method,
85+
params.ResetColor(),
86+
hideTokens(params.Path),
87+
params.ErrorMessage,
88+
)
89+
}
90+
91+
func hideTokens(path string) string {
92+
u, err := url.Parse(path)
93+
if err != nil {
94+
return path
95+
}
96+
97+
pStud := u.Query().Get("pStud")
98+
pPers := u.Query().Get("pPers")
99+
pToken := u.Query().Get("pToken")
100+
101+
if pToken == "" || (pStud == "" && pPers == "") {
102+
return path
103+
}
104+
105+
manyXes := strings.Repeat("X", 12)
106+
tokenReplaced := pToken[:4] + manyXes
107+
if pStud != "" {
108+
return fmt.Sprintf("/?pStud=%s&pToken=%s", pStud[:4]+manyXes, tokenReplaced)
109+
}
110+
return fmt.Sprintf("/?pPers=%s&pToken=%s", pPers[:4]+manyXes, tokenReplaced)
111+
}
112+
74113
func (a *App) Run() error {
75114
if err := sentry.Init(sentry.ClientOptions{
76115
Dsn: "https://[email protected]/4",
@@ -93,7 +132,7 @@ func (a *App) Run() error {
93132
gin.SetMode("release")
94133
a.engine = gin.New()
95134
a.engine.Use(sentrygin.New(sentrygin.Options{}))
96-
logger := gin.LoggerWithConfig(gin.LoggerConfig{SkipPaths: []string{"/health"}})
135+
logger := gin.LoggerWithConfig(gin.LoggerConfig{SkipPaths: []string{"/health"}, Formatter: customLogFormatter})
97136
a.engine.Use(logger, gin.Recovery())
98137
a.configRoutes()
99138

0 commit comments

Comments
 (0)