-
Notifications
You must be signed in to change notification settings - Fork 20
/
approve_list_svc.go
84 lines (71 loc) · 1.56 KB
/
approve_list_svc.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
package main
import (
"errors"
"fmt"
"os"
"github.com/ecadlabs/signatory/cmd/approve-list-svc/server"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "approve-list-svc",
Short: "Example IP approve list external policy service",
}
var confFile string
var pubCmd = &cobra.Command{
Use: "pub",
Short: "Print the authentication public key",
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := ReadConfig(confFile)
if err != nil {
return err
}
priv, err := conf.GetPrivateKey()
if err != nil {
return err
}
if priv == nil {
return errors.New("private key is not specified")
}
fmt.Printf("Public key: %v\n", priv.Public())
return nil
},
}
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start the server",
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := ReadConfig(confFile)
if err != nil {
return err
}
pk, err := conf.GetPrivateKey()
if err != nil {
return err
}
ips, nets, err := conf.Addresses()
if err != nil {
return err
}
srv := server.Server{
Address: conf.Address,
PrivateKey: pk,
Addresses: ips,
Nets: nets,
}
s := srv.New()
log.Printf("HTTP server is listening for connections on %s", srv.Address)
log.Println(s.ListenAndServe())
return nil
},
}
func init() {
rootCmd.PersistentFlags().StringVarP(&confFile, "config", "c", "", "Config file")
rootCmd.AddCommand(pubCmd)
rootCmd.AddCommand(serveCmd)
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}