sarif.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package manager
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. //Sarif ...
  7. type Sarif struct {
  8. Schema string `json:"$schema"`
  9. Version string `json:"version"`
  10. Runs []Runs `json:"runs"`
  11. }
  12. //ShortDescription ...
  13. type ShortDescription struct {
  14. Text string `json:"text"`
  15. }
  16. //FullDescription ...
  17. type FullDescription struct {
  18. Text string `json:"text"`
  19. }
  20. //Rules ...
  21. type Rules struct {
  22. ID string `json:"id"`
  23. Name string `json:"name"`
  24. }
  25. //Driver ...
  26. type Driver struct {
  27. Name string `json:"name"`
  28. SemanticVersion string `json:"semanticVersion"`
  29. Rules []Rules `json:"rules"`
  30. }
  31. //Tool ...
  32. type Tool struct {
  33. Driver Driver `json:"driver"`
  34. }
  35. //Message ...
  36. type Message struct {
  37. Text string `json:"text"`
  38. }
  39. //ArtifactLocation ...
  40. type ArtifactLocation struct {
  41. URI string `json:"uri"`
  42. }
  43. //Region ...
  44. type Region struct {
  45. StartLine int `json:"startLine"`
  46. Snippet Snippet `json:"snippet"`
  47. }
  48. //Snippet ...
  49. type Snippet struct {
  50. Text string `json:"text"`
  51. }
  52. //PhysicalLocation ...
  53. type PhysicalLocation struct {
  54. ArtifactLocation ArtifactLocation `json:"artifactLocation"`
  55. Region Region `json:"region"`
  56. }
  57. //Locations ...
  58. type Locations struct {
  59. PhysicalLocation PhysicalLocation `json:"physicalLocation"`
  60. }
  61. //Results ...
  62. type Results struct {
  63. Message Message `json:"message"`
  64. Properties ResultProperties `json:"properties"`
  65. Locations []Locations `json:"locations"`
  66. }
  67. //ResultProperties ...
  68. type ResultProperties struct {
  69. Commit string `json:"commit"`
  70. Offender string `json:"offender"`
  71. Date time.Time `json:"date"`
  72. Author string `json:"author"`
  73. Email string `json:"email"`
  74. CommitMessage string `json:"commitMessage"`
  75. Operation string `json:"gitOperation"`
  76. Repo string `json:"repo"`
  77. }
  78. //Runs ...
  79. type Runs struct {
  80. Tool Tool `json:"tool"`
  81. Results []Results `json:"results"`
  82. }
  83. func (manager *Manager) configToRules() []Rules {
  84. var rules []Rules
  85. for _, rule := range manager.Config.Rules {
  86. rules = append(rules, Rules{
  87. ID: rule.Description,
  88. Name: rule.Description,
  89. })
  90. }
  91. return rules
  92. }
  93. func (manager *Manager) leaksToResults() []Results {
  94. var results []Results
  95. for _, leak := range manager.leaks {
  96. results = append(results, Results{
  97. Message: Message{
  98. Text: fmt.Sprintf("%s secret detected", leak.Rule),
  99. },
  100. Properties: ResultProperties{
  101. Commit: leak.Commit,
  102. Offender: leak.Offender,
  103. Date: leak.Date,
  104. Author: leak.Author,
  105. Email: leak.Email,
  106. CommitMessage: leak.Message,
  107. Operation: leak.Operation,
  108. Repo: leak.Repo,
  109. },
  110. Locations: leakToLocation(leak),
  111. })
  112. }
  113. return results
  114. }
  115. func leakToLocation(leak Leak) []Locations {
  116. return []Locations{
  117. {
  118. PhysicalLocation:
  119. PhysicalLocation{
  120. ArtifactLocation: ArtifactLocation{
  121. URI: leak.File,
  122. },
  123. Region: Region{
  124. StartLine: leak.LineNumber,
  125. Snippet: Snippet{
  126. Text: leak.Line,
  127. },
  128. },
  129. },
  130. },
  131. }
  132. }