| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722 |
- package gorm
- import (
- "github.com/porter-dev/porter/internal/encryption"
- "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"
- )
- // KubeIntegrationRepository uses gorm.DB for querying the database
- type KubeIntegrationRepository struct {
- db *gorm.DB
- key *[32]byte
- }
- // NewKubeIntegrationRepository returns a KubeIntegrationRepository which uses
- // gorm.DB for querying the database. It accepts an encryption key to encrypt
- // sensitive data
- func NewKubeIntegrationRepository(
- db *gorm.DB,
- key *[32]byte,
- ) repository.KubeIntegrationRepository {
- return &KubeIntegrationRepository{db, key}
- }
- // CreateKubeIntegration creates a new kube auth mechanism
- func (repo *KubeIntegrationRepository) CreateKubeIntegration(
- am *ints.KubeIntegration,
- ) (*ints.KubeIntegration, error) {
- err := repo.EncryptKubeIntegrationData(am, repo.key)
- if err != nil {
- return nil, err
- }
- project := &models.Project{}
- if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
- return nil, err
- }
- assoc := repo.db.Model(&project).Association("KubeIntegrations")
- if assoc.Error != nil {
- return nil, assoc.Error
- }
- if err := assoc.Append(am); err != nil {
- return nil, err
- }
- return am, nil
- }
- // ReadKubeIntegration finds a kube auth mechanism by id
- func (repo *KubeIntegrationRepository) ReadKubeIntegration(
- projectID, id uint,
- ) (*ints.KubeIntegration, error) {
- ki := &ints.KubeIntegration{}
- if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&ki).Error; err != nil {
- return nil, err
- }
- err := repo.DecryptKubeIntegrationData(ki, repo.key)
- if err != nil {
- return nil, err
- }
- return ki, nil
- }
- // ListKubeIntegrationsByProjectID finds all kube auth mechanisms
- // for a given project id
- func (repo *KubeIntegrationRepository) ListKubeIntegrationsByProjectID(
- projectID uint,
- ) ([]*ints.KubeIntegration, error) {
- kis := []*ints.KubeIntegration{}
- if err := repo.db.Where("project_id = ?", projectID).Find(&kis).Error; err != nil {
- return nil, err
- }
- return kis, nil
- }
- // EncryptKubeIntegrationData will encrypt the kube integration data before
- // writing to the DB
- func (repo *KubeIntegrationRepository) EncryptKubeIntegrationData(
- ki *ints.KubeIntegration,
- key *[32]byte,
- ) error {
- if len(ki.ClientCertificateData) > 0 {
- cipherData, err := encryption.Encrypt(ki.ClientCertificateData, key)
- if err != nil {
- return err
- }
- ki.ClientCertificateData = cipherData
- }
- if len(ki.ClientKeyData) > 0 {
- cipherData, err := encryption.Encrypt(ki.ClientKeyData, key)
- if err != nil {
- return err
- }
- ki.ClientKeyData = cipherData
- }
- if len(ki.Token) > 0 {
- cipherData, err := encryption.Encrypt(ki.Token, key)
- if err != nil {
- return err
- }
- ki.Token = cipherData
- }
- if len(ki.Username) > 0 {
- cipherData, err := encryption.Encrypt(ki.Username, key)
- if err != nil {
- return err
- }
- ki.Username = cipherData
- }
- if len(ki.Password) > 0 {
- cipherData, err := encryption.Encrypt(ki.Password, key)
- if err != nil {
- return err
- }
- ki.Password = cipherData
- }
- if len(ki.Kubeconfig) > 0 {
- cipherData, err := encryption.Encrypt(ki.Kubeconfig, key)
- if err != nil {
- return err
- }
- ki.Kubeconfig = cipherData
- }
- return nil
- }
- // DecryptKubeIntegrationData will decrypt the kube integration data before
- // returning it from the DB
- func (repo *KubeIntegrationRepository) DecryptKubeIntegrationData(
- ki *ints.KubeIntegration,
- key *[32]byte,
- ) error {
- if len(ki.ClientCertificateData) > 0 {
- plaintext, err := encryption.Decrypt(ki.ClientCertificateData, key)
- if err != nil {
- return err
- }
- ki.ClientCertificateData = plaintext
- }
- if len(ki.ClientKeyData) > 0 {
- plaintext, err := encryption.Decrypt(ki.ClientKeyData, key)
- if err != nil {
- return err
- }
- ki.ClientKeyData = plaintext
- }
- if len(ki.Token) > 0 {
- plaintext, err := encryption.Decrypt(ki.Token, key)
- if err != nil {
- return err
- }
- ki.Token = plaintext
- }
- if len(ki.Username) > 0 {
- plaintext, err := encryption.Decrypt(ki.Username, key)
- if err != nil {
- return err
- }
- ki.Username = plaintext
- }
- if len(ki.Password) > 0 {
- plaintext, err := encryption.Decrypt(ki.Password, key)
- if err != nil {
- return err
- }
- ki.Password = plaintext
- }
- if len(ki.Kubeconfig) > 0 {
- plaintext, err := encryption.Decrypt(ki.Kubeconfig, key)
- if err != nil {
- return err
- }
- ki.Kubeconfig = plaintext
- }
- return nil
- }
- // BasicIntegrationRepository uses gorm.DB for querying the database
- type BasicIntegrationRepository struct {
- db *gorm.DB
- key *[32]byte
- }
- // NewBasicIntegrationRepository returns a BasicIntegrationRepository which uses
- // gorm.DB for querying the database. It accepts an encryption key to encrypt
- // sensitive data
- func NewBasicIntegrationRepository(
- db *gorm.DB,
- key *[32]byte,
- ) repository.BasicIntegrationRepository {
- return &BasicIntegrationRepository{db, key}
- }
- // CreateBasicIntegration creates a new basic auth mechanism
- func (repo *BasicIntegrationRepository) CreateBasicIntegration(
- am *ints.BasicIntegration,
- ) (*ints.BasicIntegration, error) {
- err := repo.EncryptBasicIntegrationData(am, repo.key)
- if err != nil {
- return nil, err
- }
- project := &models.Project{}
- if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
- return nil, err
- }
- assoc := repo.db.Model(&project).Association("BasicIntegrations")
- if assoc.Error != nil {
- return nil, assoc.Error
- }
- if err := assoc.Append(am); err != nil {
- return nil, err
- }
- return am, nil
- }
- // ReadBasicIntegration finds a basic auth mechanism by id
- func (repo *BasicIntegrationRepository) ReadBasicIntegration(
- projectID, id uint,
- ) (*ints.BasicIntegration, error) {
- basic := &ints.BasicIntegration{}
- if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&basic).Error; err != nil {
- return nil, err
- }
- err := repo.DecryptBasicIntegrationData(basic, repo.key)
- if err != nil {
- return nil, err
- }
- return basic, nil
- }
- // ListBasicIntegrationsByProjectID finds all basic auth mechanisms
- // for a given project id
- func (repo *BasicIntegrationRepository) ListBasicIntegrationsByProjectID(
- projectID uint,
- ) ([]*ints.BasicIntegration, error) {
- basics := []*ints.BasicIntegration{}
- if err := repo.db.Where("project_id = ?", projectID).Find(&basics).Error; err != nil {
- return nil, err
- }
- return basics, nil
- }
- // DeleteBasicIntegration deletes an existing basic auth mechanism
- func (repo *BasicIntegrationRepository) DeleteBasicIntegration(
- am *ints.BasicIntegration,
- ) (*ints.BasicIntegration, error) {
- project := &models.Project{}
- if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
- return nil, err
- }
- assoc := repo.db.Model(&project).Association("BasicIntegrations")
- if assoc.Error != nil {
- return nil, assoc.Error
- }
- if err := assoc.Delete(am); err != nil {
- return nil, err
- }
- if err := repo.db.Delete(am).Error; err != nil {
- return nil, err
- }
- return am, nil
- }
- // EncryptBasicIntegrationData will encrypt the basic integration data before
- // writing to the DB
- func (repo *BasicIntegrationRepository) EncryptBasicIntegrationData(
- basic *ints.BasicIntegration,
- key *[32]byte,
- ) error {
- if len(basic.Username) > 0 {
- cipherData, err := encryption.Encrypt(basic.Username, key)
- if err != nil {
- return err
- }
- basic.Username = cipherData
- }
- if len(basic.Password) > 0 {
- cipherData, err := encryption.Encrypt(basic.Password, key)
- if err != nil {
- return err
- }
- basic.Password = cipherData
- }
- return nil
- }
- // DecryptBasicIntegrationData will decrypt the basic integration data before
- // returning it from the DB
- func (repo *BasicIntegrationRepository) DecryptBasicIntegrationData(
- basic *ints.BasicIntegration,
- key *[32]byte,
- ) error {
- if len(basic.Username) > 0 {
- plaintext, err := encryption.Decrypt(basic.Username, key)
- if err != nil {
- return err
- }
- basic.Username = plaintext
- }
- if len(basic.Password) > 0 {
- plaintext, err := encryption.Decrypt(basic.Password, key)
- if err != nil {
- return err
- }
- basic.Password = plaintext
- }
- return nil
- }
- // OIDCIntegrationRepository uses gorm.DB for querying the database
- type OIDCIntegrationRepository struct {
- db *gorm.DB
- key *[32]byte
- }
- // NewOIDCIntegrationRepository returns a OIDCIntegrationRepository which uses
- // gorm.DB for querying the database. It accepts an encryption key to encrypt
- // sensitive data
- func NewOIDCIntegrationRepository(
- db *gorm.DB,
- key *[32]byte,
- ) repository.OIDCIntegrationRepository {
- return &OIDCIntegrationRepository{db, key}
- }
- // CreateOIDCIntegration creates a new oidc auth mechanism
- func (repo *OIDCIntegrationRepository) CreateOIDCIntegration(
- am *ints.OIDCIntegration,
- ) (*ints.OIDCIntegration, error) {
- err := repo.EncryptOIDCIntegrationData(am, repo.key)
- if err != nil {
- return nil, err
- }
- project := &models.Project{}
- if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
- return nil, err
- }
- assoc := repo.db.Model(&project).Association("OIDCIntegrations")
- if assoc.Error != nil {
- return nil, assoc.Error
- }
- if err := assoc.Append(am); err != nil {
- return nil, err
- }
- return am, nil
- }
- // ReadOIDCIntegration finds a oidc auth mechanism by id
- func (repo *OIDCIntegrationRepository) ReadOIDCIntegration(
- projectID, id uint,
- ) (*ints.OIDCIntegration, error) {
- oidc := &ints.OIDCIntegration{}
- if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&oidc).Error; err != nil {
- return nil, err
- }
- err := repo.DecryptOIDCIntegrationData(oidc, repo.key)
- if err != nil {
- return nil, err
- }
- return oidc, nil
- }
- // ListOIDCIntegrationsByProjectID finds all oidc auth mechanisms
- // for a given project id
- func (repo *OIDCIntegrationRepository) ListOIDCIntegrationsByProjectID(
- projectID uint,
- ) ([]*ints.OIDCIntegration, error) {
- oidcs := []*ints.OIDCIntegration{}
- if err := repo.db.Where("project_id = ?", projectID).Find(&oidcs).Error; err != nil {
- return nil, err
- }
- return oidcs, nil
- }
- // EncryptOIDCIntegrationData will encrypt the oidc integration data before
- // writing to the DB
- func (repo *OIDCIntegrationRepository) EncryptOIDCIntegrationData(
- oidc *ints.OIDCIntegration,
- key *[32]byte,
- ) error {
- if len(oidc.IssuerURL) > 0 {
- cipherData, err := encryption.Encrypt(oidc.IssuerURL, key)
- if err != nil {
- return err
- }
- oidc.IssuerURL = cipherData
- }
- if len(oidc.ClientID) > 0 {
- cipherData, err := encryption.Encrypt(oidc.ClientID, key)
- if err != nil {
- return err
- }
- oidc.ClientID = cipherData
- }
- if len(oidc.ClientSecret) > 0 {
- cipherData, err := encryption.Encrypt(oidc.ClientSecret, key)
- if err != nil {
- return err
- }
- oidc.ClientSecret = cipherData
- }
- if len(oidc.CertificateAuthorityData) > 0 {
- cipherData, err := encryption.Encrypt(oidc.CertificateAuthorityData, key)
- if err != nil {
- return err
- }
- oidc.CertificateAuthorityData = cipherData
- }
- if len(oidc.IDToken) > 0 {
- cipherData, err := encryption.Encrypt(oidc.IDToken, key)
- if err != nil {
- return err
- }
- oidc.IDToken = cipherData
- }
- if len(oidc.RefreshToken) > 0 {
- cipherData, err := encryption.Encrypt(oidc.RefreshToken, key)
- if err != nil {
- return err
- }
- oidc.RefreshToken = cipherData
- }
- return nil
- }
- // DecryptOIDCIntegrationData will decrypt the kube integration data before
- // returning it from the DB
- func (repo *OIDCIntegrationRepository) DecryptOIDCIntegrationData(
- oidc *ints.OIDCIntegration,
- key *[32]byte,
- ) error {
- if len(oidc.IssuerURL) > 0 {
- plaintext, err := encryption.Decrypt(oidc.IssuerURL, key)
- if err != nil {
- return err
- }
- oidc.IssuerURL = plaintext
- }
- if len(oidc.ClientID) > 0 {
- plaintext, err := encryption.Decrypt(oidc.ClientID, key)
- if err != nil {
- return err
- }
- oidc.ClientID = plaintext
- }
- if len(oidc.ClientSecret) > 0 {
- plaintext, err := encryption.Decrypt(oidc.ClientSecret, key)
- if err != nil {
- return err
- }
- oidc.ClientSecret = plaintext
- }
- if len(oidc.CertificateAuthorityData) > 0 {
- plaintext, err := encryption.Decrypt(oidc.CertificateAuthorityData, key)
- if err != nil {
- return err
- }
- oidc.CertificateAuthorityData = plaintext
- }
- if len(oidc.IDToken) > 0 {
- plaintext, err := encryption.Decrypt(oidc.IDToken, key)
- if err != nil {
- return err
- }
- oidc.IDToken = plaintext
- }
- if len(oidc.RefreshToken) > 0 {
- plaintext, err := encryption.Decrypt(oidc.RefreshToken, key)
- if err != nil {
- return err
- }
- oidc.RefreshToken = plaintext
- }
- return nil
- }
- // OAuthIntegrationRepository uses gorm.DB for querying the database
- type OAuthIntegrationRepository struct {
- db *gorm.DB
- key *[32]byte
- storageBackend credentials.CredentialStorage
- }
- // NewOAuthIntegrationRepository returns a OAuthIntegrationRepository which uses
- // gorm.DB for querying the database. It accepts an encryption key to encrypt
- // sensitive data
- func NewOAuthIntegrationRepository(
- db *gorm.DB,
- key *[32]byte,
- storageBackend credentials.CredentialStorage,
- ) repository.OAuthIntegrationRepository {
- return &OAuthIntegrationRepository{db, key, storageBackend}
- }
- // CreateOAuthIntegration creates a new oauth auth mechanism
- func (repo *OAuthIntegrationRepository) CreateOAuthIntegration(
- am *ints.OAuthIntegration,
- ) (*ints.OAuthIntegration, error) {
- err := repo.EncryptOAuthIntegrationData(am, repo.key)
- if err != nil {
- 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
- 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 {
- return nil, err
- }
- assoc := repo.db.Model(&project).Association("OAuthIntegrations")
- if assoc.Error != nil {
- return nil, assoc.Error
- }
- if err := assoc.Append(am); err != nil {
- return nil, err
- }
- if repo.storageBackend != nil {
- err = repo.storageBackend.WriteOAuthCredential(am, credentialData)
- if err != nil {
- return nil, err
- }
- }
- return am, nil
- }
- // ReadOAuthIntegration finds a oauth auth mechanism by id
- func (repo *OAuthIntegrationRepository) ReadOAuthIntegration(
- projectID, id uint,
- ) (*ints.OAuthIntegration, error) {
- oauth := &ints.OAuthIntegration{}
- if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&oauth).Error; err != nil {
- 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 {
- return nil, err
- }
- return oauth, nil
- }
- // ListOAuthIntegrationsByProjectID finds all oauth auth mechanisms
- // for a given project id
- func (repo *OAuthIntegrationRepository) ListOAuthIntegrationsByProjectID(
- projectID uint,
- ) ([]*ints.OAuthIntegration, error) {
- oauths := []*ints.OAuthIntegration{}
- if err := repo.db.Where("project_id = ?", projectID).Find(&oauths).Error; err != nil {
- return nil, err
- }
- return oauths, nil
- }
- // UpdateOAuthIntegration modifies an existing oauth integration in the database
- func (repo *OAuthIntegrationRepository) UpdateOAuthIntegration(
- am *ints.OAuthIntegration,
- ) (*ints.OAuthIntegration, error) {
- err := repo.EncryptOAuthIntegrationData(am, repo.key)
- if err != nil {
- 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
- 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
- }
- err = repo.DecryptOAuthIntegrationData(am, repo.key)
- if err != nil {
- return nil, err
- }
- if repo.storageBackend != nil {
- err = repo.storageBackend.WriteOAuthCredential(am, credentialData)
- if err != nil {
- return nil, err
- }
- }
- return am, nil
- }
- // EncryptOAuthIntegrationData will encrypt the oauth integration data before
- // writing to the DB
- func (repo *OAuthIntegrationRepository) EncryptOAuthIntegrationData(
- oauth *ints.OAuthIntegration,
- key *[32]byte,
- ) error {
- if len(oauth.ClientID) > 0 {
- cipherData, err := encryption.Encrypt(oauth.ClientID, key)
- if err != nil {
- return err
- }
- oauth.ClientID = cipherData
- }
- if len(oauth.AccessToken) > 0 {
- cipherData, err := encryption.Encrypt(oauth.AccessToken, key)
- if err != nil {
- return err
- }
- oauth.AccessToken = cipherData
- }
- if len(oauth.RefreshToken) > 0 {
- cipherData, err := encryption.Encrypt(oauth.RefreshToken, key)
- if err != nil {
- return err
- }
- oauth.RefreshToken = cipherData
- }
- return nil
- }
- // DecryptOAuthIntegrationData will decrypt the oauth integration data before
- // returning it from the DB
- func (repo *OAuthIntegrationRepository) DecryptOAuthIntegrationData(
- oauth *ints.OAuthIntegration,
- key *[32]byte,
- ) error {
- if len(oauth.ClientID) > 0 {
- plaintext, err := encryption.Decrypt(oauth.ClientID, key)
- if err != nil {
- return err
- }
- oauth.ClientID = plaintext
- }
- if len(oauth.AccessToken) > 0 {
- plaintext, err := encryption.Decrypt(oauth.AccessToken, key)
- if err != nil {
- return err
- }
- oauth.AccessToken = plaintext
- }
- if len(oauth.RefreshToken) > 0 {
- plaintext, err := encryption.Decrypt(oauth.RefreshToken, key)
- if err != nil {
- return err
- }
- oauth.RefreshToken = plaintext
- }
- return nil
- }
- // GCPIntegrationRepository uses gorm.DB for querying the database
- type GCPIntegrationRepository struct {
- db *gorm.DB
- key *[32]byte
- storageBackend credentials.CredentialStorage
- }
- // NewGCPIntegrationRepository returns a GCPIntegrationRepository which uses
- // gorm.DB for querying the database. It accepts an encryption key to encrypt
- // sensitive data
- func NewGCPIntegrationRepository(
- db *gorm.DB,
- key *[32]byte,
- storageBackend credentials.CredentialStorage,
- ) repository.GCPIntegrationRepository {
- return &GCPIntegrationRepository{db, key, storageBackend}
- }
- // CreateGCPIntegration creates a new gcp auth mechanism
- func (repo *GCPIntegrationRepository) CreateGCPIntegration(
- am *ints.GCPIntegration,
- ) (*ints.GCPIntegration, error) {
- err := repo.EncryptGCPIntegrationData(am, repo.key)
- if err != nil {
- 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
- 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 {
- return nil, err
- }
- assoc := repo.db.Model(&project).Association("GCPIntegrations")
- if assoc.Error != nil {
- return nil, assoc.Error
- }
- if err := assoc.Append(am); err != nil {
- return nil, err
- }
- if repo.storageBackend != nil {
- err = repo.storageBackend.WriteGCPCredential(am, credentialData)
- if err != nil {
- return nil, err
- }
- }
- return am, nil
- }
- // ReadGCPIntegration finds a gcp auth mechanism by id
- func (repo *GCPIntegrationRepository) ReadGCPIntegration(
- projectID, id uint,
- ) (*ints.GCPIntegration, error) {
- gcp := &ints.GCPIntegration{}
- if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&gcp).Error; err != nil {
- 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 {
- return nil, err
- }
- return gcp, nil
- }
- // ListGCPIntegrationsByProjectID finds all gcp auth mechanisms
- // for a given project id
- func (repo *GCPIntegrationRepository) ListGCPIntegrationsByProjectID(
- projectID uint,
- ) ([]*ints.GCPIntegration, error) {
- gcps := []*ints.GCPIntegration{}
- if err := repo.db.Where("project_id = ?", projectID).Find(&gcps).Error; err != nil {
- return nil, err
- }
- return gcps, nil
- }
- // EncryptGCPIntegrationData will encrypt the gcp integration data before
- // writing to the DB
- func (repo *GCPIntegrationRepository) EncryptGCPIntegrationData(
- gcp *ints.GCPIntegration,
- key *[32]byte,
- ) error {
- if len(gcp.GCPKeyData) > 0 {
- cipherData, err := encryption.Encrypt(gcp.GCPKeyData, key)
- if err != nil {
- return err
- }
- gcp.GCPKeyData = cipherData
- }
- return nil
- }
- // DecryptGCPIntegrationData will decrypt the gcp integration data before
- // returning it from the DB
- func (repo *GCPIntegrationRepository) DecryptGCPIntegrationData(
- gcp *ints.GCPIntegration,
- key *[32]byte,
- ) error {
- if len(gcp.GCPKeyData) > 0 {
- plaintext, err := encryption.Decrypt(gcp.GCPKeyData, key)
- if err != nil {
- return err
- }
- gcp.GCPKeyData = plaintext
- }
- return nil
- }
- // AWSIntegrationRepository uses gorm.DB for querying the database
- type AWSIntegrationRepository struct {
- db *gorm.DB
- key *[32]byte
- storageBackend credentials.CredentialStorage
- }
- // NewAWSIntegrationRepository returns a AWSIntegrationRepository which uses
- // gorm.DB for querying the database. It accepts an encryption key to encrypt
- // sensitive data
- func NewAWSIntegrationRepository(
- db *gorm.DB,
- key *[32]byte,
- storageBackend credentials.CredentialStorage,
- ) repository.AWSIntegrationRepository {
- return &AWSIntegrationRepository{db, key, storageBackend}
- }
- // CreateAWSIntegration creates a new aws auth mechanism
- func (repo *AWSIntegrationRepository) CreateAWSIntegration(
- am *ints.AWSIntegration,
- ) (*ints.AWSIntegration, error) {
- err := repo.EncryptAWSIntegrationData(am, repo.key)
- if err != nil {
- 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
- 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 {
- return nil, err
- }
- assoc := repo.db.Model(&project).Association("AWSIntegrations")
- if assoc.Error != nil {
- return nil, assoc.Error
- }
- if err := assoc.Append(am); err != nil {
- return nil, err
- }
- if repo.storageBackend != nil {
- err = repo.storageBackend.WriteAWSCredential(am, credentialData)
- if err != nil {
- return nil, err
- }
- }
- return am, nil
- }
- // UpdateCluster modifies an existing Cluster in the database
- func (repo *AWSIntegrationRepository) OverwriteAWSIntegration(
- am *ints.AWSIntegration,
- ) (*ints.AWSIntegration, error) {
- err := repo.EncryptAWSIntegrationData(am, repo.key)
- if err != nil {
- 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
- 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
- }
- // ReadAWSIntegration finds a aws auth mechanism by id
- func (repo *AWSIntegrationRepository) ReadAWSIntegration(
- projectID, id uint,
- ) (*ints.AWSIntegration, error) {
- aws := &ints.AWSIntegration{}
- if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&aws).Error; err != nil {
- 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 {
- return nil, err
- }
- return aws, nil
- }
- // ListAWSIntegrationsByProjectID finds all aws auth mechanisms
- // for a given project id
- func (repo *AWSIntegrationRepository) ListAWSIntegrationsByProjectID(
- projectID uint,
- ) ([]*ints.AWSIntegration, error) {
- awss := []*ints.AWSIntegration{}
- if err := repo.db.Where("project_id = ?", projectID).Find(&awss).Error; err != nil {
- return nil, err
- }
- return awss, nil
- }
- // EncryptAWSIntegrationData will encrypt the aws integration data before
- // writing to the DB
- func (repo *AWSIntegrationRepository) EncryptAWSIntegrationData(
- aws *ints.AWSIntegration,
- key *[32]byte,
- ) error {
- if len(aws.AWSClusterID) > 0 {
- cipherData, err := encryption.Encrypt(aws.AWSClusterID, key)
- if err != nil {
- return err
- }
- aws.AWSClusterID = cipherData
- }
- if len(aws.AWSAccessKeyID) > 0 {
- cipherData, err := encryption.Encrypt(aws.AWSAccessKeyID, key)
- if err != nil {
- return err
- }
- aws.AWSAccessKeyID = cipherData
- }
- if len(aws.AWSSecretAccessKey) > 0 {
- cipherData, err := encryption.Encrypt(aws.AWSSecretAccessKey, key)
- if err != nil {
- return err
- }
- aws.AWSSecretAccessKey = cipherData
- }
- if len(aws.AWSSessionToken) > 0 {
- cipherData, err := encryption.Encrypt(aws.AWSSessionToken, key)
- if err != nil {
- return err
- }
- aws.AWSSessionToken = cipherData
- }
- return nil
- }
- // DecryptAWSIntegrationData will decrypt the aws integration data before
- // returning it from the DB
- func (repo *AWSIntegrationRepository) DecryptAWSIntegrationData(
- aws *ints.AWSIntegration,
- key *[32]byte,
- ) error {
- if len(aws.AWSClusterID) > 0 {
- plaintext, err := encryption.Decrypt(aws.AWSClusterID, key)
- if err != nil {
- return err
- }
- aws.AWSClusterID = plaintext
- }
- if len(aws.AWSAccessKeyID) > 0 {
- plaintext, err := encryption.Decrypt(aws.AWSAccessKeyID, key)
- if err != nil {
- return err
- }
- aws.AWSAccessKeyID = plaintext
- }
- if len(aws.AWSSecretAccessKey) > 0 {
- plaintext, err := encryption.Decrypt(aws.AWSSecretAccessKey, key)
- if err != nil {
- return err
- }
- aws.AWSSecretAccessKey = plaintext
- }
- if len(aws.AWSSessionToken) > 0 {
- plaintext, err := encryption.Decrypt(aws.AWSSessionToken, key)
- if err != nil {
- return err
- }
- aws.AWSSessionToken = plaintext
- }
- return nil
- }
- // GithubAppInstallationRepository implements repository.GithubAppInstallationRepository
- type GithubAppInstallationRepository struct {
- db *gorm.DB
- }
- // NewGithubAppInstallationRepository creates a new GithubAppInstallationRepository
- func NewGithubAppInstallationRepository(db *gorm.DB) repository.GithubAppInstallationRepository {
- return &GithubAppInstallationRepository{db}
- }
- // CreateGithubAppInstallation creates a new GithubAppInstallation instance
- func (repo *GithubAppInstallationRepository) CreateGithubAppInstallation(am *ints.GithubAppInstallation) (*ints.GithubAppInstallation, error) {
- if err := repo.db.Create(am).Error; err != nil {
- return nil, err
- }
- return am, nil
- }
- // ReadGithubAppInstallationByInstallationID finds a GithubAppInstallation by id
- func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByInstallationID(gaID uint) (*ints.GithubAppInstallation, error) {
- ret := &ints.GithubAppInstallation{}
- if err := repo.db.Where("installation_id = ?", gaID).First(&ret).Error; err != nil {
- return nil, err
- }
- return ret, nil
- }
- // ReadGithubAppInstallationByAccountID finds a GithubAppInstallation by an account ID
- func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountID(accountID int64) (*ints.GithubAppInstallation, error) {
- ret := &ints.GithubAppInstallation{}
- if err := repo.db.Where("account_id = ?", accountID).First(&ret).Error; err != nil {
- return nil, err
- }
- return ret, nil
- }
- // ReadGithubAppInstallationByAccountIDs finds all instances of GithubInstallations given a list of account IDs
- // note that if there is not Installation for a given ID, no error will be generated
- func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountIDs(accountIDs []int64) ([]*ints.GithubAppInstallation, error) {
- ret := make([]*ints.GithubAppInstallation, 0)
- if err := repo.db.Where("account_id IN ?", accountIDs).Find(&ret).Error; err != nil {
- return nil, err
- }
- return ret, nil
- }
- // DeleteGithubAppInstallationByAccountID deletes a GithubAppInstallation given an account ID
- // note that this deletion is done with db.Unscoped(), so the record is actually deleted
- func (repo *GithubAppInstallationRepository) DeleteGithubAppInstallationByAccountID(accountID int64) error {
- if err := repo.db.Unscoped().Where("account_id = ?", accountID).Delete(&ints.GithubAppInstallation{}).Error; err != nil {
- return err
- }
- return nil
- }
- // GithubAppOAuthIntegrationRepository implements repository.GithubAppOAuthIntegrationRepository
- type GithubAppOAuthIntegrationRepository struct {
- db *gorm.DB
- }
- // NewGithubAppOAuthIntegrationRepository creates a GithubAppOAuthIntegrationRepository
- func NewGithubAppOAuthIntegrationRepository(db *gorm.DB) repository.GithubAppOAuthIntegrationRepository {
- return &GithubAppOAuthIntegrationRepository{db}
- }
- // CreateGithubAppOAuthIntegration creates a new GithubAppOAuthIntegration
- func (repo *GithubAppOAuthIntegrationRepository) CreateGithubAppOAuthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
- if err := repo.db.Create(am).Error; err != nil {
- return nil, err
- }
- return am, nil
- }
- // ReadGithubAppOauthIntegration finds a GithubAppOauthIntegration by id
- func (repo *GithubAppOAuthIntegrationRepository) ReadGithubAppOauthIntegration(id uint) (*ints.GithubAppOAuthIntegration, error) {
- ret := &ints.GithubAppOAuthIntegration{}
- if err := repo.db.Where("id = ?", id).First(&ret).Error; err != nil {
- return nil, err
- }
- return ret, nil
- }
- // UpdateGithubAppOauthIntegration updates a GithubAppOauthIntegration
- func (repo *GithubAppOAuthIntegrationRepository) UpdateGithubAppOauthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
- err := repo.db.Save(am).Error
- if err != nil {
- return nil, err
- }
- return am, nil
- }
- // AzureIntegrationRepository uses gorm.DB for querying the database
- type AzureIntegrationRepository struct {
- db *gorm.DB
- key *[32]byte
- storageBackend credentials.CredentialStorage
- }
- // NewAzureIntegrationRepository returns a AzureIntegrationRepository which uses
- // gorm.DB for querying the database. It accepts an encryption key to encrypt
- // sensitive data
- func NewAzureIntegrationRepository(
- db *gorm.DB,
- key *[32]byte,
- storageBackend credentials.CredentialStorage,
- ) repository.AzureIntegrationRepository {
- return &AzureIntegrationRepository{db, key, storageBackend}
- }
- // CreateAzureIntegration creates a new Azure auth mechanism
- func (repo *AzureIntegrationRepository) CreateAzureIntegration(
- az *ints.AzureIntegration,
- ) (*ints.AzureIntegration, error) {
- err := repo.EncryptAzureIntegrationData(az, repo.key)
- if err != nil {
- 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
- credentialData := &credentials.AzureCredential{}
- if repo.storageBackend != nil {
- credentialData.ServicePrincipalSecret = az.ServicePrincipalSecret
- credentialData.ACRPassword1 = az.ACRPassword1
- credentialData.ACRPassword2 = az.ACRPassword2
- credentialData.AKSPassword = az.AKSPassword
- az.ServicePrincipalSecret = []byte{}
- az.ACRPassword1 = []byte{}
- az.ACRPassword2 = []byte{}
- az.AKSPassword = []byte{}
- }
- project := &models.Project{}
- if err := repo.db.Where("id = ?", az.ProjectID).First(&project).Error; err != nil {
- return nil, err
- }
- assoc := repo.db.Model(&project).Association("AzureIntegrations")
- if assoc.Error != nil {
- return nil, assoc.Error
- }
- if err := assoc.Append(az); err != nil {
- return nil, err
- }
- if repo.storageBackend != nil {
- err = repo.storageBackend.WriteAzureCredential(az, credentialData)
- if err != nil {
- return nil, err
- }
- }
- return az, nil
- }
- // OverwriteAzureIntegration overwrites the Azure credential in the DB
- func (repo *AzureIntegrationRepository) OverwriteAzureIntegration(
- az *ints.AzureIntegration,
- ) (*ints.AzureIntegration, error) {
- err := repo.EncryptAzureIntegrationData(az, repo.key)
- if err != nil {
- 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
- credentialData := &credentials.AzureCredential{}
- if repo.storageBackend != nil {
- credentialData.ServicePrincipalSecret = az.ServicePrincipalSecret
- credentialData.ACRPassword1 = az.ACRPassword1
- credentialData.ACRPassword2 = az.ACRPassword2
- credentialData.AKSPassword = az.AKSPassword
- az.ServicePrincipalSecret = []byte{}
- az.ACRPassword1 = []byte{}
- az.ACRPassword2 = []byte{}
- az.AKSPassword = []byte{}
- }
- if err := repo.db.Save(az).Error; err != nil {
- return nil, err
- }
- if repo.storageBackend != nil {
- err = repo.storageBackend.WriteAzureCredential(az, credentialData)
- if err != nil {
- return nil, err
- }
- }
- // perform another read
- return repo.ReadAzureIntegration(az.ProjectID, az.ID)
- }
- // ReadAzureIntegration finds a Azure auth mechanism by id
- func (repo *AzureIntegrationRepository) ReadAzureIntegration(
- projectID, id uint,
- ) (*ints.AzureIntegration, error) {
- az := &ints.AzureIntegration{}
- if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&az).Error; err != nil {
- return nil, err
- }
- if repo.storageBackend != nil {
- credentialData, err := repo.storageBackend.GetAzureCredential(az)
- if err != nil {
- return nil, err
- }
- az.ServicePrincipalSecret = credentialData.ServicePrincipalSecret
- az.ACRPassword1 = credentialData.ACRPassword1
- az.ACRPassword2 = credentialData.ACRPassword2
- az.AKSPassword = credentialData.AKSPassword
- }
- err := repo.DecryptAzureIntegrationData(az, repo.key)
- if err != nil {
- return nil, err
- }
- return az, nil
- }
- // ListAzureIntegrationsByProjectID finds all Azure auth mechanisms
- // for a given project id
- func (repo *AzureIntegrationRepository) ListAzureIntegrationsByProjectID(
- projectID uint,
- ) ([]*ints.AzureIntegration, error) {
- azs := []*ints.AzureIntegration{}
- if err := repo.db.Where("project_id = ?", projectID).Find(&azs).Error; err != nil {
- return nil, err
- }
- return azs, nil
- }
- // EncryptAWSIntegrationData will encrypt the aws integration data before
- // writing to the DB
- func (repo *AzureIntegrationRepository) EncryptAzureIntegrationData(
- az *ints.AzureIntegration,
- key *[32]byte,
- ) error {
- if len(az.ServicePrincipalSecret) > 0 {
- cipherData, err := encryption.Encrypt(az.ServicePrincipalSecret, key)
- if err != nil {
- return err
- }
- az.ServicePrincipalSecret = cipherData
- }
- if len(az.ACRPassword1) > 0 {
- cipherData, err := encryption.Encrypt(az.ACRPassword1, key)
- if err != nil {
- return err
- }
- az.ACRPassword1 = cipherData
- }
- if len(az.ACRPassword2) > 0 {
- cipherData, err := encryption.Encrypt(az.ACRPassword2, key)
- if err != nil {
- return err
- }
- az.ACRPassword2 = cipherData
- }
- if len(az.AKSPassword) > 0 {
- cipherData, err := encryption.Encrypt(az.AKSPassword, key)
- if err != nil {
- return err
- }
- az.AKSPassword = cipherData
- }
- return nil
- }
- // DecryptAzureIntegrationData will decrypt the Azure integration data before
- // returning it from the DB
- func (repo *AzureIntegrationRepository) DecryptAzureIntegrationData(
- az *ints.AzureIntegration,
- key *[32]byte,
- ) error {
- if len(az.ServicePrincipalSecret) > 0 {
- plaintext, err := encryption.Decrypt(az.ServicePrincipalSecret, key)
- if err != nil {
- return err
- }
- az.ServicePrincipalSecret = plaintext
- }
- if len(az.ACRPassword1) > 0 {
- plaintext, err := encryption.Decrypt(az.ACRPassword1, key)
- if err != nil {
- return err
- }
- az.ACRPassword1 = plaintext
- }
- if len(az.ACRPassword2) > 0 {
- plaintext, err := encryption.Decrypt(az.ACRPassword2, key)
- if err != nil {
- return err
- }
- az.ACRPassword2 = plaintext
- }
- if len(az.AKSPassword) > 0 {
- plaintext, err := encryption.Decrypt(az.AKSPassword, key)
- if err != nil {
- return err
- }
- az.AKSPassword = plaintext
- }
- return nil
- }
- // GitlabIntegrationRepository uses gorm.DB for querying the database
- type GitlabIntegrationRepository struct {
- db *gorm.DB
- key *[32]byte
- storageBackend credentials.CredentialStorage
- }
- // NewGitlabIntegrationRepository returns a GitlabIntegrationRepository which uses
- // gorm.DB for querying the database
- func NewGitlabIntegrationRepository(
- db *gorm.DB,
- key *[32]byte,
- storageBackend credentials.CredentialStorage,
- ) repository.GitlabIntegrationRepository {
- return &GitlabIntegrationRepository{db, key, storageBackend}
- }
- // CreateIntegration adds a new GitlabIntegration row to the gitlab_integration table in the database
- func (repo *GitlabIntegrationRepository) CreateGitlabIntegration(gi *ints.GitlabIntegration) (*ints.GitlabIntegration, error) {
- err := repo.EncryptGitlabIntegrationData(gi, repo.key)
- if err != nil {
- 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
- credentialData := &credentials.GitlabCredential{}
- if repo.storageBackend != nil {
- credentialData.AppClientID = gi.AppClientID
- credentialData.AppClientSecret = gi.AppClientSecret
- gi.AppClientID = []byte{}
- gi.AppClientSecret = []byte{}
- }
- project := &models.Project{}
- if err := repo.db.Where("id = ?", gi.ProjectID).First(&project).Error; err != nil {
- return nil, err
- }
- assoc := repo.db.Model(&project).Association("GitlabIntegrations")
- if assoc.Error != nil {
- return nil, assoc.Error
- }
- if err := assoc.Append(gi); err != nil {
- return nil, err
- }
- if repo.storageBackend != nil {
- err = repo.storageBackend.WriteGitlabCredential(gi, credentialData)
- if err != nil {
- return nil, err
- }
- }
- return gi, nil
- }
- func (repo *GitlabIntegrationRepository) ReadGitlabIntegration(projectID, id uint) (*ints.GitlabIntegration, error) {
- gi := &ints.GitlabIntegration{}
- if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&gi).Error; err != nil {
- return nil, err
- }
- if repo.storageBackend != nil {
- credentialData, err := repo.storageBackend.GetGitlabCredential(gi)
- if err != nil {
- return nil, err
- }
- gi.AppClientID = credentialData.AppClientID
- gi.AppClientSecret = credentialData.AppClientSecret
- }
- err := repo.DecryptGitlabIntegrationData(gi, repo.key)
- if err != nil {
- return nil, err
- }
- return gi, nil
- }
- func (repo *GitlabIntegrationRepository) ListGitlabIntegrationsByProjectID(projectID uint) ([]*ints.GitlabIntegration, error) {
- gi := []*ints.GitlabIntegration{}
- if err := repo.db.Where("project_id = ? AND deleted_at IS NULL", projectID).Find(&gi).Error; err != nil {
- return nil, err
- }
- return gi, nil
- }
- // EncryptGitlabIntegrationData will encrypt the gitlab integration data before
- // writing to the DB
- func (repo *GitlabIntegrationRepository) EncryptGitlabIntegrationData(
- gi *ints.GitlabIntegration,
- key *[32]byte,
- ) error {
- if len(gi.AppClientID) > 0 {
- cipherData, err := encryption.Encrypt(gi.AppClientID, key)
- if err != nil {
- return err
- }
- gi.AppClientID = cipherData
- }
- if len(gi.AppClientSecret) > 0 {
- cipherData, err := encryption.Encrypt(gi.AppClientSecret, key)
- if err != nil {
- return err
- }
- gi.AppClientSecret = cipherData
- }
- return nil
- }
- // DecryptGitlabIntegrationData will decrypt the gitlab integration data before
- // returning it from the DB
- func (repo *GitlabIntegrationRepository) DecryptGitlabIntegrationData(
- gi *ints.GitlabIntegration,
- key *[32]byte,
- ) error {
- if len(gi.AppClientID) > 0 {
- plaintext, err := encryption.Decrypt(gi.AppClientID, key)
- if err != nil {
- return err
- }
- gi.AppClientID = plaintext
- }
- if len(gi.AppClientSecret) > 0 {
- plaintext, err := encryption.Decrypt(gi.AppClientSecret, key)
- if err != nil {
- return err
- }
- gi.AppClientSecret = plaintext
- }
- return nil
- }
- // GitlabAppOAuthIntegrationRepository uses gorm.DB for querying the database
- type GitlabAppOAuthIntegrationRepository struct {
- db *gorm.DB
- key *[32]byte
- storageBackend credentials.CredentialStorage
- }
- // NewGitlabAppOAuthIntegrationRepository returns a GitlabAppOAuthIntegrationRepository which uses
- // gorm.DB for querying the database
- func NewGitlabAppOAuthIntegrationRepository(
- db *gorm.DB,
- key *[32]byte,
- storageBackend credentials.CredentialStorage,
- ) repository.GitlabAppOAuthIntegrationRepository {
- return &GitlabAppOAuthIntegrationRepository{db, key, storageBackend}
- }
- func (repo *GitlabAppOAuthIntegrationRepository) CreateGitlabAppOAuthIntegration(
- gi *ints.GitlabAppOAuthIntegration,
- ) (*ints.GitlabAppOAuthIntegration, error) {
- if err := repo.db.Create(gi).Error; err != nil {
- return nil, err
- }
- return gi, nil
- }
- func (repo *GitlabAppOAuthIntegrationRepository) ReadGitlabAppOAuthIntegration(
- userID, projectID, integrationID uint,
- ) (*ints.GitlabAppOAuthIntegration, error) {
- gi := &ints.GitlabAppOAuthIntegration{}
- if err := repo.db.
- Order("gitlab_app_o_auth_integrations.id desc").
- Joins("INNER JOIN gitlab_integrations ON gitlab_integrations.id = gitlab_app_o_auth_integrations.gitlab_integration_id").
- Joins("INNER JOIN o_auth_integrations ON o_auth_integrations.id = gitlab_app_o_auth_integrations.o_auth_integration_id").
- Where("o_auth_integrations.user_id = ? AND o_auth_integrations.project_id = ? AND"+
- " gitlab_integrations.id = ? AND gitlab_integrations.deleted_at IS NULL AND"+
- " gitlab_app_o_auth_integrations.deleted_at IS NULL AND o_auth_integrations.deleted_at IS NULL",
- userID, projectID, integrationID).First(&gi).Error; err != nil {
- return nil, err
- }
- return gi, nil
- }
|