github.com/jfrog/jfrog-cli-core/v2@v2.52.0/artifactory/utils/utils.go (about) 1 package utils 2 3 import ( 4 "context" 5 "encoding/json" 6 "errors" 7 ioutils "github.com/jfrog/gofrog/io" 8 "io" 9 "net/http" 10 "net/url" 11 "os" 12 "path" 13 "path/filepath" 14 "time" 15 16 "github.com/jfrog/jfrog-cli-core/v2/utils/config" 17 "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" 18 "github.com/jfrog/jfrog-client-go/access" 19 "github.com/jfrog/jfrog-client-go/artifactory" 20 "github.com/jfrog/jfrog-client-go/auth" 21 clientConfig "github.com/jfrog/jfrog-client-go/config" 22 "github.com/jfrog/jfrog-client-go/distribution" 23 "github.com/jfrog/jfrog-client-go/http/httpclient" 24 "github.com/jfrog/jfrog-client-go/lifecycle" 25 clientUtils "github.com/jfrog/jfrog-client-go/utils" 26 "github.com/jfrog/jfrog-client-go/utils/errorutils" 27 ioUtils "github.com/jfrog/jfrog-client-go/utils/io" 28 ) 29 30 func GetProjectDir(global bool) (string, error) { 31 configDir, err := getConfigDir(global) 32 if err != nil { 33 return "", errorutils.CheckError(err) 34 } 35 return filepath.Join(configDir, "projects"), nil 36 } 37 38 func getConfigDir(global bool) (string, error) { 39 if !global { 40 wd, err := os.Getwd() 41 if err != nil { 42 return "", err 43 } 44 return filepath.Join(wd, ".jfrog"), nil 45 } 46 return coreutils.GetJfrogHomeDir() 47 } 48 49 func GetEncryptedPasswordFromArtifactory(artifactoryAuth auth.ServiceDetails, insecureTls bool) (string, error) { 50 u, err := url.Parse(artifactoryAuth.GetUrl()) 51 if err != nil { 52 return "", err 53 } 54 u.Path = path.Join(u.Path, "api/security/encryptedPassword") 55 httpClientsDetails := artifactoryAuth.CreateHttpClientDetails() 56 certsPath, err := coreutils.GetJfrogCertsDir() 57 if err != nil { 58 return "", err 59 } 60 client, err := httpclient.ClientBuilder(). 61 SetCertificatesPath(certsPath). 62 SetInsecureTls(insecureTls). 63 SetClientCertPath(artifactoryAuth.GetClientCertPath()). 64 SetClientCertKeyPath(artifactoryAuth.GetClientCertKeyPath()). 65 Build() 66 if err != nil { 67 return "", err 68 } 69 resp, body, _, err := client.SendGet(u.String(), true, httpClientsDetails, "") 70 if err != nil { 71 return "", err 72 } 73 74 if resp.StatusCode == http.StatusOK { 75 return string(body), nil 76 } 77 78 if resp.StatusCode == http.StatusConflict { 79 message := "\nYour Artifactory server is not configured to encrypt passwords.\n" + 80 "You may use \"art config --enc-password=false\"" 81 return "", errorutils.CheckErrorf(message) 82 } 83 84 return "", errorutils.CheckErrorf("Artifactory response: " + resp.Status + "\n" + clientUtils.IndentJson(body)) 85 } 86 87 func CreateServiceManager(serverDetails *config.ServerDetails, httpRetries, httpRetryWaitMilliSecs int, isDryRun bool) (artifactory.ArtifactoryServicesManager, error) { 88 return CreateServiceManagerWithContext(context.Background(), serverDetails, isDryRun, 0, httpRetries, httpRetryWaitMilliSecs, 0) 89 } 90 91 // Create a service manager with threads. 92 // If the value sent for httpRetries is negative, the default will be used. 93 func CreateServiceManagerWithThreads(serverDetails *config.ServerDetails, isDryRun bool, threads, httpRetries, httpRetryWaitMilliSecs int) (artifactory.ArtifactoryServicesManager, error) { 94 return CreateServiceManagerWithContext(context.Background(), serverDetails, isDryRun, threads, httpRetries, httpRetryWaitMilliSecs, 0) 95 } 96 97 func CreateServiceManagerWithContext(context context.Context, serverDetails *config.ServerDetails, isDryRun bool, threads, httpRetries, httpRetryWaitMilliSecs int, timeout time.Duration) (artifactory.ArtifactoryServicesManager, error) { 98 certsPath, err := coreutils.GetJfrogCertsDir() 99 if err != nil { 100 return nil, err 101 } 102 artAuth, err := serverDetails.CreateArtAuthConfig() 103 if err != nil { 104 return nil, err 105 } 106 configBuilder := clientConfig.NewConfigBuilder(). 107 SetServiceDetails(artAuth). 108 SetCertificatesPath(certsPath). 109 SetInsecureTls(serverDetails.InsecureTls). 110 SetDryRun(isDryRun). 111 SetContext(context) 112 if httpRetries >= 0 { 113 configBuilder.SetHttpRetries(httpRetries) 114 configBuilder.SetHttpRetryWaitMilliSecs(httpRetryWaitMilliSecs) 115 } 116 if threads > 0 { 117 configBuilder.SetThreads(threads) 118 } 119 if timeout > 0 { 120 configBuilder.SetOverallRequestTimeout(timeout) 121 } 122 serviceConfig, err := configBuilder.Build() 123 if err != nil { 124 return nil, err 125 } 126 return artifactory.New(serviceConfig) 127 } 128 129 func CreateServiceManagerWithProgressBar(serverDetails *config.ServerDetails, threads, httpRetries, httpRetryWaitMilliSecs int, dryRun bool, progressBar ioUtils.ProgressMgr) (artifactory.ArtifactoryServicesManager, error) { 130 certsPath, err := coreutils.GetJfrogCertsDir() 131 if err != nil { 132 return nil, err 133 } 134 artAuth, err := serverDetails.CreateArtAuthConfig() 135 if err != nil { 136 return nil, err 137 } 138 servicesConfig, err := clientConfig.NewConfigBuilder(). 139 SetServiceDetails(artAuth). 140 SetDryRun(dryRun). 141 SetCertificatesPath(certsPath). 142 SetInsecureTls(serverDetails.InsecureTls). 143 SetThreads(threads). 144 SetHttpRetries(httpRetries). 145 SetHttpRetryWaitMilliSecs(httpRetryWaitMilliSecs). 146 Build() 147 148 if err != nil { 149 return nil, err 150 } 151 return artifactory.NewWithProgress(servicesConfig, progressBar) 152 } 153 154 func CreateDistributionServiceManager(serviceDetails *config.ServerDetails, isDryRun bool) (*distribution.DistributionServicesManager, error) { 155 certsPath, err := coreutils.GetJfrogCertsDir() 156 if err != nil { 157 return nil, err 158 } 159 distAuth, err := serviceDetails.CreateDistAuthConfig() 160 if err != nil { 161 return nil, err 162 } 163 serviceConfig, err := clientConfig.NewConfigBuilder(). 164 SetServiceDetails(distAuth). 165 SetCertificatesPath(certsPath). 166 SetInsecureTls(serviceDetails.InsecureTls). 167 SetDryRun(isDryRun). 168 Build() 169 if err != nil { 170 return nil, err 171 } 172 return distribution.New(serviceConfig) 173 } 174 175 func CreateAccessServiceManager(serviceDetails *config.ServerDetails, isDryRun bool) (*access.AccessServicesManager, error) { 176 certsPath, err := coreutils.GetJfrogCertsDir() 177 if err != nil { 178 return nil, err 179 } 180 accessAuth, err := serviceDetails.CreateAccessAuthConfig() 181 if err != nil { 182 return nil, err 183 } 184 serviceConfig, err := clientConfig.NewConfigBuilder(). 185 SetServiceDetails(accessAuth). 186 SetCertificatesPath(certsPath). 187 SetInsecureTls(serviceDetails.InsecureTls). 188 SetDryRun(isDryRun). 189 Build() 190 if err != nil { 191 return nil, err 192 } 193 return access.New(serviceConfig) 194 } 195 196 func CreateLifecycleServiceManager(serviceDetails *config.ServerDetails, isDryRun bool) (*lifecycle.LifecycleServicesManager, error) { 197 certsPath, err := coreutils.GetJfrogCertsDir() 198 if err != nil { 199 return nil, err 200 } 201 lcAuth, err := serviceDetails.CreateLifecycleAuthConfig() 202 if err != nil { 203 return nil, err 204 } 205 serviceConfig, err := clientConfig.NewConfigBuilder(). 206 SetServiceDetails(lcAuth). 207 SetCertificatesPath(certsPath). 208 SetInsecureTls(serviceDetails.InsecureTls). 209 SetDryRun(isDryRun). 210 Build() 211 if err != nil { 212 return nil, err 213 } 214 return lifecycle.New(serviceConfig) 215 } 216 217 // This error indicates that the build was scanned by Xray, but Xray found issues with the build. 218 // If Xray failed to scan the build, for example due to a networking issue, a regular error should be returned. 219 var errBuildScan = errors.New("issues found during xray build scan") 220 221 func GetBuildScanError() error { 222 return errBuildScan 223 } 224 225 // Download and unmarshal a file from artifactory. 226 func RemoteUnmarshal(serviceManager artifactory.ArtifactoryServicesManager, remoteFileUrl string, loadTarget interface{}) (err error) { 227 ioReaderCloser, err := serviceManager.ReadRemoteFile(remoteFileUrl) 228 if err != nil { 229 return 230 } 231 defer ioutils.Close(ioReaderCloser, &err) 232 content, err := io.ReadAll(ioReaderCloser) 233 if err != nil { 234 return errorutils.CheckError(err) 235 } 236 return errorutils.CheckError(json.Unmarshal(content, loadTarget)) 237 } 238 239 // Returns an error if the given repo doesn't exist. 240 func ValidateRepoExists(repoKey string, serviceDetails auth.ServiceDetails) error { 241 servicesManager, err := createServiceManager(serviceDetails) 242 if err != nil { 243 return err 244 } 245 exists, err := servicesManager.IsRepoExists(repoKey) 246 if err != nil { 247 return err 248 } 249 250 if !exists { 251 return errorutils.CheckErrorf("The repository '" + repoKey + "' does not exist.") 252 } 253 return nil 254 } 255 256 func createServiceManager(serviceDetails auth.ServiceDetails) (artifactory.ArtifactoryServicesManager, error) { 257 certsPath, err := coreutils.GetJfrogCertsDir() 258 if err != nil { 259 return nil, err 260 } 261 serviceConfig, err := clientConfig.NewConfigBuilder(). 262 SetServiceDetails(serviceDetails). 263 SetCertificatesPath(certsPath). 264 SetDryRun(false). 265 Build() 266 if err != nil { 267 return nil, err 268 } 269 return artifactory.New(serviceConfig) 270 } 271 272 func GetTestDataPath() (string, error) { 273 dir, err := os.Getwd() 274 if err != nil { 275 return "", errorutils.CheckError(err) 276 } 277 return filepath.Join(dir, "testdata"), nil 278 }