Skip to content

Instantly share code, notes, and snippets.

@salrashid123
Created December 1, 2022 14:27
Show Gist options
  • Save salrashid123/714a5b67f254eba6954333be8bc03c0c to your computer and use it in GitHub Desktop.
Save salrashid123/714a5b67f254eba6954333be8bc03c0c to your computer and use it in GitHub Desktop.
Generating synthetic logs for GCP Cloud Logging
package main
/*
$ gcloud compute instances create vm1 --shielded-secure-boot --scopes=cloud-platform --zone=us-central1-a --shielded-vtpm --shielded-integrity-monitoring
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
vm1 us-central1-a n1-standard-1 10.128.0.104 35.224.160.133 RUNNING
$ gcloud compute instances describe vm1 --format="value(id)"
8208965068975117794
*/
import (
"flag"
"fmt"
"cloud.google.com/go/logging"
"golang.org/x/net/context"
"google.golang.org/genproto/googleapis/api/monitoredres"
)
const ()
type PcrValue struct {
HashAlgo string `json:"hashAlgo,omitempty"`
PcrNum string `json:"pcrNum,omitempty"`
Value string `json:"value,omitempty"`
}
type EarlyBootAttestationReportEvent struct {
ActualMeasurements []PcrValue `json:"actualMeasurements,omitempty"`
PolicyEvaluationPassed bool `json:"policyEvaluationPassed,omitempty"`
}
// type.googleapis.com/cloud_integrity.IntegrityEvent is not exported to github so have to make parts
// of the struct by hand
type IntegrityEvent struct {
Type string `json:"@type,omitempty"`
BootCounter uint64 `json:"bootCounter,omitempty"`
EarlyBootReportEvent EarlyBootAttestationReportEvent `json:"earlyBootReportEvent,omitempty"`
}
var (
projectID = flag.String("projectID", "mineral-minutia-820", "projectID")
)
func main() {
flag.Parse()
if *projectID == "" {
fmt.Println("projectID must be set")
return
}
ctx := context.Background()
client, err := logging.NewClient(ctx, *projectID)
if err != nil {
fmt.Printf("%v", err)
return
}
defer client.Close()
logName := "compute.googleapis.com%2Fshielded_vm_integrity"
logger := client.Logger(logName)
jsonPayload := &IntegrityEvent{
Type: "type.googleapis.com/cloud_integrity.IntegrityEvent",
EarlyBootReportEvent: EarlyBootAttestationReportEvent{
ActualMeasurements: []PcrValue{
{
HashAlgo: "SHA1",
PcrNum: "PCR_1",
Value: "foooooo",
},
},
PolicyEvaluationPassed: true,
},
BootCounter: 10,
}
m := make(map[string]string)
m["project_id"] = "mineral-minutia-820"
m["instance_id"] = "8208965068975117794"
m["zone"] = "us-central1-a"
logger.Log(logging.Entry{
Resource: &monitoredres.MonitoredResource{
Type: "gce_instance",
Labels: m,
},
Payload: jsonPayload,
})
if err := client.Close(); err != nil {
fmt.Printf("Failed to close client: %v", err)
return
}
fmt.Printf("Logged:\n")
}
@salrashid123
Copy link
Author

Actual logs

r_log

Synthetic logs

s_log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment