mirror of
https://github.com/kairos-io/kcrypt.git
synced 2025-11-10 23:40:22 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7310c313ee | ||
|
|
b9572125e1 | ||
|
|
44ccb84dfb | ||
|
|
a0a7c1269f |
@@ -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
10
main.go
@@ -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",
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user