| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- package gorm
- import (
- "encoding/hex"
- "fmt"
- "github.com/porter-dev/porter/internal/encryption"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/repository"
- "gorm.io/gorm"
- )
- // InfraRepository uses gorm.DB for querying the database
- type InfraRepository struct {
- db *gorm.DB
- key *[32]byte
- }
- // NewInfraRepository returns a InfraRepository which uses
- // gorm.DB for querying the database
- func NewInfraRepository(db *gorm.DB, key *[32]byte) repository.InfraRepository {
- return &InfraRepository{db, key}
- }
- // CreateInfra creates a new aws infra
- func (repo *InfraRepository) CreateInfra(infra *models.Infra) (*models.Infra, error) {
- err := repo.EncryptInfraData(infra, repo.key)
- if err != nil {
- return nil, err
- }
- project := &models.Project{}
- if err := repo.db.Where("id = ?", infra.ProjectID).First(&project).Error; err != nil {
- return nil, err
- }
- assoc := repo.db.Model(&project).Association("Infras")
- if assoc.Error != nil {
- return nil, assoc.Error
- }
- if err := assoc.Append(infra); err != nil {
- return nil, err
- }
- err = repo.DecryptInfraData(infra, repo.key)
- if err != nil {
- return nil, err
- }
- return infra, nil
- }
- // ReadInfra gets a aws infra specified by a unique id
- func (repo *InfraRepository) ReadInfra(projectID, infraID uint) (*models.Infra, error) {
- infra := &models.Infra{}
- if err := repo.db.Where("project_id = ? AND id = ?", projectID, infraID).First(&infra).Error; err != nil {
- return nil, err
- }
- err := repo.DecryptInfraData(infra, repo.key)
- if err != nil {
- return nil, err
- }
- return infra, nil
- }
- // ListInfrasByProjectID finds all aws infras
- // for a given project id
- func (repo *InfraRepository) ListInfrasByProjectID(
- projectID uint,
- apiVersion string,
- ) ([]*models.Infra, error) {
- infras := []*models.Infra{}
- query := repo.db.Where("project_id = ?", projectID).Order("updated_at desc")
- if apiVersion != "" {
- query = query.Where("api_version = ?", apiVersion)
- }
- if err := query.Find(&infras).Error; err != nil {
- return nil, err
- }
- infraIDs := make([]uint, 0)
- for _, infra := range infras {
- repo.DecryptInfraData(infra, repo.key)
- infraIDs = append(infraIDs, infra.ID)
- }
- // get the latest operation for each infra and use it to set LastApplied
- operations := make([]*models.Operation, 0)
- if err := repo.db.Where("operations.infra_id IN (?)", infraIDs).Where(`
- operations.id IN (
- SELECT o2.id FROM (SELECT MAX(operations.id) id FROM operations WHERE operations.infra_id IN (?) GROUP BY operations.infra_id) o2
- )
- `, infraIDs).Find(&operations).Error; err != nil {
- return nil, err
- }
- // insert operations into a map
- infraIDToOperationMap := make(map[uint]models.Operation)
- for _, op := range operations {
- err := repo.DecryptOperationData(op, repo.key)
- if err == nil {
- infraIDToOperationMap[op.InfraID] = *op
- }
- }
- // look up each revision for each stack
- for _, infra := range infras {
- if _, exists := infraIDToOperationMap[infra.ID]; exists {
- infra.LastApplied = infraIDToOperationMap[infra.ID].LastApplied
- }
- }
- return infras, nil
- }
- // UpdateInfra modifies an existing Infra in the database
- func (repo *InfraRepository) UpdateInfra(
- ai *models.Infra,
- ) (*models.Infra, error) {
- err := repo.EncryptInfraData(ai, repo.key)
- if err != nil {
- return nil, err
- }
- if err := repo.db.Save(ai).Error; err != nil {
- return nil, err
- }
- err = repo.DecryptInfraData(ai, repo.key)
- if err != nil {
- return nil, err
- }
- return ai, nil
- }
- func (repo *InfraRepository) AddOperation(infra *models.Infra, operation *models.Operation) (*models.Operation, error) {
- // don't accept operations within a 10-length unique ID
- if len(operation.UID) != hex.EncodedLen(10) {
- return nil, fmt.Errorf("operation must have unique ID with hex-decoded length 10, length is %d", len(operation.UID))
- }
- // encrypt the operation data
- err := repo.EncryptOperationData(operation, repo.key)
- if err != nil {
- return nil, err
- }
- assoc := repo.db.Model(&infra).Association("Operations")
- if assoc.Error != nil {
- return nil, assoc.Error
- }
- if err := assoc.Append(operation); err != nil {
- return nil, err
- }
- if err := repo.db.Save(operation).Error; err != nil {
- return nil, err
- }
- // decrypt the operation data before returning it
- if err := repo.DecryptOperationData(operation, repo.key); err != nil {
- return nil, err
- }
- return operation, nil
- }
- func (repo *InfraRepository) ListOperations(infraID uint) ([]*models.Operation, error) {
- operations := make([]*models.Operation, 0)
- if err := repo.db.Where("infra_id = ?", infraID).Order("id desc").Find(&operations).Error; err != nil {
- return nil, err
- }
- return operations, nil
- }
- func (repo *InfraRepository) ReadOperation(infraID uint, operationUID string) (*models.Operation, error) {
- operation := &models.Operation{}
- if err := repo.db.Order("id desc").Where("infra_id = ? AND uid = ?", infraID, operationUID).First(&operation).Error; err != nil {
- return nil, err
- }
- // decrypt the operation data before returning it
- if err := repo.DecryptOperationData(operation, repo.key); err != nil {
- return nil, err
- }
- return operation, nil
- }
- func (repo *InfraRepository) GetLatestOperation(infra *models.Infra) (*models.Operation, error) {
- operation := &models.Operation{}
- if err := repo.db.Order("id desc").Where("infra_id = ?", infra.ID).First(&operation).Error; err != nil {
- return nil, err
- }
- // decrypt the operation data before returning it
- if err := repo.DecryptOperationData(operation, repo.key); err != nil {
- return nil, err
- }
- return operation, nil
- }
- // UpdateInfra modifies an existing Infra in the database
- func (repo *InfraRepository) UpdateOperation(
- operation *models.Operation,
- ) (*models.Operation, error) {
- err := repo.EncryptOperationData(operation, repo.key)
- if err != nil {
- return nil, err
- }
- if err := repo.db.Save(operation).Error; err != nil {
- return nil, err
- }
- err = repo.DecryptOperationData(operation, repo.key)
- if err != nil {
- return nil, err
- }
- return operation, nil
- }
- // EncryptInfraData will encrypt the infra data before
- // writing to the DB
- func (repo *InfraRepository) EncryptInfraData(
- infra *models.Infra,
- key *[32]byte,
- ) error {
- if len(infra.LastApplied) > 0 {
- cipherData, err := encryption.Encrypt(infra.LastApplied, key)
- if err != nil {
- return err
- }
- infra.LastApplied = cipherData
- }
- return nil
- }
- // DecryptInfraData will decrypt the user's infra data before
- // returning it from the DB
- func (repo *InfraRepository) DecryptInfraData(
- infra *models.Infra,
- key *[32]byte,
- ) error {
- if len(infra.LastApplied) > 0 {
- plaintext, err := encryption.Decrypt(infra.LastApplied, key)
- if err != nil {
- return err
- }
- infra.LastApplied = plaintext
- }
- return nil
- }
- // EncryptOperationData will encrypt the operation data before
- // writing to the DB
- func (repo *InfraRepository) EncryptOperationData(
- operation *models.Operation,
- key *[32]byte,
- ) error {
- if len(operation.LastApplied) > 0 {
- cipherData, err := encryption.Encrypt(operation.LastApplied, key)
- if err != nil {
- return err
- }
- operation.LastApplied = cipherData
- }
- return nil
- }
- // DecryptOperationData will decrypt the user's operation data before
- // returning it from the DB
- func (repo *InfraRepository) DecryptOperationData(
- operation *models.Operation,
- key *[32]byte,
- ) error {
- if len(operation.LastApplied) > 0 {
- plaintext, err := encryption.Decrypt(operation.LastApplied, key)
- if err != nil {
- return err
- }
- operation.LastApplied = plaintext
- }
- return nil
- }
|