github.com/jfrog/jfrog-cli-core@v1.12.1/artifactory/utils/dependenciesutils.go (about) 1 package utils 2 3 import ( 4 "errors" 5 "fmt" 6 "net/http" 7 "os" 8 "path" 9 "strings" 10 11 "github.com/jfrog/jfrog-cli-core/utils/config" 12 "github.com/jfrog/jfrog-cli-core/utils/coreutils" 13 "github.com/jfrog/jfrog-client-go/http/httpclient" 14 "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" 15 "github.com/jfrog/jfrog-client-go/utils/errorutils" 16 "github.com/jfrog/jfrog-client-go/utils/io/fileutils" 17 "github.com/jfrog/jfrog-client-go/utils/log" 18 ) 19 20 const ( 21 // This env var should be used for downloading the extractor jars through an Artifactory remote 22 // repository, instead of downloading directly from ojo. The remote repository should be 23 // configured to proxy ojo. 24 25 // Deprecated. This env var should store a server ID configured by JFrog CLI. 26 JCenterRemoteServerEnv = "JFROG_CLI_JCENTER_REMOTE_SERVER" 27 // Deprecated. If the JCenterRemoteServerEnv env var is used, a maven remote repository named jcenter is assumed. 28 // This env var can be used to use a different remote repository name. 29 JCenterRemoteRepoEnv = "JFROG_CLI_JCENTER_REMOTE_REPO" 30 31 // This env var should store a server ID and a remote repository in form of '<ServerID>/<RemoteRepo>' 32 ExtractorsRemoteEnv = "JFROG_CLI_EXTRACTORS_REMOTE" 33 ) 34 35 // Download the relevant build-info-extractor jar, if it does not already exist locally. 36 // By default, the jar is downloaded directly from jcenter. 37 // If the JCenterRemoteServerEnv environment variable is configured, the jar will be 38 // downloaded from a remote Artifactory repository which proxies jcenter. 39 // 40 // downloadPath: The Bintray or Artifactory download path. 41 // filename: The local file name. 42 // targetPath: The local download path (without the file name). 43 func DownloadExtractorIfNeeded(downloadPath, targetPath string) error { 44 // If the file exists locally, we're done. 45 exists, err := fileutils.IsFileExists(targetPath, false) 46 if exists || err != nil { 47 return err 48 } 49 log.Info("The build-info-extractor jar is not cached locally. Downloading it now...\n You can set the repository from which this jar is downloaded. Read more about it at https://www.jfrog.com/confluence/display/CLI/CLI+for+JFrog+Artifactory#CLIforJFrogArtifactory-DownloadingtheMavenandGradleExtractorJARs") 50 artDetails, remotePath, err := GetExtractorsRemoteDetails(downloadPath) 51 if err != nil { 52 return err 53 } 54 55 return downloadExtractor(artDetails, remotePath, targetPath) 56 } 57 58 func GetExtractorsRemoteDetails(downloadPath string) (*config.ServerDetails, string, error) { 59 // Download through a remote repository in Artifactory, if configured to do so. 60 jCenterRemoteServer := os.Getenv(JCenterRemoteServerEnv) 61 if jCenterRemoteServer != "" { 62 return getJcenterRemoteDetails(jCenterRemoteServer, downloadPath) 63 } 64 extractorsRemote := os.Getenv(ExtractorsRemoteEnv) 65 if extractorsRemote != "" { 66 return getExtractorsRemoteDetails(extractorsRemote, downloadPath) 67 } 68 69 log.Debug("'" + ExtractorsRemoteEnv + "' environment variable is not configured. Downloading directly from oss.jfrog.org.") 70 // If not configured to download through a remote repository in Artifactory, download from ojo. 71 return &config.ServerDetails{ArtifactoryUrl: "https://oss.jfrog.org/artifactory/"}, path.Join("oss-release-local", downloadPath), nil 72 } 73 74 // Deprecated. Return the version of the build-info extractor to download. 75 // If 'JFROG_CLI_JCENTER_REMOTE_SERVER' is used, choose the latest published JCenter version. 76 func GetExtractorVersion(ojoVersion, jCenterVersion string) string { 77 if os.Getenv(JCenterRemoteServerEnv) != "" { 78 return jCenterVersion 79 } 80 return ojoVersion 81 } 82 83 // Deprecated. Get Artifactory server details and a repository proxying JCenter/oss.jfrog.org according to 'JFROG_CLI_JCENTER_REMOTE_SERVER' and 'JFROG_CLI_JCENTER_REMOTE_REPO' env vars. 84 func getJcenterRemoteDetails(serverId, downloadPath string) (*config.ServerDetails, string, error) { 85 log.Warn(`It looks like the 'JFROG_CLI_JCENTER_REMOTE_SERVER' or 'JFROG_CLI_JCENTER_REMOTE_REPO' environment variables are set. 86 These environment variables were used by the JFrog CLI to download the build-info extractors JARs for Maven and Gradle builds. 87 These environment variables are now deprecated. 88 For more information, please refer to the documentation at https://www.jfrog.com/confluence/display/CLI/CLI+for+JFrog+Artifactory#CLIforJFrogArtifactory-DownloadingtheMavenandGradleExtractorJARs.`) 89 serverDetails, err := config.GetSpecificConfig(serverId, false, true) 90 repoName := os.Getenv(JCenterRemoteRepoEnv) 91 if repoName == "" { 92 repoName = "jcenter" 93 } 94 return serverDetails, path.Join(repoName, downloadPath), err 95 } 96 97 // Get Artifactory server details and a repository proxying oss.jfrog.org according to JFROG_CLI_EXTRACTORS_REMOTE env var. 98 func getExtractorsRemoteDetails(extractorsRemote, downloadPath string) (*config.ServerDetails, string, error) { 99 lastSlashIndex := strings.LastIndex(extractorsRemote, "/") 100 if lastSlashIndex == -1 { 101 return nil, "", errorutils.CheckError(errors.New(fmt.Sprintf("'%s' environment variable is '%s' but should be '<server ID>/<repo name>'.", ExtractorsRemoteEnv, extractorsRemote))) 102 } 103 104 serverDetails, err := config.GetSpecificConfig(extractorsRemote[:lastSlashIndex], false, true) 105 repoName := extractorsRemote[lastSlashIndex+1:] 106 return serverDetails, path.Join(repoName, downloadPath), err 107 } 108 109 func downloadExtractor(artDetails *config.ServerDetails, downloadPath, targetPath string) error { 110 downloadUrl := fmt.Sprintf("%s%s", artDetails.ArtifactoryUrl, downloadPath) 111 log.Info("Downloading build-info-extractor from", downloadUrl) 112 filename, localDir := fileutils.GetFileAndDirFromPath(targetPath) 113 114 downloadFileDetails := &httpclient.DownloadFileDetails{ 115 FileName: filename, 116 DownloadPath: downloadUrl, 117 LocalPath: localDir, 118 LocalFileName: filename, 119 } 120 121 auth, err := artDetails.CreateArtAuthConfig() 122 if err != nil { 123 return err 124 } 125 certsPath, err := coreutils.GetJfrogCertsDir() 126 if err != nil { 127 return err 128 } 129 130 client, err := jfroghttpclient.JfrogClientBuilder(). 131 SetCertificatesPath(certsPath). 132 SetInsecureTls(artDetails.InsecureTls). 133 SetClientCertPath(auth.GetClientCertPath()). 134 SetClientCertKeyPath(auth.GetClientCertKeyPath()). 135 AppendPreRequestInterceptor(auth.RunPreRequestFunctions). 136 Build() 137 if err != nil { 138 return err 139 } 140 141 httpClientDetails := auth.CreateHttpClientDetails() 142 resp, err := client.DownloadFile(downloadFileDetails, "", &httpClientDetails, false) 143 if err == nil && resp.StatusCode != http.StatusOK { 144 err = errorutils.CheckError(errors.New(resp.Status + " received when attempting to download " + downloadUrl)) 145 } 146 147 return err 148 }