Skip to content

Commit

Permalink
Add go-lang example
Browse files Browse the repository at this point in the history
  • Loading branch information
karmatr0n committed Sep 19, 2015
1 parent 082c6f2 commit 4544c1b
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/client-go/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
PROGRAM := client
DEST_DIR := /usr/local/bin

build:
go build -o $(PROGRAM)

install:
cp -p $(PROGRAM) $(DEST_DIR)

all: build install

clean:
rm -f $(PROGRAM)

uninstall:
rm -f $(DEST_DIR)/$(P
21 changes: 21 additions & 0 deletions examples/client-go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# CVE Client for Go

This is a proof of concept to build a REST Client for the CVE Server using the Go language.

# Getting started

1. Install the Go programming language

http://go-lang.org

2. Install the gopencils library

https://github.com/bndr/gopencils

3. Compile the source code

make build

4. Execute the generated binary

./client
116 changes: 116 additions & 0 deletions examples/client-go/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"fmt"
"github.com/bndr/gopencils"
"errors"
)

type Cvss struct {
Score string
AccessVector string
AccessComplexity string
Authentication string
ConfidentialityImpact string
IntegrityImpact string
AvailabilityImpact string
Source string
GeneratedOnDatetime string
Vector string
}

type Reference struct {
Type string
Name string
Href string
Content string
}

type Cve struct {
Id string
Summary string
Cwe string
PublishedAt string
UpdatedAt string
Cvss Cvss
References []Reference
Cpes []string
}

type CveClient struct {
api *gopencils.Resource
}

func Start(url string) (*CveClient, error) {
if url != "" {
api := gopencils.Api(url)
cve_client := CveClient{api: api}
return &cve_client, nil
} else {
return nil, errors.New("You must specify an url")
}
}

func (c *CveClient) DetailsPerCve(cveId string) (*Cve, error) {
resource := c.api.Res("cve")
cve := new(Cve)
_, err := resource.Id(cveId, cve).Get()

if err != nil {
return nil, err
} else {
return cve, nil
}
}

func (c *CveClient) CvesPerCpe(cpe string) (*[]string, error) {
resource := c.api.Res("cpe")
cves := new([]string)
_, err := resource.Id(cpe, cves).Get()

if err != nil {
return nil, err
} else {
return cves, nil
}
}

func (c *CveClient) Cpes() (*[]string, error) {
cpes := new([]string)
resource := c.api.Res("cpe", cpes)
_, err := resource.Get()

if err != nil {
return nil, err
} else {
return cpes, nil
}
}

func main() {
client, err := Start("http://0.0.0.0:9292/v1")

if err != nil {
fmt.Println(err)
} else {

cve, cve_err := client.DetailsPerCve("CVE-2015-0001")
if cve_err != nil {
fmt.Println(cve_err)
} else {
fmt.Println(cve.Id)
fmt.Println(cve.Summary)
fmt.Println(cve.Cvss.Score)
}

// cves, _ := client.CvesPerCpe("oracle:application_server_10g")
// for _, cve := range *cves {
// fmt.Println(cve)
// }

// cpes, _ := client.Cpes()
// for _, cpe := range *cpes {
// fmt.Println(cpe)
// }
}
}

0 comments on commit 4544c1b

Please sign in to comment.