This repository was archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
180 lines (151 loc) · 4.49 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/furio/kes-hsm-go/auth"
keshttp "github.com/furio/kes-hsm-go/http"
"github.com/furio/kes-hsm-go/lib"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
// Version from Makefile
var Version string
// Build from Makefile
var Build string
func main() {
envFile := flag.String("env", "", "a path to a .env file")
boolVersion := flag.Bool("v", false, "a bool")
flag.Usage = func() {
w := flag.CommandLine.Output()
fmt.Fprintf(w, "KES-HSM \n")
fmt.Fprintf(w, "Usage of %s: \n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
// Version
if *boolVersion {
fmt.Fprintf(flag.CommandLine.Output(), "KES-HSM Version: %s, Build: %s \n", Version, Build)
return
}
// Load env file
err := godotenv.Load(*envFile)
if err != nil {
log.Fatal("Error loading .env file in argument -env : " + *envFile)
}
startKes()
}
func startKes() {
// Start
production := os.Getenv("KES_MODE") == "production"
log.Printf("KES-HSM Version: %s, Production: %t\n\n", Version, production)
// Configure AUTH middleware
authMiddleware, err := auth.New(os.Getenv("KES_CONFIG_AUTH"))
if err != nil {
log.Fatal("Cannot configure Auth interface: ", err)
return // log.Fatal quits with error
}
// Connect to HSM
overrideHSMPin := ""
if !production {
overrideHSMPin = os.Getenv("KES_CONFIG_HSM_PIN")
}
hsm, err := lib.NewHSM(os.Getenv("KES_CONFIG_KEYS"), os.Getenv("KES_CONFIG_HSM"), overrideHSMPin)
if err != nil {
log.Fatal("Cannot configure HSM interface: ", err)
return // log.Fatal quits with error
}
// Set Gin mode
if production {
gin.SetMode(gin.ReleaseMode)
}
// Secure server
var srvHTTPS *http.Server = nil
if os.Getenv("KES_HTTPS") != "" {
routerConfig := keshttp.GenerateHTTPRoutes(keshttp.RouterConfig{
Production: production,
EnableKESApi: true,
EnableAuth: !(os.Getenv("KES_HTTPS_AUTH_DISABLED") == "on"),
EnableMetrics: os.Getenv("KES_METRICS") == "on",
AuthMiddleware: authMiddleware,
HsmHandle: hsm,
RouterVersion: map[string]string{"version": Version, "build": Build},
})
srv, err := keshttp.CreateAndRunHTTPSServer(keshttp.SecureWebServerConfig{
BindIP: os.Getenv("KES_HTTPS"),
CaPath: os.Getenv("KES_HTTPS_CA"),
ClientCaPath: os.Getenv("KES_HTTPS_CA_CLIENTS"),
PrivateKeyPath: os.Getenv("KES_HTTPS_KEY"),
CertificatePath: os.Getenv("KES_HTTPS_CERT"),
Production: production,
EnableSelfSignedCerts: os.Getenv("KES_HTTPS_CA_SELFSIGNED") != "",
FIPSmode: os.Getenv("KES_HTTPS_FIPS_CYPHERS") == "on",
}, routerConfig)
if err != nil {
log.Printf("Cannot start HTTPS server due to %s\n", err)
return
}
srvHTTPS = srv
}
// Localhost HTTP (default 8080) server
var srvHTTPLocal *http.Server = nil
if os.Getenv("KES_HTTP_DISABLE") == "" {
routerConfig := keshttp.GenerateHTTPRoutes(keshttp.RouterConfig{
Production: production,
EnableKESApi: !production,
EnableAuth: false,
EnableMetrics: os.Getenv("KES_METRICS") == "on",
AuthMiddleware: authMiddleware,
HsmHandle: hsm,
RouterVersion: map[string]string{"version": Version, "build": Build},
})
srv, err := keshttp.CreateAndRunHTTPServer(keshttp.WebServerConfig{
Port: os.Getenv("KES_HTTP"),
Production: production,
}, routerConfig)
if err != nil {
log.Printf("Cannot start HTTP server due to %s\n", err)
return
}
srvHTTPLocal = srv
}
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
fatal := false
if srvHTTPLocal != nil {
if err := srvHTTPLocal.Shutdown(ctx); err != nil {
log.Println("Server (HTTP) forced to shutdown:", err)
fatal = true
}
}
if srvHTTPS != nil {
if err := srvHTTPS.Shutdown(ctx); err != nil {
log.Println("Server (HTTPS) forced to shutdown:", err)
fatal = true
}
}
log.Println("Server exiting")
// Clear HSM at the end
hsm.Close()
// Exit fatally
if fatal {
os.Exit(1)
}
}