Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/YubicoLabe/action-conftest

go 1.23
36 changes: 32 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,17 @@ func run() error {
var policiesWithFails, policiesWithWarns []string
var fails, warns []string
var successes int

for _, result := range results {
successes += result.Successes

for _, fail := range result.Failures {
fails = append(fails, fmt.Sprintf("%s - %s", result.FileName, fail.Message))

if metricsURL == "" || policyIDKey == "" {
continue
}

policyID, err := getPolicyIDFromMetadata(fail.Metadata, policyIDKey)
if err != nil {
continue
Expand All @@ -138,6 +144,11 @@ func run() error {

for _, warn := range result.Warnings {
warns = append(warns, fmt.Sprintf("%s - %s", result.FileName, warn.Message))

if metricsURL == "" || policyIDKey == "" {
continue
}

policyID, err := getPolicyIDFromMetadata(warn.Metadata, policyIDKey)
if err != nil {
continue
Expand Down Expand Up @@ -295,12 +306,29 @@ func runConftestTest() ([]CheckResult, error) {
}

func getPolicyIDFromMetadata(metadata map[string]interface{}, policyIDKey string) (string, error) {
details := metadata["details"].(map[string]interface{})
if details[policyIDKey] == nil {
return "", fmt.Errorf("empty policyID key")
if metadata == nil {
return "", fmt.Errorf("metadata is nil")
}
if policyIDKey == "" {
return "", fmt.Errorf("policyIDKey is empty")
}

rawDetails, ok := metadata["details"]
if !ok || rawDetails == nil {
return "", fmt.Errorf("missing details in metadata")
}

details, ok := rawDetails.(map[string]interface{})
if !ok {
return "", fmt.Errorf("details has unexpected type %T", rawDetails)
}

val, ok := details[policyIDKey]
if !ok || val == nil {
return "", fmt.Errorf("empty policyID key %q", policyIDKey)
}

return fmt.Sprintf("%v", details[policyIDKey]), nil
return fmt.Sprintf("%v", val), nil
}

func getFlagsFromEnv() []string {
Expand Down
28 changes: 28 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,31 @@ func TestGetPolicyIDFromMetadata_Empty(t *testing.T) {
t.Errorf("should error when policyIDKey does not exist")
}
}

func TestGetPolicyIDFromMetadata_NilMetadata(t *testing.T) {
var metadata map[string]interface{} = nil

if _, err := getPolicyIDFromMetadata(metadata, "policyID"); err == nil {
t.Errorf("expected error when metadata is nil")
}
}

func TestGetPolicyIDFromMetadata_NoDetails(t *testing.T) {
metadata := map[string]interface{}{
"somethingElse": "value",
}

if _, err := getPolicyIDFromMetadata(metadata, "policyID"); err == nil {
t.Errorf("expected error when details is missing")
}
}

func TestGetPolicyIDFromMetadata_DetailsWrongType(t *testing.T) {
metadata := map[string]interface{}{
"details": "not-a-map",
}

if _, err := getPolicyIDFromMetadata(metadata, "policyID"); err == nil {
t.Errorf("expected error when details is wrong type")
}
}