Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"elif",
"emeraldlake",
"exitcode",
"filenamify",
"githubaction",
"githubactions",
"gocyclo",
Expand Down Expand Up @@ -149,6 +150,7 @@
"startswith",
"staticcheck",
"stmsg",
"tailscale",
"testregex",
"textfile",
"tianocore",
Expand Down
37 changes: 37 additions & 0 deletions cmd/firmware-action/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"log/slog"
"os"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/plus3it/gorecurcopy"
Expand Down Expand Up @@ -244,6 +246,10 @@ func AnyFileNewerThan(path string, givenTime time.Time) (bool, error) {
return nil
})
if errors.Is(errMod, ErrFileModified) {
slog.Debug(
"Detected changes in files",
slog.Any("error", errMod),
)
return true, nil
}
return false, nil
Expand All @@ -265,3 +271,34 @@ func AnyFileNewerThan(path string, givenTime time.Time) (bool, error) {
// If path is neither file nor directory
return false, err
}

func sanitizeAndTruncate(input string, length int) string {
// What characters are forbidden in Windows and Linux directory names?
// https://stackoverflow.com/a/31976060

// ASCII characters (printable and non-printable)
// https://en.wikipedia.org/wiki/Control_character#In_ASCII
charactersASCII := regexp.MustCompile(`[<>:"/\\|?*\x00-\x1F\x7F]`)
result := charactersASCII.ReplaceAllString(input, "_")

// just for a good measure
betterAvoid := regexp.MustCompile(`[!{}()[\]^$+*?. -]`)
result = betterAvoid.ReplaceAllString(result, "_")

// Remove invalid UTF-8 byte sequences
// - this includes unicode control characters
// https://www.compart.com/en/unicode/category/Cc
result = strings.ToValidUTF8(result, "_")

if len(result) > length {
return string(result[:length])
}
return result
}

// Filenamify converts a string to a valid and safe filename
func Filenamify(name string, extension string) string {
// Maximum length (let's assume maximum of 255 bytes and let's assume 14 bytes for extension)
// https://serverfault.com/a/306726
return sanitizeAndTruncate(name, 240) + "." + sanitizeAndTruncate(extension, 14)
}
124 changes: 124 additions & 0 deletions cmd/firmware-action/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,127 @@ func TestAnyFileNewerThan(t *testing.T) {
assert.NoError(t, err)
assert.False(t, mod)
}

func TestFilenamify(t *testing.T) {
testCases := []struct {
name string
inputName string
inputExtension string
output string
}{
{
name: "empty strings",
inputName: "",
inputExtension: "",
output: ".",
},
{
name: "unicode control characters",
inputName: "foo\u0000bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo<bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo>bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo:bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo\"bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo/bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo\\bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo\bar",
inputExtension: "",
output: "foo_ar.",
},
{
inputName: "foo|bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo?bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo*bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo/bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo!bar",
inputExtension: "",
output: "foo_bar.",
},
{
inputName: "foo//bar",
inputExtension: "",
output: "foo__bar.",
},
{
inputName: "//foo//bar//",
inputExtension: "",
output: "__foo__bar__.",
},
{
inputName: "foo\\\\\\bar",
inputExtension: "",
output: "foo___bar.",
},
{
inputName: "foo[*]bar",
inputExtension: "",
output: "foo___bar.",
},
{
inputName: "foo bar",
inputExtension: "01234567890123456789",
output: "foo_bar.01234567890123",
},
{
inputName: "foo\nbar",
inputExtension: "txt",
output: "foo_bar.txt",
},
{
inputName: "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789junk",
inputExtension: "txt",
output: "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.txt",
},
}
for _, tc := range testCases {
t.Run(tc.inputName, func(t *testing.T) {
result := Filenamify(tc.inputName, tc.inputExtension)
assert.Equal(t, tc.output, result)
assert.LessOrEqual(t, len(result), 255)
})
}
}
14 changes: 8 additions & 6 deletions cmd/firmware-action/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/9elements/firmware-action/cmd/firmware-action

go 1.23.0
go 1.23.1

toolchain go1.24.0

