Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
incorporated iza's feedback 2
Browse files Browse the repository at this point in the history
  • Loading branch information
candysmurf committed Aug 8, 2017
1 parent b0dd0d7 commit e71f076
Showing 1 changed file with 36 additions and 19 deletions.
55 changes: 36 additions & 19 deletions mgmt/rest/v2/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func (s *apiV2) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter.
return
}

var certPath, keyPath, caCertPaths string
if strings.HasPrefix(mediaType, "multipart/") {
var certPath, keyPath, caCertPaths string
var signature []byte
var checkSum [sha256.Size]byte
mr := multipart.NewReader(r.Body, params["boundary"])
Expand Down Expand Up @@ -235,7 +235,8 @@ func (s *apiV2) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter.
certPath = fn
// plugin_data is from REST API and snap-plugins is from rest_v2_test.go.
case "plugin_data", "snap-plugins":
if rp, err = core.NewRequestedPlugin(p.FileName(), s.metricManager.GetTempDir(), b); err != nil {
rp, err = core.NewRequestedPlugin(p.FileName(), s.metricManager.GetTempDir(), b)
if err != nil {
Write(500, FromError(err), w)
return
}
Expand All @@ -258,11 +259,21 @@ func (s *apiV2) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter.
Write(400, FromError(e), w)
return
}

if hasTLS(certPath, keyPath) {
if isTLSEnabled(certPath, keyPath) {
rp.SetTLSEnabled(true)
rp.SetCACertPaths(caCertPaths)
rp.SetCertPath(certPath)
rp.SetKeyPath(keyPath)
} else {
e := errors.New("Error: TLS setup incomplete - Both plugin TLS certificate and the key are required")
Write(500, FromError(e), w)
return
}
}
rp.SetSignature(signature)
rp.SetCACertPaths(caCertPaths)
rp.SetCertPath(certPath)
rp.SetKeyPath(keyPath)
rp.SetTLSEnabled(isTLSEnabled(certPath, keyPath))

restLogger.Info("Loading plugin: ", rp.Path())
pl, err := s.metricManager.Load(rp)
if err != nil {
Expand All @@ -280,7 +291,7 @@ func (s *apiV2) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter.
default:
ec = 500
}
cleanUpTempFiles(rp)
defer cleanUpTempFiles(rp)
Write(ec, rb, w)
return
}
Expand All @@ -289,6 +300,13 @@ func (s *apiV2) loadPlugin(w http.ResponseWriter, r *http.Request, _ httprouter.
}
}

func hasTLS(cert, key string) bool {
if cert == "" && key == "" {
return false
}
return true
}

func isTLSEnabled(cert, key string) bool {
if cert != "" && key != "" {
return true
Expand Down Expand Up @@ -505,17 +523,16 @@ func (s *apiV2) getPlugin(w http.ResponseWriter, r *http.Request, p httprouter.P
}
w.WriteHeader(200)
return
} else {
pluginRet := Plugin{
Name: plugin.Name(),
Version: plugin.Version(),
Type: plugin.TypeName(),
Signed: plugin.IsSigned(),
Status: plugin.Status(),
LoadedTimestamp: plugin.LoadedTimestamp().Unix(),
Href: pluginURI(r.Host, plugin),
ConfigPolicy: configPolicy,
}
Write(200, pluginRet, w)
}
pluginRet := Plugin{
Name: plugin.Name(),
Version: plugin.Version(),
Type: plugin.TypeName(),
Signed: plugin.IsSigned(),
Status: plugin.Status(),
LoadedTimestamp: plugin.LoadedTimestamp().Unix(),
Href: pluginURI(r.Host, plugin),
ConfigPolicy: configPolicy,
}
Write(200, pluginRet, w)
}

0 comments on commit e71f076

Please sign in to comment.