| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package servecmd |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "net" |
| 23 | "net/http" |
| 24 | |
| 25 | "github.com/rs/zerolog/log" |
| 26 | "github.com/spf13/cobra" |
| 27 | |
| 28 | "github.com/sassoftware/relic/v7/cmdline/shared" |
| 29 | "github.com/sassoftware/relic/v7/server/daemon" |
| 30 | |
| 31 | _ "net/http/pprof" |
| 32 | ) |
| 33 | |
| 34 | var ServeCmd = &cobra.Command{ |
| 35 | Use: "serve", |
| 36 | Short: "Offer signing services over a HTTPS API", |
| 37 | RunE: serveCmd, |
| 38 | } |
| 39 | |
| 40 | var argTest bool |
| 41 | |
| 42 | func init() { |
| 43 | shared.RootCmd.AddCommand(ServeCmd) |
| 44 | ServeCmd.Flags().BoolP("force", "f", false, "(ignored)") |
| 45 | ServeCmd.Flags().BoolVarP(&argTest, "test", "t", false, "Test configuration and exit") |
| 46 | } |
| 47 | |
| 48 | func MakeServer() (*daemon.Daemon, error) { |
| 49 | if err := shared.InitConfig(); err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | if shared.CurrentConfig.Server == nil { |
| 53 | return nil, errors.New("Missing server section in configuration file") |
| 54 | } |
| 55 | if shared.CurrentConfig.Clients == nil { |
| 56 | return nil, errors.New("Missing clients section in configuration file") |
| 57 | } |
| 58 | if shared.CurrentConfig.Server.Listen == "" && shared.CurrentConfig.Server.ListenHTTP == "" { |
| 59 | shared.CurrentConfig.Server.Listen = ":6300" |
| 60 | } |
| 61 | if shared.CurrentConfig.Server.Listen != "" { |
| 62 | if shared.CurrentConfig.Server.KeyFile == "" { |
| 63 | return nil, errors.New("missing keyfile option in server configuration file") |
| 64 | } |
| 65 | if shared.CurrentConfig.Server.CertFile == "" { |
| 66 | return nil, errors.New("missing certfile option in server configuration file") |
| 67 | } |
| 68 | } |
| 69 | return daemon.New(shared.CurrentConfig, argTest) |
| 70 | } |
| 71 | |
| 72 | func listenDebug() error { |
| 73 | if !shared.CurrentConfig.Server.ListenDebug { |
| 74 | return nil |
| 75 | } |
| 76 | lis, err := net.Listen("tcp", ":0") |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | log.Info().Msgf("serving debug info on http://%s/debug/pprof/", lis.Addr()) |
| 81 | go func() { |
| 82 | // pprof installs itself into the default handler on import |
| 83 | err := http.Serve(lis, nil) |
| 84 | log.Err(err).Msg("debug listener stopped") |
| 85 | }() |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | func serveCmd(cmd *cobra.Command, args []string) error { |
| 90 | // let journald add timestamps |
| 91 | srv, err := MakeServer() |
| 92 | if err != nil { |
| 93 | return shared.Fail(err) |
| 94 | } else if argTest { |
| 95 | fmt.Println("OK") |
| 96 | return nil |
| 97 | } |
| 98 | go watchSignals(srv) |
| 99 | if err := listenDebug(); err != nil { |
| 100 | return err |
| 101 | } |
| 102 | if err := srv.Serve(); err != nil && err != http.ErrServerClosed { |
| 103 | return shared.Fail(err) |
| 104 | } |
| 105 | return nil |
| 106 | } |