-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (136 loc) · 3.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"encoding/json"
"fmt"
"github.com/aarnaud/salt-api-oauth2-middleware/utils"
"github.com/aarnaud/salt-api-oauth2-middleware/utils/helpers"
"github.com/gin-contrib/logger"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"sync"
"syscall"
)
type LoginData struct {
Username string `json:"username" form:"username"`
Password string `json:"password" form:"password"`
Eauth string `json:"eauth" form:"eauth"`
}
type Challenge struct {
Data map[string]string
Mutex sync.Mutex
}
func (c *Challenge) NewChallenge(email string) string {
c.Mutex.Lock()
defer c.Mutex.Unlock()
c.Data[email] = helpers.RandomString(64)
return c.Data[email]
}
func (c *Challenge) CheckChallenge(email string, value string) bool {
c.Mutex.Lock()
defer func() {
delete(c.Data, email)
c.Mutex.Unlock()
}()
return c.Data[email] == value
}
func NewChallengeDB() *Challenge {
return &Challenge{
Data: make(map[string]string),
Mutex: sync.Mutex{},
}
}
func main() {
log.Info().Msg("initializing...")
zerolog.SetGlobalLevel(zerolog.InfoLevel)
appconf := utils.GetConfig()
router := gin.New()
router.Use(logger.SetLogger(logger.WithSkipPath([]string{"/healthz"})))
router.Use(gin.Recovery())
cdb := NewChallengeDB()
if gin.Mode() == gin.DebugMode {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
router.GET("/healthz", func(c *gin.Context) {
cdb.Mutex.Lock()
defer cdb.Mutex.Unlock()
c.JSON(http.StatusOK, gin.H{})
})
router.POST("/login", func(c *gin.Context) {
email := c.Request.Header.Get(appconf.UserHeaderName)
if email != "" {
data := LoginData{
Username: email,
Password: cdb.NewChallenge(email),
Eauth: "rest",
}
jsonstring, err := json.Marshal(data)
if err != nil {
log.Err(err).Msg("failed to marshall data")
c.AbortWithStatus(http.StatusInternalServerError)
}
c.Request.ContentLength = int64(len(jsonstring))
c.Request.Header.Set("Content-Length", fmt.Sprintf("%d", c.Request.ContentLength))
c.Request.Body = io.NopCloser(strings.NewReader(string(jsonstring)))
ReverseProxy(appconf.SaltApiUrl)(c)
return
}
// allow other auth method in salt-api if no header
if appconf.ReverseProxy {
ReverseProxy(appconf.SaltApiUrl)(c)
}
})
router.POST("/_callback", func(c *gin.Context) {
var challengeData LoginData
err := c.Bind(&challengeData)
if err != nil {
log.Err(err).Msg("failed to parse data challenge")
c.AbortWithStatus(http.StatusBadRequest)
return
}
if cdb.CheckChallenge(challengeData.Username, challengeData.Password) {
// return at least one acl to allow connexion
c.JSON(http.StatusOK, []string{
"test.ping",
})
return
}
c.JSON(http.StatusForbidden, gin.H{})
})
if appconf.ReverseProxy {
// send all other request to salt-api as proxy
router.NoRoute(ReverseProxy(appconf.SaltApiUrl))
}
srv := &http.Server{
Addr: appconf.GetListeningAddr(),
Handler: router,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
// if shutdown, ignore error
if err == http.ErrServerClosed {
return
}
log.Error().Err(err).Msgf("failed to listen on %s", appconf.GetListeningAddr())
appconf.GracefullShutdown <- syscall.SIGTERM
}
}()
appconf.WaitForInterruptSignal(srv)
}
func ReverseProxy(target *url.URL) gin.HandlerFunc {
return func(c *gin.Context) {
director := func(req *http.Request) {
req.URL = c.Request.URL
req.URL.Host = target.Host
req.URL.Scheme = target.Scheme
req.Body = c.Request.Body
}
proxy := &httputil.ReverseProxy{Director: director}
proxy.ServeHTTP(c.Writer, c.Request)
}
}