Skip to content

Commit

Permalink
[CWS] Move JSON schemas to secl package (#30913)
Browse files Browse the repository at this point in the history
  • Loading branch information
lebauce authored and chouetz committed Nov 21, 2024
1 parent c3442f5 commit ff36676
Show file tree
Hide file tree
Showing 57 changed files with 1,593 additions and 346 deletions.
70 changes: 70 additions & 0 deletions pkg/security/generators/schemas/policy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

//go:generate go run github.com/DataDog/datadog-agent/pkg/security/generators/schemas/policy -output ../../../secl/schemas/policy.schema.json

// Package main holds main related files
package main

import (
"encoding/json"
"flag"
"os"
"reflect"
"time"

"github.com/invopop/jsonschema"

"github.com/DataDog/datadog-agent/pkg/security/secl/rules"
)

func main() {
var output string
flag.StringVar(&output, "output", "", "output file")
flag.Parse()

if output == "" {
panic("an output file argument is required")
}

reflector := jsonschema.Reflector{
ExpandedStruct: true,
Mapper: func(t reflect.Type) *jsonschema.Schema {
switch t {
case reflect.TypeOf(time.Duration(0)):
return &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
{
Type: "string",
Format: "duration",
Description: "Duration in Go format (e.g. 1h30m, see https://pkg.go.dev/time#ParseDuration)",
},
{
Type: "integer",
Description: "Duration in nanoseconds",
},
},
}
}
return nil
},
}

if err := reflector.AddGoComments("github.com/DataDog/datadog-agent/pkg/security/secl/rules/model.go", "../../../secl/rules"); err != nil {
panic(err)
}

schema := reflector.Reflect(&rules.PolicyDef{})
schema.ID = "https://github.com/DataDog/datadog-agent/tree/main/pkg/security/secl/rules"

data, err := json.MarshalIndent(schema, "", " ")
if err != nil {
panic(err)
}

if err := os.WriteFile(output, data, 0644); err != nil {
panic(err)
}
}
Loading

0 comments on commit ff36676

Please sign in to comment.