Skip to content

Commit

Permalink
Merge pull request #107 from cjeker/naming_is_hard
Browse files Browse the repository at this point in the history
Naming is hard
  • Loading branch information
job authored Dec 21, 2023
2 parents c83f672 + d1f8fb2 commit 2dfc26e
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 148 deletions.
14 changes: 7 additions & 7 deletions cmd/rtrdump/rtrdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var (
)

type Client struct {
Data prefixfile.VRPList
Data prefixfile.RPKIList

InitSerial bool
Serial uint32
Expand All @@ -84,7 +84,7 @@ func (c *Client) HandlePDU(cs *rtr.ClientSession, pdu rtr.PDU) {
ASN: uint32(pdu.ASN),
Length: pdu.MaxLen,
}
c.Data.Data = append(c.Data.Data, rj)
c.Data.ROA = append(c.Data.ROA, rj)
c.Data.Metadata.Counts++

if *LogDataPDU {
Expand All @@ -96,7 +96,7 @@ func (c *Client) HandlePDU(cs *rtr.ClientSession, pdu rtr.PDU) {
ASN: uint32(pdu.ASN),
Length: pdu.MaxLen,
}
c.Data.Data = append(c.Data.Data, rj)
c.Data.ROA = append(c.Data.ROA, rj)
c.Data.Metadata.Counts++

if *LogDataPDU {
Expand All @@ -117,9 +117,9 @@ func (c *Client) HandlePDU(cs *rtr.ClientSession, pdu rtr.PDU) {

case *rtr.PDUASPA:
if c.Data.ASPA == nil {
c.Data.ASPA = make([]prefixfile.ASPAJson, 0)
c.Data.ASPA = make([]prefixfile.VAPJson, 0)
}
aj := prefixfile.ASPAJson{
aj := prefixfile.VAPJson{
CustomerAsid: pdu.CustomerASNumber,
Providers: pdu.ProviderASNumbers,
}
Expand Down Expand Up @@ -182,9 +182,9 @@ func main() {
}

client := &Client{
Data: prefixfile.VRPList{
Data: prefixfile.RPKIList{
Metadata: prefixfile.MetaData{},
Data: make([]prefixfile.VRPJson, 0),
ROA: make([]prefixfile.VRPJson, 0),
},
InitSerial: *InitSerial,
Serial: uint32(*Serial),
Expand Down
8 changes: 4 additions & 4 deletions cmd/rtrmon/rtrmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ func (t *thresholds) Set(value string) error {
return nil
}

func decodeJSON(data []byte) (*prefixfile.VRPList, error) {
func decodeJSON(data []byte) (*prefixfile.RPKIList, error) {
buf := bytes.NewBuffer(data)
dec := json.NewDecoder(buf)

var vrplistjson prefixfile.VRPList
var vrplistjson prefixfile.RPKIList
err := dec.Decode(&vrplistjson)
return &vrplistjson, err
}
Expand Down Expand Up @@ -369,8 +369,8 @@ func (c *Client) Start(id int, ch chan int) {
// * contains all the VRPs in newVRPs
// * keeps the firstSeen value for VRPs already in the old map
// * keeps elements around for GracePeriod after they are not in the input.
func BuildNewVrpMap(log *log.Entry, currentVrps VRPMap, pfxFile *prefixfile.VRPList, now time.Time) (VRPMap, int) {
var newVrps = pfxFile.Data
func BuildNewVrpMap(log *log.Entry, currentVrps VRPMap, pfxFile *prefixfile.RPKIList, now time.Time) (VRPMap, int) {
var newVrps = pfxFile.ROA
tCurrentUpdate := now.Unix()
res := make(VRPMap)

Expand Down
36 changes: 18 additions & 18 deletions cmd/rtrmon/rtrmon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

func TestBuildNewVrpMap_expiry(t *testing.T) {
stuff := testDataFile()
emptyFile := &prefixfile.VRPList{
emptyFile := &prefixfile.RPKIList{
Metadata: prefixfile.MetaData{},
Data: []prefixfile.VRPJson{},
ROA: []prefixfile.VRPJson{},
BgpSecKeys: []prefixfile.BgpSecKeyJson{},
}

Expand All @@ -36,9 +36,9 @@ func TestBuildNewVrpMap_expiry(t *testing.T) {
res, inGracePeriod = BuildNewVrpMap(log, res, emptyFile, t1)

assertLastSeenMatchesTimeCount(t, res, t1, 0)
assertLastSeenMatchesTimeCount(t, res, now, len(stuff.Data))
if inGracePeriod != len(stuff.Data) {
t.Errorf("All objects should be in grace period. Expected: %d, actual: %d", len(stuff.Data), inGracePeriod)
assertLastSeenMatchesTimeCount(t, res, now, len(stuff.ROA))
if inGracePeriod != len(stuff.ROA) {
t.Errorf("All objects should be in grace period. Expected: %d, actual: %d", len(stuff.ROA), inGracePeriod)
}

// 1s after grace period ends, they are removed
Expand All @@ -59,18 +59,18 @@ func TestBuildNewVrpMap_firsSeen_lastSeen(t *testing.T) {
var res, _ = BuildNewVrpMap(log, make(VRPMap), stuff, t0)

// All have firstSeen + lastSeen equal to t0
assertFirstSeenMatchesTimeCount(t, res, t0, len(stuff.Data))
assertLastSeenMatchesTimeCount(t, res, t0, len(stuff.Data))
assertVisibleMatchesTimeCount(t, res, len(stuff.Data))
assertFirstSeenMatchesTimeCount(t, res, t0, len(stuff.ROA))
assertLastSeenMatchesTimeCount(t, res, t0, len(stuff.ROA))
assertVisibleMatchesTimeCount(t, res, len(stuff.ROA))

// Supply same data again later
t1 := t0.Add(time.Minute * 10)
res, _ = BuildNewVrpMap(log, res, stuff, t1)

// FirstSeen is constant, LastSeen gets updated, none removed
assertFirstSeenMatchesTimeCount(t, res, t0, len(stuff.Data))
assertLastSeenMatchesTimeCount(t, res, t1, len(stuff.Data))
assertVisibleMatchesTimeCount(t, res, len(stuff.Data))
assertFirstSeenMatchesTimeCount(t, res, t0, len(stuff.ROA))
assertLastSeenMatchesTimeCount(t, res, t1, len(stuff.ROA))
assertVisibleMatchesTimeCount(t, res, len(stuff.ROA))

// Supply one new VRP, expect one at new time, others at old time
otherStuff := []prefixfile.VRPJson{
Expand All @@ -81,17 +81,17 @@ func TestBuildNewVrpMap_firsSeen_lastSeen(t *testing.T) {
TA: "testrir",
},
}
otherStuffFile := prefixfile.VRPList{
otherStuffFile := prefixfile.RPKIList{
Metadata: prefixfile.MetaData{},
Data: otherStuff,
ROA: otherStuff,
BgpSecKeys: []prefixfile.BgpSecKeyJson{},
}
t2 := t1.Add(time.Minute * 10)
res, _ = BuildNewVrpMap(log, res, &otherStuffFile, t2)

// LastSeen gets updated just for the new item
assertFirstSeenMatchesTimeCount(t, res, t0, len(stuff.Data))
assertLastSeenMatchesTimeCount(t, res, t1, len(stuff.Data))
assertFirstSeenMatchesTimeCount(t, res, t0, len(stuff.ROA))
assertLastSeenMatchesTimeCount(t, res, t1, len(stuff.ROA))

assertFirstSeenMatchesTimeCount(t, res, t2, len(otherStuff))
assertLastSeenMatchesTimeCount(t, res, t2, len(otherStuff))
Expand Down Expand Up @@ -164,10 +164,10 @@ func testData() []prefixfile.VRPJson {
return stuff
}

func testDataFile() *prefixfile.VRPList {
stuff := prefixfile.VRPList{
func testDataFile() *prefixfile.RPKIList {
stuff := prefixfile.RPKIList{
Metadata: prefixfile.MetaData{},
Data: testData(),
ROA: testData(),
BgpSecKeys: []prefixfile.BgpSecKeyJson{},
}
return &stuff
Expand Down
50 changes: 25 additions & 25 deletions cmd/stayrtr/stayrtr.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ func newSHA256(data []byte) []byte {
return hash[:]
}

func decodeJSON(data []byte) (*prefixfile.VRPList, error) {
func decodeJSON(data []byte) (*prefixfile.RPKIList, error) {
buf := bytes.NewBuffer(data)
dec := json.NewDecoder(buf)

var vrplistjson prefixfile.VRPList
err := dec.Decode(&vrplistjson)
return &vrplistjson, err
var rpkilistjson prefixfile.RPKIList
err := dec.Decode(&rpkilistjson)
return &rpkilistjson, err
}

func isValidPrefixLength(prefix netip.Prefix, maxLength uint8) bool {
Expand All @@ -200,7 +200,7 @@ func isValidPrefixLength(prefix netip.Prefix, maxLength uint8) bool {
// Will return a deduped slice, as well as total VRPs, IPv4 VRPs, IPv6 VRPs, BGPsec Keys and ASPA records
func processData(vrplistjson []prefixfile.VRPJson,
brklistjson []prefixfile.BgpSecKeyJson,
aspajson []prefixfile.ASPAJson) /*Export*/ ([]rtr.VRP, []rtr.BgpsecKey, []rtr.VAP, int, int, int) {
aspajson []prefixfile.VAPJson) /*Export*/ ([]rtr.VRP, []rtr.BgpsecKey, []rtr.VAP, int, int, int) {
filterDuplicates := make(map[string]struct{})

// It may be tempting to change this to a simple time.Since() but that will
Expand Down Expand Up @@ -342,13 +342,13 @@ func (e IdenticalFile) Error() string {
return fmt.Sprintf("File %s is identical to the previous version", e.File)
}

var errVRPJsonFileTooOld = errors.New("VRP JSON file is older than 24 hours")
var errRPKIJsonFileTooOld = errors.New("RPKI JSON file is older than 24 hours")

// Update the state based on the current slurm file and data.
func (s *state) updateFromNewState() error {
sessid := s.server.GetSessionId()

vrpsjson := s.lastdata.Data
vrpsjson := s.lastdata.ROA
if vrpsjson == nil {
return nil
}
Expand All @@ -358,7 +358,7 @@ func (s *state) updateFromNewState() error {
}
aspajson := s.lastdata.ASPA
if aspajson == nil {
aspajson = make([]prefixfile.ASPAJson, 0)
aspajson = make([]prefixfile.VAPJson, 0)
}

buildtime, err := time.Parse(time.RFC3339, s.lastdata.Metadata.Buildtime)
Expand All @@ -371,8 +371,8 @@ func (s *state) updateFromNewState() error {
}
notafter := buildtime.Add(time.Hour * 24)
if time.Now().UTC().After(notafter) {
log.Warnf("VRP JSON file is older than 24 hours: %v", buildtime)
return errVRPJsonFileTooOld
log.Warnf("RPKI JSON file is older than 24 hours: %v", buildtime)
return errRPKIJsonFileTooOld
}
}

Expand All @@ -390,7 +390,7 @@ func (s *state) updateFromNewState() error {
func (s *state) reloadFromCurrentState() error {
sessid := s.server.GetSessionId()

vrpsjson := s.lastdata.Data
vrpsjson := s.lastdata.ROA
if vrpsjson == nil {
return nil
}
Expand All @@ -400,7 +400,7 @@ func (s *state) reloadFromCurrentState() error {
}
aspajson := s.lastdata.ASPA
if aspajson == nil {
aspajson = make([]prefixfile.ASPAJson, 0)
aspajson = make([]prefixfile.VAPJson, 0)
}

buildtime, err := time.Parse(time.RFC3339, s.lastdata.Metadata.Buildtime)
Expand All @@ -413,8 +413,8 @@ func (s *state) reloadFromCurrentState() error {
}
notafter := buildtime.Add(time.Hour * 24)
if time.Now().UTC().After(notafter) {
log.Warnf("VRP JSON file is older than 24 hours: %v", buildtime)
return errVRPJsonFileTooOld
log.Warnf("RPKI JSON file is older than 24 hours: %v", buildtime)
return errRPKIJsonFileTooOld
}
}

Expand All @@ -423,16 +423,16 @@ func (s *state) reloadFromCurrentState() error {
}

vrps, brks, vaps, count, countv4, countv6 := processData(vrpsjson, bgpsecjson, aspajson)
if s.server.CountVRPs() != count {
log.Infof("New update to old state (%v uniques, %v total prefixes). (old %v - new %v)", len(vrps), count, s.server.CountVRPs(), count)
if s.server.CountSDs() != count {
log.Infof("New update to old state (%v uniques, %v total prefixes). (old %v - new %v)", len(vrps), count, s.server.CountSDs(), count)
return s.applyUpdateFromNewState(vrps, brks, vaps, sessid, vrpsjson, bgpsecjson, aspajson, countv4, countv6)
}
return nil
}

func (s *state) applyUpdateFromNewState(vrps []rtr.VRP, brks []rtr.BgpsecKey, vaps []rtr.VAP,
sessid uint16,
vrpsjson []prefixfile.VRPJson, brksjson []prefixfile.BgpSecKeyJson, aspajson []prefixfile.ASPAJson,
vrpsjson []prefixfile.VRPJson, brksjson []prefixfile.BgpSecKeyJson, aspajson []prefixfile.VAPJson,
countv4 int, countv6 int) error {

SDs := make([]rtr.SendableData, 0, len(vrps)+len(brks)+len(vaps))
Expand All @@ -455,12 +455,12 @@ func (s *state) applyUpdateFromNewState(vrps []rtr.VRP, brks []rtr.BgpsecKey, va
}

s.lockJson.Lock()
s.exported = prefixfile.VRPList{
s.exported = prefixfile.RPKIList{
Metadata: prefixfile.MetaData{
Counts: len(vrpsjson),
Buildtime: s.lastdata.Metadata.Buildtime,
},
Data: vrpsjson,
ROA: vrpsjson,
BgpSecKeys: brksjson,
ASPA: aspajson,
}
Expand Down Expand Up @@ -508,14 +508,14 @@ func (s *state) updateFile(file string) (bool, error) {

log.Infof("new cache file: Updating sha256 hash %x -> %x", s.lasthash, hsum)

vrplistjson, err := decodeJSON(data)
rpkilistjson, err := decodeJSON(data)
if err != nil {
return false, err
}

s.lasthash = hsum
s.lastchange = time.Now().UTC()
s.lastdata = vrplistjson
s.lastdata = rpkilistjson

return true, nil
}
Expand Down Expand Up @@ -617,7 +617,7 @@ func (s *state) routineUpdate(file string, interval int, slurmFile string) {
if cacheUpdated || slurmNotPresentOrUpdated {
err := s.updateFromNewState()
if err != nil {
if err == errVRPJsonFileTooOld {
if err == errRPKIJsonFileTooOld {
// If the exiting build time is over 24 hours, It's time to drop everything out.
// to avoid routing on stale data
buildTime := s.exported.Metadata.GetBuildTime()
Expand Down Expand Up @@ -645,7 +645,7 @@ func (s *state) exporter(wr http.ResponseWriter, r *http.Request) {
}

type state struct {
lastdata *prefixfile.VRPList
lastdata *prefixfile.RPKIList
lasthash []byte
lastchange time.Time
lastts time.Time
Expand All @@ -657,7 +657,7 @@ type state struct {

metricsEvent *metricsEvent

exported prefixfile.VRPList
exported prefixfile.RPKIList
lockJson *sync.RWMutex

slurm *prefixfile.SlurmConfig
Expand Down Expand Up @@ -754,7 +754,7 @@ func run() error {

s := state{
server: server,
lastdata: &prefixfile.VRPList{},
lastdata: &prefixfile.RPKIList{},
metricsEvent: me,
sendNotifs: *SendNotifs,
checktime: *TimeCheck,
Expand Down
4 changes: 2 additions & 2 deletions cmd/stayrtr/stayrtr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ func TestJson(t *testing.T) {
Ex1 := int64(1627568318)
Ex2 := int64(1627575699)

want := (&prefixfile.VRPList{
want := (&prefixfile.RPKIList{
Metadata: prefixfile.MetaData{
Counts: 2,
Buildtime: "2021-07-27T18:56:02Z",
},
Data: []prefixfile.VRPJson{
ROA: []prefixfile.VRPJson{
{Prefix: "1.0.0.0/24",
Length: 24,
ASN: float64(13335),
Expand Down
6 changes: 3 additions & 3 deletions lib/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
)

type TestClient struct {
Data prefixfile.VRPList
Data prefixfile.RPKIList

InitSerial bool
Serial uint32
Expand All @@ -34,9 +34,9 @@ func getBasicClientConguration(version int) ClientConfiguration {

func getClient() *TestClient {
return &TestClient{
Data: prefixfile.VRPList{
Data: prefixfile.RPKIList{
Metadata: prefixfile.MetaData{},
Data: make([]prefixfile.VRPJson, 0),
ROA: make([]prefixfile.VRPJson, 0),
},
InitSerial: InitSerial,
Serial: Serial,
Expand Down
Loading

0 comments on commit 2dfc26e

Please sign in to comment.