Source file src/cmd/go/internal/modfetch/sumdb.go

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Go checksum database lookup
     6  
     7  //go:build !cmd_go_bootstrap
     8  
     9  package modfetch
    10  
    11  import (
    12  	"bytes"
    13  	"errors"
    14  	"fmt"
    15  	"io"
    16  	"io/fs"
    17  	"net/url"
    18  	"os"
    19  	"path/filepath"
    20  	"strings"
    21  	"sync"
    22  	"time"
    23  
    24  	"cmd/go/internal/base"
    25  	"cmd/go/internal/cfg"
    26  	"cmd/go/internal/lockedfile"
    27  	"cmd/go/internal/web"
    28  
    29  	"golang.org/x/mod/module"
    30  	"golang.org/x/mod/sumdb"
    31  	"golang.org/x/mod/sumdb/note"
    32  )
    33  
    34  // useSumDB reports whether to use the Go checksum database for the given module.
    35  func useSumDB(mod module.Version) bool {
    36  	return cfg.GOSUMDB != "off" && !module.MatchPrefixPatterns(cfg.GONOSUMDB, mod.Path)
    37  }
    38  
    39  // lookupSumDB returns the Go checksum database's go.sum lines for the given module,
    40  // along with the name of the database.
    41  func lookupSumDB(mod module.Version) (dbname string, lines []string, err error) {
    42  	dbOnce.Do(func() {
    43  		dbName, db, dbErr = dbDial()
    44  	})
    45  	if dbErr != nil {
    46  		return "", nil, dbErr
    47  	}
    48  	lines, err = db.Lookup(mod.Path, mod.Version)
    49  	return dbName, lines, err
    50  }
    51  
    52  var (
    53  	dbOnce sync.Once
    54  	dbName string
    55  	db     *sumdb.Client
    56  	dbErr  error
    57  )
    58  
    59  func dbDial() (dbName string, db *sumdb.Client, err error) {
    60  	// $GOSUMDB can be "key" or "key url",
    61  	// and the key can be a full verifier key
    62  	// or a host on our list of known keys.
    63  
    64  	// Special case: sum.golang.google.cn
    65  	// is an alias, reachable inside mainland China,
    66  	// for sum.golang.org. If there are more
    67  	// of these we should add a map like knownGOSUMDB.
    68  	gosumdb := cfg.GOSUMDB
    69  	if gosumdb == "sum.golang.google.cn" {
    70  		gosumdb = "sum.golang.org https://sum.golang.google.cn"
    71  	}
    72  
    73  	key := strings.Fields(gosumdb)
    74  	if len(key) >= 1 {
    75  		if k := knownGOSUMDB[key[0]]; k != "" {
    76  			key[0] = k
    77  		}
    78  	}
    79  	if len(key) == 0 {
    80  		return "", nil, fmt.Errorf("missing GOSUMDB")
    81  	}
    82  	if len(key) > 2 {
    83  		return "", nil, fmt.Errorf("invalid GOSUMDB: too many fields")
    84  	}
    85  	vkey, err := note.NewVerifier(key[0])
    86  	if err != nil {
    87  		return "", nil, fmt.Errorf("invalid GOSUMDB: %v", err)
    88  	}
    89  	name := vkey.Name()
    90  
    91  	// No funny business in the database name.
    92  	direct, err := url.Parse("https://" + name)
    93  	if err != nil || strings.HasSuffix(name, "/") || *direct != (url.URL{Scheme: "https", Host: direct.Host, Path: direct.Path, RawPath: direct.RawPath}) || direct.RawPath != "" || direct.Host == "" {
    94  		return "", nil, fmt.Errorf("invalid sumdb name (must be host[/path]): %s %+v", name, *direct)
    95  	}
    96  
    97  	// Determine how to get to database.
    98  	var base *url.URL
    99  	if len(key) >= 2 {
   100  		// Use explicit alternate URL listed in $GOSUMDB,
   101  		// bypassing both the default URL derivation and any proxies.
   102  		u, err := url.Parse(key[1])
   103  		if err != nil {
   104  			return "", nil, fmt.Errorf("invalid GOSUMDB URL: %v", err)
   105  		}
   106  		base = u
   107  	}
   108  
   109  	return name, sumdb.NewClient(&dbClient{key: key[0], name: name, direct: direct, base: base}), nil
   110  }
   111  
   112  type dbClient struct {
   113  	key    string
   114  	name   string
   115  	direct *url.URL
   116  
   117  	once    sync.Once
   118  	base    *url.URL
   119  	baseErr error
   120  }
   121  
   122  func (c *dbClient) ReadRemote(path string) ([]byte, error) {
   123  	c.once.Do(c.initBase)
   124  	if c.baseErr != nil {
   125  		return nil, c.baseErr
   126  	}
   127  
   128  	var data []byte
   129  	start := time.Now()
   130  	targ := web.Join(c.base, path)
   131  	data, err := web.GetBytes(targ)
   132  	if false {
   133  		fmt.Fprintf(os.Stderr, "%.3fs %s\n", time.Since(start).Seconds(), targ.Redacted())
   134  	}
   135  	return data, err
   136  }
   137  
   138  // initBase determines the base URL for connecting to the database.
   139  // Determining the URL requires sending network traffic to proxies,
   140  // so this work is delayed until we need to download something from
   141  // the database. If everything we need is in the local cache and
   142  // c.ReadRemote is never called, we will never do this work.
   143  func (c *dbClient) initBase() {
   144  	if c.base != nil {
   145  		return
   146  	}
   147  
   148  	// Try proxies in turn until we find out how to connect to this database.
   149  	//
   150  	// Before accessing any checksum database URL using a proxy, the proxy
   151  	// client should first fetch <proxyURL>/sumdb/<sumdb-name>/supported.
   152  	//
   153  	// If that request returns a successful (HTTP 200) response, then the proxy
   154  	// supports proxying checksum database requests. In that case, the client
   155  	// should use the proxied access method only, never falling back to a direct
   156  	// connection to the database.
   157  	//
   158  	// If the /sumdb/<sumdb-name>/supported check fails with a “not found” (HTTP
   159  	// 404) or “gone” (HTTP 410) response, or if the proxy is configured to fall
   160  	// back on errors, the client will try the next proxy. If there are no
   161  	// proxies left or if the proxy is "direct" or "off", the client should
   162  	// connect directly to that database.
   163  	//
   164  	// Any other response is treated as the database being unavailable.
   165  	//
   166  	// See https://golang.org/design/25530-sumdb#proxying-a-checksum-database.
   167  	err := TryProxies(func(proxy string) error {
   168  		switch proxy {
   169  		case "noproxy":
   170  			return errUseProxy
   171  		case "direct", "off":
   172  			return errProxyOff
   173  		default:
   174  			proxyURL, err := url.Parse(proxy)
   175  			if err != nil {
   176  				return err
   177  			}
   178  			if _, err := web.GetBytes(web.Join(proxyURL, "sumdb/"+c.name+"/supported")); err != nil {
   179  				return err
   180  			}
   181  			// Success! This proxy will help us.
   182  			c.base = web.Join(proxyURL, "sumdb/"+c.name)
   183  			return nil
   184  		}
   185  	})
   186  	if errors.Is(err, fs.ErrNotExist) {
   187  		// No proxies, or all proxies failed (with 404, 410, or were allowed
   188  		// to fall back), or we reached an explicit "direct" or "off".
   189  		c.base = c.direct
   190  	} else if err != nil {
   191  		c.baseErr = err
   192  	}
   193  }
   194  
   195  // ReadConfig reads the key from c.key
   196  // and otherwise reads the config (a latest tree head) from GOPATH/pkg/sumdb/<file>.
   197  func (c *dbClient) ReadConfig(file string) (data []byte, err error) {
   198  	if file == "key" {
   199  		return []byte(c.key), nil
   200  	}
   201  
   202  	if cfg.SumdbDir == "" {
   203  		return nil, fmt.Errorf("could not locate sumdb file: missing $GOPATH: %s",
   204  			cfg.GoPathError)
   205  	}
   206  	targ := filepath.Join(cfg.SumdbDir, file)
   207  	data, err = lockedfile.Read(targ)
   208  	if errors.Is(err, fs.ErrNotExist) {
   209  		// Treat non-existent as empty, to bootstrap the "latest" file
   210  		// the first time we connect to a given database.
   211  		return []byte{}, nil
   212  	}
   213  	return data, err
   214  }
   215  
   216  // WriteConfig rewrites the latest tree head.
   217  func (*dbClient) WriteConfig(file string, old, new []byte) error {
   218  	if file == "key" {
   219  		// Should not happen.
   220  		return fmt.Errorf("cannot write key")
   221  	}
   222  	if cfg.SumdbDir == "" {
   223  		return fmt.Errorf("could not locate sumdb file: missing $GOPATH: %s",
   224  			cfg.GoPathError)
   225  	}
   226  	targ := filepath.Join(cfg.SumdbDir, file)
   227  	os.MkdirAll(filepath.Dir(targ), 0777)
   228  	f, err := lockedfile.Edit(targ)
   229  	if err != nil {
   230  		return err
   231  	}
   232  	defer f.Close()
   233  	data, err := io.ReadAll(f)
   234  	if err != nil {
   235  		return err
   236  	}
   237  	if len(data) > 0 && !bytes.Equal(data, old) {
   238  		return sumdb.ErrWriteConflict
   239  	}
   240  	if _, err := f.Seek(0, 0); err != nil {
   241  		return err
   242  	}
   243  	if err := f.Truncate(0); err != nil {
   244  		return err
   245  	}
   246  	if _, err := f.Write(new); err != nil {
   247  		return err
   248  	}
   249  	return f.Close()
   250  }
   251  
   252  // ReadCache reads cached lookups or tiles from
   253  // GOPATH/pkg/mod/cache/download/sumdb,
   254  // which will be deleted by "go clean -modcache".
   255  func (*dbClient) ReadCache(file string) ([]byte, error) {
   256  	targ := filepath.Join(cfg.GOMODCACHE, "cache/download/sumdb", file)
   257  	data, err := lockedfile.Read(targ)
   258  	// lockedfile.Write does not atomically create the file with contents.
   259  	// There is a moment between file creation and locking the file for writing,
   260  	// during which the empty file can be locked for reading.
   261  	// Treat observing an empty file as file not found.
   262  	if err == nil && len(data) == 0 {
   263  		err = &fs.PathError{Op: "read", Path: targ, Err: fs.ErrNotExist}
   264  	}
   265  	return data, err
   266  }
   267  
   268  // WriteCache updates cached lookups or tiles.
   269  func (*dbClient) WriteCache(file string, data []byte) {
   270  	targ := filepath.Join(cfg.GOMODCACHE, "cache/download/sumdb", file)
   271  	os.MkdirAll(filepath.Dir(targ), 0777)
   272  	lockedfile.Write(targ, bytes.NewReader(data), 0666)
   273  }
   274  
   275  func (*dbClient) Log(msg string) {
   276  	// nothing for now
   277  }
   278  
   279  func (*dbClient) SecurityError(msg string) {
   280  	base.Fatalf("%s", msg)
   281  }
   282  

View as plain text