From b778f2baac071cb0a3c1ca15d1617cdf0ead48a7 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Wed, 8 May 2024 10:59:55 +0200 Subject: [PATCH] eskip: remove flowid dependency (#3065) eskip package is generally useful without proxy or filters and therefore ideally should have minimal number of dependencies. This change deprecates GenerateIfNeeded which is only used by etcd dataclient and reimplements it to remove dependency on github.com/zalando/skipper/filters/flowid See 35ba6cc76dee89a57cc7869e79824bd9fc56e750 Signed-off-by: Alexander Yastrebov --- eskip/eskip.go | 31 +++++++++++++++---------------- etcd/etcd.go | 1 + 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/eskip/eskip.go b/eskip/eskip.go index e68ab5d22c..229789b873 100644 --- a/eskip/eskip.go +++ b/eskip/eskip.go @@ -5,13 +5,13 @@ package eskip import ( "errors" "fmt" + "math/rand" "net/url" "regexp" "strings" "sync" log "github.com/sirupsen/logrus" - "github.com/zalando/skipper/filters/flowid" ) const duplicateHeaderPredicateErrorFmt = "duplicate header predicate: %s" @@ -712,29 +712,28 @@ func ParsePredicates(p string) ([]*Predicate, error) { return ps, nil } -const randomIdLength = 16 - -var routeIdRx = regexp.MustCompile(`\W`) +const ( + randomIdLength = 16 + // does not contain underscore to produce compatible output with previously used flow id generator + alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" +) // generate weak random id for a route if // it doesn't have one. +// +// Deprecated: do not use, generate valid route id that matches [a-zA-Z_] yourself. func GenerateIfNeeded(existingId string) string { if existingId != "" { return existingId } - // using this to avoid adding a new dependency. - g, err := flowid.NewStandardGenerator(randomIdLength) - if err != nil { - return existingId - } - id, err := g.Generate() - if err != nil { - return existingId + var sb strings.Builder + sb.WriteString("route") + + for i := 0; i < randomIdLength; i++ { + ai := rand.Intn(len(alphabet)) + sb.WriteByte(alphabet[ai]) } - // replace characters that are not allowed - // for eskip route ids. - id = routeIdRx.ReplaceAllString(id, "x") - return "route" + id + return sb.String() } diff --git a/etcd/etcd.go b/etcd/etcd.go index 3dda1216ed..50843f103a 100644 --- a/etcd/etcd.go +++ b/etcd/etcd.go @@ -493,6 +493,7 @@ func (c *Client) Delete(id string) error { func (c *Client) UpsertAll(routes []*eskip.Route) error { for _, r := range routes { + //lint:ignore SA1019 due to backward compatibility r.Id = eskip.GenerateIfNeeded(r.Id) err := c.Upsert(r) if err != nil {