Skip to content
18 changes: 18 additions & 0 deletions api/restHandler/DockerRegRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ func (impl DockerRegRestHandlerImpl) SaveDockerRegistryConfig(w http.ResponseWri
return
}
//RBAC enforcer Ends
exist, err := impl.dockerRegistryConfig.CheckInActiveDockerAccount(bean.Id)

if err != nil {
impl.logger.Errorw("service err, SaveDockerRegistryConfig", "err", err, "payload", bean)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}

if exist {
res, err := impl.dockerRegistryConfig.UpdateInactive(&bean)
if err != nil {
impl.logger.Errorw("service err, UpdateDockerRegistryConfig", "err", err, "payload", bean)
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
common.WriteJsonResp(w, err, res, http.StatusOK)
return
}

res, err := impl.dockerRegistryConfig.Create(&bean)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ type DockerArtifactStoreRepository interface {
FindAllActiveForAutocomplete() ([]DockerArtifactStore, error)
FindAll() ([]DockerArtifactStore, error)
FindOne(storeId string) (*DockerArtifactStore, error)
FindOneInactive(storeId string) (*DockerArtifactStore, error)
Update(artifactStore *DockerArtifactStore, tx *pg.Tx) error
Delete(storeId string) error
MarkRegistryDeleted(artifactStore *DockerArtifactStore) error
FindInactive(storeId string) (bool, error)
}
type DockerArtifactStoreRepositoryImpl struct {
dbConnection *pg.DB
Expand Down Expand Up @@ -137,6 +139,16 @@ func (impl DockerArtifactStoreRepositoryImpl) FindOne(storeId string) (*DockerAr
return &provider, err
}

func (impl DockerArtifactStoreRepositoryImpl) FindOneInactive(storeId string) (*DockerArtifactStore, error) {
var provider DockerArtifactStore
err := impl.dbConnection.Model(&provider).
Column("docker_artifact_store.*", "IpsConfig").
Where("docker_artifact_store.id = ?", storeId).
Where("active = ?", false).
Select()
return &provider, err
}

func (impl DockerArtifactStoreRepositoryImpl) Update(artifactStore *DockerArtifactStore, tx *pg.Tx) error {
//TODO check for unique default
//there can be only one default
Expand Down Expand Up @@ -170,3 +182,12 @@ func (impl DockerArtifactStoreRepositoryImpl) MarkRegistryDeleted(deleteReq *Doc
deleteReq.Active = false
return impl.dbConnection.Update(deleteReq)
}

func (impl DockerArtifactStoreRepositoryImpl) FindInactive(storeId string) (bool, error) {
var provider DockerArtifactStore
exist, err := impl.dbConnection.Model(&provider).
Where("docker_artifact_store.id = ?", storeId).
Where("active = ?", false).
Exists()
return exist, err
}
164 changes: 126 additions & 38 deletions pkg/pipeline/DockerRegistryConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ type DockerRegistryConfig interface {
FetchAllDockerAccounts() ([]DockerArtifactStoreBean, error)
FetchOneDockerAccount(storeId string) (*DockerArtifactStoreBean, error)
Update(bean *DockerArtifactStoreBean) (*DockerArtifactStoreBean, error)
UpdateInactive(bean *DockerArtifactStoreBean) (*DockerArtifactStoreBean, error)
Delete(storeId string) (string, error)
DeleteReg(bean *DockerArtifactStoreBean) error
CheckInActiveDockerAccount(storeId string) (bool, error)
}

type DockerArtifactStoreBean struct {
Expand Down Expand Up @@ -79,6 +81,25 @@ func NewDockerRegistryConfigImpl(logger *zap.SugaredLogger, dockerArtifactStoreR
}
}

func NewDockerArtifactStore(bean *DockerArtifactStoreBean, isActive bool, createdOn time.Time, updatedOn time.Time, createdBy int32, updateBy int32) *repository.DockerArtifactStore {
return &repository.DockerArtifactStore{
Id: bean.Id,
PluginId: bean.PluginId,
RegistryURL: bean.RegistryURL,
RegistryType: bean.RegistryType,
AWSAccessKeyId: bean.AWSAccessKeyId,
AWSSecretAccessKey: bean.AWSSecretAccessKey,
AWSRegion: bean.AWSRegion,
Username: bean.Username,
Password: bean.Password,
IsDefault: bean.IsDefault,
Connection: bean.Connection,
Cert: bean.Cert,
Active: isActive,
AuditLog: sql.AuditLog{CreatedBy: createdBy, CreatedOn: createdOn, UpdatedOn: updatedOn, UpdatedBy: updateBy},
}
}

func (impl DockerRegistryConfigImpl) Create(bean *DockerArtifactStoreBean) (*DockerArtifactStoreBean, error) {
impl.logger.Debugw("docker registry create request", "request", bean)

Expand All @@ -93,29 +114,14 @@ func (impl DockerRegistryConfigImpl) Create(bean *DockerArtifactStoreBean) (*Doc
defer tx.Rollback()

// 2- insert docker_registry_config
store := &repository.DockerArtifactStore{
Id: bean.Id,
PluginId: bean.PluginId,
RegistryURL: bean.RegistryURL,
RegistryType: bean.RegistryType,
AWSAccessKeyId: bean.AWSAccessKeyId,
AWSSecretAccessKey: bean.AWSSecretAccessKey,
AWSRegion: bean.AWSRegion,
Username: bean.Username,
Password: bean.Password,
IsDefault: bean.IsDefault,
Connection: bean.Connection,
Cert: bean.Cert,
Active: true,
AuditLog: sql.AuditLog{CreatedBy: bean.User, CreatedOn: time.Now(), UpdatedOn: time.Now(), UpdatedBy: bean.User},
}
store := NewDockerArtifactStore(bean, true, time.Now(), time.Now(), bean.User, bean.User)
err = impl.dockerArtifactStoreRepository.Save(store, tx)
if err != nil {
impl.logger.Errorw("error in saving registry config", "config", store, "err", err)
err = &util.ApiError{
Code: constants.DockerRegCreateFailedInDb,
InternalMessage: "docker registry failed to create in db",
UserMessage: fmt.Sprintf("requested by %d", bean.User),
UserMessage: fmt.Sprintf("Container registry [%s] already exists.", bean.Id),
}
return nil, err
}
Expand All @@ -137,7 +143,7 @@ func (impl DockerRegistryConfigImpl) Create(bean *DockerArtifactStoreBean) (*Doc
err = &util.ApiError{
Code: constants.DockerRegCreateFailedInDb,
InternalMessage: "docker registry ips config to create in db",
UserMessage: fmt.Sprintf("requested by %d", bean.User),
UserMessage: fmt.Sprintf("Container registry [%s] already exists.", bean.Id),
}
return nil, err
}
Expand All @@ -154,7 +160,7 @@ func (impl DockerRegistryConfigImpl) Create(bean *DockerArtifactStoreBean) (*Doc
return bean, nil
}

//list all active artifact store
// list all active artifact store
func (impl DockerRegistryConfigImpl) ListAllActive() ([]DockerArtifactStoreBean, error) {
impl.logger.Debug("list docker repo request")
stores, err := impl.dockerArtifactStoreRepository.FindAllActiveForAutocomplete()
Expand All @@ -175,7 +181,8 @@ func (impl DockerRegistryConfigImpl) ListAllActive() ([]DockerArtifactStoreBean,
return storeBeans, err
}

/**
/*
*
this method used for getting all the docker account details
*/
func (impl DockerRegistryConfigImpl) FetchAllDockerAccounts() ([]DockerArtifactStoreBean, error) {
Expand All @@ -194,10 +201,10 @@ func (impl DockerRegistryConfigImpl) FetchAllDockerAccounts() ([]DockerArtifactS
RegistryURL: store.RegistryURL,
RegistryType: store.RegistryType,
AWSAccessKeyId: store.AWSAccessKeyId,
AWSSecretAccessKey: store.AWSSecretAccessKey,
AWSSecretAccessKey: "",
AWSRegion: store.AWSRegion,
Username: store.Username,
Password: store.Password,
Password: "",
IsDefault: store.IsDefault,
Connection: store.Connection,
Cert: store.Cert,
Expand All @@ -216,7 +223,8 @@ func (impl DockerRegistryConfigImpl) FetchAllDockerAccounts() ([]DockerArtifactS
return storeBeans, err
}

/**
/*
*
this method used for getting all the docker account details
*/
func (impl DockerRegistryConfigImpl) FetchOneDockerAccount(storeId string) (*DockerArtifactStoreBean, error) {
Expand Down Expand Up @@ -275,22 +283,22 @@ func (impl DockerRegistryConfigImpl) Update(bean *DockerArtifactStoreBean) (*Doc
defer tx.Rollback()

// 3- update docker_registry_config
store := &repository.DockerArtifactStore{
Id: bean.Id,
PluginId: existingStore.PluginId,
RegistryURL: bean.RegistryURL,
RegistryType: bean.RegistryType,
AWSAccessKeyId: bean.AWSAccessKeyId,
AWSSecretAccessKey: bean.AWSSecretAccessKey,
AWSRegion: bean.AWSRegion,
Username: bean.Username,
Password: bean.Password,
IsDefault: bean.IsDefault,
Connection: bean.Connection,
Cert: bean.Cert,
Active: true, // later it will change
AuditLog: sql.AuditLog{CreatedBy: existingStore.CreatedBy, CreatedOn: existingStore.CreatedOn, UpdatedOn: time.Now(), UpdatedBy: bean.User},
if bean.Password == "" {
bean.Password = existingStore.Password
}

if bean.AWSSecretAccessKey == "" {
bean.AWSSecretAccessKey = existingStore.AWSSecretAccessKey
}

if bean.Cert == "" {
bean.Cert = existingStore.Cert
}

bean.PluginId = existingStore.PluginId

store := NewDockerArtifactStore(bean, true, existingStore.CreatedOn, time.Now(), existingStore.CreatedBy, bean.User)

err = impl.dockerArtifactStoreRepository.Update(store, tx)
if err != nil {
impl.logger.Errorw("error in updating registry config in db", "config", store, "err", err)
Expand Down Expand Up @@ -336,6 +344,77 @@ func (impl DockerRegistryConfigImpl) Update(bean *DockerArtifactStoreBean) (*Doc
return bean, nil
}

func (impl DockerRegistryConfigImpl) UpdateInactive(bean *DockerArtifactStoreBean) (*DockerArtifactStoreBean, error) {
impl.logger.Debugw("docker registry update request", "request", bean)

// 1- find by id, if err - return error
existingStore, err0 := impl.dockerArtifactStoreRepository.FindOneInactive(bean.Id)
if err0 != nil {
impl.logger.Errorw("no matching entry found of update ..", "err", err0)
return nil, err0
}

// 2- initiate DB transaction
dbConnection := impl.dockerArtifactStoreRepository.GetConnection()
tx, err := dbConnection.Begin()
if err != nil {
impl.logger.Errorw("error in initiating db tx", "err", err)
return nil, err
}
// Rollback tx on error.
defer tx.Rollback()

// 3- update docker_registry_config

bean.PluginId = existingStore.PluginId

store := NewDockerArtifactStore(bean, true, existingStore.CreatedOn, time.Now(), bean.User, bean.User)

err = impl.dockerArtifactStoreRepository.Update(store, tx)
if err != nil {
impl.logger.Errorw("error in updating registry config in db", "config", store, "err", err)
err = &util.ApiError{
Code: constants.DockerRegUpdateFailedInDb,
InternalMessage: "docker registry failed to update in db",
UserMessage: "docker registry failed to update in db",
}
return nil, err
}
impl.logger.Infow("updated repository ", "repository", store)
bean.Id = store.Id

// 4- update imagePullSecretConfig for this docker registry
dockerRegistryIpsConfig := bean.DockerRegistryIpsConfig
ipsConfig := &repository.DockerRegistryIpsConfig{
Id: existingStore.IpsConfig.Id,
DockerArtifactStoreId: store.Id,
CredentialType: dockerRegistryIpsConfig.CredentialType,
CredentialValue: dockerRegistryIpsConfig.CredentialValue,
AppliedClusterIdsCsv: dockerRegistryIpsConfig.AppliedClusterIdsCsv,
IgnoredClusterIdsCsv: dockerRegistryIpsConfig.IgnoredClusterIdsCsv,
}
err = impl.dockerRegistryIpsConfigRepository.Update(ipsConfig, tx)
if err != nil {
impl.logger.Errorw("error in updating registry config ips", "ipsConfig", ipsConfig, "err", err)
err = &util.ApiError{
Code: constants.DockerRegUpdateFailedInDb,
InternalMessage: "docker registry ips config failed to update in db",
UserMessage: "docker registry ips config failed to update in db",
}
return nil, err
}
impl.logger.Infow("updated ips config for this docker repository ", "ipsConfig", ipsConfig)

// 5- now commit transaction
err = tx.Commit()
if err != nil {
impl.logger.Errorw("error in committing transaction", "err", err)
return nil, err
}

return bean, nil
}

func (impl DockerRegistryConfigImpl) Delete(storeId string) (string, error) {
impl.logger.Debugw("docker registry update request", "request", storeId)

Expand Down Expand Up @@ -370,3 +449,12 @@ func (impl DockerRegistryConfigImpl) DeleteReg(bean *DockerArtifactStoreBean) er
}
return nil
}

func (impl DockerRegistryConfigImpl) CheckInActiveDockerAccount(storeId string) (bool, error) {
exist, err := impl.dockerArtifactStoreRepository.FindInactive(storeId)
if err != nil {
impl.logger.Errorw("err in deleting docker registry", "id", storeId, "err", err)
return false, err
}
return exist, nil
}
12 changes: 6 additions & 6 deletions pkg/pipeline/GitRegistryConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (impl GitRegistryConfigImpl) Create(request *GitRegistry) (*GitRegistry, er
return request, nil
}

//get all active git providers
// get all active git providers
func (impl GitRegistryConfigImpl) GetAll() ([]GitRegistry, error) {
impl.logger.Debug("get all provider request")
providers, err := impl.gitProviderRepo.FindAllActiveForAutocomplete()
Expand Down Expand Up @@ -264,19 +264,19 @@ func (impl GitRegistryConfigImpl) Update(request *GitRegistry) (*GitRegistry, er
return request, nil
}

func (impl GitRegistryConfigImpl) Delete(request *GitRegistry) error{
func (impl GitRegistryConfigImpl) Delete(request *GitRegistry) error {
providerId := strconv.Itoa(request.Id)
gitProviderConfig, err := impl.gitProviderRepo.FindOne(providerId)
if err != nil{
impl.logger.Errorw("No matching entry found for delete.", "id", request.Id, "err",err)
if err != nil {
impl.logger.Errorw("No matching entry found for delete.", "id", request.Id, "err", err)
return err
}
deleteReq := gitProviderConfig
deleteReq.UpdatedOn = time.Now()
deleteReq.UpdatedBy = request.UserId
err = impl.gitProviderRepo.MarkProviderDeleted(&deleteReq)
if err != nil{
impl.logger.Errorw("err in deleting git account", "id", request.Id,"err",err)
if err != nil {
impl.logger.Errorw("err in deleting git account", "id", request.Id, "err", err)
return err
}
deleteReq.Active = false
Expand Down