Skip to content

Commit 89ec5f0

Browse files
committed
enable encryption with the '-tls' flag
* to enable https, start zfs-snap-diff per: zfs-snap-diff -tls -cert <CERT_FILE> -key <KEY_FILE> <ZFS_NAME> * to create a self signed key / certificate use: openssl genrsa -out server.key 2048 openssl req -new -x509 -sha256 -key server.key -out server.pem -days 3650 and start zfs-snap-diff per: zfs-snap-diff -tls -cert server.pem -key server.key <ZFS_NAME>
1 parent ad354f0 commit 89ec5f0

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

main.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ var (
2424
logError *log.Logger
2525
)
2626

27-
// FrontendConfig hold the configuration for the ui
28-
type FrontendConfig map[string]interface{}
27+
type webServerConfig struct {
28+
useTLS bool
29+
certFile string
30+
keyFile string
31+
}
32+
type frontendConfig map[string]interface{}
2933

3034
func main() {
3135
// formate help
@@ -37,6 +41,9 @@ func main() {
3741

3842
// define flags / parse flags
3943
portFlag := flag.Int("p", 12345, "web server port")
44+
useTLSFlag := flag.Bool("tls", false, "use TLS - NOTE: -cert <CERT_FILE> -key <KEY_FILE> are mandatory")
45+
certFileFlag := flag.String("cert", "", "certificate file for TLS")
46+
keyFileFlag := flag.String("key", "", "private key file for TLS")
4047
listenOnAllInterfacesFlag := flag.Bool("a", false, "listen on all interfaces")
4148
printVersionFlag := flag.Bool("V", false, "print version and exit")
4249
verboseLoggingFlag := flag.Bool("v", false, "verbose logging")
@@ -68,12 +75,31 @@ func main() {
6875

6976
// abort if zfs name is missing
7077
if len(zfsName) == 0 {
71-
fmt.Println("parameter <ZFS_NAME> missing")
78+
fmt.Println("ABORT: parameter <ZFS_NAME> missing")
7279
fmt.Println()
7380
flag.Usage()
7481
os.Exit(1)
7582
}
7683

84+
// validate args for tls
85+
if *useTLSFlag {
86+
if len(*certFileFlag) == 0 || len(*keyFileFlag) == 0 {
87+
fmt.Println("ABORT: parameter -cert <CERT_FILE> -key <KEY_FILE> are mandatory")
88+
os.Exit(1)
89+
}
90+
91+
if _, err := os.Stat(*certFileFlag); os.IsNotExist(err) {
92+
fmt.Printf("ABORT: cert file '%s' not found\n", *certFileFlag)
93+
os.Exit(1)
94+
}
95+
96+
if _, err := os.Stat(*keyFileFlag); os.IsNotExist(err) {
97+
fmt.Printf("ABORT: key file '%s' not found\n", *keyFileFlag)
98+
os.Exit(1)
99+
}
100+
}
101+
webServerCfg := webServerConfig{*useTLSFlag, *certFileFlag, *keyFileFlag}
102+
77103
// initialize zfs handler
78104
var err error
79105
zfs, err = NewZFS(zfsName, *useSudoFlag)
@@ -87,9 +113,12 @@ func main() {
87113
var addr string
88114
if *listenOnAllInterfacesFlag {
89115
fmt.Println("")
90-
fmt.Println("!! ** WARNING ** !!")
91-
fmt.Println("!! LISTEN ON ALL INTERFACES !!")
92-
fmt.Println("!! CURRENTLY NO ENCRYPTION / AUTHENTICATION !!")
116+
fmt.Println("!! ** WARNING ** !!")
117+
fmt.Println("!! LISTEN ON ALL INTERFACES !!")
118+
fmt.Println("!! CURRENTLY NO AUTHENTICATION !!")
119+
if !*useTLSFlag {
120+
fmt.Println("\nHINT: USE -tls -cert <CERT_FILE> -key <KEY_FILE> to enable encryption!")
121+
}
93122
fmt.Println("")
94123
addr = fmt.Sprintf(":%d", *portFlag)
95124
} else {
@@ -106,20 +135,19 @@ func main() {
106135
}
107136

108137
// frontend-config
109-
frontendConfig := FrontendConfig{
138+
frontendCfg := frontendConfig{
110139
"diffContextSize": *diffContextSizeFlag,
111140
"defaultFileAction": *defaultFileActionFlag,
112141
"compareFileMethod": *compareFileMethodFlag,
113142
"datasets": zfs.Datasets,
114143
}
115144
if *scanSnapLimitFlag >= 0 {
116145
// only add positive values - negative values: scan all snapshots
117-
frontendConfig["scanSnapLimit"] = *scanSnapLimitFlag
146+
frontendCfg["scanSnapLimit"] = *scanSnapLimitFlag
118147
}
119148

120149
// startup web server
121-
logInfo.Printf("start server and listen on: '%s'\n", addr)
122-
listenAndServe(addr, frontendConfig)
150+
listenAndServe(addr, webServerCfg, frontendCfg)
123151
}
124152

125153
func initLogHandlers(debugHndl, infoHndl, noticeHndl, warnHndl, errorHndl io.Writer) {

web.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ var mimeTypes = map[string]string{
1919
}
2020

2121
// registers response handlers and starts the web server
22-
func listenAndServe(addr string, frontendConfig FrontendConfig) {
23-
http.HandleFunc("/config", configHndl(frontendConfig))
22+
func listenAndServe(addr string, webServerCfg webServerConfig, frontendCfg frontendConfig) {
23+
http.HandleFunc("/config", configHndl(frontendCfg))
2424
http.HandleFunc("/snapshots-for-dataset", snapshotsForDatasetHndl)
2525
http.HandleFunc("/snapshots-for-file", snapshotsForFileHndl)
2626
http.HandleFunc("/snapshot-diff", snapshotDiffHndl)
@@ -38,11 +38,18 @@ func listenAndServe(addr string, frontendConfig FrontendConfig) {
3838
} else {
3939
http.HandleFunc("/", serveStaticContentFromBinaryHndl)
4040
}
41-
logError.Println(http.ListenAndServe(addr, nil))
41+
42+
if webServerCfg.useTLS {
43+
logInfo.Printf("start server and listen on: 'https://%s'\n", addr)
44+
logError.Println(http.ListenAndServeTLS(addr, webServerCfg.certFile, webServerCfg.keyFile, nil))
45+
} else {
46+
logInfo.Printf("start server and listen on: 'http://%s'\n", addr)
47+
logError.Println(http.ListenAndServe(addr, nil))
48+
}
4249
}
4350

4451
// frontend-config
45-
func configHndl(config FrontendConfig) http.HandlerFunc {
52+
func configHndl(config frontendConfig) http.HandlerFunc {
4653
return func(w http.ResponseWriter, r *http.Request) {
4754

4855
// marshal

0 commit comments

Comments
 (0)