This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
centOS.go
183 lines (149 loc) · 4.59 KB
/
centOS.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package firewall
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/pkg/errors"
"github.com/caos/orbos/internal/operator/common"
"github.com/caos/orbos/internal/operator/nodeagent"
"github.com/caos/orbos/mntr"
)
func centosEnsurer(monitor mntr.Monitor, ignore []string) nodeagent.FirewallEnsurer {
return nodeagent.FirewallEnsurerFunc(func(desired common.Firewall) ([]*common.Allowed, func() error, error) {
outBuf := new(bytes.Buffer)
defer outBuf.Reset()
errBuf := new(bytes.Buffer)
defer errBuf.Reset()
cmd := exec.Command("firewall-cmd", "--list-ports", "--zone", "public")
cmd.Stderr = errBuf
cmd.Stdout = outBuf
if err := cmd.Run(); err != nil {
return nil, nil, errors.Wrapf(err, "running firewall-cmd --list-ports in order to get the already open firewalld ports failed with stderr %s", errBuf.String())
}
stdout := outBuf.String()
if monitor.IsVerbose() {
fmt.Println(strings.Join(cmd.Args, " "))
fmt.Println(stdout)
}
alreadyOpen := strings.Fields(stdout)
addPorts := make([]string, 0)
removePorts := make([]string, 0)
ensureOpen := append(desired.Ports(), ignoredPorts(ignore)...)
openloop:
for _, des := range ensureOpen {
desStr := fmt.Sprintf("%s/%s", des.Port, des.Protocol)
for _, already := range alreadyOpen {
if desStr == already {
continue openloop
}
}
addPorts = append(addPorts, fmt.Sprintf("--add-port=%s", desStr))
}
current := make([]*common.Allowed, len(alreadyOpen))
closeloop:
for idx, already := range alreadyOpen {
fields := strings.Split(already, "/")
port := fields[0]
protocol := fields[1]
current[idx] = &common.Allowed{Port: port, Protocol: protocol}
for _, des := range ensureOpen {
if des.Port == port && des.Protocol == protocol {
continue closeloop
}
}
removePorts = append(removePorts, fmt.Sprintf("--remove-port=%s", already))
}
cmd = exec.Command("systemctl", "is-active", "firewalld")
if monitor.IsVerbose() {
fmt.Println(strings.Join(cmd.Args, " "))
cmd.Stdout = os.Stdout
}
monitor.WithFields(map[string]interface{}{
"open": strings.Join(addPorts, ";"),
"close": strings.Join(removePorts, ";"),
}).Debug("firewall changes determined")
if cmd.Run() != nil || len(addPorts) == 0 && len(removePorts) == 0 {
monitor.Debug("Not changing firewall")
return current, nil, nil
}
return current, func() error {
if err := ensure(monitor, addPorts); err != nil {
return err
}
return ensure(monitor, removePorts)
}, nil
})
}
func ignoredPorts(ports []string) []*common.Allowed {
allowed := make([]*common.Allowed, len(ports))
for idx, port := range ports {
allowed[idx] = &common.Allowed{
Port: port,
Protocol: "tcp",
}
}
return allowed
}
func ensure(monitor mntr.Monitor, changes []string) error {
errBuf := new(bytes.Buffer)
defer errBuf.Reset()
cmd := exec.Command("systemctl", "enable", "firewalld")
cmd.Stderr = errBuf
fullCmd := strings.Join(cmd.Args, " ")
if monitor.IsVerbose() {
fmt.Println(fullCmd)
cmd.Stdout = os.Stdout
}
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "running %s failed with stderr %s", fullCmd, errBuf.String())
}
errBuf.Reset()
cmd = exec.Command("systemctl", "start", "firewalld")
cmd.Stderr = errBuf
fullCmd = strings.Join(cmd.Args, " ")
if monitor.IsVerbose() {
fmt.Println(fullCmd)
cmd.Stdout = os.Stdout
}
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "running %s failed with stderr %s", fullCmd, errBuf.String())
}
return changeFirewall(monitor, changes)
}
func changeFirewall(monitor mntr.Monitor, changes []string) (err error) {
changesMonitor := monitor.WithField("changes", strings.Join(changes, ";"))
changesMonitor.Debug("Changing firewall")
defer func() {
if err == nil {
changesMonitor.Debug("firewall changed")
} else {
changesMonitor.Error(err)
}
}()
errBuf := new(bytes.Buffer)
defer errBuf.Reset()
if len(changes) == 0 {
return nil
}
errBuf.Reset()
cmd := exec.Command("firewall-cmd", append([]string{"--permanent", "--zone", "public"}, changes...)...)
cmd.Stderr = errBuf
fullCmd := strings.Join(cmd.Args, " ")
if monitor.IsVerbose() {
fmt.Println(fullCmd)
cmd.Stdout = os.Stdout
}
if err := cmd.Run(); err != nil {
return errors.Wrapf(err, "running %s failed with stderr %s", fullCmd, errBuf.String())
}
errBuf.Reset()
cmd = exec.Command("firewall-cmd", "--reload")
cmd.Stderr = errBuf
if monitor.IsVerbose() {
fmt.Println(strings.Join(cmd.Args, " "))
cmd.Stdout = os.Stdout
}
return errors.Wrapf(cmd.Run(), "running firewall-cmd --reload failed with stderr %s", errBuf.String())
}