Skip to content

Instantly share code, notes, and snippets.

@judavi
Created February 11, 2020 16:39
Show Gist options
  • Save judavi/04608729337bc600bbfa3d73532e839b to your computer and use it in GitHub Desktop.
Save judavi/04608729337bc600bbfa3d73532e839b to your computer and use it in GitHub Desktop.
Basic Grafeas List Occurrences Client
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/genproto/googleapis/devtools/containeranalysis/v1beta1/grafeas"
)
const (
address = "grafeas-server:443"
defaultName = "world"
caFile = "ca.pem"
)
func main() {
log.Printf("Hello")
// Load CA cert
caCert, err := ioutil.ReadFile(caFile)
if err != nil {
log.Fatal(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
creds := credentials.NewTLS(&tls.Config{
RootCAs: caCertPool,
})
conn, err := grpc.Dial(address, grpc.WithTransportCredentials(creds))
defer conn.Close()
client := grafeas.NewGrafeasV1Beta1Client(conn)
log.Println("Connecting to Grafeas server")
ctx := context.Background()
resp, err := client.ListOccurrences(ctx,
&grafeas.ListOccurrencesRequest{
Parent: "projects/kritis",
PageSize : 100,
Filter: fmt.Sprintf("resource_url=%q AND kind=%q", "https://docker.io/judavi/amazon-k8s@sha256:7e26e2b87a3050f7143e5ed4cc19ca7a71c1b6c308c315e3611fed2590c18a4c", "PACKAGE_VULNERABILITY"),
})
if err != nil {
log.Fatal(err)
}
if len(resp.Occurrences) != 0 {
log.Println("Listing notes...")
log.Println(resp.Occurrences)
} else {
log.Println("Project does not contain any notes")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment