Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
Test simple trust command line actions
Browse files Browse the repository at this point in the history
Remove a default header in some but not all 'list' commands.

Fix the sudigen behavior with respect to uuid and serial-number.

Signed-off-by: Serge Hallyn <[email protected]>
  • Loading branch information
hallyn committed Apr 4, 2023
1 parent 0874dd3 commit ea56ef7
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 18 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ jobs:
- name: install dependencies
run: |
sudo apt-get update
sudo apt-get install golang make openssl swtpm tpm2-tools qemu-kvm
- run: |
sudo apt-get install bats golang make openssl swtpm tpm2-tools qemu-kvm
- name: build
run: |
make
- name: test
run: |
make test
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ trust: cmd/trust/*.go pkg/trust/*.go

clean:
rm -f trust

.PHONY: test
test: trust
bats tests/keyset.bats
bats tests/project.bats
bats tests/sudi.bats
1 change: 0 additions & 1 deletion cmd/trust/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func doListProjects(ctx *cli.Context) error {
return fmt.Errorf("Failed reading keys directory %q: %w", trustDir, err)
}

fmt.Printf("Projects in %s:\n", keysetName)
for _, keyname := range dirs {
fmt.Printf("%s\n", keyname.Name())
}
Expand Down
22 changes: 7 additions & 15 deletions cmd/trust/sudi.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,24 @@ var sudiCmd = cli.Command{
Name: "add",
Action: doGenSudi,
Usage: "add a new sudi key to project",
Flags: []cli.Flag{
cli.StringFlag {
Name: "uuid",
Usage: "specify a machine-uuid. If unspecified, use random.",
},
},
ArgsUsage: "<keyset-name> <project-name> <serial-number>",
ArgsUsage: "<keyset-name> <project-name> [<serial-number>|uuid]",
},
},
}

// ~/.local/share/machine/trust/keys/
// keyset1/manifest/project-name/{uuid,privkey.pem,sudi.pem}
// keyset1/sudi/host-serial/{uuid,privkey.pem,sudi.pem,project-name}
// keyset1/manifest/project-name/{uuid,privkey.pem,cert.pem}
// keyset1/manifest/project-name/sudi/host-serial/{uuid,privkey.pem,cert.pem}
func doGenSudi(ctx *cli.Context) error {
args := ctx.Args()
if len(args) != 3 && len(args) != 4 {
if len(args) != 2 && len(args) != 3 {
return fmt.Errorf("Wrong number of arguments (see \"--help\")")
}
keysetName := args[0]
projName := args[1]
serial := args[2]
var myUUID string
if ctx.IsSet("uuid") {
myUUID = ctx.String("uuid")
if len(args) == 3 {
myUUID = args[2]
} else {
myUUID = uuid.NewString()
}
Expand All @@ -73,7 +66,7 @@ func doGenSudi(ctx *cli.Context) error {
}

capath := filepath.Join(keysetPath, "sudi-ca")
snPath := filepath.Join(projPath, "sudi", serial)
snPath := filepath.Join(projPath, "sudi", myUUID)
prodUUID, err := os.ReadFile(filepath.Join(projPath, "uuid"))
if err != nil {
return errors.Wrapf(err, "Failed reading project UUID")
Expand Down Expand Up @@ -146,7 +139,6 @@ func doListSudi(ctx *cli.Context) error {
return fmt.Errorf("Failed reading sudi directory %q: %w", dir, err)
}

fmt.Printf("Sudis in %s:%s:\n", keysetName, projName)
for _, sn := range serials {
fmt.Printf("%s\n", sn.Name())
}
Expand Down
26 changes: 26 additions & 0 deletions tests/helpers.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

MDIR=~/.local/share/machine
BACKUP=~/.local/share/machine.backup

function common_setup {
export TOP_DIR=$(git rev-parse --show-toplevel)
export PATH=${TOP_DIR}:$PATH

MDIR=~/.local/share/machine
BACKUP=~/.local/share/machine.backup
if [ -d "$BACKUP" ]; then
rm -rf "$BACKUP"
fi
if [ -d "$MDIR" ]; then
mv "$MDIR" "$BACKUP"
fi
}

function common_teardown {
if [ -d "$MDIR" ]; then
rm -rf "$MDIR"
fi
if [ -d "$BACKUP" ]; then
mv "$BACKUP" "$MDIR"
fi
}
22 changes: 22 additions & 0 deletions tests/keyset.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load helpers

function setup() {
common_setup
}

function teardown() {
common_teardown
}

@test "Create snakeoil keyset" {
trust keyset add snakeoil
[ -d "$MDIR/trust/keys/snakeoil/.git" ]
trust keyset list | grep snakeoil
}

@test "Create new keysets" {
trust keyset add zomg
trust keyset add --org "My organization" homenet
cnt=$(trust keyset list | wc -l)
[ $cnt -eq 2 ]
}
22 changes: 22 additions & 0 deletions tests/project.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load helpers

function setup() {
common_setup
}

function teardown() {
common_teardown
}

@test "Keyset creation creates default project" {
trust keyset add zomg
trust project list zomg | grep default
}

@test "Create project" {
trust keyset add zomg
trust project add zomg newproject
trust project list zomg | grep newproject
cnt=$(trust project list zomg | wc -l)
[ $cnt -eq 2 ]
}
33 changes: 33 additions & 0 deletions tests/sudi.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load helpers

function setup() {
common_setup
}

function teardown() {
common_teardown
}

@test "Keyset creation creates sudi" {
trust keyset add zomg
trust sudi list zomg default
}

@test "Project creation creates sudi" {
trust keyset add zomg
trust project add zomg newproject
trust sudi list zomg newproject
}

@test "Create sudi" {
trust keyset add zomg
trust project add zomg newproject
trust sudi add zomg newproject # auto-create uuid
trust sudi add zomg newproject 88db65c5-8896-4908-bf8d-8ac04ff20d5c
[ -e "$MDIR/trust/keys/zomg/manifest/newproject/sudi/88db65c5-8896-4908-bf8d-8ac04ff20d5c/cert.pem" ]
trust sudi add zomg newproject SN0001
trust sudi add zomg newproject SN0002
trust sudi list zomg newproject | grep SN0001
cnt=$(trust sudi list zomg newproject | wc -l)
[ $cnt -eq 4 ]
}

0 comments on commit ea56ef7

Please sign in to comment.