-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathDockerRegistryConfig.go
923 lines (845 loc) · 34.5 KB
/
DockerRegistryConfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
/*
* Copyright (c) 2020-2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pipeline
import (
"context"
"fmt"
bean2 "github.com/devtron-labs/devtron/api/helm-app/gRPC"
client "github.com/devtron-labs/devtron/api/helm-app/service"
"github.com/devtron-labs/devtron/pkg/argoRepositoryCreds"
"github.com/devtron-labs/devtron/pkg/pipeline/types"
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/go-pg/pg"
"k8s.io/utils/strings/slices"
"net/http"
"strings"
"time"
"github.com/devtron-labs/devtron/internal/constants"
"github.com/devtron-labs/devtron/internal/sql/repository/dockerRegistry"
"github.com/devtron-labs/devtron/internal/util"
"go.uber.org/zap"
)
type DockerRegistryConfig interface {
Create(bean *types.DockerArtifactStoreBean) (*types.DockerArtifactStoreBean, error)
ListAllActive() ([]types.DockerArtifactStoreBean, error)
FetchAllDockerAccounts() ([]types.DockerArtifactStoreBean, error)
FetchOneDockerAccount(storeId string) (*types.DockerArtifactStoreBean, error)
Update(bean *types.DockerArtifactStoreBean) (*types.DockerArtifactStoreBean, error)
UpdateInactive(bean *types.DockerArtifactStoreBean) (*types.DockerArtifactStoreBean, error)
Delete(storeId string) (string, error)
DeleteReg(bean *types.DockerArtifactStoreBean) error
CheckInActiveDockerAccount(storeId string) (bool, error)
ValidateRegistryCredentials(bean *types.DockerArtifactStoreBean) error
ConfigureOCIRegistry(bean *types.DockerArtifactStoreBean, isUpdate bool, userId int32, tx *pg.Tx) error
CreateOrUpdateOCIRegistryConfig(ociRegistryConfig *repository.OCIRegistryConfig, userId int32, tx *pg.Tx) error
FilterOCIRegistryConfigForSpecificRepoType(ociRegistryConfigList []*repository.OCIRegistryConfig, repositoryType string) *repository.OCIRegistryConfig
FilterRegistryBeanListBasedOnStorageTypeAndAction(bean []types.DockerArtifactStoreBean, storageType string, actionTypes ...string) []types.DockerArtifactStoreBean
ValidateRegistryStorageType(registryId string, storageType string, storageActions ...string) bool
}
const (
DISABLED_CONTAINER types.DisabledFields = "CONTAINER"
DISABLED_CHART_PULL types.DisabledFields = "CHART_PULL"
)
type DockerRegistryConfigImpl struct {
logger *zap.SugaredLogger
helmAppService client.HelmAppService
dockerArtifactStoreRepository repository.DockerArtifactStoreRepository
dockerRegistryIpsConfigRepository repository.DockerRegistryIpsConfigRepository
ociRegistryConfigRepository repository.OCIRegistryConfigRepository
RepositorySecret argoRepositoryCreds.RepositorySecret
}
func NewDockerRegistryConfigImpl(logger *zap.SugaredLogger, helmAppService client.HelmAppService, dockerArtifactStoreRepository repository.DockerArtifactStoreRepository,
dockerRegistryIpsConfigRepository repository.DockerRegistryIpsConfigRepository, ociRegistryConfigRepository repository.OCIRegistryConfigRepository,
RepositorySecret argoRepositoryCreds.RepositorySecret) *DockerRegistryConfigImpl {
return &DockerRegistryConfigImpl{
logger: logger,
helmAppService: helmAppService,
dockerArtifactStoreRepository: dockerArtifactStoreRepository,
dockerRegistryIpsConfigRepository: dockerRegistryIpsConfigRepository,
ociRegistryConfigRepository: ociRegistryConfigRepository,
RepositorySecret: RepositorySecret,
}
}
func NewDockerArtifactStore(bean *types.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,
IsOCICompliantRegistry: bean.IsOCICompliantRegistry,
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},
}
}
/*
ValidateRegistryStorageType
Parameters:
- registryId,
- storageType:
it can be any from repository.OCI_REGISRTY_REPO_TYPE_LIST
- list of storageActions:
it can be PULL, PUSH, PULL/PUSH
Logic:
- Fetch registry config for the given registryId and validate if the storageType has any of the given storageActions
Returns:
- isValid bool
*/
func (impl DockerRegistryConfigImpl) ValidateRegistryStorageType(registryId string, storageType string, storageActions ...string) bool {
isValid := false
store, err := impl.dockerArtifactStoreRepository.FindOne(registryId)
if err != nil {
return false
}
if store.IsOCICompliantRegistry {
for _, ociRegistryConfig := range store.OCIRegistryConfig {
if ociRegistryConfig.RepositoryType == storageType && slices.Contains(storageActions, ociRegistryConfig.RepositoryAction) {
isValid = true
}
}
} else {
return true
}
return isValid
}
/*
FilterRegistriesBasedOnStorageTypeAndAction
Parameters:
- List of DockerArtifactStoreBean,
- storageType:
it can be any from repository.OCI_REGISRTY_REPO_TYPE_LIST
- list of actionTypes:
it can be PULL, PUSH, PULL/PUSH
Returns:
- List of DockerArtifactStoreBean
- Error: if invalid storageType
*/
func (impl DockerRegistryConfigImpl) FilterRegistryBeanListBasedOnStorageTypeAndAction(bean []types.DockerArtifactStoreBean, storageType string, actionTypes ...string) []types.DockerArtifactStoreBean {
var registryConfigs []types.DockerArtifactStoreBean
for _, registryConfig := range bean {
// For OCI registries
if registryConfig.IsOCICompliantRegistry {
// Appends the OCI registries for specific Repo type; CHARTS or CONTAINERS
if slices.Contains(actionTypes, registryConfig.OCIRegistryConfig[storageType]) {
registryConfigs = append(registryConfigs, registryConfig)
}
} else if storageType == repository.OCI_REGISRTY_REPO_TYPE_CONTAINER {
// Appends the container registries (not OCI)
registryConfigs = append(registryConfigs, registryConfig)
}
}
return registryConfigs
}
// CreateOrUpdateOCIRegistryConfig Takes the OCIRegistryConfig to be saved/updated and the DB context. For update OCIRegistryConfig.Id should be the record id to be updated. Returns Error if any.
func (impl DockerRegistryConfigImpl) CreateOrUpdateOCIRegistryConfig(ociRegistryConfig *repository.OCIRegistryConfig, userId int32, tx *pg.Tx) error {
if ociRegistryConfig.Id > 0 {
ociRegistryConfig.UpdatedOn = time.Now()
ociRegistryConfig.UpdatedBy = userId
err := impl.ociRegistryConfigRepository.Update(ociRegistryConfig, tx)
if err != nil {
impl.logger.Errorw("error in updating OCI config db", "err", err)
return err
}
} else {
// Prevents from creating entry with deleted True
if ociRegistryConfig.Deleted {
return nil
}
ociRegistryConfig.CreatedOn = time.Now()
ociRegistryConfig.CreatedBy = userId
ociRegistryConfig.UpdatedOn = time.Now()
ociRegistryConfig.UpdatedBy = userId
err := impl.ociRegistryConfigRepository.Save(ociRegistryConfig, tx)
if err != nil {
impl.logger.Errorw("error in saving OCI config db", "err", err)
return err
}
}
return nil
}
// FilterOCIRegistryConfigForSpecificRepoType Takes the list of OCIRegistryConfigs and the RepositoryType to be filtered. Returns the first entry that matches the repositoryType.
func (impl DockerRegistryConfigImpl) FilterOCIRegistryConfigForSpecificRepoType(ociRegistryConfigList []*repository.OCIRegistryConfig, repositoryType string) *repository.OCIRegistryConfig {
ociRegistryConfig := &repository.OCIRegistryConfig{}
for _, registryConfig := range ociRegistryConfigList {
if registryConfig.RepositoryType == repositoryType {
ociRegistryConfig = registryConfig
break
}
}
return ociRegistryConfig
}
// ConfigureOCIRegistry Takes DockerArtifactStoreBean, IsUpdate flag and the DB context. It finally creates/updates the OCI config in the DB. Returns Error if any.
func (impl DockerRegistryConfigImpl) ConfigureOCIRegistry(bean *types.DockerArtifactStoreBean, isUpdate bool, userId int32, tx *pg.Tx) error {
ociRegistryConfigList, err := impl.ociRegistryConfigRepository.FindByDockerRegistryId(bean.Id)
if err != nil && (isUpdate || err != pg.ErrNoRows) {
return err
}
// If the ociRegistryConfigBean doesn't have any repoType, then mark delete true.
for _, repoType := range repository.OCI_REGISRTY_REPO_TYPE_LIST {
if _, ok := bean.OCIRegistryConfig[repoType]; !ok {
bean.OCIRegistryConfig[repoType] = ""
}
}
for repositoryType, storageActionType := range bean.OCIRegistryConfig {
if !slices.Contains(repository.OCI_REGISRTY_REPO_TYPE_LIST, repositoryType) {
return fmt.Errorf("invalid repository type for OCI registry configuration")
}
var ociRegistryConfig *repository.OCIRegistryConfig
if !isUpdate {
ociRegistryConfig = &repository.OCIRegistryConfig{
DockerArtifactStoreId: bean.Id,
Deleted: false,
}
} else {
ociRegistryConfig = impl.FilterOCIRegistryConfigForSpecificRepoType(ociRegistryConfigList, repositoryType)
if ociRegistryConfig.Id == 0 {
ociRegistryConfig.DockerArtifactStoreId = bean.Id
ociRegistryConfig.Deleted = false
}
}
ociRegistryConfig.IsPublic = false
switch storageActionType {
case repository.STORAGE_ACTION_TYPE_PULL:
ociRegistryConfig.RepositoryAction = repository.STORAGE_ACTION_TYPE_PULL
ociRegistryConfig.RepositoryType = repositoryType
if repositoryType == repository.OCI_REGISRTY_REPO_TYPE_CHART {
ociRegistryConfig.RepositoryList = strings.Join(bean.RepositoryList, ",")
ociRegistryConfig.IsPublic = bean.IsPublic
ociRegistryConfig.IsChartPullActive = true
}
err := impl.CreateOrUpdateOCIRegistryConfig(ociRegistryConfig, userId, tx)
if err != nil {
return err
}
case repository.STORAGE_ACTION_TYPE_PUSH:
ociRegistryConfig.RepositoryAction = repository.STORAGE_ACTION_TYPE_PUSH
ociRegistryConfig.RepositoryType = repositoryType
err := impl.CreateOrUpdateOCIRegistryConfig(ociRegistryConfig, userId, tx)
if err != nil {
return err
}
case repository.STORAGE_ACTION_TYPE_PULL_AND_PUSH:
ociRegistryConfig.RepositoryAction = repository.STORAGE_ACTION_TYPE_PULL_AND_PUSH
ociRegistryConfig.RepositoryType = repositoryType
if repositoryType == repository.OCI_REGISRTY_REPO_TYPE_CHART {
ociRegistryConfig.RepositoryList = strings.Join(bean.RepositoryList, ",")
ociRegistryConfig.IsPublic = bean.IsPublic
ociRegistryConfig.IsChartPullActive = true
}
err := impl.CreateOrUpdateOCIRegistryConfig(ociRegistryConfig, userId, tx)
if err != nil {
return err
}
case "":
ociRegistryConfig.Deleted = true
ociRegistryConfig.IsChartPullActive = false
err := impl.CreateOrUpdateOCIRegistryConfig(ociRegistryConfig, userId, tx)
if err != nil {
return err
}
delete(bean.OCIRegistryConfig, repositoryType)
default:
return fmt.Errorf("invalid repository action type for OCI registry configuration")
}
if repositoryType == repository.OCI_REGISRTY_REPO_TYPE_CHART &&
(storageActionType == repository.STORAGE_ACTION_TYPE_PULL || storageActionType == repository.STORAGE_ACTION_TYPE_PULL_AND_PUSH) {
err = impl.CreateArgoRepositorySecretForRepositories(bean, ociRegistryConfig)
if err != nil {
impl.logger.Errorw("error in creating repo secret", "err", err)
return err
}
}
}
return nil
}
func (impl DockerRegistryConfigImpl) CreateArgoRepositorySecretForRepositories(artifactStore *types.DockerArtifactStoreBean, ociRegistryConfig *repository.OCIRegistryConfig) error {
for _, repo := range artifactStore.RepositoryList {
err := impl.RepositorySecret.CreateArgoRepositorySecret(artifactStore.Username,
artifactStore.Password,
ociRegistryConfig.Id,
artifactStore.RegistryURL,
repo,
artifactStore.IsPublic)
if err != nil {
impl.logger.Errorw("error in create/update k8s secret", "dockerArtifactStoreId", artifactStore.Id, "err", err)
return err
}
}
return nil
}
// Create Takes the DockerArtifactStoreBean and creates the record in DB. Returns Error if any
func (impl DockerRegistryConfigImpl) Create(bean *types.DockerArtifactStoreBean) (*types.DockerArtifactStoreBean, error) {
impl.logger.Debugw("docker registry create request", "request", bean)
// 1- 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()
// 2- insert docker_registry_config
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("Container registry [%s] already exists.", bean.Id),
}
return nil, err
}
impl.logger.Infow("created repository ", "repository", store)
bean.Id = store.Id
// 3- insert OCIRegistryConfig for this docker registry
if store.IsOCICompliantRegistry {
err = impl.ConfigureOCIRegistry(bean, false, bean.User, tx)
if err != nil {
impl.logger.Errorw("error in saving OCI registry config", "OCIRegistryConfig", bean.OCIRegistryConfig, "err", err)
err = &util.ApiError{
Code: constants.DockerRegCreateFailedInDb,
InternalMessage: err.Error(),
UserMessage: "Error in creating OCI registry config in db",
}
return nil, err
}
impl.logger.Infow("created OCI registry config successfully")
}
if !bean.IsPublic && bean.DockerRegistryIpsConfig != nil {
// 4- insert imagePullSecretConfig for this docker registry
dockerRegistryIpsConfig := bean.DockerRegistryIpsConfig
ipsConfig := &repository.DockerRegistryIpsConfig{
DockerArtifactStoreId: store.Id,
CredentialType: dockerRegistryIpsConfig.CredentialType,
CredentialValue: dockerRegistryIpsConfig.CredentialValue,
AppliedClusterIdsCsv: dockerRegistryIpsConfig.AppliedClusterIdsCsv,
IgnoredClusterIdsCsv: dockerRegistryIpsConfig.IgnoredClusterIdsCsv,
Active: true,
}
err = impl.createDockerIpConfig(tx, ipsConfig)
if err != nil {
return nil, err
}
dockerRegistryIpsConfig.Id = ipsConfig.Id
}
// 4- now commit transaction
err = tx.Commit()
if err != nil {
impl.logger.Errorw("error in committing transaction", "err", err)
return nil, err
}
return bean, nil
}
// ListAllActive Returns the list all active artifact stores
func (impl DockerRegistryConfigImpl) ListAllActive() ([]types.DockerArtifactStoreBean, error) {
impl.logger.Debug("list docker repo request")
stores, err := impl.dockerArtifactStoreRepository.FindAllActiveForAutocomplete()
if err != nil {
impl.logger.Errorw("error in listing artifact", "err", err)
return nil, err
}
var storeBeans []types.DockerArtifactStoreBean
for _, store := range stores {
storeBean := types.DockerArtifactStoreBean{
Id: store.Id,
RegistryURL: store.RegistryURL,
IsDefault: store.IsDefault,
RegistryType: store.RegistryType,
IsOCICompliantRegistry: store.IsOCICompliantRegistry,
}
if store.IsOCICompliantRegistry {
impl.PopulateOCIRegistryConfig(&store, &storeBean)
}
storeBeans = append(storeBeans, storeBean)
}
return storeBeans, err
}
// FetchAllDockerAccounts method used for getting all registry accounts with complete details
func (impl DockerRegistryConfigImpl) FetchAllDockerAccounts() ([]types.DockerArtifactStoreBean, error) {
impl.logger.Debug("list docker repo request")
stores, err := impl.dockerArtifactStoreRepository.FindAll()
if err != nil {
impl.logger.Errorw("error in listing artifact", "err", err)
return nil, err
}
var storeBeans []types.DockerArtifactStoreBean
for _, store := range stores {
ipsConfig := store.IpsConfig
storeBean := types.DockerArtifactStoreBean{
Id: store.Id,
PluginId: store.PluginId,
RegistryURL: store.RegistryURL,
RegistryType: store.RegistryType,
AWSAccessKeyId: store.AWSAccessKeyId,
AWSSecretAccessKey: "",
AWSRegion: store.AWSRegion,
Username: store.Username,
Password: "",
IsDefault: store.IsDefault,
Connection: store.Connection,
Cert: store.Cert,
Active: store.Active,
IsOCICompliantRegistry: store.IsOCICompliantRegistry,
}
if store.IsOCICompliantRegistry {
impl.PopulateOCIRegistryConfig(&store, &storeBean)
}
if ipsConfig != nil {
storeBean.DockerRegistryIpsConfig = &types.DockerRegistryIpsConfigBean{
Id: ipsConfig.Id,
CredentialType: ipsConfig.CredentialType,
CredentialValue: ipsConfig.CredentialValue,
AppliedClusterIdsCsv: ipsConfig.AppliedClusterIdsCsv,
IgnoredClusterIdsCsv: ipsConfig.IgnoredClusterIdsCsv,
Active: ipsConfig.Active,
}
}
storeBeans = append(storeBeans, storeBean)
}
return storeBeans, err
}
// PopulateOCIRegistryConfig Takes the DB docker_artifact_store response and generates
func (impl DockerRegistryConfigImpl) PopulateOCIRegistryConfig(store *repository.DockerArtifactStore, storeBean *types.DockerArtifactStoreBean) *types.DockerArtifactStoreBean {
ociRegistryConfigs := map[string]string{}
for _, ociRegistryConfig := range store.OCIRegistryConfig {
ociRegistryConfigs[ociRegistryConfig.RepositoryType] = ociRegistryConfig.RepositoryAction
if ociRegistryConfig.RepositoryType == repository.OCI_REGISRTY_REPO_TYPE_CHART {
storeBean.RepositoryList = strings.Split(ociRegistryConfig.RepositoryList, ",")
storeBean.IsPublic = ociRegistryConfig.IsPublic
}
}
storeBean.OCIRegistryConfig = ociRegistryConfigs
return storeBean
}
// FetchOneDockerAccount this method takes the docker account id and Returns DockerArtifactStoreBean and Error (if any)
func (impl DockerRegistryConfigImpl) FetchOneDockerAccount(storeId string) (*types.DockerArtifactStoreBean, error) {
impl.logger.Debug("fetch docker account by id from db")
store, err := impl.dockerArtifactStoreRepository.FindOne(storeId)
if err != nil {
impl.logger.Errorw("error in listing artifact", "err", err)
return nil, err
}
ipsConfig := store.IpsConfig
storeBean := &types.DockerArtifactStoreBean{
Id: store.Id,
PluginId: store.PluginId,
RegistryURL: store.RegistryURL,
RegistryType: store.RegistryType,
AWSAccessKeyId: store.AWSAccessKeyId,
AWSSecretAccessKey: store.AWSSecretAccessKey,
AWSRegion: store.AWSRegion,
Username: store.Username,
Password: store.Password,
IsDefault: store.IsDefault,
Connection: store.Connection,
Cert: store.Cert,
Active: store.Active,
IsOCICompliantRegistry: store.IsOCICompliantRegistry,
}
if store.IsOCICompliantRegistry {
impl.PopulateOCIRegistryConfig(store, storeBean)
}
if ipsConfig != nil {
storeBean.DockerRegistryIpsConfig = &types.DockerRegistryIpsConfigBean{
Id: ipsConfig.Id,
CredentialType: ipsConfig.CredentialType,
CredentialValue: ipsConfig.CredentialValue,
AppliedClusterIdsCsv: ipsConfig.AppliedClusterIdsCsv,
IgnoredClusterIdsCsv: ipsConfig.IgnoredClusterIdsCsv,
Active: ipsConfig.Active,
}
}
return storeBean, err
}
// Update will update the existing registry with the given DockerArtifactStoreBean
func (impl DockerRegistryConfigImpl) Update(bean *types.DockerArtifactStoreBean) (*types.DockerArtifactStoreBean, error) {
impl.logger.Debugw("docker registry update request", "request", bean)
// 1- find by id, if err - return error
existingStore, err := impl.dockerArtifactStoreRepository.FindOne(bean.Id)
if err != nil {
impl.logger.Errorw("no matching entry found of update ..", "err", err)
return nil, err
}
// 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
if bean.Password == "" {
bean.Password = existingStore.Password
}
if bean.AWSSecretAccessKey == "" {
bean.AWSSecretAccessKey = existingStore.AWSSecretAccessKey
}
if bean.Cert == "" {
bean.Cert = existingStore.Cert
}
if bean.Cert == "" {
bean.Cert = existingStore.Cert
}
existingRepositoryList := make([]string, 0)
for _, ociRegistryConfig := range existingStore.OCIRegistryConfig {
if ociRegistryConfig.RepositoryType == repository.OCI_REGISRTY_REPO_TYPE_CHART {
existingRepositoryList = strings.Split(ociRegistryConfig.RepositoryList, ",")
}
}
deployedChartList := make([]string, 0)
for _, repository := range existingRepositoryList {
if !slices.Contains(bean.RepositoryList, repository) {
chartDeploymentCount, err := impl.dockerArtifactStoreRepository.FindOneWithChartDeploymentCount(bean.Id, repository)
if err != nil && err != pg.ErrNoRows {
return nil, err
} else if chartDeploymentCount != nil && chartDeploymentCount.DeploymentCount > 0 {
deployedChartList = append(deployedChartList, chartDeploymentCount.OCIChartName)
}
}
}
if len(deployedChartList) > 0 {
err := &util.ApiError{
HttpStatusCode: http.StatusConflict,
InternalMessage: fmt.Sprintf("%s chart(s) cannot be removed as they are being used by helm applications.", strings.Join(deployedChartList, ", ")),
UserMessage: fmt.Sprintf("%s chart(s) cannot be removed as they are being used by helm applications.", strings.Join(deployedChartList, ", "))}
return nil, err
}
bean.PluginId = existingStore.PluginId
store := NewDockerArtifactStore(bean, true, existingStore.CreatedOn, time.Now(), existingStore.CreatedBy, bean.User)
if err = impl.ValidateRegistryCredentials(bean); err != nil {
impl.logger.Errorw("registry credentials validation err, SaveDockerRegistryConfig", "err", err)
return nil, err
}
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 OCIRegistryConfig for this docker registry
if store.IsOCICompliantRegistry {
err = impl.ConfigureOCIRegistry(bean, true, bean.User, tx)
if err != nil {
impl.logger.Errorw("error in updating OCI registry config", "OCIRegistryConfig", bean.OCIRegistryConfig, "err", err)
err = &util.ApiError{
Code: constants.DockerRegCreateFailedInDb,
InternalMessage: err.Error(),
UserMessage: "Error in updating OCI registry config in db",
}
return nil, err
}
impl.logger.Infow("updated OCI registry config successfully")
}
// 5- update imagePullSecretConfig for this docker registry
existingIpsConfig, err := impl.dockerRegistryIpsConfigRepository.FindByDockerRegistryId(store.Id)
if !bean.IsPublic && bean.DockerRegistryIpsConfig != nil {
dockerRegistryIpsConfig := bean.DockerRegistryIpsConfig
ipsConfig := &repository.DockerRegistryIpsConfig{
DockerArtifactStoreId: store.Id,
CredentialType: dockerRegistryIpsConfig.CredentialType,
CredentialValue: dockerRegistryIpsConfig.CredentialValue,
AppliedClusterIdsCsv: dockerRegistryIpsConfig.AppliedClusterIdsCsv,
IgnoredClusterIdsCsv: dockerRegistryIpsConfig.IgnoredClusterIdsCsv,
Active: true,
}
if err != nil {
impl.logger.Errorw("Error while getting docker registry ips config", "dockerRegistryId", store.Id, "err", err)
// Create a new docker registry ips config
if err == pg.ErrNoRows {
err = impl.createDockerIpConfig(tx, ipsConfig)
if err != nil {
return nil, err
}
} else {
// Throw error
return nil, err
}
} else {
// Update the docker registry ips config
ipsConfig.Id = existingIpsConfig.Id
err = impl.updateDockerIpConfig(tx, ipsConfig)
if err != nil {
return nil, err
}
}
bean.DockerRegistryIpsConfig.Id = ipsConfig.Id
} else {
if err == nil {
// Update the docker registry ips config to inactive
existingIpsConfig.Active = false
err = impl.updateDockerIpConfig(tx, existingIpsConfig)
if err != nil {
return nil, err
}
}
}
// 6- 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) updateDockerIpConfig(tx *pg.Tx, existingIpsConfig *repository.DockerRegistryIpsConfig) error {
err := impl.dockerRegistryIpsConfigRepository.Update(existingIpsConfig, tx)
if err != nil {
impl.logger.Errorw("error in updating registry config ips", "ipsConfig", existingIpsConfig, "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 err
}
impl.logger.Infow("updated ips config for this docker repository ", "ipsConfig", existingIpsConfig)
return nil
}
func (impl DockerRegistryConfigImpl) createDockerIpConfig(tx *pg.Tx, ipsConfig *repository.DockerRegistryIpsConfig) error {
existingIpsConfig, err := impl.dockerRegistryIpsConfigRepository.FindInActiveByDockerRegistryId(ipsConfig.DockerArtifactStoreId)
if err != nil {
if err == pg.ErrNoRows {
err = impl.dockerRegistryIpsConfigRepository.Save(ipsConfig, tx)
if err != nil {
impl.logger.Errorw("error in saving registry config ips", "ipsConfig", ipsConfig, "err", err)
err = &util.ApiError{
Code: constants.DockerRegCreateFailedInDb,
InternalMessage: "docker registry ips config to create in db",
UserMessage: fmt.Sprintf("Container registry [%s] already exists.", ipsConfig.DockerArtifactStoreId),
}
return err
}
impl.logger.Infow("created ips config for this docker repository", "ipsConfig", ipsConfig)
return nil
} else {
return err
}
}
ipsConfig.Id = existingIpsConfig.Id
err = impl.updateDockerIpConfig(tx, ipsConfig)
if err != nil {
return err
}
return nil
}
// UpdateInactive will update the existing soft deleted registry with the given DockerArtifactStoreBean instead of creating one
func (impl DockerRegistryConfigImpl) UpdateInactive(bean *types.DockerArtifactStoreBean) (*types.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 OCIRegistryConfig for this docker registry
if store.IsOCICompliantRegistry {
err = impl.ConfigureOCIRegistry(bean, true, bean.User, tx)
if err != nil {
impl.logger.Errorw("error in updating OCI registry config", "OCIRegistryConfig", bean.OCIRegistryConfig, "err", err)
err = &util.ApiError{
Code: constants.DockerRegCreateFailedInDb,
InternalMessage: err.Error(),
UserMessage: "Error in updating OCI registry config in db",
}
return nil, err
}
impl.logger.Infow("updated OCI registry config successfully")
}
if !bean.IsPublic && bean.DockerRegistryIpsConfig != nil {
// 5- update imagePullSecretConfig for this docker registry
dockerRegistryIpsConfig := bean.DockerRegistryIpsConfig
ipsConfig := &repository.DockerRegistryIpsConfig{
DockerArtifactStoreId: store.Id,
CredentialType: dockerRegistryIpsConfig.CredentialType,
CredentialValue: dockerRegistryIpsConfig.CredentialValue,
AppliedClusterIdsCsv: dockerRegistryIpsConfig.AppliedClusterIdsCsv,
IgnoredClusterIdsCsv: dockerRegistryIpsConfig.IgnoredClusterIdsCsv,
Active: true,
}
if existingStore.IpsConfig != nil {
ipsConfig.Id = existingStore.IpsConfig.Id
err = impl.updateDockerIpConfig(tx, ipsConfig)
if err != nil {
return nil, err
}
} else {
err = impl.createDockerIpConfig(tx, ipsConfig)
if err != nil {
return nil, err
}
}
}
// 6- now commit transaction
err = tx.Commit()
if err != nil {
impl.logger.Errorw("error in committing transaction", "err", err)
return nil, err
}
return bean, nil
}
// Delete is a Deprecated function. It was used to hard delete the registry from DB.
func (impl DockerRegistryConfigImpl) Delete(storeId string) (string, error) {
impl.logger.Debugw("docker registry update request", "request", storeId)
err := impl.dockerArtifactStoreRepository.Delete(storeId)
if err != nil {
impl.logger.Errorw("error in delete registry in db", "storeId", storeId, "err", err)
err = &util.ApiError{
Code: constants.DockerRegDeleteFailedInDb,
InternalMessage: "docker registry failed to delete in db",
UserMessage: err.Error(),
}
return "", err
}
impl.logger.Infow("delete docker registry ", "storeId", storeId)
return storeId, nil
}
// DeleteReg Takes DockerArtifactStoreBean and soft deletes the OCI configs (if exists), finally soft deletes the registry. Returns Error if any.
func (impl DockerRegistryConfigImpl) DeleteReg(bean *types.DockerArtifactStoreBean) error {
// 1- fetching Artifact Registry
dockerReg, err := impl.dockerArtifactStoreRepository.FindOne(bean.Id)
if err != nil {
impl.logger.Errorw("No matching entry found for delete.", "id", bean.Id, "err", err)
return err
}
// 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 err
}
// Rollback tx on error.
defer tx.Rollback()
// 3- fetching OCI config attached to the Artifact Registry
ociRegistryConfigs, err := impl.ociRegistryConfigRepository.FindByDockerRegistryId(dockerReg.Id)
if err != nil {
impl.logger.Errorw("No matching entry found for delete.", "id", bean.Id, "err", err)
return err
}
// 4- marking deleted, OCI configs attached to the Artifact Registry
for _, ociRegistryConfig := range ociRegistryConfigs {
if !ociRegistryConfig.Deleted {
ociRegistryConfig.Deleted = true
ociRegistryConfig.UpdatedOn = time.Now()
ociRegistryConfig.UpdatedBy = bean.User
err = impl.ociRegistryConfigRepository.Update(ociRegistryConfig, tx)
if err != nil {
impl.logger.Errorw("err in deleting OCI configs for registry", "registryId", bean.Id, "err", err)
return err
}
}
}
// 4- mark deleted, Artifact Registry
deleteReq := dockerReg
deleteReq.UpdatedOn = time.Now()
deleteReq.UpdatedBy = bean.User
err = impl.dockerArtifactStoreRepository.MarkRegistryDeleted(deleteReq, tx)
if err != nil {
impl.logger.Errorw("err in deleting docker registry", "id", bean.Id, "err", err)
return err
}
// 6- now commit transaction
err = tx.Commit()
if err != nil {
impl.logger.Errorw("error in committing transaction", "err", err)
return err
}
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
}
const ociRegistryInvalidCredsMsg = "Invalid authentication credentials. Please verify."
func (impl DockerRegistryConfigImpl) ValidateRegistryCredentials(bean *types.DockerArtifactStoreBean) error {
if bean.IsPublic ||
bean.RegistryType == repository.REGISTRYTYPE_GCR ||
bean.RegistryType == repository.REGISTRYTYPE_ARTIFACT_REGISTRY ||
bean.RegistryType == repository.REGISTRYTYPE_OTHER {
return nil
}
request := &bean2.RegistryCredential{
RegistryUrl: bean.RegistryURL,
Username: bean.Username,
Password: bean.Password,
AwsRegion: bean.AWSRegion,
AccessKey: bean.AWSAccessKeyId,
SecretKey: bean.AWSSecretAccessKey,
RegistryType: string(bean.RegistryType),
IsPublic: bean.IsPublic,
Connection: bean.Connection,
}
isLoggedIn, err := impl.helmAppService.ValidateOCIRegistry(context.Background(), request)
if err != nil {
impl.logger.Errorw("error in fetching chart", "err", err)
return util.NewApiError().
WithUserMessage("error in validating oci registry").
WithInternalMessage(err.Error()).
WithHttpStatusCode(http.StatusInternalServerError)
}
if !isLoggedIn {
return util.NewApiError().
WithUserMessage(ociRegistryInvalidCredsMsg).
WithInternalMessage(ociRegistryInvalidCredsMsg).
WithHttpStatusCode(http.StatusBadRequest)
}
return nil
}