-
Notifications
You must be signed in to change notification settings - Fork 106
/
resultserver.go
252 lines (220 loc) · 6.77 KB
/
resultserver.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright © 2019 Red Hat Inc.
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 main
import (
"context"
"crypto/tls"
"crypto/x509"
"flag"
"io"
"io/ioutil"
"net/http"
"os"
"os/signal"
"path"
"path/filepath"
"sort"
"strings"
"syscall"
"time"
"github.com/operator-framework/operator-sdk/pkg/log/zap"
"github.com/spf13/cobra"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
libgocrypto "github.com/openshift/library-go/pkg/crypto"
utils "github.com/openshift/compliance-operator/pkg/utils"
)
var resultServerCmd = &cobra.Command{
Use: "resultserver",
Short: "A tool to receive raw SCAP scan results.",
Long: "A tool to receive raw SCAP scan results.",
Run: func(cmd *cobra.Command, args []string) {
server(parseResultServerConfig(cmd))
},
}
func init() {
rootCmd.AddCommand(resultServerCmd)
defineResultServerFlags(resultServerCmd)
}
func defineResultServerFlags(cmd *cobra.Command) {
cmd.Flags().String("address", "1.1.1.1", "Server address")
cmd.Flags().String("port", "8443", "Server port")
cmd.Flags().String("path", "/", "Content path")
cmd.Flags().String("owner", "", "Object owner")
cmd.Flags().String("scan-index", "", "The current index of the scan")
cmd.Flags().String("tls-server-cert", "", "Path to the server cert")
cmd.Flags().String("tls-server-key", "", "Path to the server key")
cmd.Flags().String("tls-ca", "", "Path to the CA certificate")
cmd.Flags().Uint16("rotation", 3, "Amount of raw result directories to keep")
flags := cmd.Flags()
flags.AddFlagSet(zap.FlagSet())
// Add flags registered by imported packages (e.g. glog and
// controller-runtime)
flags.AddGoFlagSet(flag.CommandLine)
}
type resultServerConfig struct {
Address string
Port string
BasePath string
Path string
Cert string
Key string
CA string
Rotation uint16
}
func parseResultServerConfig(cmd *cobra.Command) *resultServerConfig {
basePath := getValidStringArg(cmd, "path")
index := getValidStringArg(cmd, "scan-index")
rotation, _ := cmd.Flags().GetUint16("rotation")
conf := &resultServerConfig{
Address: getValidStringArg(cmd, "address"),
Port: getValidStringArg(cmd, "port"),
BasePath: basePath,
Path: filepath.Join(basePath, index),
Cert: getValidStringArg(cmd, "tls-server-cert"),
Key: getValidStringArg(cmd, "tls-server-key"),
CA: getValidStringArg(cmd, "tls-ca"),
Rotation: rotation,
}
logf.SetLogger(zap.Logger())
return conf
}
func ensureDir(path string) error {
err := os.MkdirAll(path, 0750)
if err != nil && !os.IsExist(err) {
log.Error(err, "Couldn't ensure directory")
return err
}
return nil
}
func rotateResultDirectories(rootPath string, rotation uint16) error {
// If rotation is a negative number, we don't rotate
if rotation == 0 {
log.Info("Rotation policy set to '0'. No need to rotate.")
return nil
}
dirs := []utils.Directory{}
err := filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Error(err, "Error accessing directory. Prevent panic by handling failure accessing a path", "filepath", path)
return err
}
if path == rootPath {
// Do nothing on base directory
return nil
}
if strings.Contains(path, "lost+found") {
// Do nothing on base directory
log.Info("Rotation: Skipping 'lost+found' directory")
return filepath.SkipDir
}
if info.IsDir() {
dirs = append(dirs, utils.NewDirectory(path, info))
return filepath.SkipDir
}
return nil
})
if err != nil {
log.Error(err, "Couldn't rotate directories")
return err
}
sort.Slice(dirs, func(i, j int) bool { return dirs[i].CreationTime.After(dirs[j].CreationTime) })
var lastError error
// No need to rotate, we're whithin the policy
if len(dirs) <= int(rotation) {
return nil
}
for _, dir := range dirs[rotation:] {
log.Info("Removing directory because of rotation policy", "directory", dir.Path)
err := os.RemoveAll(dir.Path)
if err != nil {
lastError = err
}
}
return lastError
}
func server(c *resultServerConfig) {
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
err := ensureDir(c.Path)
if err != nil {
log.Error(err, "Error ensuring result path: %s", c.Path)
os.Exit(1)
}
rotateResultDirectories(c.BasePath, c.Rotation)
caCert, err := ioutil.ReadFile(c.CA)
if err != nil {
log.Error(err, "Error reading CA file")
os.Exit(1)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig := &tls.Config{}
// Configures TLS 1.2
tlsConfig = libgocrypto.SecureTLSConfig(tlsConfig)
tlsConfig.ClientCAs = caCertPool
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
tlsConfig.BuildNameToCertificate()
server := &http.Server{
Addr: c.Address + ":" + c.Port,
TLSConfig: tlsConfig,
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
filename := r.Header.Get("X-Report-Name")
if filename == "" {
log.Info("Rejecting. No \"X-Report-Name\" header given.")
http.Error(w, "Missing report name header", 400)
return
}
encoding := r.Header.Get("Content-Encoding")
extraExtension := encoding
if encoding != "" && encoding != "bzip2" {
log.Info("Rejecting. Invalid \"Content-Encoding\" header given.")
http.Error(w, "invalid content encoding header", 400)
return
} else if encoding == "bzip2" {
extraExtension = "." + extraExtension
}
// TODO(jaosorior): Check that content-type is application/xml
filePath := path.Join(c.Path, filename+".xml"+extraExtension)
f, err := os.Create(filePath)
if err != nil {
log.Info("Error creating file", "file-path", filePath)
http.Error(w, "Error creating file", 500)
return
}
// #nosec
defer f.Close()
_, err = io.Copy(f, r.Body)
if err != nil {
log.Info("Error writing file", "file-path", filePath)
http.Error(w, "Error writing file", 500)
return
}
log.Info("Received file", "file-path", filePath)
})
log.Info("Listening...")
go func() {
err := server.ListenAndServeTLS(c.Cert, c.Key)
if err != nil && err != http.ErrServerClosed {
log.Error(err, "Error in result server")
}
}()
<-exit
log.Info("Server stopped.")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Error(err, "Server shutdown failed")
}
log.Info("Server exited gracefully")
}