Compare commits

..

4 Commits

Author SHA1 Message Date
Dimitris Karakasilis
7310c313ee Merge pull request #5 from kairos-io/fix-version-bug-and-typpos
Fix typo in name, bad function call and version of the binary
2022-11-15 15:13:05 +02:00
Dimitris Karakasilis
b9572125e1 Fix typo in name, bad function call and version of the binary
Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
2022-11-15 15:09:56 +02:00
Dimitris Karakasilis
44ccb84dfb Merge pull request #4 from kairos-io/create-info-file-if-not-exists
Create the partition file if it doesn't exist
2022-11-15 12:39:00 +02:00
Dimitris Karakasilis
a0a7c1269f Create the partition file if id doesn't exist
so that the caller doesn't have to care if it's the first time the
partition is encrypted or not.

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>
2022-11-15 12:17:32 +02:00
4 changed files with 67 additions and 15 deletions

View File

@@ -6,9 +6,11 @@ ARG BASE_IMAGE=quay.io/kairos/core-opensuse
build-kcrypt:
FROM golang:alpine
RUN apk add git
COPY . /work
WORKDIR /work
RUN CGO_ENABLED=0 go build -o kcrypt
ARG VERSION="$(git describe --tags)"
RUN CGO_ENABLED=0 go build -o kcrypt -ldflags "-X main.Version=$VERSION"
SAVE ARTIFACT /work/kcrypt AS LOCAL kcrypt
build-dracut:

10
main.go
View File

@@ -21,6 +21,8 @@ import (
pi "github.com/kairos-io/kcrypt/pkg/partition_info"
)
var Version = "v0.0.0-dev"
func waitdevice(device string, attempts int) error {
for tries := 0; tries < attempts; tries++ {
sh("udevadm settle")
@@ -279,7 +281,7 @@ func injectInitrd(initrd string, file, dst string) error {
func unlockAll() error {
bus.Manager.Initialize()
partitionInfo, err := pi.NewPartitionInfoFromFile(pi.DefaultPartitionInfoFile)
partitionInfo, _, err := pi.NewPartitionInfoFromFile(pi.DefaultPartitionInfoFile)
if err != nil {
return err
}
@@ -305,10 +307,10 @@ func unlockAll() error {
func main() {
app := &cli.App{
Name: "keiros-kcrypt",
Version: "0.1",
Name: "kairos-kcrypt",
Version: Version,
Author: "Ettore Di Giacinto",
Usage: "keiros escrow key agent component",
Usage: "kairos escrow key agent component",
Description: ``,
UsageText: ``,
Copyright: "Ettore Di Giacinto",

View File

@@ -3,6 +3,7 @@ package partition_info
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/jaypipes/ghw/pkg/block"
@@ -22,16 +23,27 @@ type PartitionInfo struct {
mapping map[string]string
}
func NewPartitionInfoFromFile(file string) (*PartitionInfo, error) {
// NewPartitionInfoFromFile reads the given partition info file (if one exists)
// and returns a pointer to a PartitionInfo object.
// If a file doesn't exist, the function will create one and return an "empty"
// PartitionInfo object.
// The boolean return value indicates whether a file existed or not (true means,
// a file existed already).
func NewPartitionInfoFromFile(file string) (*PartitionInfo, bool, error) {
existed, err := createInfoFileIfNotExists(file)
if err != nil {
return nil, existed, err
}
mapping, err := ParsePartitionInfoFile(file)
if err != nil {
return nil, err
return nil, existed, err
}
return &PartitionInfo{
file: file,
mapping: mapping,
}, nil
}, existed, nil
}
func (pi PartitionInfo) LookupUUIDForLabel(l string) string {
@@ -98,3 +110,16 @@ func ParsePartitionInfoFile(file string) (map[string]string, error) {
return result, nil
}
// createInfoFileIfNotExists returns true if file already exists or creates the
// the file if it doesn't exist and returns false.
func createInfoFileIfNotExists(fileName string) (bool, error) {
_, err := os.Stat(fileName)
if errors.Is(err, os.ErrNotExist) {
if _, err := os.Create(fileName); err != nil {
return false, err
}
return false, nil
}
return true, nil
}

View File

@@ -1,8 +1,11 @@
package partition_info_test
import (
"fmt"
"io/ioutil"
"os"
"path"
"time"
"github.com/jaypipes/ghw/pkg/block"
. "github.com/onsi/ginkgo/v2"
@@ -18,11 +21,31 @@ var _ = Describe("Partition Info file parsing", func() {
BeforeEach(func() {
file = "../../tests/assets/partition_info.yaml"
})
When("the files exists already", func() {
It("returns 'true' and a PartitionInfo object", func() {
result, existed, err := pi.NewPartitionInfoFromFile(file)
Expect(err).ToNot(HaveOccurred())
Expect(result).ToNot(BeNil())
Expect(existed).To(BeTrue())
})
})
It("returns a PartitionInfo", func() {
result, err := pi.NewPartitionInfoFromFile(file)
Expect(err).ToNot(HaveOccurred())
Expect(result).ToNot(BeNil())
When("a file doesn't exist", func() {
var fileName string
BeforeEach(func() {
fileName = path.Join(
os.TempDir(),
fmt.Sprintf("partition-info-%d.yaml", time.Now().UnixNano()))
})
It("creates the file and returns 'false' and an (empty) mapping", func() {
result, existed, err := pi.NewPartitionInfoFromFile(fileName)
Expect(err).ToNot(HaveOccurred())
Expect(result).ToNot(BeNil())
Expect(existed).To(BeFalse())
_, err = os.Stat(fileName)
Expect(err).ToNot(HaveOccurred())
})
})
})
@@ -86,7 +109,7 @@ var _ = Describe("Partition Info file parsing", func() {
_, err = file.Write([]byte("TO_KEEP: old_uuid_1"))
Expect(err).ToNot(HaveOccurred())
partitionInfo, err = pi.NewPartitionInfoFromFile(file.Name())
partitionInfo, _, err = pi.NewPartitionInfoFromFile(file.Name())
Expect(err).ToNot(HaveOccurred())
})
@@ -117,7 +140,7 @@ TO_KEEP: old_uuid_1
BeforeEach(func() {
file = "../../tests/assets/partition_info.yaml"
partitionInfo, err = pi.NewPartitionInfoFromFile(file)
partitionInfo, _, err = pi.NewPartitionInfoFromFile(file)
Expect(err).ToNot(HaveOccurred())
})
@@ -139,7 +162,7 @@ TO_KEEP: old_uuid_1
BeforeEach(func() {
file = "../../tests/assets/partition_info.yaml"
partitionInfo, err = pi.NewPartitionInfoFromFile(file)
partitionInfo, _, err = pi.NewPartitionInfoFromFile(file)
Expect(err).ToNot(HaveOccurred())
})