@@ -19,9 +19,12 @@ package restHandler
1919
2020import (
2121 "encoding/json"
22+ "fmt"
2223 "github.com/devtron-labs/devtron/api/restHandler/common"
24+ repository "github.com/devtron-labs/devtron/internal/sql/repository/dockerRegistry"
2325 delete2 "github.com/devtron-labs/devtron/pkg/delete"
2426 "github.com/devtron-labs/devtron/pkg/user/casbin"
27+ "k8s.io/utils/strings/slices"
2528 "net/http"
2629 "strings"
2730
@@ -79,6 +82,27 @@ func NewDockerRegRestHandlerImpl(dockerRegistryConfig pipeline.DockerRegistryCon
7982 }
8083}
8184
85+ func ValidateDockerArtifactStoreRequestBean (bean pipeline.DockerArtifactStoreBean ) bool {
86+ // validating secure connection configs
87+ if (bean .Connection == secureWithCert && bean .Cert == "" ) ||
88+ (bean .Connection != secureWithCert && bean .Cert != "" ) {
89+ return false
90+ }
91+ // validating OCI Registry configs
92+ if bean .IsOCICompliantRegistry {
93+ if bean .OCIRegistryConfig == nil {
94+ return false
95+ }
96+ containerStorageActionType , containerStorageActionExists := bean .OCIRegistryConfig [repository .OCI_REGISRTY_REPO_TYPE_CONTAINER ]
97+ if containerStorageActionExists && containerStorageActionType != repository .STORAGE_ACTION_TYPE_PULL_AND_PUSH {
98+ return false
99+ }
100+ } else if bean .OCIRegistryConfig != nil {
101+ return false
102+ }
103+ return true
104+ }
105+
82106func (impl DockerRegRestHandlerImpl ) SaveDockerRegistryConfig (w http.ResponseWriter , r * http.Request ) {
83107 decoder := json .NewDecoder (r .Body )
84108 userId , err := impl .userAuthService .GetLoggedInUser (r )
@@ -94,7 +118,9 @@ func (impl DockerRegRestHandlerImpl) SaveDockerRegistryConfig(w http.ResponseWri
94118 return
95119 }
96120 bean .User = userId
97- if (bean .Connection == secureWithCert && bean .Cert == "" ) || (bean .Connection != secureWithCert && bean .Cert != "" ) {
121+ if ! ValidateDockerArtifactStoreRequestBean (bean ) {
122+ err = fmt .Errorf ("invalid payload, missing or incorrect values for required fields" )
123+ impl .logger .Errorw ("validation err, SaveDockerRegistryConfig" , "err" , err , "payload" , bean )
98124 common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
99125 return
100126 } else {
@@ -222,7 +248,9 @@ func (impl DockerRegRestHandlerImpl) UpdateDockerRegistryConfig(w http.ResponseW
222248 return
223249 }
224250 bean .User = userId
225- if (bean .Connection == secureWithCert && bean .Cert == "" ) || (bean .Connection != secureWithCert && bean .Cert != "" ) {
251+ if ! ValidateDockerArtifactStoreRequestBean (bean ) {
252+ err = fmt .Errorf ("invalid payload, missing or incorrect values for required fields" )
253+ impl .logger .Errorw ("validation err, SaveDockerRegistryConfig" , "err" , err , "payload" , bean )
226254 common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
227255 return
228256 } else {
@@ -267,15 +295,36 @@ func (impl DockerRegRestHandlerImpl) FetchAllDockerRegistryForAutocomplete(w htt
267295}
268296
269297func (impl DockerRegRestHandlerImpl ) IsDockerRegConfigured (w http.ResponseWriter , r * http.Request ) {
298+ v := r .URL .Query ()
299+ storageType := v .Get ("storageType" )
300+ if storageType == "" {
301+ storageType = repository .OCI_REGISRTY_REPO_TYPE_CONTAINER
302+ }
303+ if ! slices .Contains (repository .OCI_REGISRTY_REPO_TYPE_LIST , storageType ) {
304+ common .WriteJsonResp (w , fmt .Errorf ("invalid query parameters" ), nil , http .StatusBadRequest )
305+ return
306+ }
307+ storageAction := v .Get ("storageAction" )
308+ if storageAction == "" {
309+ storageAction = repository .STORAGE_ACTION_TYPE_PUSH
310+ }
311+ if ! (storageAction == repository .STORAGE_ACTION_TYPE_PULL || storageAction == repository .STORAGE_ACTION_TYPE_PUSH ) {
312+ common .WriteJsonResp (w , fmt .Errorf ("invalid query parameters" ), nil , http .StatusBadRequest )
313+ return
314+ }
270315 isConfigured := false
271- res , err := impl .dockerRegistryConfig .ListAllActive ()
316+ registryConfigs , err := impl .dockerRegistryConfig .ListAllActive ()
272317 if err != nil && err != pg .ErrNoRows {
273318 impl .logger .Errorw ("service err, IsDockerRegConfigured" , "err" , err )
274319 common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
275320 return
276321 }
277- if len (res ) > 0 {
278- isConfigured = true
322+ if len (registryConfigs ) > 0 {
323+ // Filter out all registries with CONTAINER push or pull/push access
324+ res := impl .dockerRegistryConfig .FilterRegistryBeanListBasedOnStorageTypeAndAction (registryConfigs , storageType , storageAction , repository .STORAGE_ACTION_TYPE_PULL_AND_PUSH )
325+ if len (res ) > 0 {
326+ isConfigured = true
327+ }
279328 }
280329
281330 common .WriteJsonResp (w , err , isConfigured , http .StatusOK )
0 commit comments