|
|
@@ -3,6 +3,7 @@ package gorm
|
|
|
import (
|
|
|
"github.com/porter-dev/porter/internal/models"
|
|
|
"github.com/porter-dev/porter/internal/repository"
|
|
|
+ "github.com/porter-dev/porter/internal/repository/credentials"
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
ints "github.com/porter-dev/porter/internal/models/integrations"
|
|
|
@@ -83,10 +84,6 @@ func (repo *KubeIntegrationRepository) ListKubeIntegrationsByProjectID(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- for _, ki := range kis {
|
|
|
- repo.DecryptKubeIntegrationData(ki, repo.key)
|
|
|
- }
|
|
|
-
|
|
|
return kis, nil
|
|
|
}
|
|
|
|
|
|
@@ -303,10 +300,6 @@ func (repo *BasicIntegrationRepository) ListBasicIntegrationsByProjectID(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- for _, basic := range basics {
|
|
|
- repo.DecryptBasicIntegrationData(basic, repo.key)
|
|
|
- }
|
|
|
-
|
|
|
return basics, nil
|
|
|
}
|
|
|
|
|
|
@@ -443,10 +436,6 @@ func (repo *OIDCIntegrationRepository) ListOIDCIntegrationsByProjectID(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- for _, oidc := range oidcs {
|
|
|
- repo.DecryptOIDCIntegrationData(oidc, repo.key)
|
|
|
- }
|
|
|
-
|
|
|
return oidcs, nil
|
|
|
}
|
|
|
|
|
|
@@ -590,8 +579,9 @@ func (repo *OIDCIntegrationRepository) DecryptOIDCIntegrationData(
|
|
|
|
|
|
// OAuthIntegrationRepository uses gorm.DB for querying the database
|
|
|
type OAuthIntegrationRepository struct {
|
|
|
- db *gorm.DB
|
|
|
- key *[32]byte
|
|
|
+ db *gorm.DB
|
|
|
+ key *[32]byte
|
|
|
+ storageBackend credentials.CredentialStorage
|
|
|
}
|
|
|
|
|
|
// NewOAuthIntegrationRepository returns a OAuthIntegrationRepository which uses
|
|
|
@@ -600,8 +590,9 @@ type OAuthIntegrationRepository struct {
|
|
|
func NewOAuthIntegrationRepository(
|
|
|
db *gorm.DB,
|
|
|
key *[32]byte,
|
|
|
+ storageBackend credentials.CredentialStorage,
|
|
|
) repository.OAuthIntegrationRepository {
|
|
|
- return &OAuthIntegrationRepository{db, key}
|
|
|
+ return &OAuthIntegrationRepository{db, key, storageBackend}
|
|
|
}
|
|
|
|
|
|
// CreateOAuthIntegration creates a new oauth auth mechanism
|
|
|
@@ -614,6 +605,19 @@ func (repo *OAuthIntegrationRepository) CreateOAuthIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ // if storage backend is not nil, strip out credential data, which will be stored in credential
|
|
|
+ // storage backend after write to DB
|
|
|
+ var credentialData = &credentials.OAuthCredential{}
|
|
|
+
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ credentialData.AccessToken = am.AccessToken
|
|
|
+ credentialData.RefreshToken = am.RefreshToken
|
|
|
+ credentialData.ClientID = am.ClientID
|
|
|
+ am.AccessToken = []byte{}
|
|
|
+ am.RefreshToken = []byte{}
|
|
|
+ am.ClientID = []byte{}
|
|
|
+ }
|
|
|
+
|
|
|
project := &models.Project{}
|
|
|
|
|
|
if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
|
|
|
@@ -630,6 +634,14 @@ func (repo *OAuthIntegrationRepository) CreateOAuthIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ err = repo.storageBackend.WriteOAuthCredential(am, credentialData)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return am, nil
|
|
|
}
|
|
|
|
|
|
@@ -643,6 +655,18 @@ func (repo *OAuthIntegrationRepository) ReadOAuthIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ credentialData, err := repo.storageBackend.GetOAuthCredential(oauth)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ oauth.AccessToken = credentialData.AccessToken
|
|
|
+ oauth.RefreshToken = credentialData.RefreshToken
|
|
|
+ oauth.ClientID = credentialData.ClientID
|
|
|
+ }
|
|
|
+
|
|
|
err := repo.DecryptOAuthIntegrationData(oauth, repo.key)
|
|
|
|
|
|
if err != nil {
|
|
|
@@ -663,10 +687,6 @@ func (repo *OAuthIntegrationRepository) ListOAuthIntegrationsByProjectID(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- for _, oauth := range oauths {
|
|
|
- repo.DecryptOAuthIntegrationData(oauth, repo.key)
|
|
|
- }
|
|
|
-
|
|
|
return oauths, nil
|
|
|
}
|
|
|
|
|
|
@@ -680,6 +700,19 @@ func (repo *OAuthIntegrationRepository) UpdateOAuthIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ // if storage backend is not nil, strip out credential data, which will be stored in credential
|
|
|
+ // storage backend after write to DB
|
|
|
+ var credentialData = &credentials.OAuthCredential{}
|
|
|
+
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ credentialData.AccessToken = am.AccessToken
|
|
|
+ credentialData.RefreshToken = am.RefreshToken
|
|
|
+ credentialData.ClientID = am.ClientID
|
|
|
+ am.AccessToken = []byte{}
|
|
|
+ am.RefreshToken = []byte{}
|
|
|
+ am.ClientID = []byte{}
|
|
|
+ }
|
|
|
+
|
|
|
if err := repo.db.Save(am).Error; err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
@@ -690,6 +723,14 @@ func (repo *OAuthIntegrationRepository) UpdateOAuthIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ err = repo.storageBackend.WriteOAuthCredential(am, credentialData)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return am, nil
|
|
|
}
|
|
|
|
|
|
@@ -773,8 +814,9 @@ func (repo *OAuthIntegrationRepository) DecryptOAuthIntegrationData(
|
|
|
|
|
|
// GCPIntegrationRepository uses gorm.DB for querying the database
|
|
|
type GCPIntegrationRepository struct {
|
|
|
- db *gorm.DB
|
|
|
- key *[32]byte
|
|
|
+ db *gorm.DB
|
|
|
+ key *[32]byte
|
|
|
+ storageBackend credentials.CredentialStorage
|
|
|
}
|
|
|
|
|
|
// NewGCPIntegrationRepository returns a GCPIntegrationRepository which uses
|
|
|
@@ -783,8 +825,9 @@ type GCPIntegrationRepository struct {
|
|
|
func NewGCPIntegrationRepository(
|
|
|
db *gorm.DB,
|
|
|
key *[32]byte,
|
|
|
+ storageBackend credentials.CredentialStorage,
|
|
|
) repository.GCPIntegrationRepository {
|
|
|
- return &GCPIntegrationRepository{db, key}
|
|
|
+ return &GCPIntegrationRepository{db, key, storageBackend}
|
|
|
}
|
|
|
|
|
|
// CreateGCPIntegration creates a new gcp auth mechanism
|
|
|
@@ -797,6 +840,15 @@ func (repo *GCPIntegrationRepository) CreateGCPIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ // if storage backend is not nil, strip out credential data, which will be stored in credential
|
|
|
+ // storage backend after write to DB
|
|
|
+ var credentialData = &credentials.GCPCredential{}
|
|
|
+
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ credentialData.GCPKeyData = am.GCPKeyData
|
|
|
+ am.GCPKeyData = []byte{}
|
|
|
+ }
|
|
|
+
|
|
|
project := &models.Project{}
|
|
|
|
|
|
if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
|
|
|
@@ -813,6 +865,14 @@ func (repo *GCPIntegrationRepository) CreateGCPIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ err = repo.storageBackend.WriteGCPCredential(am, credentialData)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return am, nil
|
|
|
}
|
|
|
|
|
|
@@ -826,6 +886,16 @@ func (repo *GCPIntegrationRepository) ReadGCPIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ credentialData, err := repo.storageBackend.GetGCPCredential(gcp)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ gcp.GCPKeyData = credentialData.GCPKeyData
|
|
|
+ }
|
|
|
+
|
|
|
err := repo.DecryptGCPIntegrationData(gcp, repo.key)
|
|
|
|
|
|
if err != nil {
|
|
|
@@ -846,10 +916,6 @@ func (repo *GCPIntegrationRepository) ListGCPIntegrationsByProjectID(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- for _, gcp := range gcps {
|
|
|
- repo.DecryptGCPIntegrationData(gcp, repo.key)
|
|
|
- }
|
|
|
-
|
|
|
return gcps, nil
|
|
|
}
|
|
|
|
|
|
@@ -893,8 +959,9 @@ func (repo *GCPIntegrationRepository) DecryptGCPIntegrationData(
|
|
|
|
|
|
// AWSIntegrationRepository uses gorm.DB for querying the database
|
|
|
type AWSIntegrationRepository struct {
|
|
|
- db *gorm.DB
|
|
|
- key *[32]byte
|
|
|
+ db *gorm.DB
|
|
|
+ key *[32]byte
|
|
|
+ storageBackend credentials.CredentialStorage
|
|
|
}
|
|
|
|
|
|
// NewAWSIntegrationRepository returns a AWSIntegrationRepository which uses
|
|
|
@@ -903,8 +970,9 @@ type AWSIntegrationRepository struct {
|
|
|
func NewAWSIntegrationRepository(
|
|
|
db *gorm.DB,
|
|
|
key *[32]byte,
|
|
|
+ storageBackend credentials.CredentialStorage,
|
|
|
) repository.AWSIntegrationRepository {
|
|
|
- return &AWSIntegrationRepository{db, key}
|
|
|
+ return &AWSIntegrationRepository{db, key, storageBackend}
|
|
|
}
|
|
|
|
|
|
// CreateAWSIntegration creates a new aws auth mechanism
|
|
|
@@ -917,6 +985,21 @@ func (repo *AWSIntegrationRepository) CreateAWSIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ // if storage backend is not nil, strip out credential data, which will be stored in credential
|
|
|
+ // storage backend after write to DB
|
|
|
+ var credentialData = &credentials.AWSCredential{}
|
|
|
+
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ credentialData.AWSAccessKeyID = am.AWSAccessKeyID
|
|
|
+ credentialData.AWSClusterID = am.AWSClusterID
|
|
|
+ credentialData.AWSSecretAccessKey = am.AWSSecretAccessKey
|
|
|
+ credentialData.AWSSessionToken = am.AWSSessionToken
|
|
|
+ am.AWSAccessKeyID = []byte{}
|
|
|
+ am.AWSClusterID = []byte{}
|
|
|
+ am.AWSSecretAccessKey = []byte{}
|
|
|
+ am.AWSSessionToken = []byte{}
|
|
|
+ }
|
|
|
+
|
|
|
project := &models.Project{}
|
|
|
|
|
|
if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
|
|
|
@@ -933,6 +1016,14 @@ func (repo *AWSIntegrationRepository) CreateAWSIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ err = repo.storageBackend.WriteAWSCredential(am, credentialData)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return am, nil
|
|
|
}
|
|
|
|
|
|
@@ -946,10 +1037,33 @@ func (repo *AWSIntegrationRepository) OverwriteAWSIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ // if storage backend is not nil, strip out credential data, which will be stored in credential
|
|
|
+ // storage backend after write to DB
|
|
|
+ var credentialData = &credentials.AWSCredential{}
|
|
|
+
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ credentialData.AWSAccessKeyID = am.AWSAccessKeyID
|
|
|
+ credentialData.AWSClusterID = am.AWSClusterID
|
|
|
+ credentialData.AWSSecretAccessKey = am.AWSSecretAccessKey
|
|
|
+ credentialData.AWSSessionToken = am.AWSSessionToken
|
|
|
+ am.AWSAccessKeyID = []byte{}
|
|
|
+ am.AWSClusterID = []byte{}
|
|
|
+ am.AWSSecretAccessKey = []byte{}
|
|
|
+ am.AWSSessionToken = []byte{}
|
|
|
+ }
|
|
|
+
|
|
|
if err := repo.db.Save(am).Error; err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ err = repo.storageBackend.WriteAWSCredential(am, credentialData)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return am, nil
|
|
|
}
|
|
|
|
|
|
@@ -963,6 +1077,19 @@ func (repo *AWSIntegrationRepository) ReadAWSIntegration(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
+ if repo.storageBackend != nil {
|
|
|
+ credentialData, err := repo.storageBackend.GetAWSCredential(aws)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ aws.AWSAccessKeyID = credentialData.AWSAccessKeyID
|
|
|
+ aws.AWSClusterID = credentialData.AWSClusterID
|
|
|
+ aws.AWSSecretAccessKey = credentialData.AWSSecretAccessKey
|
|
|
+ aws.AWSSessionToken = credentialData.AWSSessionToken
|
|
|
+ }
|
|
|
+
|
|
|
err := repo.DecryptAWSIntegrationData(aws, repo.key)
|
|
|
|
|
|
if err != nil {
|
|
|
@@ -983,10 +1110,6 @@ func (repo *AWSIntegrationRepository) ListAWSIntegrationsByProjectID(
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- for _, aws := range awss {
|
|
|
- repo.DecryptAWSIntegrationData(aws, repo.key)
|
|
|
- }
|
|
|
-
|
|
|
return awss, nil
|
|
|
}
|
|
|
|