Source file src/cmd/go/internal/fips140/_mkzip/mkzip.go

     1  // Copyright 2024 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  // Mkzip creates a FIPS snapshot zip file.
     6  // See GOROOT/lib/fips140/README.md and GOROOT/lib/fips140/Makefile
     7  // for more details about when and why to use this.
     8  //
     9  // Usage:
    10  //
    11  //	cd GOROOT/src/cmd/go/internal/fips140/_mkzip
    12  //	GOTOOLCHAIN=go1.26.10 go run . [-b branch] v1.2.3
    13  //
    14  // Mkzip creates a zip file named for the version on the command line
    15  // using the sources in the named branch (default origin/master,
    16  // to avoid accidentally including local commits).
    17  //
    18  // Mkzip must be run with Go 1.26, so that all snapshot zip files are
    19  // compressed by the same compress/flate encoder and can be stored by
    20  // git as small deltas against previous ones.
    21  package main
    22  
    23  import (
    24  	"archive/zip"
    25  	"bytes"
    26  	"flag"
    27  	"fmt"
    28  	"io"
    29  	"log"
    30  	"os"
    31  	"path/filepath"
    32  	"runtime"
    33  	"strings"
    34  
    35  	"golang.org/x/mod/module"
    36  	"golang.org/x/mod/semver"
    37  	modzip "golang.org/x/mod/zip"
    38  )
    39  
    40  var flagBranch = flag.String("b", "origin/master", "branch to use")
    41  
    42  func usage() {
    43  	fmt.Fprintf(os.Stderr, "usage: GOTOOLCHAIN=go1.26.10 go run . [-b branch] vX.Y.Z\n")
    44  	os.Exit(2)
    45  }
    46  
    47  func main() {
    48  	log.SetFlags(0)
    49  	log.SetPrefix("mkzip: ")
    50  	flag.Usage = usage
    51  	flag.Parse()
    52  	if flag.NArg() != 1 {
    53  		usage()
    54  	}
    55  
    56  	if !strings.HasPrefix(runtime.Version(), "go1.26.") {
    57  		log.Fatalf("must be run with GOTOOLCHAIN=go1.26.10, not %s", runtime.Version())
    58  	}
    59  
    60  	// Must run in this module's directory. The snapshots live in lib/fips140.
    61  	goroot := "../../../../../.."
    62  	dir := filepath.Join(goroot, "lib/fips140")
    63  	if _, err := os.Stat(filepath.Join(dir, "fips140.sum")); err != nil {
    64  		log.Fatalf("must be run in the src/cmd/go/internal/fips140/_mkzip directory")
    65  	}
    66  
    67  	// Must have valid version, and must not overwrite existing file.
    68  	version := flag.Arg(0)
    69  	if semver.Canonical(version) != version {
    70  		log.Fatalf("invalid version %q; must be vX.Y.Z", version)
    71  	}
    72  	zipFile := filepath.Join(dir, version+".zip")
    73  	if _, err := os.Stat(zipFile); err == nil {
    74  		log.Fatalf("%s.zip already exists", version)
    75  	}
    76  
    77  	// Make standard module zip file in memory.
    78  	// The module path "golang.org/fips140" needs to be a valid module name,
    79  	// and it is the path where the zip file will be unpacked in the module cache.
    80  	// The path must begin with a domain name to satisfy the module validation rules,
    81  	// but otherwise the path is not used. The cmd/go code using these zips
    82  	// knows that the zip contains crypto/internal/fips140.
    83  	var zbuf bytes.Buffer
    84  	err := modzip.CreateFromVCS(&zbuf,
    85  		module.Version{Path: "golang.org/fips140", Version: version},
    86  		goroot, *flagBranch, "src/crypto/internal/fips140")
    87  	if err != nil {
    88  		log.Fatal(err)
    89  	}
    90  
    91  	// Write new zip file with longer paths: fips140/v1.2.3/foo.go instead of foo.go.
    92  	// That way we can bind the fips140 directory onto the
    93  	// GOROOT/src/crypto/internal/fips140 directory and get a
    94  	// crypto/internal/fips140/v1.2.3 with the snapshot code
    95  	// and an otherwise empty crypto/internal/fips140 directory.
    96  	zr, err := zip.NewReader(bytes.NewReader(zbuf.Bytes()), int64(zbuf.Len()))
    97  	if err != nil {
    98  		log.Fatal(err)
    99  	}
   100  
   101  	var zbuf2 bytes.Buffer
   102  	zw := zip.NewWriter(&zbuf2)
   103  	foundVersion := false
   104  	for _, f := range zr.File {
   105  		// golang.org/fips140@v1.2.3/dir/file.go ->
   106  		// golang.org/fips140@v1.2.3/fips140/v1.2.3/dir/file.go
   107  		if f.Name != "golang.org/fips140@"+version+"/LICENSE" {
   108  			f.Name = "golang.org/fips140@" + version + "/fips140/" + version +
   109  				strings.TrimPrefix(f.Name, "golang.org/fips140@"+version)
   110  		}
   111  		// Inject version in [crypto/internal/fips140.Version].
   112  		if f.Name == "golang.org/fips140@"+version+"/fips140/"+version+"/fips140.go" {
   113  			rf, err := f.Open()
   114  			if err != nil {
   115  				log.Fatal(err)
   116  			}
   117  			contents, err := io.ReadAll(rf)
   118  			if err != nil {
   119  				log.Fatal(err)
   120  			}
   121  			returnLine := `return "latest" //mkzip:version`
   122  			if !bytes.Contains(contents, []byte(returnLine)) {
   123  				log.Fatalf("did not find %q in fips140.go", returnLine)
   124  			}
   125  			// Use only the vX.Y.Z part of a possible vX.Y.Z-hash version.
   126  			v, _, _ := strings.Cut(version, "-")
   127  			newLine := `return "` + v + `"`
   128  			contents = bytes.ReplaceAll(contents, []byte(returnLine), []byte(newLine))
   129  			wf, err := zw.Create(f.Name)
   130  			if err != nil {
   131  				log.Fatal(err)
   132  			}
   133  			if _, err := wf.Write(contents); err != nil {
   134  				log.Fatal(err)
   135  			}
   136  			foundVersion = true
   137  			continue
   138  		}
   139  		wf, err := zw.CreateRaw(&f.FileHeader)
   140  		if err != nil {
   141  			log.Fatal(err)
   142  		}
   143  		rf, err := f.OpenRaw()
   144  		if err != nil {
   145  			log.Fatal(err)
   146  		}
   147  		if _, err := io.Copy(wf, rf); err != nil {
   148  			log.Fatal(err)
   149  		}
   150  	}
   151  	if err := zw.Close(); err != nil {
   152  		log.Fatal(err)
   153  	}
   154  	if !foundVersion {
   155  		log.Fatal("did not find fips140.go file")
   156  	}
   157  
   158  	err = os.WriteFile(zipFile, zbuf2.Bytes(), 0666)
   159  	if err != nil {
   160  		log.Fatal(err)
   161  	}
   162  
   163  	log.Printf("wrote %s.zip", version)
   164  }
   165  

View as plain text