Skip to content

Commit

Permalink
Merge pull request #305 from alfrunes/MEN-7003
Browse files Browse the repository at this point in the history
MEN-7003: Add "html" encoding option
  • Loading branch information
alfrunes authored Feb 2, 2024
2 parents cdff437 + 3049032 commit 670c316
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 520 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM golang:1.21.5-alpine3.19 as builder
FROM --platform=$BUILDPLATFORM golang:1.21.6-alpine3.19 as builder
ARG TARGETARCH
WORKDIR /go/src/github.com/mendersoftware/workflows
RUN mkdir -p /etc_extra
Expand All @@ -7,6 +7,7 @@ RUN echo "nobody:!::0:::::" > /etc_extra/shadow
RUN echo "nobody:x:65534:65534:Nobody:/:" > /etc_extra/passwd
RUN chown -R nobody:nobody /etc_extra
RUN apk add --no-cache \
git \
xz-dev \
musl-dev \
ca-certificates \
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile.acceptance-testing
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
FROM golang:1.21.5-alpine3.19 as builder
FROM golang:1.21.6-alpine3.19 as builder
WORKDIR /go/src/github.com/mendersoftware/workflows
RUN mkdir -p /etc_extra
RUN echo "nobody:x:65534:" > /etc_extra/group
RUN echo "nobody:!::0:::::" > /etc_extra/shadow
RUN echo "nobody:x:65534:65534:Nobody:/:" > /etc_extra/passwd
RUN chown -R nobody:nobody /etc_extra
RUN apk add --no-cache \
git \
xz-dev \
musl-dev \
ca-certificates \
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile.acceptance-testing.worker
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
FROM golang:1.19.3-alpine3.15 as builder
FROM golang:1.21.6-alpine3.19 as builder
WORKDIR /go/src/github.com/mendersoftware/workflows
RUN mkdir -p /etc_extra
RUN echo "nobody:x:65534:" > /etc_extra/group
RUN echo "nobody:!::0:::::" > /etc_extra/shadow
RUN echo "nobody:x:65534:65534:Nobody:/:" > /etc_extra/passwd
RUN chown -R nobody:nobody /etc_extra
RUN apk add --no-cache \
git \
xz-dev \
musl-dev \
ca-certificates \
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile.worker
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM golang:1.21.5-alpine3.19 as builder
FROM --platform=$BUILDPLATFORM golang:1.21.6-alpine3.19 as builder
ARG TARGETARCH
WORKDIR /go/src/github.com/mendersoftware/workflows
RUN mkdir -p /etc_extra
Expand All @@ -7,6 +7,7 @@ RUN echo "nobody:!::0:::::" > /etc_extra/shadow
RUN echo "nobody:x:65534:65534:Nobody:/:" > /etc_extra/passwd
RUN chown -R nobody:nobody /etc_extra
RUN apk add --no-cache \
git \
xz-dev \
musl-dev \
ca-certificates \
Expand Down
41 changes: 34 additions & 7 deletions app/processor/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package processor

