This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
163 lines (148 loc) · 4.24 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"fmt"
"os/exec"
"time"
"go.uber.org/zap"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"io/ioutil"
"os"
controller "github.com/metal-stack/firewall-policy-controller/pkg/controller"
"github.com/metal-stack/firewall-policy-controller/pkg/droptailer"
"github.com/metal-stack/firewall-policy-controller/pkg/watcher"
"github.com/metal-stack/v"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
moduleName = "firewall-policy-controller"
nftFile = "/etc/nftables/firewall-policy-controller.v4"
nftBin = "/usr/sbin/nft"
nftablesService = "nftables.service"
systemctlBin = "/bin/systemctl"
)
var rootCmd = &cobra.Command{
Use: moduleName,
Short: "a service that assembles and enforces firewall rules based on k8s resources",
Version: v.V.String(),
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
var logger *zap.SugaredLogger
func main() {
zap, _ := zap.NewProduction()
defer func() {
_ = zap.Sync()
}()
logger = zap.Sugar()
if err := rootCmd.Execute(); err != nil {
logger.Error("failed executing root command", "error", err)
}
}
func init() {
viper.SetEnvPrefix("firewall")
homedir, err := homedir.Dir()
if err != nil {
logger.Fatal(err)
}
rootCmd.PersistentFlags().StringP("kubecfg", "k", homedir+"/.kube/config", "kubecfg path to the cluster to account")
rootCmd.PersistentFlags().Bool("dry-run", false, "just print the rules that would be enforced without applying them")
rootCmd.PersistentFlags().Duration("fetch-interval", 10*time.Second, "interval for reassembling firewall rules")
viper.AutomaticEnv()
err = viper.BindPFlags(rootCmd.PersistentFlags())
if err != nil {
logger.Fatal(err)
}
}
func run() {
client, err := loadClient(viper.GetString("kubecfg"))
if err != nil {
logger.Errorw("unable to connect to k8s", "error", err)
os.Exit(1)
}
ctr := controller.NewFirewallController(client, logger)
svcWatcher := watcher.NewServiceWatcher(logger, client)
npWatcher := watcher.NewNetworkPolicyWatcher(logger, client)
dropTailer, err := droptailer.NewDropTailer(logger, client)
if err != nil {
logger.Errorw("unable to create droptailer client", "error", err)
os.Exit(1)
}
// watch for services and network policies
c := make(chan bool)
go svcWatcher.Watch(c)
go npWatcher.Watch(c)
go dropTailer.WatchServerIP()
go dropTailer.WatchClientSecret()
// regularly trigger fetch of k8s resources
go func() {
t := time.NewTicker(viper.GetDuration("fetch-interval"))
for {
<-t.C
c <- true
}
}()
// debounce events and handle fetch
d := time.Second * 3
t := time.NewTimer(d)
var old *controller.FirewallRules
var new *controller.FirewallRules
for {
select {
case <-c:
t.Reset(d)
case <-t.C:
new, err = ctr.FetchAndAssemble()
if err != nil {
logger.Errorw("could not fetch k8s entities to build firewall rules", "error", err)
}
if !new.HasChanged(old) {
old = new
continue
}
old = new
logger.Infow("new fw rules to enforce", "ingress", len(new.IngressRules), "egress", len(new.EgressRules))
for k, i := range new.IngressRules {
fmt.Printf("%d ingress: %s\n", k+1, i)
}
for k, e := range new.EgressRules {
fmt.Printf("%d egress: %s\n", k+1, e)
}
if !viper.GetBool("dry-run") {
rs, err := new.Render()
if err != nil {
logger.Errorw("error rendering nftables rules", "error", err)
continue
}
err = ioutil.WriteFile(nftFile, []byte(rs), 0644)
if err != nil {
logger.Errorw("error writing nftables file", "file", nftFile, "error", err)
continue
}
c := exec.Command(nftBin, "-c", "-f", nftFile)
out, err := c.Output()
if err != nil {
logger.Errorw("nftables file is invalid", "file", nftFile, "error", fmt.Sprint(out))
continue
}
c = exec.Command(systemctlBin, "reload", nftablesService)
err = c.Run()
if err != nil {
logger.Errorw("nftables.service file could not be reloaded")
continue
}
logger.Info("applied new set of nftable rules")
}
}
}
}
func loadClient(kubeconfigPath string) (*k8s.Clientset, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
if err != nil {
return nil, err
}
return k8s.NewForConfig(config)
}