-
Notifications
You must be signed in to change notification settings - Fork 106
/
profileparser.go
259 lines (216 loc) · 7.04 KB
/
profileparser.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
253
254
255
256
257
258
259
package main
import (
"bufio"
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
"github.com/operator-framework/operator-sdk/pkg/log/zap"
"github.com/spf13/cobra"
"github.com/subchen/go-xmldom"
"k8s.io/apimachinery/pkg/api/errors"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
cmpv1alpha1 "github.com/openshift/compliance-operator/pkg/apis/compliance/v1alpha1"
"github.com/openshift/compliance-operator/pkg/profileparser"
)
var profileparserCmd = &cobra.Command{
Use: "profileparser",
Short: "Runs the profile parser",
Long: `The profileparser reads a data stream file and generates profile objects from it.`,
Run: runProfileParser,
}
func init() {
rootCmd.AddCommand(profileparserCmd)
defineProfileParserFlags(profileparserCmd)
}
func defineProfileParserFlags(cmd *cobra.Command) {
cmd.Flags().String("ds-path", "/content/ssg-ocp4-ds.xml", "Path to the datastream xml file")
cmd.Flags().String("name", "", "Name of the ProfileBundle object")
cmd.Flags().String("namespace", "", "Namespace of the ProfileBundle object")
flags := cmd.Flags()
flags.AddFlagSet(zap.FlagSet())
// Add flags registered by imported packages (e.g. glog and
// controller-runtime)
flags.AddGoFlagSet(flag.CommandLine)
}
// XMLDocument is a wrapper that keeps the interface XML-parser-agnostic
type XMLDocument struct {
*xmldom.Document
}
func newParserConfig(cmd *cobra.Command) *profileparser.ParserConfig {
pcfg := profileparser.ParserConfig{}
flags := cmd.Flags()
if err := flags.Parse(zap.FlagSet().Args()); err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse zap flagset: %v", zap.FlagSet().Args())
os.Exit(1)
}
pcfg.DataStreamPath = getValidStringArg(cmd, "ds-path")
pcfg.ProfileBundleKey.Name = getValidStringArg(cmd, "name")
pcfg.ProfileBundleKey.Namespace = getValidStringArg(cmd, "namespace")
logf.SetLogger(zap.Logger())
printVersion()
// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
log.Error(err, "")
os.Exit(1)
}
crclient, err := createCrClient(cfg)
if err != nil {
fmt.Printf("Can't kubernetes client: %v\n", err)
os.Exit(1)
}
pcfg.Scheme = crclient.scheme
pcfg.Client = crclient.client
return &pcfg
}
func getProfileBundle(pcfg *profileparser.ParserConfig) (*cmpv1alpha1.ProfileBundle, error) {
pb := cmpv1alpha1.ProfileBundle{}
err := pcfg.Client.Get(context.TODO(), pcfg.ProfileBundleKey, &pb)
if err != nil {
log.Error(err, "")
os.Exit(1)
}
return &pb, nil
}
// updateProfileBundleStatus updates the status of the given ProfileBundle. If
// the given error is nil, the status will be valid, else it'll be invalid
func updateProfileBundleStatus(pcfg *profileparser.ParserConfig, pb *cmpv1alpha1.ProfileBundle, err error) {
if err != nil {
// Never update a fetched object, always just a copy
pbCopy := pb.DeepCopy()
pbCopy.Status.DataStreamStatus = cmpv1alpha1.DataStreamInvalid
pbCopy.Status.ErrorMessage = err.Error()
err = pcfg.Client.Status().Update(context.TODO(), pbCopy)
if err != nil {
log.Error(err, "Couldn't update ProfileBundle status")
os.Exit(1)
}
} else {
// Never update a fetched object, always just a copy
pbCopy := pb.DeepCopy()
pbCopy.Status.DataStreamStatus = cmpv1alpha1.DataStreamValid
err = pcfg.Client.Status().Update(context.TODO(), pbCopy)
if err != nil {
log.Error(err, "Couldn't update ProfileBundle status")
os.Exit(1)
}
}
}
func runProfileParser(cmd *cobra.Command, args []string) {
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)
pcfg := newParserConfig(cmd)
pb, err := getProfileBundle(pcfg)
if err != nil {
log.Error(err, "Couldn't get ProfileBundle")
os.Exit(1)
}
contentFile, err := readContent(pcfg.DataStreamPath)
if err != nil {
log.Error(err, "Couldn't read the content")
updateProfileBundleStatus(pcfg, pb, fmt.Errorf("Couldn't read content file: %s", err))
os.Exit(1)
}
// #nosec
defer contentFile.Close()
bufContentFile := bufio.NewReader(contentFile)
contentDom, err := xmldom.Parse(bufContentFile)
if err != nil {
log.Error(err, "Couldn't read the content XML")
updateProfileBundleStatus(pcfg, pb, fmt.Errorf("Couldn't read content XML: %s", err))
os.Exit(1)
}
err = profileparser.ParseProfilesAndDo(contentDom, pcfg, func(p *cmpv1alpha1.Profile) error {
pCopy := p.DeepCopy()
profileName := pCopy.Name
if pCopy.Labels == nil {
pCopy.Labels = make(map[string]string)
}
pCopy.Labels[cmpv1alpha1.ProfileBundleOwnerLabel] = pb.Name
// overwrite name
pCopy.SetName(profileparser.GetPrefixedName(pb.Name, profileName))
if err := controllerutil.SetControllerReference(pb, pCopy, pcfg.Scheme); err != nil {
return err
}
log.Info("Creating Profile", "Profile.name", p.Name)
err := pcfg.Client.Create(context.TODO(), pCopy)
if err != nil {
if errors.IsAlreadyExists(err) {
log.Info("Profile already exists.", "Profile.Name", p.Name)
} else {
log.Error(err, "couldn't create profile")
return err
}
}
return nil
})
if err != nil {
updateProfileBundleStatus(pcfg, pb, err)
return
}
err = profileparser.ParseRulesAndDo(contentDom, pcfg, func(r *cmpv1alpha1.Rule) error {
ruleName := r.Name
// overwrite name
r.SetName(profileparser.GetPrefixedName(pb.Name, ruleName))
if r.Labels == nil {
r.Labels = make(map[string]string)
}
r.Labels[cmpv1alpha1.ProfileBundleOwnerLabel] = pb.Name
if r.Annotations == nil {
r.Annotations = make(map[string]string)
}
r.Annotations[cmpv1alpha1.RuleIDAnnotationKey] = ruleName
if err := controllerutil.SetControllerReference(pb, r, pcfg.Scheme); err != nil {
return err
}
log.Info("Creating rule", "Rule.Name", r.Name)
err := pcfg.Client.Create(context.TODO(), r)
if err != nil {
if errors.IsAlreadyExists(err) {
log.Info("Rule already exists.", "Rule.Name", r.Name)
} else {
log.Error(err, "couldn't create Rule")
return err
}
}
return nil
})
if err != nil {
updateProfileBundleStatus(pcfg, pb, err)
return
}
err = profileparser.ParseVariablesAndDo(contentDom, pcfg, func(v *cmpv1alpha1.Variable) error {
varName := v.Name
// overwrite name
v.SetName(profileparser.GetPrefixedName(pb.Name, varName))
if v.Labels == nil {
v.Labels = make(map[string]string)
}
v.Labels[cmpv1alpha1.ProfileBundleOwnerLabel] = pb.Name
if err := controllerutil.SetControllerReference(pb, v, pcfg.Scheme); err != nil {
return err
}
log.Info("Creating variable", "Variable.Name", v.Name)
err := pcfg.Client.Create(context.TODO(), v)
if err != nil {
if errors.IsAlreadyExists(err) {
log.Info("Variable already exists.", "Variable.Name", v.Name)
} else {
log.Error(err, "couldn't create Variable")
return err
}
}
return nil
})
// The err variable might be nil, this is fine, it'll just update the status
// to valid
updateProfileBundleStatus(pcfg, pb, err)
<-exitSignal
}