import (
"bytes"
"html"
"net/url"
"os"
"regexp"
Expand All @@ -34,8 +35,10 @@ const (
regexVariable = `\$\{(?P<options>(?:(?:[a-zA-Z]+)=(?:[a-zA-Z0-9]+);)*)` +
`(?P<name>[^;\}\|]+)(?:\|(?P<default>[^\}]+))?}`
regexOutputVariable = `(.*)\.json\.(.*)`
encodingFlag = "encoding"
urlEncodingFlag = "url"

encodingFlag = "encoding"
encodingURL = "url"
encodingHTML = "html"
)

var (
Expand All @@ -52,8 +55,32 @@ type Encoding int64
const (
EncodingPlain Encoding = iota
EncodingURL
EncodingHTML
)

func ParseString(s string) Encoding {
var enc Encoding
switch s {
case encodingURL:
enc = EncodingURL
case encodingHTML:
enc = EncodingHTML
default:
enc = EncodingPlain
}
return enc
}

func (enc Encoding) Apply(s string) string {
switch enc {
case EncodingURL:
return url.QueryEscape(s)
case EncodingHTML:
return html.EscapeString(s)
}
return s
}

type JobStringProcessor struct {
workflow *model.Workflow
job *model.Job
Expand All @@ -80,14 +107,16 @@ func processOptionString(expression string) (opts Options) {
rValueIndex = 1
)
for _, flagToken := range strings.Split(expression, ";") {
flagValueTokens := strings.Split(flagToken, "=")
flagValueTokens := strings.SplitN(flagToken, "=", 2)
if len(flagValueTokens) < flagTokenCount {
continue
}
if flagValueTokens[lValueIndex] == encodingFlag {
switch flagValueTokens[rValueIndex] {
case urlEncodingFlag:
case encodingURL:
opts.Encoding = EncodingURL
case encodingHTML:
opts.Encoding = EncodingHTML
}
}
}
Expand Down Expand Up @@ -155,9 +184,7 @@ SubMatchLoop:
}
}
}
if options.Encoding == EncodingURL {
value = url.QueryEscape(value)
}
value = options.Encoding.Apply(value)
data = strings.ReplaceAll(data, submatch[0], value)
}

Expand Down
15 changes: 12 additions & 3 deletions app/processor/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func TestProcessOptionString(t *testing.T) {
var expectedEncoding Encoding
var o Options

setEncoding = urlEncodingFlag
setEncoding = encodingURL
expectedEncoding = EncodingURL
data = "encoding=" + setEncoding + ";"
o = processOptionString(data)
assert.Equal(t, Options{Encoding: expectedEncoding}, o)

setEncoding = urlEncodingFlag
setEncoding = encodingURL
expectedEncoding = EncodingURL
data = "rightFlag=right;encoding=" + setEncoding + ";leftFlag=left;"
o = processOptionString(data)
Expand All @@ -51,14 +51,23 @@ func TestProcessOptionString(t *testing.T) {
assert.Equal(t, Options{Encoding: expectedEncoding}, o)
}

func TestHTMLEncoding(t *testing.T) {
const (
expected = `It escapes only five such characters: &lt;, &gt;, &amp;, &#39; and &#34;`
input = `It escapes only five such characters: <, >, &, ' and "`
)
actual := EncodingHTML.Apply(input)
assert.Equal(t, expected, actual)
}

func TestVariableParse(t *testing.T) {
var data string
var setEncoding string
var expectedVariableIdentifier string
var expectedEncoding Encoding

expectedEncoding = EncodingURL
setEncoding = urlEncodingFlag
setEncoding = encodingURL
expectedVariableIdentifier = "newVariable0"
data = "${encoding=" + setEncoding + ";" + workflowInputVariable + expectedVariableIdentifier + "}"
matches := reExpression.FindAllStringSubmatch(data, -1)
Expand Down
66 changes: 33 additions & 33 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,72 @@ go 1.18

require (
github.com/gin-gonic/gin v1.9.1
github.com/mendersoftware/go-lib-micro v0.0.0-20231212090330-dbd92a22bbee
github.com/nats-io/nats.go v1.31.0
github.com/mendersoftware/go-lib-micro v0.0.0-20240110171355-b10f353cc448
github.com/nats-io/nats.go v1.32.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/thedevsaddam/gojsonq v2.3.0+incompatible
github.com/urfave/cli v1.22.14
go.mongodb.org/mongo-driver v1.13.1
golang.org/x/sys v0.15.0
golang.org/x/sys v0.16.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/ant0ine/go-json-rest v3.3.3-0.20170913041208-ebb33769ae01+incompatible // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/klauspost/compress v1.17.5 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.3.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/nats-io/nkeys v0.4.6 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.17.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.3.0 // indirect
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
Loading

0 comments on commit 670c316

Please sign in to comment.