Skip to content

Instantly share code, notes, and snippets.

@salrashid123
Last active April 18, 2022 11:20
Show Gist options
  • Select an option

  • Save salrashid123/5d17dbbf6ee7f03a17120c637a3e539a to your computer and use it in GitHub Desktop.

Select an option

Save salrashid123/5d17dbbf6ee7f03a17120c637a3e539a to your computer and use it in GitHub Desktop.
GCP CLoud Logging streaming of auditlogs
package main
import (
"fmt"
"io"
logging "cloud.google.com/go/logging/apiv2"
"golang.org/x/net/context"
"google.golang.org/genproto/googleapis/cloud/audit"
loggingpb "google.golang.org/genproto/googleapis/logging/v2"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
const ()
var ()
func main() {
projectID := "eventarc-test"
ctx := context.Background()
c, err := logging.NewClient(ctx)
if err != nil {
fmt.Printf("%v", err)
return
}
defer c.Close()
stream, err := c.TailLogEntries(ctx)
if err != nil {
fmt.Printf("%v", err)
return
}
go func() {
req := loggingpb.TailLogEntriesRequest{
ResourceNames: []string{fmt.Sprintf("projects/%s", projectID)},
}
if err := stream.Send(&req); err != nil {
fmt.Printf("%v", err)
return
}
//stream.CloseSend()
}()
for {
resp, err := stream.Recv()
if err == io.EOF {
fmt.Printf("EOF\n")
break
}
if err != nil {
fmt.Printf("%v", err)
return
}
for _, e := range resp.Entries {
fmt.Printf("Entry: %v\n", e.InsertId)
var audit_data audit.AuditLog
err := anypb.UnmarshalTo(e.GetProtoPayload(), &audit_data, proto.UnmarshalOptions{})
if err != nil {
fmt.Printf("Error: could not unmarshall to audit log")
} else {
fmt.Printf(" Authentication: %v\n", audit_data.AuthenticationInfo)
fmt.Printf(" Authorization: %v\n", audit_data.AuthorizationInfo)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment