convert.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package compliance
  2. import (
  3. "context"
  4. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  5. "github.com/porter-dev/porter/internal/telemetry"
  6. )
  7. // VendorComplianceCheckStatus is used to indicate the status of a compliance check from a vendor
  8. type VendorComplianceCheckStatus string
  9. const (
  10. // VendorComplianceCheckStatus_Passed is used to indicate that the check has passed
  11. VendorComplianceCheckStatus_Passed VendorComplianceCheckStatus = "passed"
  12. // VendorComplianceCheckStatus_Failing is used to indicate that the check is failing
  13. // this indicates that the check group has an irrecoverable error or that the check group has not been enabled for their infrastructure
  14. VendorComplianceCheckStatus_Failing VendorComplianceCheckStatus = "failing"
  15. // VendorComplianceCheckStatus_NotApplicable is used to indicate that the check is not in the realm of responsibility for Porter
  16. VendorComplianceCheckStatus_NotApplicable VendorComplianceCheckStatus = "not_applicable"
  17. )
  18. // VendorComplianceCheck is used to represent a compliance check from a vendor
  19. type VendorComplianceCheck struct {
  20. // Check is the name of the check. This a human readable name provided directly by the vendor.
  21. Check string `json:"check"`
  22. // CheckGroup refers to the name of the porter internal check group that the check is associated with.
  23. // The status of the check group is used to resolve the status of the check.
  24. CheckGroup string `json:"check_group"`
  25. // Status is the status of the check. This is derived from the status of the check group.
  26. Status VendorComplianceCheckStatus `json:"status"`
  27. // Reason is a message indicating why the check is in its current state.
  28. Reason string `json:"reason"`
  29. }
  30. // CheckGroupStatus is the status for a check group
  31. type CheckGroupStatus string
  32. const (
  33. // CheckGroupStatus_Passed is used when all checks in a group have passed
  34. CheckGroupStatus_Passed CheckGroupStatus = "PASSED"
  35. // CheckGroupStatus_Failed is used when one or more checks in a group have failed
  36. CheckGroupStatus_Failed CheckGroupStatus = "FAILED"
  37. )
  38. // CheckGroup is a group of related checks
  39. // Represents multiple infra changes run together to ensure some higher level compliance requirement is met
  40. type CheckGroup struct {
  41. Name string `json:"name"`
  42. Status CheckGroupStatus `json:"status"`
  43. Message string `json:"message"`
  44. }
  45. // Vendor is used to indicate which vendor the compliance check results are from
  46. type Vendor string
  47. const (
  48. // Vendor_Vanta is use to indicate that the compliance results are based on checks from Vanta
  49. Vendor_Vanta Vendor = "vanta"
  50. )
  51. // CheckGroupsFromProto converts the compliance check group proto to the internal representation
  52. func CheckGroupsFromProto(ctx context.Context, checkGroups []*porterv1.ContractComplianceCheckGroup) ([]CheckGroup, error) {
  53. ctx, span := telemetry.NewSpan(ctx, "compliance-checks-from-proto")
  54. defer span.End()
  55. var res []CheckGroup
  56. for _, cg := range checkGroups {
  57. var status CheckGroupStatus
  58. switch cg.Status {
  59. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_PASSED:
  60. status = CheckGroupStatus_Passed
  61. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_FAILED:
  62. status = CheckGroupStatus_Failed
  63. default:
  64. return res, telemetry.Error(ctx, span, nil, "invalid compliance check status")
  65. }
  66. res = append(res, CheckGroup{
  67. Name: cg.Name,
  68. Status: status,
  69. Message: cg.Message,
  70. })
  71. }
  72. return res, nil
  73. }
  74. // VendorCheckGroupsFromProto converts the vendor compliance check proto to the internal representation
  75. func VendorCheckGroupsFromProto(ctx context.Context, vendorCheck []*porterv1.VendorComplianceCheck) ([]VendorComplianceCheck, error) {
  76. ctx, span := telemetry.NewSpan(ctx, "vendor-compliance-checks-from-proto")
  77. defer span.End()
  78. var res []VendorComplianceCheck
  79. for _, vc := range vendorCheck {
  80. var status VendorComplianceCheckStatus
  81. switch vc.Status {
  82. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_PASSED:
  83. status = VendorComplianceCheckStatus_Passed
  84. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_FAILED:
  85. status = VendorComplianceCheckStatus_Failing
  86. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_NOT_APPLICABLE:
  87. status = VendorComplianceCheckStatus_NotApplicable
  88. default:
  89. return res, telemetry.Error(ctx, span, nil, "invalid compliance check status")
  90. }
  91. res = append(res, VendorComplianceCheck{
  92. Check: vc.Description,
  93. CheckGroup: vc.CheckGroup,
  94. Status: status,
  95. Reason: vc.Reason,
  96. })
  97. }
  98. return res, nil
  99. }