enclave_runtime.go 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
package runtime // import "github.com/opencontainers/runc/libenclave/internal/runtime"

import (
	"github.com/opencontainers/runc/libenclave/configs"
	core "github.com/opencontainers/runc/libenclave/internal/runtime/core"
	pal "github.com/opencontainers/runc/libenclave/internal/runtime/pal"
	"github.com/sirupsen/logrus"
	"os"
	"os/exec"
jia zhang's avatar
jia zhang 已提交
10
	"strings"
11 12 13 14
)

type EnclaveRuntime interface {
	Init(args string, logLevel string) error
15
	Attest(string, string, uint32, uint32) error
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
	Exec(cmd []string, envp []string, stdio [3]*os.File) (int32, error)
	Kill(sig int, pid int) error
	Destroy() error
}

type EnclaveRuntimeWrapper struct {
	runtime EnclaveRuntime
}

func StartInitialization(config *configs.InitEnclaveConfig, logLevel string) (*EnclaveRuntimeWrapper, error) {
	logrus.Debugf("enclave init config retrieved: %+v", config)

	var (
		runtime EnclaveRuntime
		err     error
	)
	runtime, err = core.StartInitialization(config)
	if err != nil {
		runtime, err = pal.StartInitialization(config)
		if err != nil {
			return nil, err
		}
	}

40
	logrus.Infof("Initializing enclave runtime")
41 42 43 44 45 46 47 48 49 50 51
	err = runtime.Init(config.Args, logLevel)
	if err != nil {
		return nil, err
	}

	rt := &EnclaveRuntimeWrapper{
		runtime: runtime,
	}
	return rt, nil
}

52
func (rt *EnclaveRuntimeWrapper) LaunchAttestation(spid string, subscriptionKey string, product uint32, quoteType uint32) error {
53
	logrus.Debugf("attesting enclave runtime")
54

55
	return rt.runtime.Attest(spid, subscriptionKey, product, quoteType)
56 57 58
}

func (rt *EnclaveRuntimeWrapper) ExecutePayload(cmd []string, envp []string, stdio [3]*os.File) (int32, error) {
jia zhang's avatar
jia zhang 已提交
59
	logrus.Debugf("enclave runtime %s executing payload with commandline", strings.Join(cmd, " "))
60 61 62 63 64 65 66 67 68 69 70

	// The executable may not exist in container at all according
	// to the design of enclave runtime, such as Occlum, which uses
	// an invisible filesystem to the container. In this case, the
	// lookup will fail.
	if fullPath, err := exec.LookPath(cmd[0]); err == nil {
		cmd[0] = fullPath
	}
	return rt.runtime.Exec(cmd, envp, stdio)
}

71
func (rt *EnclaveRuntimeWrapper) KillPayload(pid int, sig int) error {
72
	if pid != -1 {
73
		logrus.Debugf("enclave runtime killing payload %d with signal %d", pid, sig)
74
	} else {
75
		logrus.Debugf("enclave runtime killing all payloads with signal %d", sig)
76 77
	}

78
	return rt.runtime.Kill(pid, sig)
79 80 81
}

func (rt *EnclaveRuntimeWrapper) DestroyInstance() error {
82
	logrus.Debugf("Destroying enclave runtime")
83 84 85

	return rt.runtime.Destroy()
}