Пример #1
0
// handleHostFingerprintInfo obtains the host fingerprint and some information about it.
func handleHostFingerprintInfo(request *api.Request) (interface{}, error) {
	// Map the data to the custom type.
	var data api.RequestHostFingerprintInfo
	err := request.MapTo(&data)
	if err != nil {
		return nil, err
	}

	// Be sure it is a valid stripped host.
	data.Host = utils.GetHostFromUrl(data.Host)

	// Validate.
	if len(data.Host) == 0 {
		return nil, fmt.Errorf("invalid host: '%s'", data.Host)
	}

	// Skip host fingerprint checking if this is a local repository.
	if strings.HasPrefix(data.Host, "file://") {
		// Create the response value.
		response := &api.ResponseHostFingerprintInfo{
			Host:        data.Host,
			Trusted:     true,
			Fingerprint: "",
		}

		return response, nil
	}

	// Check if the fingerprint exists for the host.
	exists, err := hostFingerprintExists(data.Host)
	if err != nil {
		return nil, fmt.Errorf("failed to get information about host fingerprint: %v", err)
	}

	// Obtain the fingerprint of the host.
	fingerprint, err := getSshHostFingerprint(data.Host)
	if err != nil {
		return nil, fmt.Errorf("failed to obtain host fingerprint: %v", err)
	}

	// Create the response value.
	response := &api.ResponseHostFingerprintInfo{
		Host:        data.Host,
		Trusted:     exists,
		Fingerprint: fingerprint,
	}

	return response, nil
}
Пример #2
0
func (c CmdAdd) HandleHostFingerprint(sourceURL string) error {
	host := utils.GetHostFromUrl(sourceURL)

	// Create a new info request.
	request := api.RequestHostFingerprintInfo{
		Host: host,
	}

	// Send the info request to the daemon.
	response, err := sendRequest(api.TypeHostFingerprintInfo, request)
	if err != nil {
		return err
	}

	// Map the response data to the info value.
	var info api.ResponseHostFingerprintInfo
	if err = response.MapTo(&info); err != nil {
		return err
	}

	// Check if the host fingerprint is already trusted.
	if info.Trusted {
		return nil
	}

	// Ask the user if he trusts the given fingerprint.
	fmt.Printf("Host fingerprint is missing.\nFingerprint of host '%s':\n\n", host)
	fmt.Print(colorHint, info.Fingerprint, colorOutput)
	fmt.Println("\n\nTrust it and add it?")

	// Confirm the request.
	if !confirmCommit() {
		return fmt.Errorf("aborted: host fingerprint not trusted by user.")
	}

	// Create the request to add the fingerprint.
	rAdd := api.RequestAddHostFingerprint{
		Fingerprint: info.Fingerprint,
	}

	// Send the request to the daemon.
	response, err = sendRequest(api.TypeAddHostFingerprint, rAdd)
	if err != nil {
		return err
	}

	return nil
}