Browse Source

feat(server): Support https for rest service (#300)

* feat(server): Support https for rest service

* feat(server): use absolute path for tls config by default
ngjaying 4 years ago
parent
commit
56670d9466

+ 13 - 7
common/util.go

@@ -49,14 +49,20 @@ func LoadConf(confName string) ([]byte, error) {
 	return b, nil
 }
 
+type tlsConf struct {
+	Certfile string `yaml:"certfile"`
+	Keyfile  string `yaml:"keyfile"`
+}
+
 type XStreamConf struct {
-	Debug          bool `yaml:"debug"`
-	ConsoleLog     bool `yaml:"consoleLog"`
-	FileLog        bool `yaml:"fileLog"`
-	Port           int  `yaml:"port"`
-	RestPort       int  `yaml:"restPort"`
-	Prometheus     bool `yaml:"prometheus"`
-	PrometheusPort int  `yaml:"prometheusPort"`
+	Debug          bool     `yaml:"debug"`
+	ConsoleLog     bool     `yaml:"consoleLog"`
+	FileLog        bool     `yaml:"fileLog"`
+	Port           int      `yaml:"port"`
+	RestPort       int      `yaml:"restPort"`
+	RestTls        *tlsConf `yaml:"restTls"`
+	Prometheus     bool     `yaml:"prometheus"`
+	PrometheusPort int      `yaml:"prometheusPort"`
 }
 
 func init() {

+ 29 - 0
docs/en_US/operation/configuration_file.md

@@ -7,8 +7,37 @@ The configuration file for Kuiper is at ``$kuiper/etc/kuiper.yaml``. The configu
 basic:
   # true|false, with debug level, it prints more debug info
   debug: false
+  # true|false, if it's set to true, then the log will be print to console
+  consoleLog: false
+  # true|false, if it's set to true, then the log will be print to log file
+  fileLog: true
 ```
 
+## Cli Port
+```yaml
+basic:
+  # CLI port
+  port: 20498
+```
+The port that the CLI server listens on
+
+## Rest Service Configuration
+
+```yaml
+basic:
+  # REST service port
+  restPort: 9081
+  restTls:
+    certfile: /var/https-server.crt
+    keyfile: /var/https-server.key
+```
+
+#### restPort
+The port for the rest api http server to listen to.
+
+#### restTls
+The tls cert file path and key file path setting. If restTls is not set, the rest api server will listen on http. Otherwise, it will listen on https.
+
 ## Prometheus Configuration
 
 Kuiper can export metrics to prometheus if ``prometheus`` option is true. The prometheus will be served with the port specified by ``prometheusPort`` option.

+ 28 - 0
docs/zh_CN/operation/configuration_file.md

@@ -7,7 +7,35 @@ Kuiper的配置文件位于$ kuiper / etc / kuiper.yaml中。 配置文件为yam
 basic:
   # true|false, with debug level, it prints more debug info
   debug: false
+  # true|false, if it's set to true, then the log will be print to console
+  consoleLog: false
+  # true|false, if it's set to true, then the log will be print to log file
+  fileLog: true
 ```
+## Cli端口
+```yaml
+basic:
+  # CLI port
+  port: 20498
+```
+CLI服务器监听端口
+
+## REST服务配置
+
+```yaml
+basic:
+  # REST service port
+  restPort: 9081
+  restTls:
+    certfile: /var/https-server.crt
+    keyfile: /var/https-server.key
+```
+
+#### restPort
+REST http服务器监听端口
+
+#### restTls
+TLS证书cert文件和key文件位置。如果restTls选项未配置,则REST服务器将启动为http服务器,否则启动为https服务器。
 
 ## Prometheus配置
 

+ 6 - 0
etc/kuiper.yaml

@@ -5,7 +5,13 @@ basic:
   consoleLog: false
   # true|false, if it's set to true, then the log will be print to log file
   fileLog: true
+  # CLI port
   port: 20498
+  # REST service port
   restPort: 9081
+  #  restTls:
+  #    certfile: /var/https-server.crt
+  #    keyfile: /var/https-server.key
+  # Prometheus settings
   prometheus: false
   prometheusPort: 20499

+ 12 - 3
xstream/server/server/server.go

@@ -92,12 +92,21 @@ func StartUp(Version string) {
 	srv := createRestServer(common.Config.RestPort)
 
 	go func() {
-		if err := srv.ListenAndServe(); err != nil {
+		var err error
+		if common.Config.RestTls == nil {
+			err = srv.ListenAndServe()
+		} else {
+			err = srv.ListenAndServeTLS(common.Config.RestTls.Certfile, common.Config.RestTls.Keyfile)
+		}
+		if err != nil {
 			logger.Fatal("Error serving rest service: ", err)
 		}
 	}()
-
-	msg := fmt.Sprintf("Serving kuiper (version - %s) on port %d, and restful api on port %d. \n", Version, common.Config.Port, common.Config.RestPort)
+	t := "http"
+	if common.Config.RestTls != nil {
+		t = "https"
+	}
+	msg := fmt.Sprintf("Serving kuiper (version - %s) on port %d, and restful api on %s://0.0.0.0:%d. \n", Version, common.Config.Port, t, common.Config.RestPort)
 	logger.Info(msg)
 	fmt.Printf(msg)