diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..6eb3dc5 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,28 @@ +name: Lint +on: + push: + branches: + - main + pull_request: + paths: + - '**' +env: + FORCE_COLOR: 1 +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Install Go + uses: actions/setup-go@v4 + - name: Install earthly + uses: Luet-lab/luet-install-action@v1 + with: + repository: quay.io/kairos/packages + packages: utils/earthly + - name: Run Lint checks + run: | + earthly +lint diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 0189eb3..1a00e3f 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -1,9 +1,9 @@ name: Unit tests on: - push: - branches: - - master - pull_request: + push: + branches: + - master + pull_request: jobs: unit-tests: @@ -16,7 +16,7 @@ jobs: - name: Install Go uses: actions/setup-go@v3 with: - go-version: '^1.18' + go-version: '^1.20' - name: Install Ginkgo run: go install github.com/onsi/ginkgo/v2/ginkgo@v2.5.0 diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..242569f --- /dev/null +++ b/.yamllint @@ -0,0 +1,21 @@ +extends: default + +rules: + # 80 chars should be enough, but don't fail if a line is longer + line-length: + max: 150 + level: warning + + # accept both key: + # - item + # + # and key: + # - item + indentation: + indent-sequences: whatever + + truthy: + check-keys: false + + document-start: + present: false \ No newline at end of file diff --git a/Earthfile b/Earthfile index f762a94..41bbe6f 100644 --- a/Earthfile +++ b/Earthfile @@ -3,6 +3,9 @@ VERSION 0.6 # TODO: This needs to come from pre-built kernels in c3os repos, kcrypt included. # Framework images should use our initrd ARG BASE_IMAGE=quay.io/kairos/core-opensuse +# renovate: datasource=docker depName=golang +ARG GO_VERSION=1.20.2 +ARG GOLINT_VERSION=1.52.2 build-kcrypt: FROM golang:alpine @@ -63,3 +66,21 @@ iso: RUN sha256sum $ISO_NAME.iso > $ISO_NAME.iso.sha256 SAVE ARTIFACT /build/$ISO_NAME.iso iso AS LOCAL build/$ISO_NAME.iso SAVE ARTIFACT /build/$ISO_NAME.iso.sha256 sha256 AS LOCAL build/$ISO_NAME.iso.sha256 + +lint: + BUILD +golint + BUILD +yamllint + +golint: + ARG GO_VERSION + FROM golang:$GO_VERSION + ARG GOLINT_VERSION + RUN wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v$GOLINT_VERSION + WORKDIR /build + COPY . . + RUN golangci-lint run + +yamllint: + FROM cytopia/yamllint + COPY . . + RUN yamllint .github/workflows/ diff --git a/go.mod b/go.mod index a784a66..c75d0c3 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/kairos-io/kcrypt -go 1.18 +go 1.20 require ( github.com/anatol/luks.go v0.0.0-20230125211543-ada2562d4206 diff --git a/main.go b/main.go index 87d68dc..0094a94 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -24,8 +23,11 @@ var Version = "v0.0.0-dev" func waitdevice(device string, attempts int) error { for tries := 0; tries < attempts; tries++ { - sh("udevadm settle") - _, err := os.Lstat(device) + _, err := sh("udevadm settle") + if err != nil { + return err + } + _, err = os.Lstat(device) if !os.IsNotExist(err) { return nil } @@ -44,7 +46,10 @@ func getPassword(b *block.Partition) (password string, err error) { err = fmt.Errorf("failed discovery: %s", r.Error) } }) - bus.Manager.Publish(bus.EventDiscoveryPassword, b) + _, err = bus.Manager.Publish(bus.EventDiscoveryPassword, b) + if err != nil { + return password, err + } if password == "" { return password, fmt.Errorf("received empty password") @@ -95,19 +100,6 @@ func createLuks(dev, password, version string, cryptsetupArgs ...string) error { return nil } -func createDiskImage() (*os.File, error) { - disk, err := ioutil.TempFile("", "luksv2.go.disk") - if err != nil { - return nil, err - } - - if err := disk.Truncate(24 * 1024 * 1024); err != nil { - return nil, err - } - - return disk, err -} - // TODO: A crypt disk utility to call after install, that with discovery discoveries the password that should be used // this function should delete COS_PERSISTENT. delete the partition and create a luks+type in place. @@ -210,9 +202,13 @@ func detect(archive string) (string, error) { // TODO: replace with golang native code func extractInitrd(initrd string, dst string) error { - os.MkdirAll(dst, os.ModePerm) var out string var err error + err = os.MkdirAll(dst, os.ModePerm) + if err != nil { + return err + } + format, err := detect(initrd) if err != nil { return err @@ -256,7 +252,7 @@ func injectInitrd(initrd string, file, dst string) error { if err != nil { return err } - tmp, err := ioutil.TempDir("", "kcrypt") + tmp, err := os.MkdirTemp("", "kcrypt") if err != nil { return fmt.Errorf("cannot create tempdir, %s", err) } @@ -297,8 +293,8 @@ func unlockAll() error { if p.Type == "crypto_LUKS" { p.Label = config.LookupLabelForUUID(p.UUID) fmt.Printf("Unmounted Luks found at '%s' LABEL '%s' \n", p.Name, p.Label) - err = multierror.Append(err, unlockDisk(p)) - if err != nil { + multiError := multierror.Append(err, unlockDisk(p)) + if multiError.ErrorOrNil() != nil { fmt.Printf("Unlocking failed: '%s'\n", err.Error()) } time.Sleep(10 * time.Second) diff --git a/pkg/config/config.go b/pkg/config/config.go index a56b8b5..2c0f28e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -4,7 +4,7 @@ package config import ( "fmt" - "io/ioutil" + "os" "strings" "github.com/jaypipes/ghw/pkg/block" @@ -97,7 +97,7 @@ func (c *Config) WriteMappings(fileName string) error { data = append([]byte(collector.DefaultHeader+"\n"), data...) - err = ioutil.WriteFile(fileName, data, 0744) + err = os.WriteFile(fileName, data, 0744) if err != nil { return errors.Wrap(err, "writing the kcrypt configuration file") } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 9a09316..6b215b2 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -124,8 +124,10 @@ kcrypt: }) It("replaces the file contents", func() { - c.SetMapping("COS_PERSISTENT:the_new_name:the_new_uuid") - c.WriteMappings(tmpFile.Name()) + err := c.SetMapping("COS_PERSISTENT:the_new_name:the_new_uuid") + Expect(err).ToNot(HaveOccurred()) + err = c.WriteMappings(tmpFile.Name()) + Expect(err).ToNot(HaveOccurred()) data, err := os.ReadFile(tmpFile.Name()) Expect(err).ToNot(HaveOccurred()) Expect(collector.HasValidHeader(string(data))).To(BeTrue()) @@ -145,8 +147,9 @@ kcrypt: }) It("creates the file with the given mappings", func() { - c.SetMapping("COS_PERSISTENT:the_new_name:the_new_uuid") - err := c.WriteMappings(tmpFile.Name()) + err := c.SetMapping("COS_PERSISTENT:the_new_name:the_new_uuid") + Expect(err).ToNot(HaveOccurred()) + err = c.WriteMappings(tmpFile.Name()) Expect(err).ToNot(HaveOccurred()) newConfig, err := configpkg.GetConfiguration([]string{tmpDir}) @@ -164,8 +167,10 @@ kcrypt: tmpFile, err = os.CreateTemp(tmpDir, "config-*.yaml") Expect(err).ToNot(HaveOccurred()) // Should trim the whitespace - c.SetMapping("COS_PERSISTENT:the_new_name:some_uuid_1\n") - c.WriteMappings(tmpFile.Name()) + err = c.SetMapping("COS_PERSISTENT:the_new_name:some_uuid_1\n") + Expect(err).ToNot(HaveOccurred()) + err = c.WriteMappings(tmpFile.Name()) + Expect(err).ToNot(HaveOccurred()) }) It("returns the correct UUID", func() { @@ -192,8 +197,10 @@ kcrypt: BeforeEach(func() { tmpFile, err = os.CreateTemp(tmpDir, "config-*.yaml") Expect(err).ToNot(HaveOccurred()) - c.SetMapping("COS_PERSISTENT:the_new_name:some_uuid_1") - c.WriteMappings(tmpFile.Name()) + err = c.SetMapping("COS_PERSISTENT:the_new_name:some_uuid_1") + Expect(err).ToNot(HaveOccurred()) + err = c.WriteMappings(tmpFile.Name()) + Expect(err).ToNot(HaveOccurred()) }) It("returns the correct label", func() {