slack.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package gorm
  2. import (
  3. "github.com/porter-dev/porter/internal/encryption"
  4. "github.com/porter-dev/porter/internal/repository"
  5. "gorm.io/gorm"
  6. ints "github.com/porter-dev/porter/internal/models/integrations"
  7. )
  8. // SlackIntegrationRepository uses gorm.DB for querying the database
  9. type SlackIntegrationRepository struct {
  10. db *gorm.DB
  11. key *[32]byte
  12. }
  13. // NewSlackIntegrationRepository returns a SlackIntegrationRepository which uses
  14. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  15. // sensitive data
  16. func NewSlackIntegrationRepository(
  17. db *gorm.DB,
  18. key *[32]byte,
  19. ) repository.SlackIntegrationRepository {
  20. return &SlackIntegrationRepository{db, key}
  21. }
  22. // CreateSlackIntegration creates a new kube auth mechanism
  23. func (repo *SlackIntegrationRepository) CreateSlackIntegration(
  24. slackInt *ints.SlackIntegration,
  25. ) (*ints.SlackIntegration, error) {
  26. err := repo.EncryptSlackIntegrationData(slackInt, repo.key)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if err := repo.db.Create(slackInt).Error; err != nil {
  31. return nil, err
  32. }
  33. return slackInt, nil
  34. }
  35. // ListSlackIntegrationsByProjectID finds all kube auth mechanisms
  36. // for a given project id
  37. func (repo *SlackIntegrationRepository) ListSlackIntegrationsByProjectID(
  38. projectID uint,
  39. ) ([]*ints.SlackIntegration, error) {
  40. slackInts := []*ints.SlackIntegration{}
  41. if err := repo.db.Where("project_id = ?", projectID).Find(&slackInts).Error; err != nil {
  42. return nil, err
  43. }
  44. for _, slackInt := range slackInts {
  45. repo.DecryptSlackIntegrationData(slackInt, repo.key)
  46. }
  47. return slackInts, nil
  48. }
  49. // DeleteSlackIntegration deletes a slack integration by ID
  50. func (repo *SlackIntegrationRepository) DeleteSlackIntegration(
  51. integrationID uint,
  52. ) error {
  53. if err := repo.db.Where("id = ?", integrationID).Delete(&ints.SlackIntegration{}).Error; err != nil {
  54. return err
  55. }
  56. return nil
  57. }
  58. // EncryptSlackIntegrationData will encrypt the slack integration data before
  59. // writing to the DB
  60. func (repo *SlackIntegrationRepository) EncryptSlackIntegrationData(
  61. slackInt *ints.SlackIntegration,
  62. key *[32]byte,
  63. ) error {
  64. if len(slackInt.ClientID) > 0 {
  65. cipherData, err := encryption.Encrypt(slackInt.ClientID, key)
  66. if err != nil {
  67. return err
  68. }
  69. slackInt.ClientID = cipherData
  70. }
  71. if len(slackInt.AccessToken) > 0 {
  72. cipherData, err := encryption.Encrypt(slackInt.AccessToken, key)
  73. if err != nil {
  74. return err
  75. }
  76. slackInt.AccessToken = cipherData
  77. }
  78. if len(slackInt.RefreshToken) > 0 {
  79. cipherData, err := encryption.Encrypt(slackInt.RefreshToken, key)
  80. if err != nil {
  81. return err
  82. }
  83. slackInt.RefreshToken = cipherData
  84. }
  85. if len(slackInt.Webhook) > 0 {
  86. cipherData, err := encryption.Encrypt(slackInt.Webhook, key)
  87. if err != nil {
  88. return err
  89. }
  90. slackInt.Webhook = cipherData
  91. }
  92. return nil
  93. }
  94. // DecryptSlackIntegrationData will decrypt the slack integration data before
  95. // returning it from the DB
  96. func (repo *SlackIntegrationRepository) DecryptSlackIntegrationData(
  97. slackInt *ints.SlackIntegration,
  98. key *[32]byte,
  99. ) error {
  100. if len(slackInt.ClientID) > 0 {
  101. plaintext, err := encryption.Decrypt(slackInt.ClientID, key)
  102. if err != nil {
  103. return err
  104. }
  105. slackInt.ClientID = plaintext
  106. }
  107. if len(slackInt.AccessToken) > 0 {
  108. plaintext, err := encryption.Decrypt(slackInt.AccessToken, key)
  109. if err != nil {
  110. return err
  111. }
  112. slackInt.AccessToken = plaintext
  113. }
  114. if len(slackInt.RefreshToken) > 0 {
  115. plaintext, err := encryption.Decrypt(slackInt.RefreshToken, key)
  116. if err != nil {
  117. return err
  118. }
  119. slackInt.RefreshToken = plaintext
  120. }
  121. if len(slackInt.Webhook) > 0 {
  122. plaintext, err := encryption.Decrypt(slackInt.Webhook, key)
  123. if err != nil {
  124. return err
  125. }
  126. slackInt.Webhook = plaintext
  127. }
  128. return nil
  129. }