2021-09-26 02:12:31 -07:00
|
|
|
package response
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RuleStatus represents the status of rule execution
|
|
|
|
type RuleStatus int
|
|
|
|
|
2021-09-27 23:40:05 -07:00
|
|
|
// RuleStatusPass is used to report the result of processing a rule.
|
2021-09-26 02:12:31 -07:00
|
|
|
const (
|
2021-09-27 23:40:05 -07:00
|
|
|
// RuleStatusPass indicates that the resources meets the policy rule requirements
|
2021-09-26 02:12:31 -07:00
|
|
|
RuleStatusPass RuleStatus = iota
|
2021-09-27 23:40:05 -07:00
|
|
|
// Fail indicates that the resource does not meet the policy rule requirements
|
2021-09-26 02:12:31 -07:00
|
|
|
RuleStatusFail
|
2021-12-19 01:33:16 +05:30
|
|
|
// Warn indicates that the resource does not meet the policy rule requirements, but the policy is not scored
|
2021-09-26 02:12:31 -07:00
|
|
|
RuleStatusWarn
|
2021-09-27 23:40:05 -07:00
|
|
|
// Error indicates that the policy rule could not be evaluated due to a processing error, for
|
|
|
|
// example when a variable cannot be resolved in the policy rule definition. Note that variables
|
|
|
|
// that cannot be resolved in preconditions are replaced with empty values to allow existence
|
|
|
|
// checks.
|
2021-09-26 02:12:31 -07:00
|
|
|
RuleStatusError
|
2021-09-27 23:40:05 -07:00
|
|
|
// Skip indicates that the policy rule was not selected based on user inputs or applicability, for example
|
|
|
|
// when preconditions are not met, or when conditional or global anchors are not satistied.
|
2021-09-26 02:12:31 -07:00
|
|
|
RuleStatusSkip
|
|
|
|
)
|
|
|
|
|
2021-09-26 18:30:53 -07:00
|
|
|
func (s *RuleStatus) String() string {
|
|
|
|
return toString[*s]
|
2021-09-26 02:12:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var toString = map[RuleStatus]string{
|
2021-09-30 00:04:13 -07:00
|
|
|
RuleStatusPass: "pass",
|
|
|
|
RuleStatusFail: "fail",
|
|
|
|
RuleStatusWarn: "warning",
|
|
|
|
RuleStatusError: "error",
|
|
|
|
RuleStatusSkip: "skip",
|
2021-09-26 02:12:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var toID = map[string]RuleStatus{
|
2021-09-30 00:04:13 -07:00
|
|
|
"pass": RuleStatusPass,
|
|
|
|
"fail": RuleStatusFail,
|
|
|
|
"warning": RuleStatusWarn,
|
|
|
|
"error": RuleStatusError,
|
|
|
|
"skip": RuleStatusSkip,
|
2021-09-26 02:12:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON marshals the enum as a quoted json string
|
2021-09-26 18:30:53 -07:00
|
|
|
func (s *RuleStatus) MarshalJSON() ([]byte, error) {
|
2021-09-26 02:12:31 -07:00
|
|
|
var b strings.Builder
|
2021-09-26 18:30:53 -07:00
|
|
|
fmt.Fprintf(&b, "\"%s\"", toString[*s])
|
2021-09-26 02:12:31 -07:00
|
|
|
return []byte(b.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals a quoted json string to the enum value
|
|
|
|
func (s *RuleStatus) UnmarshalJSON(b []byte) error {
|
2021-09-26 18:30:53 -07:00
|
|
|
var strVal string
|
|
|
|
err := json.Unmarshal(b, &strVal)
|
2021-09-26 02:12:31 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-26 18:30:53 -07:00
|
|
|
statusVal, err := getRuleStatus(strVal)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = *statusVal
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 23:40:05 -07:00
|
|
|
func getRuleStatus(s string) (*RuleStatus, error) {
|
2021-09-26 02:12:31 -07:00
|
|
|
for k, v := range toID {
|
2021-09-26 18:30:53 -07:00
|
|
|
if s == k {
|
|
|
|
return &v, nil
|
2021-09-26 02:12:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 18:30:53 -07:00
|
|
|
return nil, fmt.Errorf("invalid status: %s", s)
|
|
|
|
}
|
|
|
|
|
2021-10-29 16:06:03 +01:00
|
|
|
func (s *RuleStatus) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var str string
|
|
|
|
if err := unmarshal(&str); err != nil {
|
2021-09-26 18:30:53 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-29 16:06:03 +01:00
|
|
|
statusVal, err := getRuleStatus(str)
|
2021-09-26 18:30:53 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-29 16:06:03 +01:00
|
|
|
*s = *statusVal
|
2021-09-26 18:30:53 -07:00
|
|
|
return nil
|
2021-09-26 02:12:31 -07:00
|
|
|
}
|