@@ -12,7 +12,9 @@ import (
1212 "time"
1313
1414 "github.com/open-policy-agent/gatekeeper/v3/pkg/export/util"
15+ "github.com/open-policy-agent/gatekeeper/v3/pkg/logging"
1516 "k8s.io/client-go/util/retry"
17+ logf "sigs.k8s.io/controller-runtime/pkg/log"
1618)
1719
1820type Connection struct {
@@ -32,31 +34,31 @@ type Writer struct {
3234}
3335
3436const (
37+ Name = "disk"
3538 maxAllowedAuditRuns = 5
36- )
37-
38- const (
39- Name = "disk"
39+ maxAuditResults = "maxAuditResults"
40+ violationPath = "path"
4041)
4142
4243var Connections = & Writer {
4344 openConnections : make (map [string ]Connection ),
4445}
4546
47+ var log = logf .Log .WithName ("disk-driver" ).WithValues (logging .Process , "export" )
48+
4649func (r * Writer ) CreateConnection (_ context.Context , connectionName string , config interface {}) error {
4750 cfg , ok := config .(map [string ]interface {})
4851 if ! ok {
4952 return fmt .Errorf ("invalid config format" )
5053 }
5154
52- path , pathOk := cfg ["path" ].(string )
55+ path , pathOk := cfg [violationPath ].(string )
5356 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 )
5558 }
56- var err error
57- maxResults , maxResultsOk := cfg ["maxAuditResults" ].(float64 )
59+ maxResults , maxResultsOk := cfg [maxAuditResults ].(float64 )
5860 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 )
6062 }
6163 if maxResults > maxAllowedAuditRuns {
6264 return fmt .Errorf ("maxAuditResults cannot be greater than %d" , maxAllowedAuditRuns )
@@ -66,7 +68,7 @@ func (r *Writer) CreateConnection(_ context.Context, connectionName string, conf
6668 Path : path ,
6769 MaxAuditResults : int (maxResults ),
6870 }
69- return err
71+ return nil
7072}
7173
7274func (r * Writer ) UpdateConnection (_ context.Context , connectionName string , config interface {}) error {
@@ -77,28 +79,28 @@ func (r *Writer) UpdateConnection(_ context.Context, connectionName string, conf
7779
7880 conn , exists := r .openConnections [connectionName ]
7981 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 )
8183 }
8284
8385 var cleanUpErr error
84- if path , ok := cfg ["path" ].(string ); ok {
86+ if path , ok := cfg [violationPath ].(string ); ok {
8587 if conn .Path != path {
8688 if err := os .RemoveAll (conn .Path ); err != nil {
8789 cleanUpErr = fmt .Errorf ("connection updated but failed to remove content form old path: %w" , err )
8890 }
8991 conn .Path = path
9092 }
9193 } 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 )
9395 }
9496
95- if maxResults , ok := cfg [" maxAuditResults" ].(float64 ); ok {
97+ if maxResults , ok := cfg [maxAuditResults ].(float64 ); ok {
9698 if maxResults > maxAllowedAuditRuns {
9799 return fmt .Errorf ("maxAuditResults cannot be greater than %d" , maxAllowedAuditRuns )
98100 }
99101 conn .MaxAuditResults = int (maxResults )
100102 } 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 )
102104 }
103105
104106 r .openConnections [connectionName ] = conn
@@ -108,7 +110,7 @@ func (r *Writer) UpdateConnection(_ context.Context, connectionName string, conf
108110func (r * Writer ) CloseConnection (connectionName string ) error {
109111 conn , ok := r .openConnections [connectionName ]
110112 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 )
112114 }
113115 err := os .RemoveAll (conn .Path )
114116 delete (r .openConnections , connectionName )
@@ -118,15 +120,15 @@ func (r *Writer) CloseConnection(connectionName string) error {
118120func (r * Writer ) Publish (_ context.Context , connectionName string , data interface {}, topic string ) error {
119121 conn , ok := r .openConnections [connectionName ]
120122 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 )
122124 }
123125
124126 var violation util.ExportMsg
125127 if violation , ok = data .(util.ExportMsg ); ! ok {
126128 return fmt .Errorf ("invalid data type, cannot convert data to exportMsg" )
127129 }
128130
129- if violation .Message == "audit is started" {
131+ if violation .Message == util . AuditStartedMsg {
130132 err := conn .handleAuditStart (violation .ID , topic )
131133 if err != nil {
132134 return fmt .Errorf ("error handling audit start: %w" , err )
@@ -148,7 +150,7 @@ func (r *Writer) Publish(_ context.Context, connectionName string, data interfac
148150 return fmt .Errorf ("error writing message to disk: %w" , err )
149151 }
150152
151- if violation .Message == "audit is completed" {
153+ if violation .Message == util . AuditCompletedMsg {
152154 err := conn .handleAuditEnd (topic )
153155 if err != nil {
154156 return fmt .Errorf ("error handling audit end: %w" , err )
@@ -161,6 +163,7 @@ func (r *Writer) Publish(_ context.Context, connectionName string, data interfac
161163}
162164
163165func (conn * Connection ) handleAuditStart (auditID string , topic string ) error {
166+ // Replace ':' with '_' to avoid issues with file names in windows
164167 conn .currentAuditRun = strings .ReplaceAll (auditID , ":" , "_" )
165168
166169 // Ensure the directory exists
@@ -197,6 +200,7 @@ func (conn *Connection) handleAuditEnd(topic string) error {
197200 if err := os .Rename (path .Join (conn .Path , topic , appendExtension (conn .currentAuditRun , "txt" )), readyFilePath ); err != nil {
198201 return fmt .Errorf ("failed to rename file: %w, %s" , err , conn .currentAuditRun )
199202 }
203+ log .Info ("File renamed" , "filename" , readyFilePath )
200204
201205 return conn .cleanupOldAuditFiles (topic )
202206}
0 commit comments