aws_assume_role_chain.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package gorm
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/porter-dev/porter/internal/models"
  7. "github.com/porter-dev/porter/internal/repository"
  8. "gorm.io/gorm"
  9. )
  10. // AWSAssumeRoleChain uses gorm.DB for querying the database
  11. type AWSAssumeRoleChain struct {
  12. db *gorm.DB
  13. }
  14. // NewAPIContractRevisioner creates an APIRevision connection
  15. func NewAWSAssumeRoleChainer(db *gorm.DB) repository.AWSAssumeRoleChainer {
  16. return &AWSAssumeRoleChain{db}
  17. }
  18. // List returns a list of aws assume role chains where the target arn is not owned by Porter.
  19. // This allows for only returning the customer ARNs
  20. func (cr AWSAssumeRoleChain) List(ctx context.Context, projectID uint) ([]*models.AWSAssumeRoleChain, error) {
  21. var confs []*models.AWSAssumeRoleChain
  22. if projectID == 0 {
  23. return nil, errors.New("must provide a project ID")
  24. }
  25. // porterInternalAccounts are accounts which should be hidden from users, such as bastion or production accounts
  26. porterInternalAccounts := []string{
  27. "108458755588", // CAPI Bastion
  28. "813111008191", // Internal Tooling Cluster
  29. "975032674314", // Old production account
  30. }
  31. query := "project_id = ?"
  32. for _, account := range porterInternalAccounts {
  33. query += fmt.Sprintf(" and target_arn not like '%%arn:aws:iam::%s%%'", account)
  34. }
  35. tx := cr.db.Where(query, projectID).Find(&confs)
  36. if tx.Error != nil {
  37. return nil, tx.Error
  38. }
  39. return confs, nil
  40. }
  41. // ListByAwsAccountId returns a list of aws assume role chains where the target arn is owned by the supplied AWS account ID.
  42. func (cr AWSAssumeRoleChain) ListByAwsAccountId(ctx context.Context, awsAccountID string) ([]*models.AWSAssumeRoleChain, error) {
  43. var confs []*models.AWSAssumeRoleChain
  44. if awsAccountID == "" {
  45. return nil, errors.New("must provide an AWS account ID")
  46. }
  47. if len(awsAccountID) != 12 {
  48. return nil, fmt.Errorf("must provide a valid AWS account ID: %s", awsAccountID)
  49. }
  50. targetArn := fmt.Sprintf("arn:aws:iam::%s:role/porter-manager", awsAccountID)
  51. tx := cr.db.Where("target_arn = ?", targetArn).Find(&confs)
  52. if tx.Error != nil {
  53. return nil, tx.Error
  54. }
  55. return confs, nil
  56. }
  57. // Delete deletes an AWS assume role chain by project ID
  58. func (cr AWSAssumeRoleChain) Delete(ctx context.Context, projectID uint) error {
  59. if projectID == 0 {
  60. return errors.New("must provide a project ID")
  61. }
  62. var confs []*models.AWSAssumeRoleChain
  63. tx := cr.db.Where("project_id = ?", projectID).Find(&confs)
  64. if tx.Error != nil {
  65. return tx.Error
  66. }
  67. for _, conf := range confs {
  68. tx := cr.db.Delete(conf)
  69. if tx.Error != nil {
  70. return tx.Error
  71. }
  72. }
  73. return nil
  74. }