Пример #1
0
func createBintrayDetails(c *cli.Context, includeConfig bool) (*config.BintrayDetails, error) {
	if includeConfig {
		bintrayDetails, err := offerConfig(c)
		if err != nil {
			return nil, err
		}
		if bintrayDetails != nil {
			return bintrayDetails, nil
		}
	}
	user := c.String("user")
	key := c.String("key")
	defaultPackageLicenses := c.String("licenses")
	if includeConfig && (user == "" || key == "" || defaultPackageLicenses == "") {
		confDetails, err := commands.GetConfig()
		if err != nil {
			return nil, err
		}
		if user == "" {
			user = confDetails.User
		}
		if key == "" {
			key = confDetails.Key
		}
		if key == "" {
			cliutils.Exit(cliutils.ExitCodeError, "Please set your Bintray API key using the config command or send it as the --key option.")
		}
		if defaultPackageLicenses == "" {
			defaultPackageLicenses = confDetails.DefPackageLicenses
		}
	}
	apiUrl := os.Getenv("JFROG_CLI_BINTRAY_API_URL")
	if apiUrl == "" {
		apiUrl = "https://bintray.com/api/v1/"
	}
	downloadServerUrl := os.Getenv("JFROG_CLI_BINTRAY_DOWNLOAD_URL")
	if downloadServerUrl == "" {
		downloadServerUrl = "https://dl.bintray.com/"
	}
	apiUrl = cliutils.AddTrailingSlashIfNeeded(apiUrl)
	downloadServerUrl = cliutils.AddTrailingSlashIfNeeded(downloadServerUrl)
	return &config.BintrayDetails{
		ApiUrl:             apiUrl,
		DownloadServerUrl:  downloadServerUrl,
		User:               user,
		Key:                key,
		DefPackageLicenses: defaultPackageLicenses}, nil
}
Пример #2
0
func createMissionControlDetails(c *cli.Context, includeConfig bool) (*config.MissionControlDetails, error) {
	if includeConfig {
		details, err := offerConfig(c)
		if err != nil {
			return nil, err
		}
		if details != nil {
			return details, nil
		}
	}
	details := new(config.MissionControlDetails)
	details.Url = c.String("url")
	details.User = c.String("user")
	details.Password = c.String("password")

	if includeConfig {
		if details.Url == "" || details.User == "" || details.Password == "" {
			confDetails, err := commands.GetConfig()
			if err != nil {
				return nil, err
			}
			if details.Url == "" {
				details.Url = confDetails.Url
			}
			if details.User == "" {
				details.SetUser(confDetails.User)
			}
			if details.Password == "" {
				details.SetPassword(confDetails.Password)
			}
		}
	}
	details.Url = cliutils.AddTrailingSlashIfNeeded(details.Url)
	return details, nil
}
Пример #3
0
func Config(details, defaultDetails *config.MissionControlDetails, interactive bool) (conf *config.MissionControlDetails, err error) {
	conf = details
	if conf == nil {
		conf = new(config.MissionControlDetails)
	}
	if interactive {
		if defaultDetails == nil {
			defaultDetails, err = config.ReadMissionControlConf()
			if err != nil {
				return
			}
		}
		if conf.Url == "" {
			ioutils.ScanFromConsole("Mission Control URL", &conf.Url, defaultDetails.Url)
			var u *url.URL
			u, err = url.Parse(conf.Url)
			err = cliutils.CheckError(err)
			if err != nil {
				return
			}
			if u.Scheme != "http" && u.Scheme != "https" {
				err = cliutils.CheckError(errors.New("URL scheme is not valid " + u.Scheme))
				if err != nil {
					return
				}
			}
		}
		ioutils.ReadCredentialsFromConsole(conf, defaultDetails)
	}
	conf.Url = cliutils.AddTrailingSlashIfNeeded(conf.Url)
	config.SaveMissionControlConf(conf)
	return
}
Пример #4
0
func initBintrayCredentials() {
	if bintrayConfig != nil {
		return
	}

	var err error
	bintrayConfig, err = config.ReadBintrayConf()
	if cliutils.CheckError(err) != nil {
		os.Exit(1)
	}
	if *tests.BtUser != "" {
		bintrayConfig.User = *tests.BtUser
	}
	if *tests.BtKey != "" {
		bintrayConfig.Key = *tests.BtKey
	}

	if bintrayConfig.User == "" || bintrayConfig.Key == "" {
		log.Error("To test Bintray credentials must be configured.")
		os.Exit(1)
	}

	apiUrl := os.Getenv("JFROG_CLI_BINTRAY_API_URL")
	if apiUrl == "" {
		apiUrl = "https://bintray.com/api/v1/"
	}

	downloadServerUrl := os.Getenv("JFROG_CLI_BINTRAY_DOWNLOAD_URL")
	if downloadServerUrl == "" {
		downloadServerUrl = "https://dl.bintray.com/"
	}

	apiUrl = cliutils.AddTrailingSlashIfNeeded(apiUrl)
	downloadServerUrl = cliutils.AddTrailingSlashIfNeeded(downloadServerUrl)

	bintrayConfig.ApiUrl = apiUrl
	bintrayConfig.DownloadServerUrl = downloadServerUrl
}
Пример #5
0
func Config(details, defaultDetails *config.ArtifactoryDetails, interactive,
	shouldEncPassword bool) (*config.ArtifactoryDetails, error) {

	if details == nil {
		details = new(config.ArtifactoryDetails)
	}
	var err error
	if interactive {
		if defaultDetails == nil {
			defaultDetails, err = config.ReadArtifactoryConf()
			if err != nil {
				return nil, err
			}
		}
		if details.Url == "" {
			ioutils.ScanFromConsole("Artifactory URL", &details.Url, defaultDetails.Url)
		}
		if strings.Index(details.Url, "ssh://") == 0 || strings.Index(details.Url, "SSH://") == 0 {
			err = readSshKeyPathFromConsole(details, defaultDetails)
			if err != nil {
				return nil, err
			}
		} else {
			if details.ApiKey == "" && details.Password == "" {
				ioutils.ScanFromConsole("API key (leave empty for basic authentication)", &details.ApiKey, "")
			}
			if details.ApiKey == "" {
				ioutils.ReadCredentialsFromConsole(details, defaultDetails)
			}
		}
	}
	err = checkSingleAuthMethod(details)
	if err != nil {
		return nil, err
	}

	details.Url = cliutils.AddTrailingSlashIfNeeded(details.Url)
	if shouldEncPassword {
		details, err = encryptPassword(details)
		if err != nil {
			return nil, err
		}
	}
	config.SaveArtifactoryConf(details)
	return details, nil
}
Пример #6
0
func createArtifactoryDetails(c *cli.Context, includeConfig bool) (*config.ArtifactoryDetails, error) {
	if includeConfig {
		details, err := offerConfig(c)
		if err != nil {
			return nil, err
		}
		if details != nil {
			return details, nil
		}
	}
	details := new(config.ArtifactoryDetails)
	details.Url = c.String("url")
	details.ApiKey = c.String("apikey")
	details.User = c.String("user")
	details.Password = c.String("password")
	details.SshKeyPath = c.String("ssh-key-path")

	if includeConfig {
		confDetails, err := commands.GetConfig()
		if err != nil {
			return nil, err
		}
		if details.Url == "" {
			details.Url = confDetails.Url
		}

		if !isAuthMethodSet(details) {
			if details.ApiKey == "" {
				details.ApiKey = confDetails.ApiKey
			}
			if details.User == "" {
				details.User = confDetails.User
			}
			if details.Password == "" {
				details.Password = confDetails.Password
			}
			if details.SshKeyPath == "" {
				details.SshKeyPath = confDetails.SshKeyPath
			}
		}
	}
	details.Url = cliutils.AddTrailingSlashIfNeeded(details.Url)
	return details, nil
}
Пример #7
0
func SshAuthentication(details *config.ArtifactoryDetails) error {
	_, host, port, err := parseUrl(details.Url)
	if err != nil {
		return err
	}

	log.Info("Performing SSH authentication...")
	if details.SshKeyPath == "" {
		err := cliutils.CheckError(errors.New("Cannot invoke the SshAuthentication function with no SSH key path. "))
		if err != nil {
			return err
		}
	}

	buffer, err := ioutil.ReadFile(details.SshKeyPath)
	err = cliutils.CheckError(err)
	if err != nil {
		return err
	}
	key, err := ssh.ParsePrivateKey(buffer)
	err = cliutils.CheckError(err)
	if err != nil {
		return err
	}
	sshConfig := &ssh.ClientConfig{
		User: "******",
		Auth: []ssh.AuthMethod{
			ssh.PublicKeys(key),
		},
	}

	hostAndPort := host + ":" + strconv.Itoa(port)
	connection, err := ssh.Dial("tcp", hostAndPort, sshConfig)
	err = cliutils.CheckError(err)
	if err != nil {
		return err
	}
	defer connection.Close()

	session, err := connection.NewSession()
	err = cliutils.CheckError(err)
	if err != nil {
		return err
	}
	defer session.Close()

	stdout, err := session.StdoutPipe()
	err = cliutils.CheckError(err)
	if err != nil {
		return err
	}

	var buf bytes.Buffer
	go io.Copy(&buf, stdout)

	session.Run("jfrog-authenticate")

	var result SshAuthResult
	err = json.Unmarshal(buf.Bytes(), &result)
	err = cliutils.CheckError(err)
	if err != nil {
		return err
	}
	details.Url = cliutils.AddTrailingSlashIfNeeded(result.Href)
	details.SshAuthHeaders = result.Headers
	log.Info("SSH authentication successful.")
	return nil
}