Expand Down Expand Up @@ -29,7 +29,7 @@ require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cloudflare/circl v1.6.0 // indirect
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
Expand All @@ -47,14 +47,15 @@ require (
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pjbgf/sha1cd v0.3.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.3.1 // indirect
github.com/sosodev/duration v1.3.1 // indirect
github.com/vektah/gqlparser/v2 v2.5.22 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/otel v1.33.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.8.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.8.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.32.0 // indirect
Expand All @@ -63,13 +64,14 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.32.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.32.0 // indirect
go.opentelemetry.io/otel/log v0.8.0 // indirect
go.opentelemetry.io/otel/metric v1.32.0 // indirect
go.opentelemetry.io/otel/metric v1.33.0 // indirect
go.opentelemetry.io/otel/sdk v1.32.0 // indirect
go.opentelemetry.io/otel/sdk/log v0.8.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.32.0 // indirect
go.opentelemetry.io/otel/trace v1.32.0 // indirect
go.opentelemetry.io/otel/trace v1.33.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/crypto v0.35.0 // indirect
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/sys v0.30.0 // indirect
Expand Down
24 changes: 14 additions & 10 deletions cmd/firmware-action/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ github.com/cloudflare/circl v1.6.0/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZ
github.com/cyphar/filepath-securejoin v0.4.1 h1:JyxxyPEaktOD+GAnqIqTf9A8tHyAG22rowi7HkoSU1s=
github.com/cyphar/filepath-securejoin v0.4.1/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o=
Expand Down Expand Up @@ -111,8 +112,9 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/plus3it/gorecurcopy v0.0.1 h1:H7AgvM0N/uIo7o1PQRlewEGQ92BNr7DqbPy5lnR3uJI=
github.com/plus3it/gorecurcopy v0.0.1/go.mod h1:NvVTm4RX68A1vQbHmHunDO4OtBLVroT6CrsiqAzNyJA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand All @@ -136,8 +138,10 @@ github.com/vektah/gqlparser/v2 v2.5.22 h1:yaaeJ0fu+nv1vUMW0Hl+aS1eiv1vMfapBNjpff
github.com/vektah/gqlparser/v2 v2.5.22/go.mod h1:xMl+ta8a5M1Yo1A1Iwt/k7gSpscwSnHZdw7tfhEGfTM=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
go.opentelemetry.io/otel v1.32.0 h1:WnBN+Xjcteh0zdk01SVqV55d/m62NJLJdIyb4y/WO5U=
go.opentelemetry.io/otel v1.32.0/go.mod h1:00DCVSB0RQcnzlwyTfqtxSm+DRr9hpYrHjNGiBHVQIg=
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
go.opentelemetry.io/otel v1.33.0 h1:/FerN9bax5LoK51X/sI0SVYrjSE0/yUL7DpxW4K3FWw=
go.opentelemetry.io/otel v1.33.0/go.mod h1:SUUkR6csvUQl+yjReHu5uM3EtVV7MBm5FHKRlNx4I8I=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.8.0 h1:WzNab7hOOLzdDF/EoWCt4glhrbMPVMOO5JYTmpz36Ls=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.8.0/go.mod h1:hKvJwTzJdp90Vh7p6q/9PAOd55dI6WA6sWj62a/JvSs=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.8.0 h1:S+LdBGiQXtJdowoJoQPEtI52syEP/JYBUpjO49EQhV8=
Expand All @@ -154,25 +158,25 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.32.0 h1:cMyu9
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.32.0/go.mod h1:6Am3rn7P9TVVeXYG+wtcGE7IE1tsQ+bP3AuWcKt/gOI=
go.opentelemetry.io/otel/log v0.8.0 h1:egZ8vV5atrUWUbnSsHn6vB8R21G2wrKqNiDt3iWertk=
go.opentelemetry.io/otel/log v0.8.0/go.mod h1:M9qvDdUTRCopJcGRKg57+JSQ9LgLBrwwfC32epk5NX8=
go.opentelemetry.io/otel/metric v1.32.0 h1:xV2umtmNcThh2/a/aCP+h64Xx5wsj8qqnkYZktzNa0M=
go.opentelemetry.io/otel/metric v1.32.0/go.mod h1:jH7CIbbK6SH2V2wE16W05BHCtIDzauciCRLoc/SyMv8=
go.opentelemetry.io/otel/metric v1.33.0 h1:r+JOocAyeRVXD8lZpjdQjzMadVZp2M4WmQ+5WtEnklQ=
go.opentelemetry.io/otel/metric v1.33.0/go.mod h1:L9+Fyctbp6HFTddIxClbQkjtubW6O9QS3Ann/M82u6M=
go.opentelemetry.io/otel/sdk v1.32.0 h1:RNxepc9vK59A8XsgZQouW8ue8Gkb4jpWtJm9ge5lEG4=
go.opentelemetry.io/otel/sdk v1.32.0/go.mod h1:LqgegDBjKMmb2GC6/PrTnteJG39I8/vJCAP9LlJXEjU=
go.opentelemetry.io/otel/sdk/log v0.8.0 h1:zg7GUYXqxk1jnGF/dTdLPrK06xJdrXgqgFLnI4Crxvs=
go.opentelemetry.io/otel/sdk/log v0.8.0/go.mod h1:50iXr0UVwQrYS45KbruFrEt4LvAdCaWWgIrsN3ZQggo=
go.opentelemetry.io/otel/sdk/metric v1.32.0 h1:rZvFnvmvawYb0alrYkjraqJq0Z4ZUJAiyYCU9snn1CU=
go.opentelemetry.io/otel/sdk/metric v1.32.0/go.mod h1:PWeZlq0zt9YkYAp3gjKZ0eicRYvOh1Gd+X99x6GHpCQ=
go.opentelemetry.io/otel/trace v1.32.0 h1:WIC9mYrXf8TmY/EXuULKc8hR17vE+Hjv2cssQDe03fM=
go.opentelemetry.io/otel/trace v1.32.0/go.mod h1:+i4rkvCraA+tG6AzwloGaCtkx53Fa+L+V8e9a7YvhT8=
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 h1:yqrTHse8TCMW1M1ZCP+VAR/l0kKxwaAIqN/il7x4voA=
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
Expand Down
11 changes: 7 additions & 4 deletions cmd/firmware-action/recipes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ func (opts CommonOpts) GetOutputDir() string {
return opts.OutputDir
}

// ANCHOR: CommonOptsGetSources

// GetSources returns slice of paths to all sources which are used for build
func (opts CommonOpts) GetSources() []string {
sources := []string{}
Expand All @@ -180,6 +182,8 @@ func (opts CommonOpts) GetSources() []string {
return sources
}

// ANCHOR_END: CommonOptsGetSources

// Config is for storing parsed configuration file
type Config struct {
// defined in coreboot.go
Expand Down Expand Up @@ -294,9 +298,9 @@ type FirmwareModule interface {
buildFirmware(ctx context.Context, client *dagger.Client) error
}

// ===========
// Functions
// ===========
// ======================
// Functions for Config
// ======================

// ValidateConfig is used to validate the configuration struct read out of JSON file
func ValidateConfig(conf Config) error {
Expand Down Expand Up @@ -357,7 +361,6 @@ func ReadConfig(filepath string) (*Config, error) {
)
return nil, err
}

contentStr := string(content)

// Check if all environment variables are defined
Expand Down
4 changes: 4 additions & 0 deletions cmd/firmware-action/recipes/coreboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func (opts CorebootOpts) GetArtifacts() *[]container.Artifacts {
return opts.CommonOpts.GetArtifacts()
}

// ANCHOR: CorebootOptsGetSources

// GetSources returns slice of paths to all sources which are used for build
func (opts CorebootOpts) GetSources() []string {
sources := opts.CommonOpts.GetSources()
Expand Down Expand Up @@ -109,6 +111,8 @@ func (opts CorebootOpts) GetSources() []string {
return sources
}

// ANCHOR_END: CorebootOptsGetSources

// ProcessBlobs is used to figure out blobs from provided data
func (opts CorebootOpts) ProcessBlobs() ([]BlobDef, error) {
blobs := []BlobDef{}
Expand Down
Loading
Loading