Skip to content

Commit

Permalink
Create a simple fake client for Bootz
Browse files Browse the repository at this point in the history
  • Loading branch information
gmacf committed Jul 20, 2023
1 parent 6becdc3 commit 7fe4dbe
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 9 deletions.
15 changes: 12 additions & 3 deletions client/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_binary")

go_library(
name = "client",
name = "client_lib",
srcs = ["client.go"],
importpath = "github.com/openconfig/bootz/client",
visibility = ["//visibility:public"],
deps = ["@org_golang_google_grpc//:go_default_library"],
deps = [
"@com_github_golang_glog//:go_default_library",
"@org_golang_google_grpc//:go_default_library",
],
)

go_binary(
name = "client",
embed = [":client_lib"],
visibility = ["//visibility:public"],
)
106 changes: 100 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,113 @@
package client
package main

import (
"context"
"crypto/rand"
"flag"
"fmt"

log "github.com/golang/glog"

"github.com/openconfig/bootz/proto/bootz"
"google.golang.org/grpc"
)

type Client struct {
bootz.BootstrapClient
}
// Represents a 128 bit nonce.
const nonceLength = 16

func (c *Client) GetBootstrapData(ctx context.Context, req *bootz.GetBootstrapDataRequest, opts ...grpc.CallOption) (*bootz.GetBootstrapDataResponse, error) {
var port = flag.String("port", "", "The port to listen to on localhost for the bootz server.")

// nonce() generates a fixed-length nonce.
func nonce() (string, error) {
b := make([]byte, nonceLength)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return string(b), nil
}
func (c *Client) ReportStatus(ctx context.Context, req *bootz.ReportStatusRequest, opts ...grpc.CallOption) (*bootz.EmptyResponse, error) {

func main() {
ctx := context.Background()
// 1. DHCP Discovery of Bootstrap Server
// This step emulates the retrieval of the bootz server IP
// address from a DHCP server. In this case we always connect to localhost.

if *port == "" {
log.Fatalf("No port provided.")
}

// 2. Bootstrapping Service
// Device initiates a TLS-secured gRPC connection with the Bootz server.
// TODO: Make this use TLS.
conn, err := grpc.Dial(fmt.Sprintf("localhost:%v", *port))
if err != nil {
log.Fatalf("Unable to connect to Bootstrap Server: %v", err)
}
defer conn.Close()
c := bootz.NewBootstrapClient(conn)

// Generate a nonce that the Bootz served will use to sign the response.
nonce, err := nonce()
if err != nil {
log.Fatalf("Error generating nonce: %v", err)
}

// TODO: Build or store the fields of this request programatically.
// This represents a simple dual-control card chassis manufactured by Cisco.
// In this case, the bootz request is initiated by the control card in slot 1.
req := &bootz.GetBootstrapDataRequest{
ChassisDescriptor: &bootz.ChassisDescriptor{
Manufacturer: "Cisco",
ControlCards: []*bootz.ControlCard{
{
PartNumber: "1",
SerialNumber: "ABC123",
Slot: 1,
},
{
PartNumber: "2",
SerialNumber: "ABC124",
Slot: 2,
},
},
},
// This is the active control card, e.g. the one making the bootz request.
ControlCardState: &bootz.ControlCardState{
SerialNumber: "ABC123",
Status: bootz.ControlCardState_CONTROL_CARD_STATUS_NOT_INITIALIZED,
},
Nonce: nonce,
}

// Get bootstrapping data from Bootz server
// TODO: Extract and parse response.
_, err = c.GetBootstrapData(ctx, req)
if err != nil {
log.Fatalf("Error calling GetBootstrapData: %v", err)
}

// 6. ReportProgress
statusReq := &bootz.ReportStatusRequest{
Status: bootz.ReportStatusRequest_BOOTSTRAP_STATUS_SUCCESS,
StatusMessage: "Bootstrap Success",
States: []*bootz.ControlCardState{
{
Status: bootz.ControlCardState_CONTROL_CARD_STATUS_INITIALIZED,
SerialNumber: "ABC123",
},
{
Status: bootz.ControlCardState_CONTROL_CARD_STATUS_INITIALIZED,
SerialNumber: "ABC124",
},
},
}

_, err = c.ReportStatus(ctx, statusReq)
if err != nil {
log.Fatalf("Error reporting status: %v", err)
}

// At this point the device has minimal configuration and can receive further gRPC calls. After this, the TPM Enrollment and attestation occurs.

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
)

require (
github.com/golang/glog v1.1.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/openconfig/gnmi v0.0.0-20220617175856-41246b1b3507 // indirect
github.com/openconfig/gnoi v0.0.0-20220809151450-6bddacd72ef8 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.1.1 h1:jxpi2eWoU84wbX9iIEyAeeoac3FLuifZpY9tcNUD9kw=
github.com/golang/glog v1.1.1/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down

0 comments on commit 7fe4dbe

Please sign in to comment.