Skip to content
quarry Artifact [c923d21736]
Login

Artifact [c923d21736]

Artifact c923d217369b681a857434b36396edd54d7a60ccded9e15c9dbb76551312ed19:


package pickupclient

import (
	"context"
	"crypto/rand"
	"crypto/sha256"
	"encoding/base64"
	"encoding/hex"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
	"time"
)

// quarry pickup: the client half of RFC 0030's credential pickup. It
// makes the pickup key, collects the credential from the forge's collect
// route, and hands it to zit through ZIT_PASSWORD, so the value is in no
// argv, log, file or terminal of the caller.

const (
	pickupPoll        = 10 * time.Second // the route allows 6 a minute for one key
	pickupDefaultWait = 15 * time.Minute
	pickupBodyMax     = 1 << 20
)

var pickupAgentName = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`)

type Env struct {
	Client   *http.Client
	Stdout   io.Writer
	Stderr   io.Writer
	StateDir string
	Now      func() time.Time
	Sleep    func(ctx context.Context, d time.Duration) error
	// Run starts zit in dir with env added to the caller's environment
	// and returns its combined output.
	Run func(ctx context.Context, dir string, env []string, args []string) (string, error)
}

func Main(args []string) int {
	e := &Env{Stdout: os.Stdout, Stderr: os.Stderr}
	return Command(context.Background(), args, e)
}

func (e *Env) fill() {
	if e.Client == nil {
		e.Client = &http.Client{Timeout: time.Minute}
	}
	// The key goes to the address given and nowhere else.
	c := *e.Client
	c.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
	e.Client = &c
	if e.Now == nil {
		e.Now = time.Now
	}
	if e.Sleep == nil {
		e.Sleep = func(ctx context.Context, d time.Duration) error {
			t := time.NewTimer(d)
			defer t.Stop()
			select {
			case <-ctx.Done():
				return ctx.Err()
			case <-t.C:
				return nil
			}
		}
	}
	if e.Run == nil {
		e.Run = func(ctx context.Context, dir string, env []string, args []string) (string, error) {
			cmd := exec.CommandContext(ctx, "zit", args...)
			cmd.Dir = dir
			base := os.Environ()[:0:0]
			for _, kv := range os.Environ() {
				if !strings.HasPrefix(kv, "ZIT_PASSWORD=") {
					base = append(base, kv)
				}
			}
			cmd.Env = append(base, env...)
			out, err := cmd.CombinedOutput()
			return string(out), err
		}
	}
	if e.StateDir == "" {
		if d := os.Getenv("XDG_STATE_HOME"); d != "" {
			e.StateDir = filepath.Join(d, "quarry", "pickup")
		} else if h, err := os.UserHomeDir(); err == nil {
			e.StateDir = filepath.Join(h, ".local", "state", "quarry", "pickup")
		}
	}
}

// Exit statuses: 0 done, 1 refused or closed, 2 usage, 3 still pending
// when the wait ended.
func Command(ctx context.Context, args []string, e *Env) int {
	e.fill()
	fail := func(code int, format string, a ...any) int {
		fmt.Fprintf(e.Stderr, "quarry pickup: "+format+"\n", a...)
		return code
	}
	if len(args) == 0 {
		return fail(2, "usage: quarry pickup new [--dir D] | collect URL [--key FILE] [--wait D] (--clone DIR | --checkout DIR | --print-env)")
	}
	switch args[0] {
	case "new":
		fs := flag.NewFlagSet("pickup new", flag.ContinueOnError)
		fs.SetOutput(io.Discard)
		dir := fs.String("dir", e.StateDir, "")
		if err := fs.Parse(args[1:]); err != nil || fs.NArg() != 0 || *dir == "" {
			return fail(2, "usage: quarry pickup new [--dir D]")
		}
		code, err := pickupMake(*dir)
		if err != nil {
			return fail(1, "%v", err)
		}
		fmt.Fprintln(e.Stdout, code)
		return 0
	case "collect":
		return pickupCollect(ctx, args[1:], e, fail)
	}
	return fail(2, "unknown subcommand %q", args[0])
}

func pickupMake(dir string) (string, error) {
	key := make([]byte, 32)
	if _, err := rand.Read(key); err != nil {
		return "", err
	}
	sum := sha256.Sum256(key)
	code := hex.EncodeToString(sum[:])
	if err := os.MkdirAll(dir, 0o700); err != nil {
		return "", err
	}
	// A directory that already existed may be looser; chmod also fails
	// for one that is not ours, which is a refusal.
	if err := os.Chmod(dir, 0o700); err != nil {
		return "", err
	}
	f, err := os.OpenFile(filepath.Join(dir, code), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
	if err != nil {
		return "", err
	}
	_, werr := f.Write(key)
	if cerr := f.Close(); werr == nil {
		werr = cerr
	}
	if werr != nil {
		os.Remove(f.Name())
		return "", werr
	}
	return code, nil
}

type pickupAnswer struct {
	// Request is the sign-off's id. The top-level agent is the asker
	// ("client of <person>" for a claimed start), not the agent set up,
	// so it is not read; result.agent is the agent.
	Request string `json:"request"`
	Result  struct {
		Agent string `json:"agent"`
		Token struct {
			ID string `json:"id"`
		} `json:"token"`
		Remotes []struct {
			URL string `json:"url"`
		} `json:"remotes"`
	} `json:"result"`
	Secret struct {
		Value string `json:"value"`
	} `json:"secret"`
	State string `json:"state"`
}

func pickupCollect(ctx context.Context, args []string, e *Env, fail func(int, string, ...any) int) int {
	fs := flag.NewFlagSet("pickup collect", flag.ContinueOnError)
	fs.SetOutput(io.Discard)
	keyPath := fs.String("key", "", "")
	wait := fs.Duration("wait", pickupDefaultWait, "")
	clone := fs.String("clone", "", "")
	checkout := fs.String("checkout", "", "")
	printEnv := fs.Bool("print-env", false, "")
	var rawURL string
	rest := args
	for {
		if err := fs.Parse(rest); err != nil {
			return fail(2, "bad flags: %v", err)
		}
		if fs.NArg() == 0 {
			break
		}
		if rawURL != "" {
			return fail(2, "one URL only")
		}
		rawURL, rest = fs.Arg(0), fs.Args()[1:]
	}
	modes := 0
	for _, on := range []bool{*clone != "", *checkout != "", *printEnv} {
		if on {
			modes++
		}
	}
	if rawURL == "" || modes != 1 {
		return fail(2, "usage: quarry pickup collect URL [--key FILE] [--wait D] (--clone DIR | --checkout DIR | --print-env)")
	}
	base, collectURL, err := pickupURL(rawURL)
	if err != nil {
		return fail(2, "%v", err)
	}
	if *keyPath == "" {
		ents, _ := os.ReadDir(e.StateDir)
		if len(ents) != 1 {
			return fail(2, "give --key FILE (%d keys are kept in the default directory)", len(ents))
		}
		*keyPath = filepath.Join(e.StateDir, ents[0].Name())
	}
	key, err := os.ReadFile(*keyPath)
	if err != nil || len(key) != 32 {
		return fail(2, "the key file cannot be used")
	}
	auth := "Pickup " + base64.RawURLEncoding.EncodeToString(key)

	// A checkout that already names another host is refused before the
	// key is spent.
	var prev *url.URL
	if *checkout != "" {
		out, err := e.Run(ctx, *checkout, nil, []string{"remote"})
		if err != nil {
			return fail(1, "zit remote failed in the checkout")
		}
		prev = pickupRemote(out)
		if prev != nil && !sameOrigin(prev, base) {
			return fail(1, "the checkout's remote is on another host; nothing was collected")
		}
	}

	deadline := e.Now().Add(*wait)
	backoff := time.Duration(0)
	var ans pickupAnswer
	for {
		status, retry, body, err := pickupPost(ctx, e, collectURL, auth)
		if err != nil {
			return fail(1, "%v", err)
		}
		if status == http.StatusOK {
			if err := json.Unmarshal(body, &ans); err != nil {
				return fail(1, "the forge's answer was not understood")
			}
			break
		}
		var st pickupAnswer
		json.Unmarshal(body, &st)
		pause := pickupPoll
		switch {
		case status == http.StatusAccepted:
			backoff = 0
		case status == http.StatusTooManyRequests:
			if retry > 0 {
				pause = retry
			}
		case status >= 500:
			if backoff == 0 {
				backoff = 2 * time.Second
			} else if backoff < 30*time.Second {
				backoff *= 2
			}
			pause = backoff
			if !e.Now().Add(pause).Before(deadline) {
				return fail(1, "the forge kept answering %d", status)
			}
		case status == http.StatusGone:
			os.Remove(*keyPath)
			return fail(1, "the credential cannot be collected: %s", pickupWord(st.State))
		case status == http.StatusConflict:
			return fail(1, "this credential does not reach that repository; nothing was collected")
		case status == http.StatusNotFound:
			// Kept: a 404 is also a wrong path or a mistyped host, and a
			// pending request's key must survive a typo. Only 410 is gone.
			return fail(1, "no such pickup: the forge holds no request for this key")
		default:
			return fail(1, "the forge answered %d", status)
		}
		if !e.Now().Add(pause).Before(deadline) {
			return fail(3, "still pending when the wait ended")
		}
		if err := e.Sleep(ctx, pause); err != nil {
			return fail(1, "interrupted")
		}
	}
	secret := ans.Secret.Value
	scrub := func(s string) string {
		if secret != "" {
			s = strings.ReplaceAll(s, secret, "***")
		}
		return s
	}
	// From here the forge has released the value and cleared it: if the
	// hand-off does not complete, it is saved nowhere and cannot be
	// fetched again, so the one thing left is to say what to revoke. The
	// key opens nothing more either way.
	lost := func(format string, a ...any) int {
		os.Remove(*keyPath)
		fmt.Fprintf(e.Stderr, "quarry pickup: "+format+"\n", a...)
		fmt.Fprintf(e.Stderr, "quarry pickup: a live credential was released and saved nowhere; revoke it (request %s, token %s)\n", orUnknown(ans.Request), orUnknown(ans.Result.Token.ID))
		return 1
	}

	agent := ans.Result.Agent
	if agent == "" || secret == "" || !pickupAgentName.MatchString(agent) {
		return lost("the answer is not a credential for an agent")
	}
	var first string
	for _, r := range ans.Result.Remotes {
		u, err := url.Parse(r.URL)
		if err != nil {
			return lost("the answer names a remote that cannot be read")
		}
		u = base.ResolveReference(u)
		if !sameOrigin(u, base) {
			return lost("the answer names a remote that is not on the forge's own host")
		}
		if first == "" {
			first = u.String()
		}
	}
	target := rawURL
	if strings.Trim(base.Path, "/") == "" {
		if first == "" {
			return lost("the answer names no remote to use")
		}
		target = first
	}
	env := []string{"ZIT_PASSWORD=" + secret}
	run := func(dir string, env []string, args ...string) bool {
		out, err := e.Run(ctx, dir, env, args)
		if err != nil {
			fmt.Fprintf(e.Stderr, "quarry pickup: zit %s failed: %s\n", args[0], scrub(strings.TrimSpace(tail(out, 400))))
			return false
		}
		return true
	}
	switch {
	case *clone != "":
		if !run("", env, "clone", target, *clone, "--user", agent) {
			return lost("the clone failed")
		}
	case *checkout != "":
		if !run(*checkout, nil, "remote", target) {
			return lost("setting the remote failed")
		}
		if !run(*checkout, env, "pull", "--user", agent) {
			if prev != nil {
				run(*checkout, nil, "remote", prev.String())
			} else {
				run(*checkout, nil, "remote", "--unset")
			}
			return lost("the pull failed")
		}
	default:
		fmt.Fprintf(e.Stdout, "export ZIT_PASSWORD='%s'\n", strings.ReplaceAll(secret, "'", `'\''`))
		os.Remove(*keyPath)
		return 0
	}
	os.Remove(*keyPath)
	fmt.Fprintf(e.Stdout, "logged in as %s\n", agent)
	return 0
}

func orUnknown(s string) string {
	if s == "" {
		return "unknown"
	}
	return s
}

// pickupRemote reads what `zit remote` prints: the saved address, or
// words saying there is none.
func pickupRemote(out string) *url.URL {
	u, err := url.Parse(strings.TrimSpace(out))
	if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
		return nil
	}
	return u
}

// sameOrigin is whether two URLs name one scheme, host and port, with
// the case of the host and a default port ignored.
func sameOrigin(a, b *url.URL) bool {
	port := func(u *url.URL) string {
		if p := u.Port(); p != "" {
			return p
		}
		if strings.EqualFold(u.Scheme, "https") {
			return "443"
		}
		return "80"
	}
	// ASCII case only: strings.EqualFold folds Unicode, so a host
	// spelled with the Kelvin sign would match one spelled with k.
	return a.Hostname() != "" && asciiLower(a.Scheme) == asciiLower(b.Scheme) &&
		asciiLower(a.Hostname()) == asciiLower(b.Hostname()) && port(a) == port(b)
}

func asciiLower(s string) string {
	b := []byte(s)
	for i, c := range b {
		if 'A' <= c && c <= 'Z' {
			b[i] = c + 'a' - 'A'
		}
	}
	return string(b)
}

func tail(s string, n int) string {
	if len(s) > n {
		return s[len(s)-n:]
	}
	return s
}

func pickupWord(state string) string {
	switch state {
	case "denied", "failed", "expired", "lapsed", "collected":
		return state
	}
	return "closed"
}

// pickupURL is the forge URL given and the collect route for it: a bare
// origin collects at the API's path, a repository's sync URL at its own
// /pickup.
func pickupURL(raw string) (*url.URL, string, error) {
	u, err := url.Parse(raw)
	if err != nil || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" {
		return nil, "", errors.New("URL must be a forge address with no login, query or fragment")
	}
	switch u.Scheme {
	case "https":
	case "http":
		if ip := net.ParseIP(u.Hostname()); !(ip != nil && ip.IsLoopback()) && u.Hostname() != "localhost" {
			return nil, "", errors.New("the key is sent only over https (http only to loopback)")
		}
	default:
		return nil, "", errors.New("URL must be http or https")
	}
	c := *u
	if p := strings.Trim(u.Path, "/"); p == "" {
		c.Path = "/api/v1/pickups/collect"
	} else {
		c.Path = "/" + p + "/pickup"
	}
	return u, c.String(), nil
}

func pickupPost(ctx context.Context, e *Env, u, auth string) (status int, retry time.Duration, body []byte, err error) {
	req, err := http.NewRequestWithContext(ctx, "POST", u, nil)
	if err != nil {
		return 0, 0, nil, errors.New("bad URL")
	}
	req.Header.Set("Authorization", auth)
	resp, err := e.Client.Do(req)
	if err != nil {
		// The transport's error carries the URL, never the header.
		var ue *url.Error
		if errors.As(err, &ue) {
			err = ue.Err
		}
		return 0, 0, nil, fmt.Errorf("the forge could not be reached: %v", err)
	}
	defer resp.Body.Close()
	body, err = io.ReadAll(io.LimitReader(resp.Body, pickupBodyMax))
	if err != nil {
		return 0, 0, nil, errors.New("the forge's answer was cut off")
	}
	if n, err := strconv.Atoi(resp.Header.Get("Retry-After")); err == nil && n > 0 {
		retry = time.Duration(n) * time.Second
	}
	return resp.StatusCode, retry, body, nil
}