From e0269c820b39676b550f9afe9166301b554394a0 Mon Sep 17 00:00:00 2001 From: YashasviDevtron <155513200+YashasviDevtron@users.noreply.github.com> Date: Mon, 23 Sep 2024 10:09:51 +0000 Subject: [PATCH] fix: sync with changes from YashasviDevtron-patch-1 of common-lib This PR was created by multi-gitter to sync common-lib changes across multiple repositories. --- go.mod | 2 +- go.sum | 4 +- .../common-lib/git-manager/util/Util.go | 151 ++++++++++++++++++ .../common-lib/utils/CommonUtils.go | 35 ++++ .../common-lib/utils/bean/bean.go | 63 ++++++++ vendor/modules.txt | 4 +- 6 files changed, 255 insertions(+), 4 deletions(-) create mode 100644 vendor/github.com/devtron-labs/common-lib/git-manager/util/Util.go create mode 100644 vendor/github.com/devtron-labs/common-lib/utils/bean/bean.go diff --git a/go.mod b/go.mod index 88598dd4..5fd33b43 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/aws/aws-sdk-go v1.44.116 github.com/caarlos0/env v3.5.0+incompatible github.com/caarlos0/env/v6 v6.10.1 - github.com/devtron-labs/common-lib v0.16.1-0.20240904133334-7918e7c25b63 + github.com/devtron-labs/common-lib v0.18.1-0.20240923100800-5ddd15253061 github.com/go-pg/pg v6.15.1+incompatible github.com/google/go-containerregistry v0.6.0 github.com/google/wire v0.6.0 diff --git a/go.sum b/go.sum index 7b37412a..6832a394 100644 --- a/go.sum +++ b/go.sum @@ -280,8 +280,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denverdino/aliyungo v0.0.0-20190125010748-a747050bb1ba/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0= -github.com/devtron-labs/common-lib v0.16.1-0.20240904133334-7918e7c25b63 h1:C5SMozwP2rVIKItqEZs3PtWkBhNnEeHIm9xtnDkK5VA= -github.com/devtron-labs/common-lib v0.16.1-0.20240904133334-7918e7c25b63/go.mod h1:rAY9Xd6iz+OqNQ3nO3reVHapAVr1N6Osf4Irdc0A08Q= +github.com/devtron-labs/common-lib v0.18.1-0.20240923100800-5ddd15253061 h1:EWFREGVj8nHi456MUeBWpTJjPIjV3i0LQxGI0JZ/6yA= +github.com/devtron-labs/common-lib v0.18.1-0.20240923100800-5ddd15253061/go.mod h1:rAY9Xd6iz+OqNQ3nO3reVHapAVr1N6Osf4Irdc0A08Q= github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= diff --git a/vendor/github.com/devtron-labs/common-lib/git-manager/util/Util.go b/vendor/github.com/devtron-labs/common-lib/git-manager/util/Util.go new file mode 100644 index 00000000..c438c19a --- /dev/null +++ b/vendor/github.com/devtron-labs/common-lib/git-manager/util/Util.go @@ -0,0 +1,151 @@ +/* + * 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 util + +import ( + "errors" + "fmt" + "io/ioutil" + "log" + "math/rand" + "net/url" + "os" + "path" + "strings" + "time" +) + +const ( + SSH_PRIVATE_KEY_DIR = ".ssh" + SSH_PRIVATE_KEY_FILE_NAME = "id_rsa" + GIT_CREDENTIAL_FILE_NAME = ".git-credentials" +) + +func CreateSshPrivateKeyOnDisk(fileId int, sshPrivateKeyContent string) error { + + userHomeDirectory, err := os.UserHomeDir() + if err != nil { + return err + } + + sshPrivateKeyFilePath := path.Join(userHomeDirectory, SSH_PRIVATE_KEY_DIR, SSH_PRIVATE_KEY_FILE_NAME) + + // if file exists then delete file + if _, err := os.Stat(sshPrivateKeyFilePath); os.IsExist(err) { + os.Remove(sshPrivateKeyFilePath) + } + + // create file with content + err = ioutil.WriteFile(sshPrivateKeyFilePath, []byte(sshPrivateKeyContent), 0600) + if err != nil { + return err + } + + return nil +} + +func CreateGitCredentialFileAndWriteData(data string) error { + + userHomeDirectory, err := os.UserHomeDir() + if err != nil { + return err + } + + fileName := path.Join(userHomeDirectory, GIT_CREDENTIAL_FILE_NAME) + + // if file exists then delete file + if _, err := os.Stat(fileName); os.IsExist(err) { + os.Remove(fileName) + } + + // create file with content + err = ioutil.WriteFile(fileName, []byte(data), 0600) + if err != nil { + return err + } + + return nil +} + +func CleanupAfterFetchingHttpsSubmodules() error { + + userHomeDirectory, err := os.UserHomeDir() + if err != nil { + return err + } + + // remove ~/.git-credentials + gitCredentialsFile := path.Join(userHomeDirectory, GIT_CREDENTIAL_FILE_NAME) + if _, err := os.Stat(gitCredentialsFile); os.IsExist(err) { + os.Remove(gitCredentialsFile) + } + + return nil +} + +func LogStage(name string) { + stageTemplate := ` +------------------------------------------------------------------------------------------------------------------------ +STAGE: %s +------------------------------------------------------------------------------------------------------------------------` + log.Println(fmt.Sprintf(stageTemplate, name)) +} + +var chars = []rune("abcdefghijklmnopqrstuvwxyz0123456789") + +// Generates random string +func Generate(size int) string { + rand.Seed(time.Now().UnixNano()) + var b strings.Builder + for i := 0; i < size; i++ { + b.WriteRune(chars[rand.Intn(len(chars))]) + } + str := b.String() + return str +} + +// CheckFileExists returns boolean value of file existence else error (ignoring file does not exist error) +func CheckFileExists(filename string) (bool, error) { + if _, err := os.Stat(filename); err == nil { + // exists + return true, nil + } else if errors.Is(err, os.ErrNotExist) { + // not exists + return false, nil + } else { + // Some other error + return false, err + } +} +func ParseUrl(rawURL string) (parsedURL *url.URL, err error) { + parsedURL, err = url.Parse(rawURL) + if err != nil || parsedURL.Host == "" { + parsedURL, err = url.Parse("//" + rawURL) + } + return parsedURL, err +} + +// GetProjectName this function has been designed for returning project name of git-lab and git-hub providers only +// do not remove this function +func GetProjectName(url string) string { + //if url = https://github.com/devtron-labs/git-sensor.git then it will return git-sensor + projName := strings.Split(url, ".")[1] + projectName := projName[strings.LastIndex(projName, "/")+1:] + return projectName +} + +const DEVTRON = "DEVTRON" diff --git a/vendor/github.com/devtron-labs/common-lib/utils/CommonUtils.go b/vendor/github.com/devtron-labs/common-lib/utils/CommonUtils.go index 671e4502..95c0f3b7 100644 --- a/vendor/github.com/devtron-labs/common-lib/utils/CommonUtils.go +++ b/vendor/github.com/devtron-labs/common-lib/utils/CommonUtils.go @@ -18,13 +18,20 @@ package utils import ( "fmt" + "github.com/devtron-labs/common-lib/git-manager/util" + "github.com/devtron-labs/common-lib/utils/bean" + "log" "math/rand" + "path" + "regexp" "strings" "time" ) var chars = []rune("abcdefghijklmnopqrstuvwxyz0123456789") +const DOCKER_REGISTRY_TYPE_DOCKERHUB = "docker-hub" + // Generates random string func Generate(size int) string { rand.Seed(time.Now().UnixNano()) @@ -47,3 +54,31 @@ func GetUrlWithScheme(url string) (urlWithScheme string) { } return urlWithScheme } + +func IsValidDockerTagName(tagName string) bool { + regString := "^[a-zA-Z0-9_][a-zA-Z0-9_.-]{0,127}$" + regexpCompile := regexp.MustCompile(regString) + if regexpCompile.MatchString(tagName) { + return true + } else { + return false + } +} + +func BuildDockerImagePath(dockerInfo bean.DockerRegistryInfo) (string, error) { + dest := "" + if DOCKER_REGISTRY_TYPE_DOCKERHUB == dockerInfo.DockerRegistryType { + dest = dockerInfo.DockerRepository + ":" + dockerInfo.DockerImageTag + } else { + registryUrl := dockerInfo.DockerRegistryURL + u, err := util.ParseUrl(registryUrl) + if err != nil { + log.Println("not a valid docker repository url") + return "", err + } + u.Path = path.Join(u.Path, "/", dockerInfo.DockerRepository) + dockerRegistryURL := u.Host + u.Path + dest = dockerRegistryURL + ":" + dockerInfo.DockerImageTag + } + return dest, nil +} diff --git a/vendor/github.com/devtron-labs/common-lib/utils/bean/bean.go b/vendor/github.com/devtron-labs/common-lib/utils/bean/bean.go new file mode 100644 index 00000000..11a4015f --- /dev/null +++ b/vendor/github.com/devtron-labs/common-lib/utils/bean/bean.go @@ -0,0 +1,63 @@ +/* + * Copyright (c) 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 bean + +import ( + "encoding/base64" + "encoding/json" + "github.com/docker/cli/cli/config/types" +) + +const ( + YamlSeparator string = "---\n" + RegistryTypeGcr = "gcr" + RegistryTypeEcr = "ecr" + GcrRegistryUsername = "oauth2accesstoken" + GcrRegistryScope = "https://www.googleapis.com/auth/cloud-platform" +) + +type DockerAuthConfig struct { + RegistryType string // can be ecr, gcr, docker-hub, harbor etc. + Username string + Password string + AccessKeyEcr string // used for pulling from private ecr registry + SecretAccessKeyEcr string // used for pulling from private ecr registry + EcrRegion string // used for pulling from private ecr registry + CredentialFileJsonGcr string // used for pulling from private gcr registry + IsRegistryPrivate bool +} + +func (r *DockerAuthConfig) GetEncodedRegistryAuth() (string, error) { + // Create and encode the auth config + authConfig := types.AuthConfig{ + Username: r.Username, + Password: r.Password, + } + encodedJSON, err := json.Marshal(authConfig) + if err != nil { + return "", err + } + return base64.StdEncoding.EncodeToString(encodedJSON), nil +} + +type DockerRegistryInfo struct { + DockerImageTag string `json:"dockerImageTag"` + DockerRegistryId string `json:"dockerRegistryId"` + DockerRegistryType string `json:"dockerRegistryType"` + DockerRegistryURL string `json:"dockerRegistryURL"` + DockerRepository string `json:"dockerRepository"` +} diff --git a/vendor/modules.txt b/vendor/modules.txt index f364242a..535f6715 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -72,9 +72,10 @@ github.com/cespare/xxhash/v2 github.com/coreos/clair/api/v3/clairpb github.com/coreos/clair/database github.com/coreos/clair/ext/versionfmt -# github.com/devtron-labs/common-lib v0.16.1-0.20240904133334-7918e7c25b63 +# github.com/devtron-labs/common-lib v0.18.1-0.20240923100800-5ddd15253061 ## explicit; go 1.21 github.com/devtron-labs/common-lib/constants +github.com/devtron-labs/common-lib/git-manager/util github.com/devtron-labs/common-lib/middlewares github.com/devtron-labs/common-lib/monitoring github.com/devtron-labs/common-lib/monitoring/pprof @@ -83,6 +84,7 @@ github.com/devtron-labs/common-lib/pubsub-lib github.com/devtron-labs/common-lib/pubsub-lib/metrics github.com/devtron-labs/common-lib/pubsub-lib/model github.com/devtron-labs/common-lib/utils +github.com/devtron-labs/common-lib/utils/bean github.com/devtron-labs/common-lib/utils/remoteConnection/bean # github.com/docker/cli v24.0.6+incompatible ## explicit