@@ -12,7 +12,9 @@ import (
12
12
"time"
13
13
14
14
"github.com/open-policy-agent/gatekeeper/v3/pkg/export/util"
15
+ "github.com/open-policy-agent/gatekeeper/v3/pkg/logging"
15
16
"k8s.io/client-go/util/retry"
17
+ logf "sigs.k8s.io/controller-runtime/pkg/log"
16
18
)
17
19
18
20
type Connection struct {
@@ -32,31 +34,31 @@ type Writer struct {
32
34
}
33
35
34
36
const (
37
+ Name = "disk"
35
38
maxAllowedAuditRuns = 5
36
- )
37
-
38
- const (
39
- Name = "disk"
39
+ maxAuditResults = "maxAuditResults"
40
+ violationPath = "path"
40
41
)
41
42
42
43
var Connections = & Writer {
43
44
openConnections : make (map [string ]Connection ),
44
45
}
45
46
47
+ var log = logf .Log .WithName ("disk-driver" ).WithValues (logging .Process , "export" )
48
+
46
49
func (r * Writer ) CreateConnection (_ context.Context , connectionName string , config interface {}) error {
47
50
cfg , ok := config .(map [string ]interface {})
48
51
if ! ok {
49
52
return fmt .Errorf ("invalid config format" )
50
53
}
51
54
52
- path , pathOk := cfg ["path" ].(string )
55
+ path , pathOk := cfg [violationPath ].(string )
53
56
if ! pathOk {
54
- return fmt .Errorf ("missing or invalid values in config for connection: %s" , connectionName )
57
+ return fmt .Errorf ("missing or invalid values in config for connection %s" , connectionName )
55
58
}
56
- var err error
57
- maxResults , maxResultsOk := cfg ["maxAuditResults" ].(float64 )
59
+ maxResults , maxResultsOk := cfg [maxAuditResults ].(float64 )
58
60
if ! maxResultsOk {
59
- return fmt .Errorf ("missing or invalid 'maxAuditResults' for connection: %s" , connectionName )
61
+ return fmt .Errorf ("missing or invalid 'maxAuditResults' for connection %s" , connectionName )
60
62
}
61
63
if maxResults > maxAllowedAuditRuns {
62
64
return fmt .Errorf ("maxAuditResults cannot be greater than %d" , maxAllowedAuditRuns )
@@ -66,7 +68,7 @@ func (r *Writer) CreateConnection(_ context.Context, connectionName string, conf
66
68
Path : path ,
67
69
MaxAuditResults : int (maxResults ),
68
70
}
69
- return err
71
+ return nil
70
72
}
71
73
72
74
func (r * Writer ) UpdateConnection (_ context.Context , connectionName string , config interface {}) error {
@@ -77,28 +79,28 @@ func (r *Writer) UpdateConnection(_ context.Context, connectionName string, conf
77
79
78
80
conn , exists := r .openConnections [connectionName ]
79
81
if ! exists {
80
- return fmt .Errorf ("connection not found: %s for Disk driver" , connectionName )
82
+ return fmt .Errorf ("connection %s for disk driver not found " , connectionName )
81
83
}
82
84
83
85
var cleanUpErr error
84
- if path , ok := cfg ["path" ].(string ); ok {
86
+ if path , ok := cfg [violationPath ].(string ); ok {
85
87
if conn .Path != path {
86
88
if err := os .RemoveAll (conn .Path ); err != nil {
87
89
cleanUpErr = fmt .Errorf ("connection updated but failed to remove content form old path: %w" , err )
88
90
}
89
91
conn .Path = path
90
92
}
91
93
} else {
92
- return fmt .Errorf ("missing or invalid 'path' for connection: %s" , connectionName )
94
+ return fmt .Errorf ("missing or invalid 'path' for connection %s" , connectionName )
93
95
}
94
96
95
- if maxResults , ok := cfg [" maxAuditResults" ].(float64 ); ok {
97
+ if maxResults , ok := cfg [maxAuditResults ].(float64 ); ok {
96
98
if maxResults > maxAllowedAuditRuns {
97
99
return fmt .Errorf ("maxAuditResults cannot be greater than %d" , maxAllowedAuditRuns )
98
100
}
99
101
conn .MaxAuditResults = int (maxResults )
100
102
} else {
101
- return fmt .Errorf ("missing or invalid 'maxAuditResults' for connection: %s" , connectionName )
103
+ return fmt .Errorf ("missing or invalid 'maxAuditResults' for connection %s" , connectionName )
102
104
}
103
105
104
106
r .openConnections [connectionName ] = conn
@@ -108,7 +110,7 @@ func (r *Writer) UpdateConnection(_ context.Context, connectionName string, conf
108
110
func (r * Writer ) CloseConnection (connectionName string ) error {
109
111
conn , ok := r .openConnections [connectionName ]
110
112
if ! ok {
111
- return fmt .Errorf ("connection not found: %s for disk driver" , connectionName )
113
+ return fmt .Errorf ("connection %s not found for disk driver" , connectionName )
112
114
}
113
115
err := os .RemoveAll (conn .Path )
114
116
delete (r .openConnections , connectionName )
@@ -118,15 +120,15 @@ func (r *Writer) CloseConnection(connectionName string) error {
118
120
func (r * Writer ) Publish (_ context.Context , connectionName string , data interface {}, topic string ) error {
119
121
conn , ok := r .openConnections [connectionName ]
120
122
if ! ok {
121
- return fmt .Errorf ("connection not found: %s for disk driver" , connectionName )
123
+ return fmt .Errorf ("connection %s not found for disk driver" , connectionName )
122
124
}
123
125
124
126
var violation util.ExportMsg
125
127
if violation , ok = data .(util.ExportMsg ); ! ok {
126
128
return fmt .Errorf ("invalid data type, cannot convert data to exportMsg" )
127
129
}
128
130
129
- if violation .Message == "audit is started" {
131
+ if violation .Message == util . AuditStartedMsg {
130
132
err := conn .handleAuditStart (violation .ID , topic )
131
133
if err != nil {
132
134
return fmt .Errorf ("error handling audit start: %w" , err )
@@ -148,7 +150,7 @@ func (r *Writer) Publish(_ context.Context, connectionName string, data interfac
148
150
return fmt .Errorf ("error writing message to disk: %w" , err )
149
151
}
150
152
151
- if violation .Message == "audit is completed" {
153
+ if violation .Message == util . AuditCompletedMsg {
152
154
err := conn .handleAuditEnd (topic )
153
155
if err != nil {
154
156
return fmt .Errorf ("error handling audit end: %w" , err )
@@ -161,6 +163,7 @@ func (r *Writer) Publish(_ context.Context, connectionName string, data interfac
161
163
}
162
164
163
165
func (conn * Connection ) handleAuditStart (auditID string , topic string ) error {
166
+ // Replace ':' with '_' to avoid issues with file names in windows
164
167
conn .currentAuditRun = strings .ReplaceAll (auditID , ":" , "_" )
165
168
166
169
// Ensure the directory exists
@@ -197,6 +200,7 @@ func (conn *Connection) handleAuditEnd(topic string) error {
197
200
if err := os .Rename (path .Join (conn .Path , topic , appendExtension (conn .currentAuditRun , "txt" )), readyFilePath ); err != nil {
198
201
return fmt .Errorf ("failed to rename file: %w, %s" , err , conn .currentAuditRun )
199
202
}
203
+ log .Info ("File renamed" , "filename" , readyFilePath )
200
204
201
205
return conn .cleanupOldAuditFiles (topic )
202
206
}
0 commit comments