convert.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // Profile is used to indicate which compliance profile the compliance check results are from
  52. type Profile string
  53. const (
  54. // Profile_SOC2 is used to indicate that the check results are for the SOC2 compliance profile
  55. Profile_SOC2 Profile = "soc2"
  56. // Profile_HIPAA is used to indicate that the check results are for the HIPAA compliance profile
  57. Profile_HIPAA Profile = "hipaa"
  58. )
  59. // CheckGroupsFromProto converts the compliance check group proto to the internal representation
  60. func CheckGroupsFromProto(ctx context.Context, checkGroups []*porterv1.ContractComplianceCheckGroup) ([]CheckGroup, error) {
  61. ctx, span := telemetry.NewSpan(ctx, "compliance-checks-from-proto")
  62. defer span.End()
  63. var res []CheckGroup
  64. for _, cg := range checkGroups {
  65. var status CheckGroupStatus
  66. switch cg.Status {
  67. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_PASSED:
  68. status = CheckGroupStatus_Passed
  69. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_FAILED:
  70. status = CheckGroupStatus_Failed
  71. default:
  72. return res, telemetry.Error(ctx, span, nil, "invalid compliance check status")
  73. }
  74. res = append(res, CheckGroup{
  75. Name: cg.Name,
  76. Status: status,
  77. Message: cg.Message,
  78. })
  79. }
  80. return res, nil
  81. }
  82. // VendorCheckGroupsFromProto converts the vendor compliance check proto to the internal representation
  83. func VendorCheckGroupsFromProto(ctx context.Context, vendorCheck []*porterv1.VendorComplianceCheck) ([]VendorComplianceCheck, error) {
  84. ctx, span := telemetry.NewSpan(ctx, "vendor-compliance-checks-from-proto")
  85. defer span.End()
  86. var res []VendorComplianceCheck
  87. for _, vc := range vendorCheck {
  88. var status VendorComplianceCheckStatus
  89. switch vc.Status {
  90. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_PASSED:
  91. status = VendorComplianceCheckStatus_Passed
  92. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_FAILED:
  93. status = VendorComplianceCheckStatus_Failing
  94. case porterv1.EnumComplianceCheckStatus_ENUM_COMPLIANCE_CHECK_STATUS_NOT_APPLICABLE:
  95. status = VendorComplianceCheckStatus_NotApplicable
  96. default:
  97. return res, telemetry.Error(ctx, span, nil, "invalid compliance check status")
  98. }
  99. res = append(res, VendorComplianceCheck{
  100. Check: vc.Description,
  101. CheckGroup: vc.CheckGroup,
  102. Status: status,
  103. Reason: vc.Reason,
  104. })
  105. }
  106. return res, nil
  107. }