-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (70 loc) · 1.73 KB
/
main.go
File metadata and controls
82 lines (70 loc) · 1.73 KB
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
package main
import (
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
const userKey = "user"
func main() {
if len(os.Args) < 2{
fmt.Println("must specify config yaml as args")
os.Exit(1)
}
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
conf := &Config{}
err := conf.LoadConfig(os.Args[1])
if err != nil {
return false, err
}
err = conf.Auth.LoadUserPass(os.Args[1])
if err != nil {
return false, err
}
if conf.Auth.IsPermitted(username, password) {
c.Set(userKey, username)
return true, nil
} else {
return false, fmt.Errorf("not permitted")
}
}))
// Routes
e.GET("/ping", renew)
// Start server
e.Logger.Fatal(e.Start(":1323"))
}
// Handler
func renew(c echo.Context) (err error) {
remoteIP := strings.Split(c.Request().RemoteAddr, ":")[0]
conf := &Config{}
err = conf.LoadConfig(os.Args[1])
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
err = conf.AwsSg.Init()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
desc := fmt.Sprintf("%s %s", c.Get(userKey), time.Now().Format("20060102150405"))
err = conf.AwsSg.AuthSgIngress(remoteIP, desc)
if err != nil {
if !strings.Contains(err.Error(), "Duplicate") {
return c.String(http.StatusInternalServerError, err.Error())
}
}
groupIds := make([]string, 1)
groupIds[0] = conf.AwsSg.SgId
sgs, err := conf.AwsSg.ListSg(groupIds)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.String(http.StatusOK, sgs[0].String())
}