-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08b2643
commit 4029044
Showing
6 changed files
with
186 additions
and
2 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
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,80 @@ | ||
package model | ||
|
||
import ( | ||
_ "embed" | ||
"time" | ||
|
||
"cuelang.org/go/cue/cuecontext" | ||
"github.com/m-mizutani/goerr" | ||
) | ||
|
||
type Config struct { | ||
IgnoreTargets []IgnoreTarget | ||
} | ||
|
||
//go:embed schema/ignore.cue | ||
var ignoreCue []byte | ||
|
||
type IgnoreTarget struct { | ||
File string | ||
Vulns []IgnoreVuln | ||
} | ||
|
||
func (x *IgnoreTarget) Validate() error { | ||
for _, v := range x.Vulns { | ||
if err := v.Validate(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type IgnoreVuln struct { | ||
ID string | ||
Description string | ||
ExpiresAt time.Time | ||
} | ||
|
||
func (x *IgnoreVuln) Validate() error { | ||
maxExpiresAt := time.Now().Add(time.Hour * 24 * 90) | ||
if x.ExpiresAt.After(maxExpiresAt) { | ||
return goerr.New("expiresAt is too far in the future, must be within 90 days from now") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func LoadConfig(configData ...[]byte) (*Config, error) { | ||
ctx := cuecontext.New() | ||
|
||
// Load the schema | ||
schemaInstance := ctx.CompileBytes(ignoreCue) | ||
if schemaInstance.Err() != nil { | ||
return nil, goerr.Wrap(schemaInstance.Err(), "failed to compile schema") | ||
} | ||
|
||
for _, data := range configData { | ||
// Load the configuration | ||
configInstance := ctx.CompileBytes(data) | ||
if configInstance.Err() != nil { | ||
return nil, goerr.Wrap(configInstance.Err(), "failed to compile configuration") | ||
} | ||
|
||
// Merge the schema and config | ||
mergedInstance := schemaInstance.Unify(configInstance) | ||
if mergedInstance.Err() != nil { | ||
return nil, goerr.Wrap(mergedInstance.Err(), "failed to unify schema and config") | ||
} | ||
|
||
schemaInstance = mergedInstance | ||
} | ||
|
||
// Extract the configuration into a Go struct | ||
var config Config | ||
if err := schemaInstance.Value().Decode(&config); err != nil { | ||
return nil, goerr.Wrap(err, "failed to decode configuration") | ||
} | ||
|
||
return &config, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package model_test | ||
|
||
import ( | ||
_ "embed" | ||
"testing" | ||
|
||
"github.com/m-mizutani/gt" | ||
"github.com/m-mizutani/octovy/pkg/domain/model" | ||
) | ||
|
||
//go:embed testdata/config/ignore.cue | ||
var testConfigIgnoreCue []byte | ||
|
||
func TestIgnoreConfig(t *testing.T) { | ||
cfg, err := model.LoadConfig(testConfigIgnoreCue) | ||
gt.NoError(t, err) | ||
gt.A(t, cfg.IgnoreTargets).Length(2). | ||
At(0, func(t testing.TB, v model.IgnoreTarget) { | ||
gt.Equal(t, v.File, "test.data") | ||
gt.A(t, v.Vulns).Length(1).At(0, func(t testing.TB, v model.IgnoreVuln) { | ||
gt.Equal(t, v.ID, "CVE-2017-9999") | ||
gt.Equal(t, v.Description, "This is test data") | ||
gt.Equal(t, v.ExpiresAt.Year(), 2018) | ||
}) | ||
}). | ||
At(1, func(t testing.TB, v model.IgnoreTarget) { | ||
gt.Equal(t, v.File, "test2.data") | ||
gt.A(t, v.Vulns).Length(2). | ||
At(0, func(t testing.TB, v model.IgnoreVuln) { | ||
gt.Equal(t, v.ID, "CVE-2017-11423") | ||
gt.Equal(t, v.ExpiresAt.Year(), 2022) | ||
}). | ||
At(1, func(t testing.TB, v model.IgnoreVuln) { | ||
gt.Equal(t, v.ID, "CVE-2023-11423") | ||
gt.Equal(t, v.ExpiresAt.Year(), 2023) | ||
}) | ||
}) | ||
} |
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,16 @@ | ||
package octovy | ||
|
||
import "time" | ||
|
||
#IgnoreTarget: { | ||
File: string | ||
Vulns: [...#IgnoreVuln] @go(,[]IgnoreVuln) | ||
} | ||
|
||
#IgnoreVuln: { | ||
ID: string | ||
Description?: string | ||
ExpiresAt: time.Time | ||
} | ||
|
||
IgnoreTargets?: [...#IgnoreTarget] |
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,29 @@ | ||
package octovy | ||
|
||
IgnoreTargets: [ | ||
{ | ||
File: "test.data" | ||
Vulns: [ | ||
{ | ||
ID: "CVE-2017-9999" | ||
Description: "This is test data" | ||
ExpiresAt: "2018-01-01T00:00:00Z" | ||
}, | ||
] | ||
}, | ||
{ | ||
File: "test2.data" | ||
Vulns: [ | ||
{ | ||
ID: "CVE-2017-11423" | ||
Description: "Hoge" | ||
ExpiresAt: "2022-03-04T00:00:00Z" | ||
}, | ||
{ | ||
ID: "CVE-2023-11423" | ||
Description: "Hoge" | ||
ExpiresAt: "2023-03-04T00:00:00Z" | ||
}, | ||
] | ||
}, | ||
] |