func getCredURLForAPI(cfg *config.Configuration, req *http.Request) (*url.URL, error) { operation := GetOperationForRequest(req) apiUrl, err := url.Parse(cfg.Endpoint(operation).Url) if err != nil { return nil, err } // if the LFS request doesn't match the current LFS url, don't bother // attempting to set the Authorization header from the LFS or Git remote URLs. if req.URL.Scheme != apiUrl.Scheme || req.URL.Host != apiUrl.Host { return req.URL, nil } if setRequestAuthFromUrl(cfg, req, apiUrl) { return nil, nil } credsUrl := apiUrl if len(cfg.CurrentRemote) > 0 { if u := cfg.GitRemoteUrl(cfg.CurrentRemote, operation == "upload"); u != "" { gitRemoteUrl, err := url.Parse(u) if err != nil { return nil, err } if gitRemoteUrl.Scheme == apiUrl.Scheme && gitRemoteUrl.Host == apiUrl.Host { if setRequestAuthFromUrl(cfg, req, gitRemoteUrl) { return nil, nil } credsUrl = gitRemoteUrl } } } return credsUrl, nil }
func SshAuthenticate(cfg *config.Configuration, operation, oid string) (SshAuthResponse, config.Endpoint, error) { // This is only used as a fallback where the Git URL is SSH but server doesn't support a full SSH binary protocol // and therefore we derive a HTTPS endpoint for binaries instead; but check authentication here via SSH endpoint := cfg.Endpoint(operation) res := SshAuthResponse{} if len(endpoint.SshUserAndHost) == 0 { return res, endpoint, nil } tracerx.Printf("ssh: %s git-lfs-authenticate %s %s %s", endpoint.SshUserAndHost, endpoint.SshPath, operation, oid) exe, args := sshGetExeAndArgs(cfg, endpoint) args = append(args, fmt.Sprintf("git-lfs-authenticate %s %s %s", endpoint.SshPath, operation, oid)) cmd := exec.Command(exe, args...) // Save stdout and stderr in separate buffers var outbuf, errbuf bytes.Buffer cmd.Stdout = &outbuf cmd.Stderr = &errbuf // Execute command err := cmd.Start() if err == nil { err = cmd.Wait() } // Processing result if err != nil { res.Message = errbuf.String() } else { err = json.Unmarshal(outbuf.Bytes(), &res) } return res, endpoint, err }
func setErrorRequestContext(cfg *config.Configuration, err error, req *http.Request) { errors.SetContext(err, "Endpoint", cfg.Endpoint(auth.GetOperationForRequest(req)).Url) errors.SetContext(err, "URL", TraceHttpReq(req)) setErrorHeaderContext(err, "Response", req.Header) }