-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a simple fake client for Bootz
- Loading branch information
Showing
4 changed files
with
115 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters