Skip to content

Commit

Permalink
SPKI: Generate TRC and keys config for topo (#3319)
Browse files Browse the repository at this point in the history
This PR generates the TRC and keys configuration files for a given
topology description.

Additionally, the old templating code for isd.ini and as.ini is removed.
  • Loading branch information
oncilla authored Nov 6, 2019
1 parent 73d2cd9 commit ab71749
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 281 deletions.
19 changes: 12 additions & 7 deletions go/lib/scrypto/trc/v2/primary.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,8 @@ type PrimaryAS struct {
}

// Is returns true if the primary AS holds this property.
func (p *PrimaryAS) Is(a Attribute) bool {
for _, v := range p.Attributes {
if v == a {
return true
}
}
return false
func (p *PrimaryAS) Is(attr Attribute) bool {
return p.Attributes.Contains(attr)
}

// ValidateInvariant ensures that the TRC invariant holds for the primary AS.
Expand Down Expand Up @@ -168,6 +163,16 @@ var _ json.Unmarshaler = (*Attributes)(nil)
// Attributes holds all attributes of a primary AS.
type Attributes []Attribute

// Contains indicates whether the attribute is contained.
func (t Attributes) Contains(attr Attribute) bool {
for _, v := range t {
if v == attr {
return true
}
}
return false
}

// Validate checks that the attributes list is valid.
func (t *Attributes) Validate() error {
if len(*t) > 4 || len(*t) <= 0 {
Expand Down
2 changes: 1 addition & 1 deletion go/tools/scion-pki/internal/v2/conf/testdata/trc-v1.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ base_version = 1
voting_quorum = 1
grace_period = "0s"
trust_reset_allowed = true
votes = ["ff00:0:110"]
votes = []

[validity]
not_before = 42424242
Expand Down
2 changes: 1 addition & 1 deletion go/tools/scion-pki/internal/v2/conf/testdata/trc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TRCv1(notBefore uint32) conf.TRC2 {
VotingQuorum: 1,
GracePeriod: util.DurWrap{},
TrustResetAllowed: &t,
Votes: []addr.AS{xtest.MustParseAS("ff00:0:110")},
Votes: []addr.AS{},
Validity: conf.Validity{
NotBefore: notBefore,
Validity: util.DurWrap{Duration: 5 * 24 * time.Hour},
Expand Down
6 changes: 4 additions & 2 deletions go/tools/scion-pki/internal/v2/conf/trc.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ func (cfg TRC2) Validate() error {
return serrors.New("voting_quorum not set")
case cfg.TrustResetAllowed == nil:
return serrors.New("trust_reset_allowed not set")
case len(cfg.Votes) == 0:
case cfg.BaseVersion != cfg.Version && len(cfg.Votes) == 0:
return serrors.New("votes not set")
case cfg.BaseVersion == cfg.Version && len(cfg.Votes) != 0:
return serrors.New("votes set in base TRC")
}
if err := cfg.Validity.Validate(); err != nil {
return serrors.WrapStr("invalid validity", err)
Expand All @@ -107,7 +109,7 @@ func (cfg TRC2) Validate() error {

// Primary holds the primary AS configuration.
type Primary struct {
Attributes []trc.Attribute `toml:"attributes"`
Attributes trc.Attributes `toml:"attributes"`
IssuingKeyVersion *scrypto.KeyVersion `toml:"issuing_key_version"`
VotingOnlineKeyVersion *scrypto.KeyVersion `toml:"voting_online_key_version"`
VotingOfflineKeyVersion *scrypto.KeyVersion `toml:"voting_offline_key_version"`
Expand Down
23 changes: 18 additions & 5 deletions go/tools/scion-pki/internal/v2/tmpl/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = [
"as.go",
"cmd.go",
"isd.go",
"keys.go",
"topo.go",
],
importpath = "github.com/scionproto/scion/go/tools/scion-pki/internal/v2/tmpl",
visibility = ["//go/tools/scion-pki:__subpackages__"],
deps = [
"//go/lib/addr:go_default_library",
"//go/lib/common:go_default_library",
"//go/lib/scrypto:go_default_library",
"//go/lib/scrypto/cert/v2:go_default_library",
"//go/lib/scrypto/trc/v2:go_default_library",
Expand All @@ -25,3 +21,20 @@ go_library(
"@in_gopkg_yaml_v2//:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["topo_test.go"],
embed = [":go_default_library"],
deps = [
"//go/lib/addr:go_default_library",
"//go/lib/scrypto:go_default_library",
"//go/lib/scrypto/trc/v2:go_default_library",
"//go/lib/util:go_default_library",
"//go/lib/xtest:go_default_library",
"//go/tools/scion-pki/internal/pkicmn:go_default_library",
"//go/tools/scion-pki/internal/v2/conf:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
"@com_github_stretchr_testify//require:go_default_library",
],
)
60 changes: 0 additions & 60 deletions go/tools/scion-pki/internal/v2/tmpl/as.go

This file was deleted.

73 changes: 27 additions & 46 deletions go/tools/scion-pki/internal/v2/tmpl/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package tmpl

import (
"io/ioutil"
"time"

"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/lib/util"
"github.com/scionproto/scion/go/tools/scion-pki/internal/pkicmn"
"github.com/scionproto/scion/go/tools/scion-pki/internal/v2/conf"
)

Expand All @@ -38,59 +40,26 @@ var topo = &cobra.Command{
Short: "Generate isd.ini and as.ini templates for the provided topo",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := runGenTopoTmpl(args[0]); err != nil {
return common.NewBasicError("unable to generate templates from topo", err,
"file", args[0])
val, err := validityFromFlags()
if err != nil {
return serrors.WrapStr("invalid validity period", err)
}
return nil
},
}

var isd = &cobra.Command{
Use: "isd",
Short: "Generate an isd.ini template.",
Long: `Arguments for isd
Selector:
*
All ISDs under the root directory.
X
A specific ISD X.
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := runGenISDTmpl(args[0]); err != nil {
return common.NewBasicError("unable to generate ISD templates", err,
"selector", args[0])
topo, err := readTopo(args[0])
if err != nil {
return serrors.WithCtx(err, "file", args[0])
}
return nil
},
}

var as = &cobra.Command{
Use: "as",
Short: "Generate an as.ini template.",
Long: `Arguments for as
Selector:
*-*
All ISDs and ASes under the root directory.
X-*
All ASes in ISD X.
X-Y
A specific AS X-Y, e.g. AS 1-ff00:0:300
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := runGenASTmpl(args[0]); err != nil {
return common.NewBasicError("unable to generate AS templates", err, "selector", args[0])
g := topoGen{
Dirs: pkicmn.GetDirs(),
Validity: val,
}
if err := g.Run(topo); err != nil {
return serrors.WrapStr("unable to generate templates from topo", err, "file", args[0])
}
return nil
},
}

func init() {
Cmd.AddCommand(isd)
Cmd.AddCommand(as)

topo.PersistentFlags().Uint32VarP(&notBefore, "notbefore", "b", 0,
"set not_before time in all configs")
topo.PersistentFlags().StringVar(&rawValidity, "validity", "365d",
Expand All @@ -112,3 +81,15 @@ func validityFromFlags() (conf.Validity, error) {
}
return v, v.Validate()
}

func readTopo(file string) (topoFile, error) {
var topo topoFile
raw, err := ioutil.ReadFile(file)
if err != nil {
return topo, serrors.WrapStr("unable to read topology file", err)
}
if err := yaml.Unmarshal(raw, &topo); err != nil {
return topo, serrors.WrapStr("unable to parse topology file", err)
}
return topo, nil
}
46 changes: 0 additions & 46 deletions go/tools/scion-pki/internal/v2/tmpl/isd.go

This file was deleted.

53 changes: 0 additions & 53 deletions go/tools/scion-pki/internal/v2/tmpl/keys.go

This file was deleted.

Loading

0 comments on commit ab71749

Please sign in to comment.