Skip to content

Commit

Permalink
Here we are again, replacing more scary characters
Browse files Browse the repository at this point in the history
For Fiesta / Kubernetes we are encountering (Container Image) version numbers with characters e.g. '@' which are not allowed. Filtering or replacing these characters at the fluent-bit level
is pretty hairy, so we opt to change this at the library level.
Currently, we are replacing all disallowed characters with '💀'.

The alternative of not doing this at the library level is to force every higher level logging system to deal with this (arbitrary) HSDP Logging limitation.

Note that practically every LogEvent field has a set of disallowed characters so to fully solve the issue we will have to add additional fields to the replaceMap and check each field in each message.
  • Loading branch information
loafoe committed May 6, 2022
1 parent a351549 commit 75fb1eb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
43 changes: 42 additions & 1 deletion logging/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,39 @@ var (
}
)

type replacer struct {
Regexp *regexp.Regexp
Replace map[string]string
}

func (r replacer) replace(input string) string {
for k, v := range r.Replace {
input = strings.ReplaceAll(input, k, v)
}
return input
}

var (
replacerMap = map[string]replacer{
"applicationVersion": {
Regexp: regexp.MustCompile(`^[^&+;=?@|<>()]*$`),
Replace: map[string]string{
"@": "💀",
"&": "💀",
"+": "💀",
";": "💀",
"=": "💀",
"?": "💀",
"<": "💀",
">": "💀",
"|": "💀",
"(": "💀",
")": "💀",
},
},
}
)

// Config the client
type Config struct {
Region string
Expand Down Expand Up @@ -169,7 +202,9 @@ func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()
defer func() {
_ = resp.Body.Close()
}()
if v != nil {
if w, ok := v.(io.Writer); ok {
_, err = io.Copy(w, resp.Body)
Expand Down Expand Up @@ -307,6 +342,12 @@ func (c *Client) performAndParseResponse(req *http.Request, msgs []Resource) (*S
}

func replaceScaryCharacters(msg *Resource) {
// Application version fixer
appVersion := replacerMap["applicationVersion"]
if !appVersion.Regexp.MatchString(msg.ApplicationVersion) {
msg.ApplicationVersion = appVersion.replace(msg.ApplicationVersion)
}

if len(msg.Custom) == 0 {
return
}
Expand Down
22 changes: 22 additions & 0 deletions logging/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,25 @@ func TestStoreResourceWithBearerToken(t *testing.T) {
t.Errorf("Expected HTTP 201, Got: %d", resp.StatusCode)
}
}

func TestApplicationVersionRegexp(t *testing.T) {
testsSet := []struct {
Field string
Input string
Fixed string
}{
{
Field: "applicationVersion",
Input: "public.ecr.aws/karpenter/controller@sha256:7779bf337cafe5080192f51819c5dc8a52ba6e7c4436ef9b2d164504d4ef8bea",
Fixed: "public.ecr.aws/karpenter/controller💀sha256:7779bf337cafe5080192f51819c5dc8a52ba6e7c4436ef9b2d164504d4ef8bea",
},
}

for _, test := range testsSet {
r := replacerMap[test.Field]
val := r.Regexp.MatchString(test.Input)
assert.False(t, val)
fixed := r.replace(test.Input)
assert.Equal(t, fixed, test.Fixed)
}
}

0 comments on commit 75fb1eb

Please sign in to comment.