convert.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // VendorCheckID is the unique identifier for the check from the vendor.
  30. VendorCheckID string `json:"vendor_check_id"`
  31. }
  32. // CheckGroupStatus is the status for a check group
  33. type CheckGroupStatus string
  34. const (
  35. // CheckGroupStatus_Passed is used when all checks in a group have passed
  36. CheckGroupStatus_Passed CheckGroupStatus = "PASSED"
  37. // CheckGroupStatus_Failed is used when one or more checks in a group have failed
  38. CheckGroupStatus_Failed CheckGroupStatus = "FAILED"
  39. )
  40. // CheckGroup is a group of related checks
  41. // Represents multiple infra changes run together to ensure some higher level compliance requirement is met
  42. type CheckGroup struct {
  43. Name string `json:"name"`
  44. Status CheckGroupStatus `json:"status"`
  45. Message string `json:"message"`
  46. }
  47. // Vendor is used to indicate which vendor the compliance check results are from
  48. type Vendor string
  49. const (
  50. // Vendor_Vanta is use to indicate that the compliance results are based on checks from Vanta
  51. Vendor_Vanta Vendor = "vanta"
  52. // Vendor_OneLeet is use to indicate that the compliance results are based on checks from OneLeet
  53. Vendor_OneLeet Vendor = "oneleet"
  54. )
  55. // Profile is used to indicate which compliance profile the compliance check results are from
  56. type Profile string
  57. const (
  58. // Profile_SOC2 is used to indicate that the check results are for the SOC2 compliance profile
  59. Profile_SOC2 Profile = "soc2"
  60. // Profile_HIPAA is used to indicate that the check results are for the HIPAA compliance profile
  61. Profile_HIPAA Profile = "hipaa"
  62. )
  63. // CheckGroupsFromProto converts the compliance check group proto to the internal representation
  64. func CheckGroupsFromProto(ctx context.Context, checkGroups []*porterv1.ContractComplianceCheckGroup) ([]CheckGroup, error) {
  65. ctx, span := telemetry.NewSpan(ctx, "compliance-checks-from-proto")
  66. defer span.End()
  67. var res []CheckGroup
  68. for _, cg := range checkGroups {
  69. var status CheckGroupStatus
  70. switch cg.Status {
  71. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_PASSED:
  72. status = CheckGroupStatus_Passed
  73. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_FAILED:
  74. status = CheckGroupStatus_Failed
  75. default:
  76. return res, telemetry.Error(ctx, span, nil, "invalid compliance check status")
  77. }
  78. res = append(res, CheckGroup{
  79. Name: cg.Name,
  80. Status: status,
  81. Message: cg.Message,
  82. })
  83. }
  84. return res, nil
  85. }
  86. // VendorCheckGroupsFromProto converts the vendor compliance check proto to the internal representation
  87. func VendorCheckGroupsFromProto(ctx context.Context, vendorCheck []*porterv1.VendorComplianceCheck) ([]VendorComplianceCheck, error) {
  88. ctx, span := telemetry.NewSpan(ctx, "vendor-compliance-checks-from-proto")
  89. defer span.End()
  90. var res []VendorComplianceCheck
  91. for _, vc := range vendorCheck {
  92. var status VendorComplianceCheckStatus
  93. switch vc.Status {
  94. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_PASSED:
  95. status = VendorComplianceCheckStatus_Passed
  96. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_FAILED:
  97. status = VendorComplianceCheckStatus_Failing
  98. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_NOT_APPLICABLE:
  99. status = VendorComplianceCheckStatus_NotApplicable
  100. default:
  101. return res, telemetry.Error(ctx, span, nil, "invalid compliance check status")
  102. }
  103. res = append(res, VendorComplianceCheck{
  104. Check: vc.Description,
  105. CheckGroup: vc.CheckGroup,
  106. Status: status,
  107. Reason: vc.Reason,
  108. VendorCheckID: vc.VendorCheckId,
  109. })
  110. }
  111. return res, nil
  112. }