Skip to content

Commit

Permalink
fix(contrib/clair): move commons function from old plugin package (#3391
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fsamin authored and yesnault committed Oct 3, 2018
1 parent 1dfa065 commit e05fb6a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 84 deletions.
6 changes: 3 additions & 3 deletions contrib/grpcplugins/action/clair/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/golang/protobuf/ptypes/empty"
"github.com/spf13/viper"

"github.com/ovh/cds/contrib/grpcplugins"
"github.com/ovh/cds/contrib/grpcplugins/action/clair/clairctl/clair"
"github.com/ovh/cds/contrib/grpcplugins/action/clair/clairctl/config"
"github.com/ovh/cds/contrib/grpcplugins/action/clair/clairctl/docker"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/grpcplugin/actionplugin"
"github.com/ovh/cds/sdk/plugin"
)

/* Inside contrib/grpcplugins/action
Expand All @@ -38,7 +38,7 @@ func (actPlugin *clairActionPlugin) Manifest(ctx context.Context, _ *empty.Empty
func (actPlugin *clairActionPlugin) Run(ctx context.Context, q *actionplugin.ActionQuery) (*actionplugin.ActionResult, error) {
// get clair configuration
fmt.Printf("Retrieve clair configuration...")
serv, err := plugin.GetExternalServices(actPlugin.HTTPPort, "clair")
serv, err := grpcplugins.GetExternalServices(actPlugin.HTTPPort, "clair")
if err != nil {
return fail("Unable to get clair configuration: %s", err)
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func (actPlugin *clairActionPlugin) Run(ctx context.Context, q *actionplugin.Act
Summary: summary,
Type: "docker",
}
if err := plugin.SendVulnerabilityReport(actPlugin.HTTPPort, report); err != nil {
if err := grpcplugins.SendVulnerabilityReport(actPlugin.HTTPPort, report); err != nil {
return fail("Unable to send report: %s", err)
}
return &actionplugin.ActionResult{
Expand Down
4 changes: 2 additions & 2 deletions contrib/grpcplugins/action/npm-audit-parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"github.com/golang/protobuf/ptypes/empty"

"github.com/ovh/cds/contrib/grpcplugins"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/grpcplugin/actionplugin"
"github.com/ovh/cds/sdk/plugin"
)

/* Inside contrib/grpcplugins/action
Expand Down Expand Up @@ -88,7 +88,7 @@ func (actPlugin *npmAuditParserActionPlugin) Run(ctx context.Context, q *actionp
}
report.Type = "js"
report.Summary = summary
if err := plugin.SendVulnerabilityReport(actPlugin.HTTPPort, report); err != nil {
if err := grpcplugins.SendVulnerabilityReport(actPlugin.HTTPPort, report); err != nil {
return fail("Unable to send report: %s", err)
}

Expand Down
71 changes: 71 additions & 0 deletions contrib/grpcplugins/grpcplugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package grpcplugins

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/ovh/cds/sdk"
)

// SendVulnerabilityReport call worker to send vulnerabiliry report to API
func SendVulnerabilityReport(workerHTTPPort int32, report sdk.VulnerabilityWorkerReport) error {
if workerHTTPPort == 0 {
return nil
}

data, errD := json.Marshal(report)
if errD != nil {
return fmt.Errorf("unable to marshal report: %v", errD)
}

req, err := http.NewRequest("POST", fmt.Sprintf("http://127.0.0.1:%d/vulnerability", workerHTTPPort), bytes.NewReader(data))
if err != nil {
return fmt.Errorf("send report to worker /vulnerability: %v", err)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("cannot send report to worker /vulnerability: %v", err)
}

if resp.StatusCode >= 300 {
return fmt.Errorf("cannot send report to worker /vulnerability: HTTP %d", resp.StatusCode)
}

return nil
}

// GetExternalServices call worker to get external service configuration
func GetExternalServices(workerHTTPPort int32, serviceType string) (sdk.ExternalService, error) {
if workerHTTPPort == 0 {
return sdk.ExternalService{}, nil
}

req, err := http.NewRequest("GET", fmt.Sprintf("http://127.0.0.1:%d/services/%s", workerHTTPPort, serviceType), nil)
if err != nil {
return sdk.ExternalService{}, fmt.Errorf("get service from worker /services: %v", err)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return sdk.ExternalService{}, fmt.Errorf("cannot get service from worker /services: %v", err)
}

if resp.StatusCode >= 300 {
return sdk.ExternalService{}, fmt.Errorf("cannot get services from worker /services: HTTP %d", resp.StatusCode)
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return sdk.ExternalService{}, fmt.Errorf("cannot read body /services: %v", err)
}

var serv sdk.ExternalService
if err := json.Unmarshal(b, &serv); err != nil {
return serv, fmt.Errorf("cannot unmarshal body /services: %v", err)
}
return serv, nil
}
79 changes: 0 additions & 79 deletions sdk/plugin/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -159,84 +158,6 @@ type Log struct {
Value string `json:"val"`
}

// SendVulnerabilityReport call worker to send vulnerabiliry report to API
func SendVulnerabilityReport(workerHTTPPort int32, report sdk.VulnerabilityWorkerReport) error {
if workerHTTPPort == 0 {
return nil
}

data, errD := json.Marshal(report)
if errD != nil {
e := fmt.Errorf("unable to marshal report: %v", errD)
Trace.Println(e)
return e
}

req, err := http.NewRequest("POST", fmt.Sprintf("http://127.0.0.1:%d/vulnerability", workerHTTPPort), bytes.NewReader(data))
if err != nil {
e := fmt.Errorf("send report to worker /vulnerability: %v", err)
Trace.Println(e)
return e
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
e := fmt.Errorf("cannot send report to worker /vulnerability: %v", err)
Trace.Println(e)
return e
}

if resp.StatusCode >= 300 {
e := fmt.Errorf("cannot send report to worker /vulnerability: HTTP %d", resp.StatusCode)
Trace.Println(e)
return e
}

return nil
}

// GetExternalServices call worker to get external service configuration
func GetExternalServices(workerHTTPPort int32, serviceType string) (sdk.ExternalService, error) {
if workerHTTPPort == 0 {
return sdk.ExternalService{}, nil
}

req, err := http.NewRequest("GET", fmt.Sprintf("http://127.0.0.1:%d/services/%s", workerHTTPPort, serviceType), nil)
if err != nil {
e := fmt.Errorf("get service from worker /services: %v", err)
Trace.Println(e)
return sdk.ExternalService{}, e
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
e := fmt.Errorf("cannot get service from worker /services: %v", err)
Trace.Println(e)
return sdk.ExternalService{}, e
}

if resp.StatusCode >= 300 {
e := fmt.Errorf("cannot get services from worker /services: HTTP %d", resp.StatusCode)
Trace.Println(e)
return sdk.ExternalService{}, e
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
e := fmt.Errorf("cannot read body /services: %v", err)
Trace.Println(e)
return sdk.ExternalService{}, e
}

var serv sdk.ExternalService
if err := json.Unmarshal(b, &serv); err != nil {
e := fmt.Errorf("cannot unmarshal body /services: %v", err)
Trace.Println(e)
return serv, e
}
return serv, nil
}

//SendLog send logs to CDS engine for the current
func SendLog(j IJob, format string, i ...interface{}) error {
if j == nil {
Expand Down

0 comments on commit e05fb6a

Please sign in to comment.