Skip to content

Commit 51b9109

Browse files
authored
feat: added BE support for allowing insecure tls connection in gitOps (#2738)
* added support for allowing insecure tls connection for gitlab client and argo repo create api * added sql script
1 parent 82d10ac commit 51b9109

File tree

13 files changed

+70
-96
lines changed

13 files changed

+70
-96
lines changed

api/bean/GitOpsConfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ type GitOpsConfigDto struct {
1212
AzureProjectName string `json:"azureProjectName"`
1313
BitBucketWorkspaceId string `json:"bitBucketWorkspaceId"`
1414
BitBucketProjectKey string `json:"bitBucketProjectKey"`
15+
AllowInsecureTLS bool `json:"allowInsecureTLS"`
1516
UserId int32 `json:"-"`
1617
}

internal/sql/repository/GitOpsConfigRepository.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type GitOpsConfig struct {
5454
BitBucketWorkspaceId string `sql:"bitbucket_workspace_id"`
5555
BitBucketProjectKey string `sql:"bitbucket_project_key"`
5656
EmailId string `sql:"email_id"`
57+
AllowInsecureTLS bool `sql:"allow_insecure_tls,notnull"`
5758
sql.AuditLog
5859
}
5960

internal/util/ChartService.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type ChartTemplateService interface {
6060
GetGitOpsRepoName(appName string) string
6161
GetGitOpsRepoNameFromUrl(gitRepoUrl string) string
6262
CreateGitRepositoryForApp(gitOpsRepoName, baseTemplateName, version string, userId int32) (chartGitAttribute *ChartGitAttribute, err error)
63-
RegisterInArgo(chartGitAttribute *ChartGitAttribute, ctx context.Context) error
63+
RegisterInArgo(chartGitAttribute *ChartGitAttribute, ctx context.Context, allowInsecureTLS bool) error
6464
BuildChartAndPushToGitRepo(chartMetaData *chart.Metadata, referenceTemplatePath string, gitOpsRepoName, referenceTemplate, version, repoUrl string, userId int32) error
6565
GetByteArrayRefChart(chartMetaData *chart.Metadata, referenceTemplatePath string) ([]byte, error)
6666
CreateReadmeInGitRepo(gitOpsRepoName string, userId int32) error
@@ -104,9 +104,10 @@ func NewChartTemplateServiceImpl(logger *zap.SugaredLogger,
104104
repositoryService: repositoryService,
105105
}
106106
}
107-
func (impl ChartTemplateServiceImpl) RegisterInArgo(chartGitAttribute *ChartGitAttribute, ctx context.Context) error {
107+
func (impl ChartTemplateServiceImpl) RegisterInArgo(chartGitAttribute *ChartGitAttribute, ctx context.Context, allowInsecureTLS bool) error {
108108
repo := &v1alpha1.Repository{
109-
Repo: chartGitAttribute.RepoUrl,
109+
Repo: chartGitAttribute.RepoUrl,
110+
Insecure: allowInsecureTLS,
110111
}
111112
repo, err := impl.repositoryService.Create(ctx, &repository3.RepoCreateRequest{Repo: repo, Upsert: true})
112113
if err != nil {

internal/util/GitService.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"fmt"
2323
"io/ioutil"
24-
"net/url"
2524
"path/filepath"
2625
"time"
2726

@@ -99,25 +98,7 @@ func (factory *GitFactory) Reload() error {
9998
}
10099

101100
func (factory *GitFactory) GetGitLabGroupPath(gitOpsConfig *bean2.GitOpsConfigDto) (string, error) {
102-
var gitLabClient *gitlab.Client
103-
var err error
104-
if len(gitOpsConfig.Host) > 0 {
105-
_, err = url.ParseRequestURI(gitOpsConfig.Host)
106-
if err != nil {
107-
return "", err
108-
}
109-
gitLabClient, err = gitlab.NewClient(gitOpsConfig.Token, gitlab.WithBaseURL(gitOpsConfig.Host))
110-
if err != nil {
111-
factory.logger.Errorw("error in getting new gitlab client", "err", err)
112-
return "", err
113-
}
114-
} else {
115-
gitLabClient, err = gitlab.NewClient(gitOpsConfig.Token)
116-
if err != nil {
117-
factory.logger.Errorw("error in getting new gitlab client", "err", err)
118-
return "", err
119-
}
120-
}
101+
gitLabClient, err := CreateGitlabClient(gitOpsConfig.Host, gitOpsConfig.Token, gitOpsConfig.AllowInsecureTLS)
121102
group, _, err := gitLabClient.Groups.GetGroup(gitOpsConfig.GitLabGroupId, &gitlab.GetGroupOptions{})
122103
if err != nil {
123104
factory.logger.Errorw("error in fetching gitlab group name", "err", err, "gitLab groupID", gitOpsConfig.GitLabGroupId)
@@ -141,6 +122,7 @@ func (factory *GitFactory) NewClientForValidation(gitOpsConfig *bean2.GitOpsConf
141122
GitHost: gitOpsConfig.Host,
142123
AzureToken: gitOpsConfig.Token,
143124
AzureProject: gitOpsConfig.AzureProjectName,
125+
AllowInsecureTLS: gitOpsConfig.AllowInsecureTLS,
144126
}
145127
gitService := NewGitServiceImpl(cfg, logger, factory.gitCliUtil)
146128
//factory.gitService = gitService
@@ -187,6 +169,7 @@ type GitConfig struct {
187169
AzureProject string
188170
BitbucketWorkspaceId string
189171
BitbucketProjectKey string
172+
AllowInsecureTLS bool
190173
}
191174

192175
func GetGitConfig(gitOpsRepository repository.GitOpsConfigRepository) (*GitConfig, error) {
@@ -216,6 +199,7 @@ func GetGitConfig(gitOpsRepository repository.GitOpsConfigRepository) (*GitConfi
216199
AzureProject: gitOpsConfig.AzureProject,
217200
BitbucketWorkspaceId: gitOpsConfig.BitBucketWorkspaceId,
218201
BitbucketProjectKey: gitOpsConfig.BitBucketProjectKey,
202+
AllowInsecureTLS: gitOpsConfig.AllowInsecureTLS,
219203
}
220204
return cfg, err
221205
}

internal/util/GitServiceGitlab.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package util
22

33
import (
4+
"crypto/tls"
45
"fmt"
6+
"github.com/hashicorp/go-retryablehttp"
57
"github.com/xanzy/go-gitlab"
68
"go.uber.org/zap"
9+
"net/http"
710
"net/url"
811
"path/filepath"
912
"strconv"
@@ -18,24 +21,7 @@ type GitLabClient struct {
1821
}
1922

2023
func NewGitLabClient(config *GitConfig, logger *zap.SugaredLogger, gitService GitService) (GitClient, error) {
21-
var gitLabClient *gitlab.Client
22-
var err error
23-
if len(config.GitHost) > 0 {
24-
_, err = url.ParseRequestURI(config.GitHost)
25-
if err != nil {
26-
return nil, err
27-
}
28-
gitLabClient, err = gitlab.NewClient(config.GitToken, gitlab.WithBaseURL(config.GitHost))
29-
if err != nil {
30-
return nil, err
31-
}
32-
} else {
33-
gitLabClient, err = gitlab.NewClient(config.GitToken)
34-
if err != nil {
35-
return nil, err
36-
}
37-
}
38-
24+
gitLabClient, err := CreateGitlabClient(config.GitHost, config.GitToken, config.AllowInsecureTLS)
3925
gitlabGroupId := ""
4026
if len(config.GitlabGroupId) > 0 {
4127
if _, err := strconv.Atoi(config.GitlabGroupId); err == nil {
@@ -81,6 +67,32 @@ func NewGitLabClient(config *GitConfig, logger *zap.SugaredLogger, gitService Gi
8167
}, nil
8268
}
8369

70+
func CreateGitlabClient(host, token string, allowInsecureTLS bool) (*gitlab.Client, error) {
71+
var gitLabClient *gitlab.Client
72+
var err error
73+
httpTransport := &http.Transport{
74+
TLSClientConfig: &tls.Config{InsecureSkipVerify: allowInsecureTLS},
75+
}
76+
retryClient := retryablehttp.NewClient()
77+
retryClient.HTTPClient.Transport = httpTransport
78+
if len(host) > 0 {
79+
_, err = url.ParseRequestURI(host)
80+
if err != nil {
81+
return nil, err
82+
}
83+
gitLabClient, err = gitlab.NewClient(token, gitlab.WithBaseURL(host), gitlab.WithHTTPClient(retryClient.HTTPClient))
84+
if err != nil {
85+
return nil, err
86+
}
87+
} else {
88+
gitLabClient, err = gitlab.NewClient(token, gitlab.WithHTTPClient(retryClient.HTTPClient))
89+
if err != nil {
90+
return nil, err
91+
}
92+
}
93+
return gitLabClient, err
94+
}
95+
8496
func (impl GitLabClient) DeleteRepository(name string) error {
8597
err := impl.DeleteProject(name)
8698
if err != nil {

pkg/appStore/deployment/fullMode/AppStoreDeploymentFullModeService.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package appStoreDeploymentFullMode
2020
import (
2121
"context"
2222
"github.com/devtron-labs/devtron/client/argocdServer"
23+
repository2 "github.com/devtron-labs/devtron/internal/sql/repository"
2324
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
2425
repository4 "github.com/devtron-labs/devtron/pkg/appStore/deployment/repository"
2526
appStoreDiscoverRepository "github.com/devtron-labs/devtron/pkg/appStore/discover/repository"
@@ -35,8 +36,6 @@ import (
3536
"time"
3637

3738
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
38-
repository2 "github.com/argoproj/argo-cd/v2/pkg/apiclient/repository"
39-
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
4039
application2 "github.com/devtron-labs/devtron/client/argocdServer/application"
4140
"github.com/devtron-labs/devtron/client/argocdServer/repository"
4241
"github.com/devtron-labs/devtron/internal/util"
@@ -54,7 +53,6 @@ const (
5453
type AppStoreDeploymentFullModeService interface {
5554
AppStoreDeployOperationGIT(installAppVersionRequest *appStoreBean.InstallAppVersionDTO) (*appStoreBean.InstallAppVersionDTO, *util.ChartGitAttribute, error)
5655
AppStoreDeployOperationACD(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, chartGitAttr *util.ChartGitAttribute, ctx context.Context) (*appStoreBean.InstallAppVersionDTO, error)
57-
RegisterInArgo(chartGitAttribute *util.ChartGitAttribute, ctx context.Context) error
5856
SyncACD(acdAppName string, ctx context.Context)
5957
UpdateValuesYaml(installAppVersionRequest *appStoreBean.InstallAppVersionDTO) (*appStoreBean.InstallAppVersionDTO, error)
6058
UpdateRequirementYaml(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, appStoreAppVersion *appStoreDiscoverRepository.AppStoreApplicationVersion) error
@@ -76,6 +74,7 @@ type AppStoreDeploymentFullModeServiceImpl struct {
7674
installedAppRepository repository4.InstalledAppRepository
7775
tokenCache *util2.TokenCache
7876
argoUserService argo.ArgoUserService
77+
gitOpsConfigRepository repository2.GitOpsConfigRepository
7978
}
8079

8180
func NewAppStoreDeploymentFullModeServiceImpl(logger *zap.SugaredLogger,
@@ -237,8 +236,13 @@ func (impl AppStoreDeploymentFullModeServiceImpl) AppStoreDeployOperationGIT(ins
237236
func (impl AppStoreDeploymentFullModeServiceImpl) AppStoreDeployOperationACD(installAppVersionRequest *appStoreBean.InstallAppVersionDTO, chartGitAttr *util.ChartGitAttribute, ctx context.Context) (*appStoreBean.InstallAppVersionDTO, error) {
238237
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
239238
defer cancel()
239+
gitOpsConfig, err := impl.gitOpsConfigRepository.GetGitOpsConfigActive()
240+
if err != nil {
241+
impl.logger.Errorw("error in getting active gitOps config", "err", err)
242+
return nil, err
243+
}
240244
//STEP 4: registerInArgo
241-
err := impl.RegisterInArgo(chartGitAttr, ctx)
245+
err = impl.chartTemplateService.RegisterInArgo(chartGitAttr, ctx, gitOpsConfig.AllowInsecureTLS)
242246
if err != nil {
243247
impl.logger.Errorw("error in argo registry", "err", err)
244248
return nil, err
@@ -255,18 +259,6 @@ func (impl AppStoreDeploymentFullModeServiceImpl) AppStoreDeployOperationACD(ins
255259
return installAppVersionRequest, nil
256260
}
257261

258-
func (impl AppStoreDeploymentFullModeServiceImpl) RegisterInArgo(chartGitAttribute *util.ChartGitAttribute, ctx context.Context) error {
259-
repo := &v1alpha1.Repository{
260-
Repo: chartGitAttribute.RepoUrl,
261-
}
262-
repo, err := impl.repositoryService.Create(ctx, &repository2.RepoCreateRequest{Repo: repo, Upsert: true})
263-
if err != nil {
264-
impl.logger.Errorw("error in creating argo Repository ", "err", err)
265-
}
266-
impl.logger.Debugw("repo registered in argo", "name", chartGitAttribute.RepoUrl)
267-
return err
268-
}
269-
270262
func (impl AppStoreDeploymentFullModeServiceImpl) SyncACD(acdAppName string, ctx context.Context) {
271263
req := new(application.ApplicationSyncRequest)
272264
req.Name = &acdAppName

pkg/appStore/deployment/service/InstalledAppService.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,6 @@ func (impl InstalledAppServiceImpl) performDeployStageOnAcd(installedAppVersion
377377
impl.logger.Errorw("fetching error", "err", err)
378378
return nil, err
379379
}
380-
gitOpsConfigBitbucket, err := impl.gitOpsRepository.GetGitOpsConfigByProvider(util.BITBUCKET_PROVIDER)
381-
if err != nil {
382-
if err == pg.ErrNoRows {
383-
gitOpsConfigBitbucket.BitBucketWorkspaceId = ""
384-
gitOpsConfigBitbucket.BitBucketProjectKey = ""
385-
} else {
386-
return nil, err
387-
}
388-
}
389-
390380
repoUrl, err := impl.gitFactory.Client.GetRepoUrl(installedAppVersion.AppStoreName)
391381
if err != nil {
392382
//will allow to continue to persist status on next operation

pkg/appStore/deployment/tool/gitops/AppStoreDeploymentArgoCdService.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
1212
application2 "github.com/devtron-labs/devtron/client/argocdServer/application"
1313
"github.com/devtron-labs/devtron/internal/constants"
14+
repository2 "github.com/devtron-labs/devtron/internal/sql/repository"
1415
"github.com/devtron-labs/devtron/internal/util"
1516
appStoreBean "github.com/devtron-labs/devtron/pkg/appStore/bean"
1617
appStoreDeploymentFullMode "github.com/devtron-labs/devtron/pkg/appStore/deployment/fullMode"
@@ -50,6 +51,7 @@ type AppStoreDeploymentArgoCdServiceImpl struct {
5051
chartTemplateService util.ChartTemplateService
5152
gitFactory *util.GitFactory
5253
argoUserService argo.ArgoUserService
54+
gitOpsConfigRepository repository2.GitOpsConfigRepository
5355
}
5456

5557
func NewAppStoreDeploymentArgoCdServiceImpl(logger *zap.SugaredLogger, appStoreDeploymentFullModeService appStoreDeploymentFullMode.AppStoreDeploymentFullModeService,
@@ -398,8 +400,13 @@ func (impl AppStoreDeploymentArgoCdServiceImpl) UpdateInstalledApp(ctx context.C
398400
func (impl AppStoreDeploymentArgoCdServiceImpl) patchAcdApp(ctx context.Context, installAppVersionRequest *appStoreBean.InstallAppVersionDTO, chartGitAttr *util.ChartGitAttribute) (*appStoreBean.InstallAppVersionDTO, error) {
399401
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
400402
defer cancel()
403+
gitOpsConfig, err := impl.gitOpsConfigRepository.GetGitOpsConfigActive()
404+
if err != nil {
405+
impl.Logger.Errorw("error in getting active gitOps config", "err", err)
406+
return nil, err
407+
}
401408
//registerInArgo
402-
err := impl.appStoreDeploymentFullModeService.RegisterInArgo(chartGitAttr, ctx)
409+
err = impl.chartTemplateService.RegisterInArgo(chartGitAttr, ctx, gitOpsConfig.AllowInsecureTLS)
403410
if err != nil {
404411
impl.Logger.Errorw("error in argo registry", "err", err)
405412
return nil, err

pkg/chart/ChartService.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ import (
4444
"github.com/devtron-labs/devtron/pkg/sql"
4545
dirCopy "github.com/otiai10/copy"
4646

47-
repository2 "github.com/argoproj/argo-cd/v2/pkg/apiclient/repository"
48-
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
4947
"github.com/devtron-labs/devtron/client/argocdServer/repository"
5048
"github.com/devtron-labs/devtron/internal/sql/models"
5149
repository3 "github.com/devtron-labs/devtron/internal/sql/repository"
@@ -141,7 +139,6 @@ type ChartService interface {
141139
GetLocationFromChartNameAndVersion(chartName string, chartVersion string) string
142140
ValidateUploadedFileFormat(fileName string) error
143141
ReadChartMetaDataForLocation(chartDir string, fileName string) (*ChartYamlStruct, error)
144-
RegisterInArgo(chartGitAttribute *util.ChartGitAttribute, ctx context.Context) error
145142
FetchChartInfoByFlag(userUploaded bool) ([]*ChartDto, error)
146143
CheckCustomChartByAppId(id int) (bool, error)
147144
CheckCustomChartByChartId(id int) (bool, error)
@@ -582,18 +579,6 @@ func (impl ChartServiceImpl) CreateChartFromEnvOverride(templateRequest Template
582579
return chartVal, err
583580
}
584581

585-
func (impl ChartServiceImpl) RegisterInArgo(chartGitAttribute *util.ChartGitAttribute, ctx context.Context) error {
586-
repo := &v1alpha1.Repository{
587-
Repo: chartGitAttribute.RepoUrl,
588-
}
589-
repo, err := impl.repositoryService.Create(ctx, &repository2.RepoCreateRequest{Repo: repo, Upsert: true})
590-
if err != nil {
591-
impl.logger.Errorw("error in creating argo Repository ", "err", err)
592-
}
593-
impl.logger.Infow("repo registered in argo", "name", chartGitAttribute.RepoUrl)
594-
return err
595-
}
596-
597582
// converts db object to bean
598583
func (impl ChartServiceImpl) chartAdaptor(chart *chartRepoRepository.Chart, appLevelMetrics *repository3.AppLevelMetrics) (*TemplateRequest, error) {
599584
var appMetrics bool

pkg/gitops/GitOpsConfigService.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ func (impl *GitOpsConfigServiceImpl) CreateGitOpsConfig(ctx context.Context, req
193193
AzureProject: request.AzureProjectName,
194194
BitBucketWorkspaceId: request.BitBucketWorkspaceId,
195195
BitBucketProjectKey: request.BitBucketProjectKey,
196+
AllowInsecureTLS: request.AllowInsecureTLS,
196197
AuditLog: sql.AuditLog{CreatedBy: request.UserId, CreatedOn: time.Now(), UpdatedOn: time.Now(), UpdatedBy: request.UserId},
197198
}
198199
model, err = impl.gitOpsRepository.CreateGitOpsConfig(model, tx)
@@ -403,6 +404,7 @@ func (impl *GitOpsConfigServiceImpl) UpdateGitOpsConfig(request *bean2.GitOpsCon
403404
model.AzureProject = request.AzureProjectName
404405
model.BitBucketWorkspaceId = request.BitBucketWorkspaceId
405406
model.BitBucketProjectKey = request.BitBucketProjectKey
407+
model.AllowInsecureTLS = request.AllowInsecureTLS
406408
err = impl.gitOpsRepository.UpdateGitOpsConfig(model, tx)
407409
if err != nil {
408410
impl.logger.Errorw("error in updating team", "data", model, "err", err)
@@ -699,7 +701,7 @@ func (impl *GitOpsConfigServiceImpl) GitOpsValidateDryRun(config *bean2.GitOpsCo
699701
return detailedErrorGitOpsConfigResponse
700702
}
701703
appName := DryrunRepoName + util2.Generate(6)
702-
//getting user name & emailId for commit author data
704+
//getting username & emailId for commit author data
703705
userEmailId, userName := impl.chartTemplateService.GetUserEmailIdAndNameForGitOpsCommit(config.UserId)
704706
repoUrl, _, detailedErrorCreateRepo := client.CreateRepository(appName, "sample dry-run repo", userName, userEmailId)
705707

0 commit comments

Comments
 (0)