2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-05 09:12:20 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-07-04 06:59:43 -04:00
|
|
|
"bytes"
|
2022-04-11 08:25:19 -04:00
|
|
|
"context"
|
2022-07-04 06:59:43 -04:00
|
|
|
"encoding/json"
|
2022-04-05 09:12:20 -04:00
|
|
|
"flag"
|
2022-09-08 08:45:27 -04:00
|
|
|
"net"
|
2022-07-04 06:59:43 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-04-05 09:12:20 -04:00
|
|
|
"path/filepath"
|
2022-04-11 08:25:19 -04:00
|
|
|
"strings"
|
2022-04-05 09:12:20 -04:00
|
|
|
|
2022-09-08 08:45:27 -04:00
|
|
|
"github.com/edgelesssys/constellation/disk-mapper/internal/mapper"
|
|
|
|
"github.com/edgelesssys/constellation/disk-mapper/internal/recoveryserver"
|
|
|
|
"github.com/edgelesssys/constellation/disk-mapper/internal/rejoinclient"
|
|
|
|
"github.com/edgelesssys/constellation/disk-mapper/internal/setup"
|
|
|
|
"github.com/edgelesssys/constellation/internal/atls"
|
2022-06-01 09:08:42 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/azure"
|
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/gcp"
|
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/qemu"
|
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/vtpm"
|
2022-08-29 08:30:20 -04:00
|
|
|
azurecloud "github.com/edgelesssys/constellation/internal/cloud/azure"
|
|
|
|
gcpcloud "github.com/edgelesssys/constellation/internal/cloud/gcp"
|
2022-06-29 10:17:23 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/metadata"
|
2022-08-29 08:30:20 -04:00
|
|
|
qemucloud "github.com/edgelesssys/constellation/internal/cloud/qemu"
|
2022-06-28 10:51:30 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-09-08 08:45:27 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/grpc/dialer"
|
2022-06-28 10:51:30 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/logger"
|
2022-07-04 06:59:43 -04:00
|
|
|
tpmClient "github.com/google/go-tpm-tools/client"
|
|
|
|
"github.com/google/go-tpm/tpm2"
|
2022-04-11 08:25:19 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-06-28 10:51:30 -04:00
|
|
|
"go.uber.org/zap"
|
2022-04-05 09:12:20 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-04-11 08:25:19 -04:00
|
|
|
gcpStateDiskPath = "/dev/disk/by-id/google-state-disk"
|
|
|
|
azureStateDiskPath = "/dev/disk/azure/scsi1/lun0"
|
2022-04-21 10:28:47 -04:00
|
|
|
qemuStateDiskPath = "/dev/vda"
|
2022-04-05 09:12:20 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-07-01 10:17:06 -04:00
|
|
|
csp := flag.String("csp", "", "Cloud Service Provider the image is running on")
|
|
|
|
verbosity := flag.Int("v", 0, logger.CmdLineVerbosityDescription)
|
2022-04-11 08:25:19 -04:00
|
|
|
|
2022-07-01 10:17:06 -04:00
|
|
|
flag.Parse()
|
|
|
|
log := logger.New(logger.JSONLog, logger.VerbosityFromInt(*verbosity))
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.String("version", constants.VersionInfo), zap.String("cloudProvider", *csp)).
|
|
|
|
Infof("Starting disk-mapper")
|
2022-04-21 10:28:47 -04:00
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
// set up metadata API and quote issuer for aTLS connections
|
|
|
|
var err error
|
|
|
|
var diskPath string
|
2022-09-08 08:45:27 -04:00
|
|
|
var issuer atls.Issuer
|
|
|
|
var metadataAPI setup.MetadataAPI
|
2022-04-11 08:25:19 -04:00
|
|
|
switch strings.ToLower(*csp) {
|
|
|
|
case "azure":
|
2022-07-04 06:59:43 -04:00
|
|
|
diskPath, err = filepath.EvalSymlinks(azureStateDiskPath)
|
|
|
|
if err != nil {
|
|
|
|
_ = exportPCRs()
|
|
|
|
log.With(zap.Error(err)).Fatalf("Unable to resolve Azure state disk path")
|
|
|
|
}
|
2022-09-08 08:45:27 -04:00
|
|
|
metadataAPI, err = azurecloud.NewMetadata(context.Background())
|
2022-04-11 08:25:19 -04:00
|
|
|
if err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error).Fatalf("Failed to create Azure metadata API")
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
2022-08-31 14:10:49 -04:00
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
issuer = azure.NewIssuer()
|
|
|
|
|
|
|
|
case "gcp":
|
2022-07-04 06:59:43 -04:00
|
|
|
diskPath, err = filepath.EvalSymlinks(gcpStateDiskPath)
|
|
|
|
if err != nil {
|
|
|
|
_ = exportPCRs()
|
|
|
|
log.With(zap.Error(err)).Fatalf("Unable to resolve GCP state disk path")
|
|
|
|
}
|
2022-04-11 08:25:19 -04:00
|
|
|
issuer = gcp.NewIssuer()
|
|
|
|
gcpClient, err := gcpcloud.NewClient(context.Background())
|
|
|
|
if err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error).Fatalf("Failed to create GCP client")
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
2022-09-08 08:45:27 -04:00
|
|
|
metadataAPI = gcpcloud.New(gcpClient)
|
2022-04-11 08:25:19 -04:00
|
|
|
|
2022-04-21 10:28:47 -04:00
|
|
|
case "qemu":
|
|
|
|
diskPath = qemuStateDiskPath
|
|
|
|
issuer = qemu.NewIssuer()
|
2022-09-08 08:45:27 -04:00
|
|
|
metadataAPI = &qemucloud.Metadata{}
|
|
|
|
_ = exportPCRs()
|
2022-04-21 10:28:47 -04:00
|
|
|
|
|
|
|
default:
|
2022-07-04 06:59:43 -04:00
|
|
|
log.Fatalf("CSP %s is not supported by Constellation", *csp)
|
2022-04-05 09:12:20 -04:00
|
|
|
}
|
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
// initialize device mapper
|
2022-08-02 06:35:23 -04:00
|
|
|
mapper, err := mapper.New(diskPath, log)
|
2022-04-05 09:12:20 -04:00
|
|
|
if err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to initialize device mapper")
|
2022-04-05 09:12:20 -04:00
|
|
|
}
|
|
|
|
defer mapper.Close()
|
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
setupManger := setup.New(
|
2022-06-28 10:51:30 -04:00
|
|
|
log.Named("setupManager"),
|
2022-04-11 08:25:19 -04:00
|
|
|
*csp,
|
2022-08-15 08:50:03 -04:00
|
|
|
diskPath,
|
2022-04-11 08:25:19 -04:00
|
|
|
afero.Afero{Fs: afero.NewOsFs()},
|
|
|
|
mapper,
|
|
|
|
setup.DiskMounter{},
|
|
|
|
vtpm.OpenVTPM,
|
|
|
|
)
|
|
|
|
|
|
|
|
// prepare the state disk
|
2022-04-12 08:24:36 -04:00
|
|
|
if mapper.IsLUKSDevice() {
|
2022-09-08 08:45:27 -04:00
|
|
|
// set up rejoin client
|
|
|
|
var self metadata.InstanceMetadata
|
|
|
|
self, err = metadataAPI.Self(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to get self metadata")
|
|
|
|
}
|
|
|
|
rejoinClient := rejoinclient.New(
|
|
|
|
dialer.New(issuer, nil, &net.Dialer{}),
|
|
|
|
self,
|
|
|
|
metadataAPI,
|
|
|
|
log.Named("rejoinClient"),
|
|
|
|
)
|
|
|
|
|
|
|
|
// set up recovery server
|
|
|
|
recoveryServer := recoveryserver.New(issuer, log.Named("recoveryServer"))
|
|
|
|
|
|
|
|
err = setupManger.PrepareExistingDisk(setup.NewNodeRecoverer(recoveryServer, rejoinClient))
|
2022-04-12 08:24:36 -04:00
|
|
|
} else {
|
2022-04-11 08:25:19 -04:00
|
|
|
err = setupManger.PrepareNewDisk()
|
2022-04-12 08:24:36 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error(err)).Fatalf("Failed to prepare state disk")
|
2022-04-12 08:24:36 -04:00
|
|
|
}
|
|
|
|
}
|
2022-07-04 06:59:43 -04:00
|
|
|
|
|
|
|
// exportPCRs tries to export the node's PCRs to QEMU's metadata API.
|
|
|
|
// This function is called when an Azure or GCP image boots, but is unable to find a state disk.
|
|
|
|
// This happens when we boot such an image in QEMU.
|
|
|
|
// We can use this to calculate the PCRs of the image locally.
|
|
|
|
func exportPCRs() error {
|
|
|
|
// get TPM state
|
|
|
|
pcrs, err := vtpm.GetSelectedPCRs(vtpm.OpenVTPM, tpmClient.FullPcrSel(tpm2.AlgSHA256))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pcrsPretty, err := json.Marshal(pcrs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// send PCRs to metadata API
|
|
|
|
url := &url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: "10.42.0.1:8080", // QEMU metadata endpoint
|
|
|
|
Path: "/pcrs",
|
|
|
|
}
|
2022-08-02 10:25:47 -04:00
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, url.String(), bytes.NewBuffer(pcrsPretty))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
2022-07-04 06:59:43 -04:00
|
|
|
}
|