2016-02-29 17:28:59 +00:00
|
|
|
// Copyright © 2016 Abcum Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2016-04-08 19:29:13 +00:00
|
|
|
"fmt"
|
2017-02-21 00:09:42 +00:00
|
|
|
"net"
|
2016-04-08 19:29:13 +00:00
|
|
|
"os"
|
2017-11-16 19:49:32 +00:00
|
|
|
"path"
|
2016-07-16 13:43:53 +00:00
|
|
|
"regexp"
|
2016-02-29 17:28:59 +00:00
|
|
|
"strings"
|
2016-04-08 19:29:13 +00:00
|
|
|
|
2018-04-06 00:15:19 +00:00
|
|
|
"encoding/pem"
|
|
|
|
|
2016-09-06 10:19:29 +00:00
|
|
|
"github.com/abcum/surreal/cnf"
|
2016-04-08 19:29:13 +00:00
|
|
|
"github.com/abcum/surreal/log"
|
2017-02-20 16:16:35 +00:00
|
|
|
"github.com/abcum/surreal/util/rand"
|
2016-04-08 19:29:13 +00:00
|
|
|
"github.com/abcum/surreal/util/uuid"
|
2016-02-29 17:28:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setup() {
|
|
|
|
|
2016-07-06 18:02:29 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// DB
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
|
|
|
// Ensure that the default
|
|
|
|
// database options are set
|
|
|
|
|
2017-02-09 11:16:59 +00:00
|
|
|
if opts.DB.Path == "" {
|
|
|
|
opts.DB.Path = "memory"
|
2016-07-06 18:02:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 11:16:59 +00:00
|
|
|
if opts.DB.Base == "" {
|
|
|
|
opts.DB.Base = "surreal"
|
2016-07-16 13:43:53 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 11:05:32 +00:00
|
|
|
if opts.DB.Code != "" {
|
|
|
|
opts.DB.Key = []byte(opts.DB.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch len(opts.DB.Key) {
|
|
|
|
case 0, 16, 24, 32:
|
|
|
|
default:
|
|
|
|
log.Fatal("Specify a valid encryption key length. Valid key sizes are 16bit, 24bit, or 32bit.")
|
|
|
|
}
|
|
|
|
|
2017-02-09 11:16:59 +00:00
|
|
|
if opts.DB.Path != "memory" {
|
2018-07-24 10:18:17 +00:00
|
|
|
if ok, _ := regexp.MatchString(`^(s3|gcs|logr|file|dendrodb)://(.+)$`, opts.DB.Path); !ok {
|
2017-11-16 19:49:14 +00:00
|
|
|
log.Fatalf("Invalid path %s. Specify a valid data store configuration path", opts.DB.Path)
|
2017-02-09 11:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 10:56:21 +00:00
|
|
|
if opts.DB.Proc.Size == 0 {
|
|
|
|
opts.DB.Proc.Size = 5
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.DB.Proc.Size < 0 {
|
|
|
|
log.Fatal("Specify a valid data file size policy. Valid sizes are greater than 0 and are specified in MB.")
|
|
|
|
}
|
|
|
|
|
2018-04-06 00:15:19 +00:00
|
|
|
if opts.DB.Cert.CA != "" || opts.DB.Cert.Crt != "" || opts.DB.Cert.Key != "" {
|
2016-07-19 11:05:11 +00:00
|
|
|
|
2018-04-06 00:15:19 +00:00
|
|
|
opts.DB.Cert.SSL = true
|
2016-07-19 11:05:11 +00:00
|
|
|
|
2018-04-06 00:15:19 +00:00
|
|
|
if dec, _ := pem.Decode([]byte(opts.DB.Cert.CA)); dec == nil || dec.Type != "CERTIFICATE" {
|
|
|
|
log.Fatal("Specify a valid PEM encoded CA file.")
|
2016-07-19 11:05:11 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 00:15:19 +00:00
|
|
|
if dec, _ := pem.Decode([]byte(opts.DB.Cert.Crt)); dec == nil || dec.Type != "CERTIFICATE" {
|
|
|
|
log.Fatal("Specify a valid PEM encoded certificate file.")
|
|
|
|
}
|
2016-07-19 11:05:11 +00:00
|
|
|
|
2018-04-06 00:15:19 +00:00
|
|
|
if dec, _ := pem.Decode([]byte(opts.DB.Cert.Key)); dec == nil || dec.Type != "RSA PRIVATE KEY" {
|
|
|
|
log.Fatal("Specify a valid PEM encoded private key file.")
|
|
|
|
}
|
2016-07-19 11:05:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-07-06 18:02:29 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Auth
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2016-04-08 19:29:13 +00:00
|
|
|
if opts.Auth.Auth != "" {
|
|
|
|
|
|
|
|
if opts.Auth.User != "" {
|
|
|
|
log.Fatal("Specify only --auth or --auth-user")
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Auth.Pass != "" {
|
|
|
|
log.Fatal("Specify only --auth or --auth-pass")
|
|
|
|
}
|
2016-02-29 17:28:59 +00:00
|
|
|
|
2016-04-08 19:29:13 +00:00
|
|
|
both := strings.SplitN(opts.Auth.Auth, ":", 2)
|
2016-02-29 17:28:59 +00:00
|
|
|
|
|
|
|
if len(both) == 2 {
|
2016-04-08 19:29:13 +00:00
|
|
|
opts.Auth.User = both[0]
|
|
|
|
opts.Auth.Pass = both[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that security
|
|
|
|
// is enabled by default
|
|
|
|
|
|
|
|
if opts.Auth.User == "" {
|
|
|
|
opts.Auth.User = "root"
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Auth.Pass == "" {
|
2017-02-20 16:16:35 +00:00
|
|
|
opts.Auth.Pass = string(rand.New(20))
|
2016-04-08 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 19:46:41 +00:00
|
|
|
// Ensure that login as
|
|
|
|
// root can only be from
|
|
|
|
// specified ip addresses
|
2017-02-21 00:09:42 +00:00
|
|
|
|
|
|
|
for _, cidr := range opts.Auth.Addr {
|
|
|
|
_, subn, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Invalid cidr %s. Please specify a valid CIDR address for --auth-addr", cidr)
|
|
|
|
}
|
|
|
|
opts.Auth.Nets = append(opts.Auth.Nets, subn)
|
|
|
|
}
|
|
|
|
|
2016-07-06 18:02:29 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Nodes
|
|
|
|
// --------------------------------------------------
|
2016-04-08 19:29:13 +00:00
|
|
|
|
|
|
|
// Ensure that the default
|
|
|
|
// node details are defined
|
|
|
|
|
|
|
|
if opts.Node.Host == "" {
|
|
|
|
opts.Node.Host, _ = os.Hostname()
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Node.Name == "" {
|
|
|
|
opts.Node.Name = opts.Node.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Node.UUID == "" {
|
2017-11-27 11:34:59 +00:00
|
|
|
opts.Node.UUID = opts.Node.Name + "-" + uuid.New().String()
|
2016-04-08 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 18:02:29 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Ports
|
|
|
|
// --------------------------------------------------
|
2016-04-08 19:29:13 +00:00
|
|
|
|
2016-07-06 18:12:00 +00:00
|
|
|
// Specify default port
|
|
|
|
if opts.Port.Web == 0 {
|
|
|
|
opts.Port.Web = 8000
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specify default port
|
|
|
|
if opts.Port.Tcp == 0 {
|
|
|
|
opts.Port.Tcp = 33693
|
|
|
|
}
|
|
|
|
|
2016-07-06 18:02:29 +00:00
|
|
|
// Ensure port number is valid
|
|
|
|
if opts.Port.Web < 0 || opts.Port.Web > 65535 {
|
2017-02-21 00:07:17 +00:00
|
|
|
log.Fatalf("Invalid port %d. Please specify a valid port number for --port-web", opts.Port.Web)
|
2016-04-08 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 18:02:29 +00:00
|
|
|
// Ensure port number is valid
|
|
|
|
if opts.Port.Tcp < 0 || opts.Port.Tcp > 65535 {
|
2017-02-21 00:07:17 +00:00
|
|
|
log.Fatalf("Invalid port %d. Please specify a valid port number for --port-tcp", opts.Port.Tcp)
|
2016-04-08 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 18:02:29 +00:00
|
|
|
// Store the ports in host:port string format
|
2017-02-09 11:20:23 +00:00
|
|
|
opts.Conn.Web = fmt.Sprintf("%s:%d", opts.Node.Host, opts.Port.Web)
|
|
|
|
opts.Conn.Tcp = fmt.Sprintf("%s:%d", opts.Node.Host, opts.Port.Tcp)
|
2016-04-08 19:29:13 +00:00
|
|
|
|
2016-07-06 18:02:29 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Certs
|
|
|
|
// --------------------------------------------------
|
2016-04-08 19:29:13 +00:00
|
|
|
|
2016-07-18 12:34:50 +00:00
|
|
|
if strings.HasPrefix(opts.Cert.Crt, "-----") {
|
|
|
|
var err error
|
|
|
|
var doc *os.File
|
2017-11-16 19:49:32 +00:00
|
|
|
var out string = path.Join(os.TempDir(), "surreal.crt")
|
|
|
|
if doc, err = os.Create(out); err != nil {
|
|
|
|
log.Fatalf("Can not decode PEM encoded certificate into %s", out)
|
2016-07-04 10:33:07 +00:00
|
|
|
}
|
2016-07-18 12:34:50 +00:00
|
|
|
doc.Write([]byte(opts.Cert.Crt))
|
|
|
|
doc.Close()
|
2017-11-16 19:49:32 +00:00
|
|
|
opts.Cert.Crt = out
|
2016-07-18 12:34:50 +00:00
|
|
|
}
|
2016-04-08 19:29:13 +00:00
|
|
|
|
2016-07-18 12:34:50 +00:00
|
|
|
if strings.HasPrefix(opts.Cert.Key, "-----") {
|
|
|
|
var err error
|
|
|
|
var doc *os.File
|
2017-11-16 19:49:32 +00:00
|
|
|
var out string = path.Join(os.TempDir(), "surreal.key")
|
|
|
|
if doc, err = os.Create(out); err != nil {
|
|
|
|
log.Fatalf("Can not decode PEM encoded private key into %s: %s", out)
|
2016-07-04 10:33:07 +00:00
|
|
|
}
|
2016-07-18 12:34:50 +00:00
|
|
|
doc.Write([]byte(opts.Cert.Key))
|
|
|
|
doc.Close()
|
2017-11-16 19:49:32 +00:00
|
|
|
opts.Cert.Key = out
|
2016-04-08 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 18:02:29 +00:00
|
|
|
// --------------------------------------------------
|
|
|
|
// Logging
|
|
|
|
// --------------------------------------------------
|
|
|
|
|
2017-02-22 14:48:22 +00:00
|
|
|
var chk map[string]bool
|
|
|
|
|
2018-05-02 01:31:31 +00:00
|
|
|
// Setup a default logging
|
|
|
|
// hook for cli output
|
|
|
|
|
|
|
|
logger := &log.DefaultHook{}
|
|
|
|
|
2016-04-08 19:29:13 +00:00
|
|
|
// Ensure that the specified
|
|
|
|
// logging level is allowed
|
|
|
|
|
|
|
|
if opts.Logging.Level != "" {
|
|
|
|
|
2017-02-22 14:48:22 +00:00
|
|
|
chk = map[string]bool{
|
2019-01-14 10:37:15 +00:00
|
|
|
"trace": true,
|
2018-05-02 01:31:31 +00:00
|
|
|
"debug": true,
|
|
|
|
"info": true,
|
|
|
|
"warn": true,
|
|
|
|
"error": true,
|
|
|
|
"fatal": true,
|
|
|
|
"panic": true,
|
2016-04-08 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := chk[opts.Logging.Level]; !ok {
|
|
|
|
log.Fatal("Incorrect log level specified")
|
2016-02-29 17:28:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 01:31:31 +00:00
|
|
|
logger.SetLevel(opts.Logging.Level)
|
2016-04-08 19:29:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-22 14:48:22 +00:00
|
|
|
// Ensure that the specified
|
|
|
|
// logging format is allowed
|
|
|
|
|
|
|
|
if opts.Logging.Format != "" {
|
|
|
|
|
|
|
|
chk = map[string]bool{
|
|
|
|
"text": true,
|
|
|
|
"json": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := chk[opts.Logging.Format]; !ok {
|
|
|
|
log.Fatal("Incorrect log format specified")
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:31:31 +00:00
|
|
|
logger.SetFormat(opts.Logging.Format)
|
2017-02-22 14:48:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-08 19:29:13 +00:00
|
|
|
// Ensure that the specified
|
|
|
|
// logging output is allowed
|
|
|
|
|
|
|
|
if opts.Logging.Output != "" {
|
|
|
|
|
2017-02-22 14:48:22 +00:00
|
|
|
chk = map[string]bool{
|
2017-02-22 01:40:24 +00:00
|
|
|
"none": true,
|
2016-04-08 19:29:13 +00:00
|
|
|
"stdout": true,
|
|
|
|
"stderr": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := chk[opts.Logging.Output]; !ok {
|
|
|
|
log.Fatal("Incorrect log output specified")
|
|
|
|
}
|
|
|
|
|
2018-05-02 01:31:31 +00:00
|
|
|
logger.SetOutput(opts.Logging.Output)
|
2016-04-08 19:29:13 +00:00
|
|
|
|
2016-02-29 17:28:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 01:31:31 +00:00
|
|
|
// Add the default logging hook
|
|
|
|
// to the logger instance
|
|
|
|
|
|
|
|
log.Hook(logger)
|
|
|
|
|
2017-02-22 14:48:22 +00:00
|
|
|
// Enable global options object
|
|
|
|
|
2016-09-06 10:19:29 +00:00
|
|
|
cnf.Settings = opts
|
|
|
|
|
2016-02-29 17:28:59 +00:00
|
|
|
}
|