github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/utils/utils.go (about)

     1  package utils
     2  
     3  import (
     4  	"errors"
     5  	"github.com/jfrog/jfrog-client-go/utils/io"
     6  	"net/http"
     7  	"net/url"
     8  	"os"
     9  	"path"
    10  	"path/filepath"
    11  
    12  	"github.com/jfrog/jfrog-cli-go/utils/cliutils"
    13  	"github.com/jfrog/jfrog-cli-go/utils/config"
    14  	"github.com/jfrog/jfrog-client-go/artifactory"
    15  	"github.com/jfrog/jfrog-client-go/auth"
    16  	clientConfig "github.com/jfrog/jfrog-client-go/config"
    17  	"github.com/jfrog/jfrog-client-go/distribution"
    18  	"github.com/jfrog/jfrog-client-go/httpclient"
    19  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    20  )
    21  
    22  const repoDetailsUrl = "api/repositories/"
    23  
    24  func GetProjectDir(global bool) (string, error) {
    25  	configDir, err := getConfigDir(global)
    26  	if err != nil {
    27  		return "", errorutils.CheckError(err)
    28  	}
    29  	return filepath.Join(configDir, "projects"), nil
    30  }
    31  
    32  func getConfigDir(global bool) (string, error) {
    33  	if !global {
    34  		wd, err := os.Getwd()
    35  		if err != nil {
    36  			return "", err
    37  		}
    38  		return filepath.Join(wd, ".jfrog"), nil
    39  	}
    40  	return cliutils.GetJfrogHomeDir()
    41  }
    42  
    43  func GetEncryptedPasswordFromArtifactory(artifactoryAuth auth.CommonDetails, insecureTls bool) (string, error) {
    44  	u, err := url.Parse(artifactoryAuth.GetUrl())
    45  	if err != nil {
    46  		return "", err
    47  	}
    48  	u.Path = path.Join(u.Path, "api/security/encryptedPassword")
    49  	httpClientsDetails := artifactoryAuth.CreateHttpClientDetails()
    50  	securityDir, err := cliutils.GetJfrogSecurityDir()
    51  	if err != nil {
    52  		return "", err
    53  	}
    54  	client, err := httpclient.ClientBuilder().
    55  		SetCertificatesPath(securityDir).
    56  		SetInsecureTls(insecureTls).
    57  		SetClientCertPath(artifactoryAuth.GetClientCertPath()).
    58  		SetClientCertKeyPath(artifactoryAuth.GetClientCertKeyPath()).
    59  		Build()
    60  	if err != nil {
    61  		return "", err
    62  	}
    63  	resp, body, _, err := client.SendGet(u.String(), true, httpClientsDetails)
    64  	if err != nil {
    65  		return "", err
    66  	}
    67  
    68  	if resp.StatusCode == http.StatusOK {
    69  		return string(body), nil
    70  	}
    71  
    72  	if resp.StatusCode == http.StatusConflict {
    73  		message := "\nYour Artifactory server is not configured to encrypt passwords.\n" +
    74  			"You may use \"art config --enc-password=false\""
    75  		return "", errorutils.CheckError(errors.New(message))
    76  	}
    77  
    78  	return "", errorutils.CheckError(errors.New("Artifactory response: " + resp.Status))
    79  }
    80  
    81  func CreateServiceManager(artDetails *config.ArtifactoryDetails, isDryRun bool) (*artifactory.ArtifactoryServicesManager, error) {
    82  	return CreateServiceManagerWithThreads(artDetails, isDryRun, 0)
    83  }
    84  
    85  func CreateServiceManagerWithThreads(artDetails *config.ArtifactoryDetails, isDryRun bool, threads int) (*artifactory.ArtifactoryServicesManager, error) {
    86  	certPath, err := cliutils.GetJfrogSecurityDir()
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  	artAuth, err := artDetails.CreateArtAuthConfig()
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	config := clientConfig.NewConfigBuilder().
    95  		SetArtDetails(artAuth).
    96  		SetCertificatesPath(certPath).
    97  		SetInsecureTls(artDetails.InsecureTls).
    98  		SetDryRun(isDryRun)
    99  	if threads > 0 {
   100  		config.SetThreads(threads)
   101  	}
   102  	serviceConfig, err := config.Build()
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	return artifactory.New(&artAuth, serviceConfig)
   107  }
   108  
   109  func CreateServiceManagerWithProgressBar(artDetails *config.ArtifactoryDetails, threads int, dryRun bool, progressBar io.Progress) (*artifactory.ArtifactoryServicesManager, error) {
   110  	certPath, err := cliutils.GetJfrogSecurityDir()
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  	artAuth, err := artDetails.CreateArtAuthConfig()
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	servicesConfig, err := clientConfig.NewConfigBuilder().
   119  		SetArtDetails(artAuth).
   120  		SetDryRun(dryRun).
   121  		SetCertificatesPath(certPath).
   122  		SetInsecureTls(artDetails.InsecureTls).
   123  		SetThreads(threads).
   124  		Build()
   125  
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  	return artifactory.NewWithProgress(&artAuth, servicesConfig, progressBar)
   130  }
   131  
   132  func CreateDistributionServiceManager(artDetails *config.ArtifactoryDetails, isDryRun bool) (*distribution.DistributionServicesManager, error) {
   133  	certPath, err := cliutils.GetJfrogSecurityDir()
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	distAuth, err := artDetails.CreateDistAuthConfig()
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  	serviceConfig, err := clientConfig.NewConfigBuilder().
   142  		SetArtDetails(distAuth).
   143  		SetCertificatesPath(certPath).
   144  		SetInsecureTls(artDetails.InsecureTls).
   145  		SetDryRun(isDryRun).
   146  		Build()
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  	return distribution.New(&distAuth, serviceConfig)
   151  }
   152  
   153  func isRepoExists(repository string, artDetails auth.CommonDetails) (bool, error) {
   154  	artHttpDetails := artDetails.CreateHttpClientDetails()
   155  	client, err := httpclient.ClientBuilder().Build()
   156  	if err != nil {
   157  		return false, err
   158  	}
   159  	resp, _, _, err := client.SendGet(artDetails.GetUrl()+repoDetailsUrl+repository, true, artHttpDetails)
   160  	if err != nil {
   161  		return false, errorutils.CheckError(err)
   162  	}
   163  
   164  	if resp.StatusCode != http.StatusBadRequest {
   165  		return true, nil
   166  	}
   167  	return false, nil
   168  }
   169  
   170  func CheckIfRepoExists(repository string, artDetails auth.CommonDetails) error {
   171  	repoExists, err := isRepoExists(repository, artDetails)
   172  	if err != nil {
   173  		return err
   174  	}
   175  
   176  	if !repoExists {
   177  		return errorutils.CheckError(errors.New("The repository '" + repository + "' does not exist."))
   178  	}
   179  	return nil
   180  }
   181  
   182  // Get build name and number from env, only if both missing
   183  func GetBuildNameAndNumber(buildName, buildNumber string) (string, string) {
   184  	if buildName != "" || buildNumber != "" {
   185  		return buildName, buildNumber
   186  	}
   187  	return os.Getenv(cliutils.BuildName), os.Getenv(cliutils.BuildNumber)
   188  }
   189  
   190  func GetBuildName(buildName string) string {
   191  	return getOrDefaultEnv(buildName, cliutils.BuildName)
   192  }
   193  
   194  func GetBuildUrl(buildUrl string) string {
   195  	return getOrDefaultEnv(buildUrl, cliutils.BuildUrl)
   196  }
   197  
   198  func GetEnvExclude(envExclude string) string {
   199  	return getOrDefaultEnv(envExclude, cliutils.EnvExclude)
   200  }
   201  
   202  // Return argument if not empty or retrieve from environment variable
   203  func getOrDefaultEnv(arg, envKey string) string {
   204  	if arg != "" {
   205  		return arg
   206  	}
   207  	return os.Getenv(envKey)
   208  }
   209  
   210  // This error indicates that the build was scanned by Xray, but Xray found issues with the build.
   211  // If Xray failed to scan the build, for example due to a networking issue, a regular error should be returned.
   212  var buildScanError = errors.New("issues found during xray build scan")
   213  
   214  func GetBuildScanError() error {
   215  	return buildScanError
   216  }