Skip to content

Commit

Permalink
Streamline custom error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Nirusu committed Sep 22, 2022
1 parent 0b1bb9d commit 9a17f03
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
11 changes: 4 additions & 7 deletions ego/cli/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (
"io"
"os"
"strings"

"github.com/fatih/color"
)

// ErrErrUnsupportedImportEClient is returned when an EGo binary uses the eclient package instead of the enclave package.
var ErrUnsupportedImportEClient = errors.New("unsupported import: github.com/edgelesssys/ego/eclient")

func (c *Cli) embedConfigAsPayload(path string, jsonData []byte) error {
// Load ELF executable
f, err := c.fs.OpenFile(path, os.O_RDWR, 0)
Expand Down Expand Up @@ -128,11 +129,7 @@ func (c *Cli) checkUnsupportedImports(path string) error {
// Iterate through all symbols and find whether it matches a known unsupported one
for _, symbol := range symbols {
if strings.Contains(symbol.Name, "github.com/edgelesssys/ego/eclient") {
boldPrint := color.New(color.Bold).SprintFunc()
fmt.Printf("ERROR: You cannot import the %s package within the EGo enclave.\n", boldPrint("github.com/edgelesssys/ego/eclient"))
fmt.Printf("It is intended to be used for applications running outside the SGX enclave.\n")
fmt.Printf("You can use the %s package as a replacement for usage inside the enclave.\n", boldPrint("github.com/edgelesssys/ego/enclave"))
return errors.New("unsupported import: github.com/edgelesssys/ego/eclient")
return ErrUnsupportedImportEClient
}
}

Expand Down
18 changes: 8 additions & 10 deletions ego/ego/cmd/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
package cmd

import (
"log"

"ego/cli"

"github.com/spf13/cobra"
)

func newSignCmd() *cobra.Command {
return &cobra.Command{
Use: "sign [executable | config.json]",
Short: "Sign an executable built with ego-go",
Long: "Sign an executable built with ego-go. Executables must be signed before they can be run in an enclave.",
Use: "sign [executable | config.json]",
Short: "Sign an executable built with ego-go",
Long: "Sign an executable built with ego-go. Executables must be signed before they can be run in an enclave.",
SilenceErrors: true,
Example: ` ego sign <executable>
Generates a new key "private.pem" and a default configuration "enclave.json" in the current directory and signs the executable.
Expand All @@ -36,10 +33,11 @@ func newSignCmd() *cobra.Command {
filename = args[0]
}
err := newCli().Sign(filename)
if err == cli.ErrNoOEInfo {
log.Fatalln("ERROR: The .oeinfo section is missing in the binary.\nMaybe the binary was not built with 'ego-go build'?")
handleErr(err)
if err != nil {
return err
}
return err
return nil
},
}
}
8 changes: 2 additions & 6 deletions ego/ego/cmd/signerid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ package cmd

import (
"fmt"
"log"

"ego/internal/launch"

"github.com/spf13/cobra"
)
Expand All @@ -21,13 +18,12 @@ func newSigneridCmd() *cobra.Command {
Short: "Print the SignerID of a signed executable",
Long: "Print the SignerID either from a signed executable or by reading a key file.",
Args: cobra.ExactArgs(1),
SilenceErrors: true,
DisableFlagsInUseLine: true,

RunE: func(cmd *cobra.Command, args []string) error {
id, err := newCli().Signerid(args[0])
if err == launch.ErrOECrypto {
log.Fatalf("ERROR: signerid failed with %v.\nMake sure to pass a valid public key.", err)
}
handleErr(err)
if err != nil {
return err
}
Expand Down
12 changes: 11 additions & 1 deletion ego/ego/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"ego/cli"
"ego/internal/launch"

"github.com/fatih/color"
"github.com/klauspost/cpuid/v2"
"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -69,7 +70,16 @@ func handleErr(err error) {
fmt.Println("ERROR: failed to initialize the enclave")
fmt.Println("Install the SGX base package with: sudo ego install libsgx-enclave-common")
fmt.Println("Or temporarily fix the error with: sudo mount -o remount,exec /dev")
case launch.ErrOECrypto:
fmt.Printf("ERROR: signerid failed with %v.\nMake sure to pass a valid public key.\n", err)
case cli.ErrNoOEInfo:
fmt.Println("ERROR: The .oeinfo section is missing in the binary.\nMaybe the binary was not built with 'ego-go build'?")
case cli.ErrUnsupportedImportEClient:
boldPrint := color.New(color.Bold).SprintFunc()
fmt.Printf("ERROR: You cannot import the %s package within the EGo enclave.\n", boldPrint("github.com/edgelesssys/ego/eclient"))
fmt.Printf("It is intended to be used for applications running outside the SGX enclave.\n")
fmt.Printf("You can use the %s package as a replacement for usage inside the enclave.\n", boldPrint("github.com/edgelesssys/ego/enclave"))
default:
fmt.Println(err)
fmt.Println("ERROR:", err)
}
}
4 changes: 2 additions & 2 deletions src/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ mkdir -p /tmp/ego-unsupported-import-test
cd $egoPath/ego/cmd/unsupported-import-test
run ego-go build -o /tmp/ego-unsupported-import-test/unsupported-import
cd /tmp/ego-unsupported-import-test
run ego sign unsupported-import |& grep "unsupported import"
run ego run unsupported-import |& grep "unsupported import"
run ego sign unsupported-import |& grep "You cannot import the github.com/edgelesssys/ego/eclient package"
run ego run unsupported-import |& grep "You cannot import the github.com/edgelesssys/ego/eclient package"

0 comments on commit 9a17f03

Please sign in to comment.