Skip to content

Commit c99e491

Browse files
committed
cleaning up disk driver
Signed-off-by: Jaydip Gabani <gabanijaydip@gmail.com>
1 parent d092b94 commit c99e491

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

.github/workflows/disk-export.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
egress-policy: audit
2525

2626
- name: Check out code into the Go module directory
27-
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
27+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2828

2929
- name: Bootstrap e2e
3030
run: |

pkg/audit/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func (am *Manager) audit(ctx context.Context) error {
260260
am.log = log.WithValues(logging.AuditID, timestamp)
261261
logStart(am.log)
262262
exportErrorMap := make(map[string]error)
263-
if err := am.exportSystem.Publish(context.Background(), *auditConnection, *auditChannel, exportutil.ExportMsg{Message: "audit is started", ID: timestamp}); err != nil {
263+
if err := am.exportSystem.Publish(context.Background(), *auditConnection, *auditChannel, exportutil.ExportMsg{Message: exportutil.AuditStartedMsg, ID: timestamp}); err != nil {
264264
exportErrorMap[err.Error()] = err
265265
am.log.Error(err, "failed to export audit start message")
266266
}
@@ -275,7 +275,7 @@ func (am *Manager) audit(ctx context.Context) error {
275275
if err := am.reporter.reportRunEnd(endTime); err != nil {
276276
am.log.Error(err, "failed to report run end time")
277277
}
278-
if err := am.exportSystem.Publish(context.Background(), *auditConnection, *auditChannel, exportutil.ExportMsg{Message: "audit is completed", ID: timestamp}); err != nil {
278+
if err := am.exportSystem.Publish(context.Background(), *auditConnection, *auditChannel, exportutil.ExportMsg{Message: exportutil.AuditCompletedMsg, ID: timestamp}); err != nil {
279279
exportErrorMap[err.Error()] = err
280280
}
281281
for _, v := range exportErrorMap {

pkg/controller/export/export_config_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package export
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"flag"
78
"fmt"
89

@@ -111,10 +112,10 @@ func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (
111112
}
112113

113114
if len(cfg.Data) == 0 {
114-
return reconcile.Result{}, fmt.Errorf(fmt.Sprintf("data missing in configmap %s, unable to configure exporter", request.NamespacedName))
115+
return reconcile.Result{}, errors.New("%s", fmt.Sprintf("data missing in configmap %s, unable to configure exporter", request.NamespacedName))
115116
}
116117
if _, ok := cfg.Data["driver"]; !ok {
117-
return reconcile.Result{}, fmt.Errorf(fmt.Sprintf("missing driver field in configmap %s, unable to configure exporter", request.NamespacedName))
118+
return reconcile.Result{}, errors.New("%s", fmt.Sprintf("missing driver field in configmap %s, unable to configure exporter", request.NamespacedName))
118119
}
119120
var config interface{}
120121
err = json.Unmarshal([]byte(cfg.Data["config"]), &config)

pkg/export/disk/disk.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1820
type Connection struct {
@@ -32,31 +34,31 @@ type Writer struct {
3234
}
3335

3436
const (
37+
Name = "disk"
3538
maxAllowedAuditRuns = 5
36-
)
37-
38-
const (
39-
Name = "disk"
39+
maxAuditResults = "maxAuditResults"
40+
violationPath = "path"
4041
)
4142

4243
var Connections = &Writer{
4344
openConnections: make(map[string]Connection),
4445
}
4546

47+
var log = logf.Log.WithName("disk-driver").WithValues(logging.Process, "export")
48+
4649
func (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

7274
func (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
108110
func (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 {
118120
func (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

163165
func (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
}

pkg/export/util/util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ const (
4242
ErrWritingMessage ExportError = "error_writing_message"
4343
ErrCleaningUpAudit ExportError = "error_cleaning_up_audit"
4444
)
45+
46+
const (
47+
AuditStartedMsg = "audit is started"
48+
AuditCompletedMsg = "audit is completed"
49+
)

0 commit comments

Comments
 (0)