Skip to content

Commit 936d074

Browse files
committed
added settings overrides
1 parent ad7c084 commit 936d074

File tree

8 files changed

+77
-46
lines changed

8 files changed

+77
-46
lines changed

internals/proxy/middlewares/auth.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
package middlewares
22

33
import (
4+
"context"
45
"encoding/base64"
56
"net/http"
67
"slices"
78
"strings"
89

10+
"github.com/codeshelldev/secured-signal-api/utils/config"
911
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1012
)
1113

1214
type AuthMiddleware struct {
13-
Next http.Handler
14-
Tokens []string
15+
Next http.Handler
1516
}
1617

17-
type authType string
18-
19-
const (
20-
Bearer authType = "Bearer"
21-
Basic authType = "Basic"
22-
Query authType = "Query"
23-
None authType = "None"
24-
)
25-
2618
func getAuthType(str string) authType {
2719
switch str {
2820
case "Bearer":
@@ -40,7 +32,7 @@ func isValidToken(tokens []string, match string) (bool) {
4032

4133
func (data AuthMiddleware) Use() http.Handler {
4234
next := data.Next
43-
tokens := data.Tokens
35+
tokens := config.ENV.API_TOKENS
4436

4537
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
4638
if len(tokens) <= 0 {
@@ -54,13 +46,15 @@ func (data AuthMiddleware) Use() http.Handler {
5446

5547
var authType authType = None
5648

49+
var authToken string
50+
5751
success := false
5852

5953
if authHeader != "" {
6054
authBody := strings.Split(authHeader, " ")
6155

6256
authType = getAuthType(authBody[0])
63-
authToken := authBody[1]
57+
authToken = authBody[1]
6458

6559
switch authType {
6660
case Bearer:
@@ -88,7 +82,7 @@ func (data AuthMiddleware) Use() http.Handler {
8882
} else if authQuery != "" {
8983
authType = Query
9084

91-
authToken := strings.TrimSpace(authQuery)
85+
authToken = strings.TrimSpace(authQuery)
9286

9387
if isValidToken(tokens, authToken) {
9488
success = true
@@ -109,6 +103,9 @@ func (data AuthMiddleware) Use() http.Handler {
109103
return
110104
}
111105

106+
ctx := context.WithValue(req.Context(), tokenKey, authToken)
107+
req = req.WithContext(ctx)
108+
112109
next.ServeHTTP(w, req)
113110
})
114111
}

internals/proxy/middlewares/body.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,22 @@ import (
66
"net/http"
77
"strconv"
88

9+
middlewareTypes "github.com/codeshelldev/secured-signal-api/internals/proxy/middlewares/types"
910
"github.com/codeshelldev/secured-signal-api/utils"
1011
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1112
request "github.com/codeshelldev/secured-signal-api/utils/request"
1213
)
1314

14-
type MessageAlias struct {
15-
Alias string
16-
Score int
17-
}
18-
1915
type BodyMiddleware struct {
20-
Next http.Handler
21-
MessageAliases []MessageAlias
16+
Next http.Handler
2217
}
2318

2419
func (data BodyMiddleware) Use() http.Handler {
2520
next := data.Next
26-
messageAliases := data.MessageAliases
2721

2822
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
23+
messageAliases := GetSettings(req).MESSAGE_ALIASES
24+
2925
body, err := request.GetReqBody(w, req)
3026

3127
if err != nil {
@@ -70,7 +66,7 @@ func (data BodyMiddleware) Use() http.Handler {
7066
})
7167
}
7268

73-
func getMessage(aliases []MessageAlias, data map[string]any) (string, map[string]any) {
69+
func getMessage(aliases []middlewareTypes.MessageAlias, data map[string]any) (string, map[string]any) {
7470
var content string
7571
var best int
7672

@@ -87,7 +83,7 @@ func getMessage(aliases []MessageAlias, data map[string]any) (string, map[string
8783
return content, data
8884
}
8985

90-
func processAlias(alias MessageAlias, data map[string]any) (string, int, bool) {
86+
func processAlias(alias middlewareTypes.MessageAlias, data map[string]any) (string, int, bool) {
9187
aliasKey := alias.Alias
9288

9389
value, ok := utils.GetByPath(aliasKey, data)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package middlewares
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/codeshelldev/secured-signal-api/utils/config"
7+
)
8+
9+
type Context struct {
10+
Next http.Handler
11+
}
12+
13+
type authType string
14+
15+
const (
16+
Bearer authType = "Bearer"
17+
Basic authType = "Basic"
18+
Query authType = "Query"
19+
None authType = "None"
20+
)
21+
22+
type contextKey string
23+
24+
const tokenKey contextKey = "token"
25+
26+
func GetSettings(req *http.Request) config.SETTING_ {
27+
token := req.Context().Value(tokenKey).(string)
28+
29+
settings := config.ENV.SETTINGS[token]
30+
31+
if settings == nil {
32+
settings = config.ENV.SETTINGS["*"]
33+
}
34+
35+
return *settings
36+
}

internals/proxy/middlewares/endpoints.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import (
99

1010
type EndpointsMiddleware struct {
1111
Next http.Handler
12-
BlockedEndpoints []string
1312
}
1413

1514
func (data EndpointsMiddleware) Use() http.Handler {
1615
next := data.Next
17-
BLOCKED_ENDPOINTS := data.BlockedEndpoints
1816

1917
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
18+
blockedEndpoints := GetSettings(req).BLOCKED_ENDPOINTS
19+
2020
reqPath := req.URL.Path
2121

22-
if slices.Contains(BLOCKED_ENDPOINTS, reqPath) {
22+
if slices.Contains(blockedEndpoints, reqPath) {
2323
log.Warn("User tried to access blocked endpoint: ", reqPath)
2424
http.Error(w, "Forbidden", http.StatusForbidden)
2525
return

internals/proxy/middlewares/template.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import (
1616

1717
type TemplateMiddleware struct {
1818
Next http.Handler
19-
Variables map[string]any
2019
}
2120

2221
func (data TemplateMiddleware) Use() http.Handler {
2322
next := data.Next
24-
VARIABLES := data.Variables
2523

2624
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
25+
variables := GetSettings(req).VARIABLES
26+
2727
body, err := request.GetReqBody(w, req)
2828

2929
if err != nil {
@@ -37,7 +37,7 @@ func (data TemplateMiddleware) Use() http.Handler {
3737
if !body.Empty {
3838
var modified bool
3939

40-
bodyData, modified, err = TemplateBody(body.Data, VARIABLES)
40+
bodyData, modified, err = TemplateBody(body.Data, variables)
4141

4242
if err != nil {
4343
log.Error("Error Templating JSON: ", err.Error())
@@ -51,7 +51,7 @@ func (data TemplateMiddleware) Use() http.Handler {
5151
if req.URL.RawQuery != "" {
5252
var modified bool
5353

54-
req.URL.RawQuery, bodyData, modified, err = TemplateQuery(req.URL, bodyData, VARIABLES)
54+
req.URL.RawQuery, bodyData, modified, err = TemplateQuery(req.URL, bodyData, variables)
5555

5656
if err != nil {
5757
log.Error("Error Templating Query: ", err.Error())
@@ -85,7 +85,7 @@ func (data TemplateMiddleware) Use() http.Handler {
8585
if req.URL.Path != "" {
8686
var modified bool
8787

88-
req.URL.Path, modified, err = TemplatePath(req.URL, VARIABLES)
88+
req.URL.Path, modified, err = TemplatePath(req.URL, variables)
8989

9090
if err != nil {
9191
log.Error("Error Templating Path: ", err.Error())
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package middlewareTypes
2+
3+
type MessageAlias struct {
4+
Alias string
5+
Score int
6+
}

main.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
var initHandler *httputil.ReverseProxy
1616

17-
var ENV config.ENV_
17+
var ENV *config.ENV_
1818

1919
func main() {
2020
logLevel := os.Getenv("LOG_LEVEL")
@@ -32,27 +32,23 @@ func main() {
3232
initHandler = proxy.Create(ENV.API_URL)
3333

3434
body_m4 := middlewares.BodyMiddleware{
35-
Next: initHandler,
36-
MessageAliases: ENV.MESSAGE_ALIASES,
35+
Next: initHandler,
3736
}
3837

3938
temp_m3 := middlewares.TemplateMiddleware{
40-
Next: body_m4.Use(),
41-
Variables: ENV.VARIABLES,
39+
Next: body_m4.Use(),
4240
}
4341

4442
endp_m2 := middlewares.EndpointsMiddleware{
45-
Next: temp_m3.Use(),
46-
BlockedEndpoints: ENV.BLOCKED_ENDPOINTS,
43+
Next: temp_m3.Use(),
4744
}
4845

4946
auth_m1 := middlewares.AuthMiddleware{
5047
Next: endp_m2.Use(),
51-
Tokens: ENV.API_TOKENS,
5248
}
5349

5450
log_m0 := middlewares.LogMiddleware{
55-
Next: auth_m1.Use(),
51+
Next: auth_m1.Use(),
5652
}
5753

5854
log.Info("Initialized Proxy Handler")

utils/config/loader.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strconv"
88
"strings"
99

10-
"github.com/codeshelldev/secured-signal-api/internals/proxy/middlewares"
10+
middlewareTypes "github.com/codeshelldev/secured-signal-api/internals/proxy/middlewares/types"
1111
"github.com/codeshelldev/secured-signal-api/utils"
1212
log "github.com/codeshelldev/secured-signal-api/utils/logger"
1313
"github.com/knadh/koanf/parsers/yaml"
@@ -28,18 +28,18 @@ type ENV_ struct {
2828
type SETTING_ struct {
2929
BLOCKED_ENDPOINTS []string
3030
VARIABLES map[string]any
31-
MESSAGE_ALIASES []middlewares.MessageAlias
31+
MESSAGE_ALIASES []middlewareTypes.MessageAlias
3232
}
3333

34-
var ENV ENV_ = ENV_{
34+
var ENV *ENV_ = &ENV_{
3535
CONFIG_PATH: os.Getenv("CONFIG_PATH"),
3636
DEFAULTS_PATH: os.Getenv("DEFAULTS_PATH"),
3737
TOKENS_DIR: os.Getenv("TOKENS_DIR"),
3838
API_TOKENS: []string{},
3939
SETTINGS: map[string]*SETTING_{
40-
"*": &SETTING_{
40+
"*": {
4141
BLOCKED_ENDPOINTS: []string{},
42-
MESSAGE_ALIASES: []middlewares.MessageAlias{},
42+
MESSAGE_ALIASES: []middlewareTypes.MessageAlias{},
4343
VARIABLES: map[string]any{},
4444
},
4545
},

0 commit comments

Comments
 (0)