/
main.go
151 lines (129 loc) · 3.44 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"github.com/bjorand/nombda/engine"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
var (
log = logrus.New()
listenAddr string
token = os.Getenv("NOMBDA_TOKEN")
configDir = os.Getenv("CONFIG_DIR")
version string
showVersion bool
hookEngine *engine.HookEngine
)
type tokenHeader struct {
AuthToken string `header:"Auth-Token"`
}
func Base() gin.HandlerFunc {
return func(c *gin.Context) {
// Do something
c.Next()
}
}
func AuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
if isTokenAuthenticated(c) {
c.Next()
return
}
// c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
// c.
}
}
func isTokenAuthenticated(c *gin.Context) bool {
t := tokenHeader{}
if err := c.ShouldBindHeader(&t); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, err)
return false
}
if t.AuthToken != token {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
}
return false
}
func main() {
flag.StringVar(&listenAddr, "listen-addr", ":8080", "server listen address")
flag.BoolVar(&showVersion, "version", false, "show version")
flag.Parse()
if showVersion {
fmt.Printf("nopm-sh version %s", version)
os.Exit(0)
}
token = strings.TrimSpace(token)
if token == "" {
log.Fatal("Empty NOMBDA_TOKEN environment variable. Failing to start.")
}
configDir = strings.TrimSpace(configDir)
if configDir == "" {
log.Fatal("Empty CONFIG_DIR environment variable. Failing to start.")
}
hookEngine := engine.NewHookEngine(configDir)
hookEngine.Secrets = engine.ReadSecretFromEnv()
router := gin.Default()
router.Use(gin.Recovery())
router.Use(Base())
authorized := router.Group("/")
authorized.Use(AuthRequired())
router.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"version": version})
})
authorized.GET("/hooks", func(c *gin.Context) {
hooks, err := hookEngine.Hooks()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"hooks": hooks})
})
authorized.POST("/hooks/:id/:action", func(c *gin.Context) {
hook, err := hookEngine.ReadHook(configDir, c.Param("id"), c.Param("action"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
run, err := hook.Run()
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{"id": run.ID})
})
authorized.GET("/hooks/:id/:action/:run_id", func(c *gin.Context) {
hook, err := hookEngine.ReadHook(configDir, c.Param("id"), c.Param("action"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
run, err := hook.GetRun(c.Param("run_id"))
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"message": err})
return
}
c.JSON(http.StatusOK, gin.H{"run": gin.H{
"completed": run.Completed,
"id": run.ID,
"exit_code": run.ExitCode,
}})
})
authorized.GET("/hooks/:id/:action/:run_id/log", func(c *gin.Context) {
hook, err := hookEngine.ReadHook(configDir, c.Param("id"), c.Param("action"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}
run, err := hook.GetRun(c.Param("run_id"))
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"message": err})
return
}
c.String(http.StatusOK, run.Log())
})
router.Run(listenAddr)
}