infra.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package gorm
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "github.com/porter-dev/porter/internal/encryption"
  6. "github.com/porter-dev/porter/internal/models"
  7. "github.com/porter-dev/porter/internal/repository"
  8. "gorm.io/gorm"
  9. )
  10. // InfraRepository uses gorm.DB for querying the database
  11. type InfraRepository struct {
  12. db *gorm.DB
  13. key *[32]byte
  14. }
  15. // NewInfraRepository returns a InfraRepository which uses
  16. // gorm.DB for querying the database
  17. func NewInfraRepository(db *gorm.DB, key *[32]byte) repository.InfraRepository {
  18. return &InfraRepository{db, key}
  19. }
  20. // CreateInfra creates a new aws infra
  21. func (repo *InfraRepository) CreateInfra(infra *models.Infra) (*models.Infra, error) {
  22. err := repo.EncryptInfraData(infra, repo.key)
  23. if err != nil {
  24. return nil, err
  25. }
  26. project := &models.Project{}
  27. if err := repo.db.Where("id = ?", infra.ProjectID).First(&project).Error; err != nil {
  28. return nil, err
  29. }
  30. assoc := repo.db.Model(&project).Association("Infras")
  31. if assoc.Error != nil {
  32. return nil, assoc.Error
  33. }
  34. if err := assoc.Append(infra); err != nil {
  35. return nil, err
  36. }
  37. err = repo.DecryptInfraData(infra, repo.key)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return infra, nil
  42. }
  43. // ReadInfra gets a aws infra specified by a unique id
  44. func (repo *InfraRepository) ReadInfra(projectID, infraID uint) (*models.Infra, error) {
  45. infra := &models.Infra{}
  46. if err := repo.db.Where("project_id = ? AND id = ?", projectID, infraID).First(&infra).Error; err != nil {
  47. return nil, err
  48. }
  49. err := repo.DecryptInfraData(infra, repo.key)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return infra, nil
  54. }
  55. // ListInfrasByProjectID finds all aws infras
  56. // for a given project id
  57. func (repo *InfraRepository) ListInfrasByProjectID(
  58. projectID uint,
  59. apiVersion string,
  60. ) ([]*models.Infra, error) {
  61. infras := []*models.Infra{}
  62. query := repo.db.Where("project_id = ?", projectID).Order("updated_at desc")
  63. if apiVersion != "" {
  64. query = query.Where("api_version = ?", apiVersion)
  65. }
  66. if err := query.Find(&infras).Error; err != nil {
  67. return nil, err
  68. }
  69. infraIDs := make([]uint, 0)
  70. for _, infra := range infras {
  71. repo.DecryptInfraData(infra, repo.key)
  72. infraIDs = append(infraIDs, infra.ID)
  73. }
  74. // get the latest operation for each infra and use it to set LastApplied
  75. operations := make([]*models.Operation, 0)
  76. if err := repo.db.Where("operations.infra_id IN (?)", infraIDs).Where(`
  77. operations.id IN (
  78. SELECT o2.id FROM (SELECT MAX(operations.id) id FROM operations WHERE operations.infra_id IN (?) GROUP BY operations.infra_id) o2
  79. )
  80. `, infraIDs).Find(&operations).Error; err != nil {
  81. return nil, err
  82. }
  83. // insert operations into a map
  84. infraIDToOperationMap := make(map[uint]models.Operation)
  85. for _, op := range operations {
  86. err := repo.DecryptOperationData(op, repo.key)
  87. if err == nil {
  88. infraIDToOperationMap[op.InfraID] = *op
  89. }
  90. }
  91. // look up each revision for each stack
  92. for _, infra := range infras {
  93. if _, exists := infraIDToOperationMap[infra.ID]; exists {
  94. infra.LastApplied = infraIDToOperationMap[infra.ID].LastApplied
  95. }
  96. }
  97. return infras, nil
  98. }
  99. // UpdateInfra modifies an existing Infra in the database
  100. func (repo *InfraRepository) UpdateInfra(
  101. ai *models.Infra,
  102. ) (*models.Infra, error) {
  103. err := repo.EncryptInfraData(ai, repo.key)
  104. if err != nil {
  105. return nil, err
  106. }
  107. if err := repo.db.Save(ai).Error; err != nil {
  108. return nil, err
  109. }
  110. err = repo.DecryptInfraData(ai, repo.key)
  111. if err != nil {
  112. return nil, err
  113. }
  114. return ai, nil
  115. }
  116. func (repo *InfraRepository) AddOperation(infra *models.Infra, operation *models.Operation) (*models.Operation, error) {
  117. // don't accept operations within a 10-length unique ID
  118. if len(operation.UID) != hex.EncodedLen(10) {
  119. return nil, fmt.Errorf("operation must have unique ID with hex-decoded length 10, length is %d", len(operation.UID))
  120. }
  121. // encrypt the operation data
  122. err := repo.EncryptOperationData(operation, repo.key)
  123. if err != nil {
  124. return nil, err
  125. }
  126. assoc := repo.db.Model(&infra).Association("Operations")
  127. if assoc.Error != nil {
  128. return nil, assoc.Error
  129. }
  130. if err := assoc.Append(operation); err != nil {
  131. return nil, err
  132. }
  133. if err := repo.db.Save(operation).Error; err != nil {
  134. return nil, err
  135. }
  136. // decrypt the operation data before returning it
  137. if err := repo.DecryptOperationData(operation, repo.key); err != nil {
  138. return nil, err
  139. }
  140. return operation, nil
  141. }
  142. func (repo *InfraRepository) ListOperations(infraID uint) ([]*models.Operation, error) {
  143. operations := make([]*models.Operation, 0)
  144. if err := repo.db.Where("infra_id = ?", infraID).Order("id desc").Find(&operations).Error; err != nil {
  145. return nil, err
  146. }
  147. return operations, nil
  148. }
  149. func (repo *InfraRepository) ReadOperation(infraID uint, operationUID string) (*models.Operation, error) {
  150. operation := &models.Operation{}
  151. if err := repo.db.Order("id desc").Where("infra_id = ? AND uid = ?", infraID, operationUID).First(&operation).Error; err != nil {
  152. return nil, err
  153. }
  154. // decrypt the operation data before returning it
  155. if err := repo.DecryptOperationData(operation, repo.key); err != nil {
  156. return nil, err
  157. }
  158. return operation, nil
  159. }
  160. func (repo *InfraRepository) GetLatestOperation(infra *models.Infra) (*models.Operation, error) {
  161. operation := &models.Operation{}
  162. if err := repo.db.Order("id desc").Where("infra_id = ?", infra.ID).First(&operation).Error; err != nil {
  163. return nil, err
  164. }
  165. // decrypt the operation data before returning it
  166. if err := repo.DecryptOperationData(operation, repo.key); err != nil {
  167. return nil, err
  168. }
  169. return operation, nil
  170. }
  171. // UpdateInfra modifies an existing Infra in the database
  172. func (repo *InfraRepository) UpdateOperation(
  173. operation *models.Operation,
  174. ) (*models.Operation, error) {
  175. err := repo.EncryptOperationData(operation, repo.key)
  176. if err != nil {
  177. return nil, err
  178. }
  179. if err := repo.db.Save(operation).Error; err != nil {
  180. return nil, err
  181. }
  182. err = repo.DecryptOperationData(operation, repo.key)
  183. if err != nil {
  184. return nil, err
  185. }
  186. return operation, nil
  187. }
  188. // EncryptInfraData will encrypt the infra data before
  189. // writing to the DB
  190. func (repo *InfraRepository) EncryptInfraData(
  191. infra *models.Infra,
  192. key *[32]byte,
  193. ) error {
  194. if len(infra.LastApplied) > 0 {
  195. cipherData, err := encryption.Encrypt(infra.LastApplied, key)
  196. if err != nil {
  197. return err
  198. }
  199. infra.LastApplied = cipherData
  200. }
  201. return nil
  202. }
  203. // DecryptInfraData will decrypt the user's infra data before
  204. // returning it from the DB
  205. func (repo *InfraRepository) DecryptInfraData(
  206. infra *models.Infra,
  207. key *[32]byte,
  208. ) error {
  209. if len(infra.LastApplied) > 0 {
  210. plaintext, err := encryption.Decrypt(infra.LastApplied, key)
  211. if err != nil {
  212. return err
  213. }
  214. infra.LastApplied = plaintext
  215. }
  216. return nil
  217. }
  218. // EncryptOperationData will encrypt the operation data before
  219. // writing to the DB
  220. func (repo *InfraRepository) EncryptOperationData(
  221. operation *models.Operation,
  222. key *[32]byte,
  223. ) error {
  224. if len(operation.LastApplied) > 0 {
  225. cipherData, err := encryption.Encrypt(operation.LastApplied, key)
  226. if err != nil {
  227. return err
  228. }
  229. operation.LastApplied = cipherData
  230. }
  231. return nil
  232. }
  233. // DecryptOperationData will decrypt the user's operation data before
  234. // returning it from the DB
  235. func (repo *InfraRepository) DecryptOperationData(
  236. operation *models.Operation,
  237. key *[32]byte,
  238. ) error {
  239. if len(operation.LastApplied) > 0 {
  240. plaintext, err := encryption.Decrypt(operation.LastApplied, key)
  241. if err != nil {
  242. return err
  243. }
  244. operation.LastApplied = plaintext
  245. }
  246. return nil
  247. }