forked from open-policy-agent/gatekeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jaydip Gabani <[email protected]>
- Loading branch information
1 parent
2294222
commit d0b3c26
Showing
5 changed files
with
203 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package diskwriter | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/open-policy-agent/gatekeeper/v3/pkg/pubsub/connection" | ||
) | ||
|
||
type DiskWriter struct { | ||
mu sync.Mutex | ||
auditId string | ||
Path string `json:"path,omitempty"` | ||
} | ||
|
||
const ( | ||
Name = "diskwriter" | ||
) | ||
|
||
func (r *DiskWriter) Publish(_ context.Context, data interface{}, topic string) error { | ||
jsonData, err := json.Marshal(data) | ||
if err != nil { | ||
return fmt.Errorf("error marshaling data: %w", err) | ||
} | ||
|
||
path := path.Join(r.Path, "violations.txt") | ||
|
||
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) | ||
if err != nil { | ||
return fmt.Errorf("failed to open file: %w", err) | ||
} | ||
|
||
defer file.Close() | ||
|
||
// Acquire an exclusive lock on the file | ||
if err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX); err != nil { | ||
return fmt.Errorf("failed to lock file: %w", err) | ||
} | ||
defer syscall.Flock(int(file.Fd()), syscall.LOCK_UN) | ||
|
||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
_, err = file.WriteString(string(jsonData) + "\n") | ||
if err != nil { | ||
return fmt.Errorf("error publishing message to dapr: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *DiskWriter) CloseConnection() error { | ||
return nil | ||
} | ||
|
||
func (r *DiskWriter) UpdateConnection(_ context.Context, config interface{}) error { | ||
// m, ok := config.(map[string]interface{}) | ||
// if !ok { | ||
// return fmt.Errorf("invalid type assertion, config is not in expected format") | ||
// } | ||
// path, ok := m["path"].(string) | ||
// if !ok { | ||
// return fmt.Errorf("failed to get value of path") | ||
// } | ||
// r.Path = path | ||
return nil | ||
} | ||
|
||
// Returns a new client for dapr. | ||
func NewConnection(_ context.Context, config interface{}) (connection.Connection, error) { | ||
var diskWriter DiskWriter | ||
m, ok := config.(map[string]interface{}) | ||
if !ok { | ||
return nil, fmt.Errorf("invalid type assertion, config is not in expected format") | ||
} | ||
diskWriter.Path, ok = m["path"].(string) | ||
if !ok { | ||
return nil, fmt.Errorf("failed to get value of path") | ||
} | ||
return &diskWriter, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
// "time" | ||
"syscall" | ||
) | ||
|
||
type PubsubMsg struct { | ||
ID string `json:"id,omitempty"` | ||
Details interface{} `json:"details,omitempty"` | ||
EventType string `json:"eventType,omitempty"` | ||
Group string `json:"group,omitempty"` | ||
Version string `json:"version,omitempty"` | ||
Kind string `json:"kind,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Namespace string `json:"namespace,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
EnforcementAction string `json:"enforcementAction,omitempty"` | ||
ConstraintAnnotations map[string]string `json:"constraintAnnotations,omitempty"` | ||
ResourceGroup string `json:"resourceGroup,omitempty"` | ||
ResourceAPIVersion string `json:"resourceAPIVersion,omitempty"` | ||
ResourceKind string `json:"resourceKind,omitempty"` | ||
ResourceNamespace string `json:"resourceNamespace,omitempty"` | ||
ResourceName string `json:"resourceName,omitempty"` | ||
ResourceLabels map[string]string `json:"resourceLabels,omitempty"` | ||
} | ||
|
||
func main() { | ||
path := "/mount/d/go/src/github.com/open-policy-agent/gatekeeper/violations.txt" | ||
msgId := 1 | ||
|
||
for { | ||
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) | ||
if err != nil { | ||
fmt.Println("failed to open file: %w", err) | ||
} | ||
|
||
// Acquire an exclusive lock on the file | ||
if err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX); err != nil { | ||
fmt.Println("failed to lock file: %w", err) | ||
} | ||
|
||
_, err = file.WriteString(fmt.Sprintf("violation_msg_", msgId) + "\n") | ||
if err != nil { | ||
fmt.Println("error publishing message to dapr: %w", err) | ||
} | ||
|
||
// Release the lock | ||
if err := syscall.Flock(int(file.Fd()), syscall.LOCK_UN); err != nil { | ||
fmt.Println("Error unlocking file: %v\n", err) | ||
} | ||
|
||
// Close the file | ||
if err := file.Close(); err != nil { | ||
fmt.Println("Error closing file: %v\n", err) | ||
} | ||
fmt.Println("Published message: violation_msg_", msgId) | ||
msgId++ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
--- | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: pubsub | ||
namespace: gatekeeper-system | ||
spec: | ||
type: pubsub.redis | ||
version: v1 | ||
metadata: | ||
- name: redisHost | ||
value: redis-master.default.svc.cluster.local:6379 | ||
- name: redisPassword | ||
secretKeyRef: | ||
name: redis | ||
key: redis-password | ||
--- | ||
# --- | ||
# apiVersion: dapr.io/v1alpha1 | ||
# kind: Component | ||
# metadata: | ||
# name: pubsub | ||
# namespace: gatekeeper-system | ||
# spec: | ||
# type: pubsub.redis | ||
# version: v1 | ||
# metadata: | ||
# - name: redisHost | ||
# value: redis-master.default.svc.cluster.local:6379 | ||
# - name: redisPassword | ||
# secretKeyRef: | ||
# name: redis | ||
# key: redis-password | ||
# --- | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: audit | ||
namespace: gatekeeper-system | ||
data: | ||
provider: "dapr" | ||
provider: "diskwriter" | ||
config: | | ||
{ | ||
"component": "pubsub" | ||
"path": "/tmp/violations" | ||
} |