government / cmd / government / repeal.go

@ 20dc26e627d8341795ca628664e44a83b66d316a | history


package main

import (
	"strings"
	"time"

	"github.com/spf13/cobra"

	"government/lisp2gov"
	"government/ratify"
)

// repeal writes a CRL that strikes down a program. It needs the key the
// program was compiled with.
func newRepealCmd() *cobra.Command {
	var (
		out, keyFile, seedHex, reason string
		at, issued, nextUpdate        dateFlag
	)
	cmd := &cobra.Command{
		Use:     "repeal prog.gov",
		Short:   "Write a CRL repealing a program",
		Example: "  government repeal law.gov --key ada.pem --reason superseded -o repeal.crl\n  government run law.gov --crl repeal.crl",
		Args:    oneFile,
		RunE: func(cmd *cobra.Command, args []string) error {
			path := args[0]
			code, known := ratify.Reasons[reason]
			if !known {
				return refused("unknown reason %q; want one of %s", reason, strings.Join(ratify.ReasonNames(), ", "))
			}
			seed, err := signingSeed(keyFile, seedHex)
			if err != nil {
				return refused("%v", err)
			}
			data, err := readFile(path)
			if err != nil {
				return err
			}

			// A program is repealed whatever its dates, so only its chain
			// is checked here, not its window in force.
			certs, err := ratify.Load(data)
			if err == nil {
				certs, err = ratify.Order(certs)
			}
			if err == nil {
				err = ratify.CheckChain(certs)
			}
			if err != nil {
				return refused("%s: %v", path, err)
			}

			now := time.Now().UTC().Truncate(time.Second)
			crl, err := lisp2gov.Repeal(certs, lisp2gov.RepealOptions{
				Seed:       seed,
				Reason:     code,
				At:         orDefault(at.t, now),
				Issued:     orDefault(issued.t, now),
				NextUpdate: orDefault(nextUpdate.t, time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)),
			})
			if err != nil {
				return refused("%s: %v", path, err)
			}
			return writeOutput(cmd, out, crl)
		},
	}
	f := cmd.Flags()
	f.StringVarP(&out, "output", "o", "", "write the CRL to `file` instead of standard output")
	f.StringVar(&keyFile, "key", "", "the Ed25519 PKCS#8 private key `file` the program was compiled with")
	f.StringVar(&seedHex, "seed", "", "the `hex` seed the program was compiled with, instead of a key file")
	f.StringVar(&reason, "reason", "unspecified", "the CRLReason: `name` is one of "+strings.Join(ratify.ReasonNames(), ", "))
	f.Var(&at, "at", "the repeal takes effect at this `time` (default now); a future time schedules it")
	f.Var(&issued, "issued", "the CRL's issue `time` (thisUpdate; default now)")
	f.Var(&nextUpdate, "next-update", "the CRL goes stale at this `time` (default 9999-12-31T23:59:59Z, never)")
	cmd.MarkFlagsMutuallyExclusive("key", "seed")
	return cmd
}

func orDefault(t, def time.Time) time.Time {
	if t.IsZero() {
		return def
	}
	return t
}