items.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package cwhub
  2. import (
  3. "fmt"
  4. "github.com/enescakir/emoji"
  5. "golang.org/x/mod/semver"
  6. )
  7. const (
  8. // managed item types
  9. COLLECTIONS = "collections"
  10. PARSERS = "parsers"
  11. POSTOVERFLOWS = "postoverflows"
  12. SCENARIOS = "scenarios"
  13. )
  14. // XXX: The order is important, as it is used to range over sub-items in collections
  15. var ItemTypes = []string{PARSERS, POSTOVERFLOWS, SCENARIOS, COLLECTIONS}
  16. type HubItems map[string]map[string]Item
  17. // ItemVersion is used to detect the version of a given item
  18. // by comparing the hash of each version to the local file.
  19. // If the item does not match any known version, it is considered tainted.
  20. type ItemVersion struct {
  21. Digest string `json:"digest,omitempty"` // meow
  22. Deprecated bool `json:"deprecated,omitempty"` // XXX: do we keep this?
  23. }
  24. // Item represents an object managed in the hub. It can be a parser, scenario, collection..
  25. type Item struct {
  26. // descriptive info
  27. Type string `json:"type,omitempty" yaml:"type,omitempty"` // can be any of the ItemTypes
  28. Stage string `json:"stage,omitempty" yaml:"stage,omitempty"` // Stage for parser|postoverflow: s00-raw/s01-...
  29. Name string `json:"name,omitempty"` // as seen in .index.json, usually "author/name"
  30. FileName string `json:"file_name,omitempty"` // the filename, ie. apache2-logs.yaml
  31. Description string `json:"description,omitempty" yaml:"description,omitempty"` // as seen in .index.json
  32. Author string `json:"author,omitempty"` // as seen in .index.json
  33. References []string `json:"references,omitempty" yaml:"references,omitempty"` // as seen in .index.json
  34. BelongsToCollections []string `json:"belongs_to_collections,omitempty" yaml:"belongs_to_collections,omitempty"` // parent collection if any
  35. // remote (hub) info
  36. RemotePath string `json:"path,omitempty" yaml:"remote_path,omitempty"` // the path relative to (git | hub API) ie. /parsers/stage/author/file.yaml
  37. Version string `json:"version,omitempty"` // the last version
  38. Versions map[string]ItemVersion `json:"versions,omitempty" yaml:"-"` // the list of existing versions
  39. // local (deployed) info
  40. LocalPath string `json:"local_path,omitempty" yaml:"local_path,omitempty"` // the local path relative to ${CFG_DIR}
  41. LocalVersion string `json:"local_version,omitempty"`
  42. LocalHash string `json:"local_hash,omitempty"` // the local meow
  43. Installed bool `json:"installed,omitempty"`
  44. Downloaded bool `json:"downloaded,omitempty"`
  45. UpToDate bool `json:"up_to_date,omitempty"`
  46. Tainted bool `json:"tainted,omitempty"` // has it been locally modified?
  47. Local bool `json:"local,omitempty"` // if it's a non versioned control one
  48. // if it's a collection, it can have sub items
  49. Parsers []string `json:"parsers,omitempty" yaml:"parsers,omitempty"`
  50. PostOverflows []string `json:"postoverflows,omitempty" yaml:"postoverflows,omitempty"`
  51. Scenarios []string `json:"scenarios,omitempty" yaml:"scenarios,omitempty"`
  52. Collections []string `json:"collections,omitempty" yaml:"collections,omitempty"`
  53. }
  54. type SubItem struct {
  55. Type string
  56. Name string
  57. }
  58. func (i *Item) SubItems() []SubItem {
  59. sub := make([]SubItem,
  60. len(i.Parsers) +
  61. len(i.PostOverflows) +
  62. len(i.Scenarios) +
  63. len(i.Collections))
  64. n := 0
  65. for _, name := range i.Parsers {
  66. sub[n] = SubItem{Type: PARSERS, Name: name}
  67. n++
  68. }
  69. for _, name := range i.PostOverflows {
  70. sub[n] = SubItem{Type: POSTOVERFLOWS, Name: name}
  71. n++
  72. }
  73. for _, name := range i.Scenarios {
  74. sub[n] = SubItem{Type: SCENARIOS, Name: name}
  75. n++
  76. }
  77. for _, name := range i.Collections {
  78. sub[n] = SubItem{Type: COLLECTIONS, Name: name}
  79. n++
  80. }
  81. return sub
  82. }
  83. // Status returns the status of the item as a string and an emoji
  84. // ie. "enabled,update-available" and emoji.Warning
  85. func (i *Item) Status() (string, emoji.Emoji) {
  86. status := "disabled"
  87. ok := false
  88. if i.Installed {
  89. ok = true
  90. status = "enabled"
  91. }
  92. managed := true
  93. if i.Local {
  94. managed = false
  95. status += ",local"
  96. }
  97. warning := false
  98. if i.Tainted {
  99. warning = true
  100. status += ",tainted"
  101. } else if !i.UpToDate && !i.Local {
  102. warning = true
  103. status += ",update-available"
  104. }
  105. emo := emoji.QuestionMark
  106. switch {
  107. case !managed:
  108. emo = emoji.House
  109. case !i.Installed:
  110. emo = emoji.Prohibited
  111. case warning:
  112. emo = emoji.Warning
  113. case ok:
  114. emo = emoji.CheckMark
  115. }
  116. return status, emo
  117. }
  118. // versionStatus: semver requires 'v' prefix
  119. func (i *Item) versionStatus() int {
  120. return semver.Compare("v"+i.Version, "v"+i.LocalVersion)
  121. }
  122. // validPath returns true if the (relative) path is allowed for the item
  123. // dirNmae: the directory name (ie. crowdsecurity)
  124. // fileName: the filename (ie. apache2-logs.yaml)
  125. func (i *Item) validPath(dirName, fileName string) bool {
  126. return (dirName+"/"+fileName == i.Name+".yaml") || (dirName+"/"+fileName == i.Name+".yml")
  127. }
  128. // GetItemMap returns the map of items for a given type
  129. func (h *Hub) GetItemMap(itemType string) map[string]Item {
  130. m, ok := h.Items[itemType]
  131. if !ok {
  132. return nil
  133. }
  134. return m
  135. }
  136. // GetItem returns the item from hub based on its type and full name (author/name)
  137. func (h *Hub) GetItem(itemType string, itemName string) *Item {
  138. m, ok := h.GetItemMap(itemType)[itemName]
  139. if !ok {
  140. return nil
  141. }
  142. return &m
  143. }
  144. // GetItemNames returns the list of item (full) names for a given type
  145. // ie. for parsers: crowdsecurity/apache2 crowdsecurity/nginx
  146. // The names can be used to retrieve the item with GetItem()
  147. func (h *Hub) GetItemNames(itemType string) []string {
  148. m := h.GetItemMap(itemType)
  149. if m == nil {
  150. return nil
  151. }
  152. names := make([]string, 0, len(m))
  153. for k := range m {
  154. names = append(names, k)
  155. }
  156. return names
  157. }
  158. // AddItem adds an item to the hub index
  159. func (h *Hub) AddItem(item Item) error {
  160. for _, t := range ItemTypes {
  161. if t == item.Type {
  162. h.Items[t][item.Name] = item
  163. return nil
  164. }
  165. }
  166. return fmt.Errorf("ItemType %s is unknown", item.Type)
  167. }
  168. // GetInstalledItems returns the list of installed items
  169. func (h *Hub) GetInstalledItems(itemType string) ([]Item, error) {
  170. items, ok := h.Items[itemType]
  171. if !ok {
  172. return nil, fmt.Errorf("no %s in hubIdx", itemType)
  173. }
  174. retItems := make([]Item, 0)
  175. for _, item := range items {
  176. if item.Installed {
  177. retItems = append(retItems, item)
  178. }
  179. }
  180. return retItems, nil
  181. }
  182. // GetInstalledItemsAsString returns the names of the installed items
  183. func (h *Hub) GetInstalledItemsAsString(itemType string) ([]string, error) {
  184. items, err := h.GetInstalledItems(itemType)
  185. if err != nil {
  186. return nil, err
  187. }
  188. retStr := make([]string, len(items))
  189. for i, it := range items {
  190. retStr[i] = it.Name
  191. }
  192. return retStr, nil
  193. }