Skip to content

Commit

Permalink
[FAB-4167] Expose config update compute via REST
Browse files Browse the repository at this point in the history
As part of FAB-1678, the tool needs to support computing a config update
based on the contents of two configs.  This CR adds a new REST endpoint
for handling this computation.

It add the target /configtxlator/compute/update-from-configs which takes
a POST of multipart/formdata with two files, the first for the field
'original' and the second for the form 'updated'.  These should both be
proto marshaled common.Config structures. A final 'channel' parameter
may be supplied to set the channel id for the update.

The server then invokes the configtxlator.update package to compute the
config update based on the inputs, and returns it as a marshaled
common.ConfigUpdate message.

Change-Id: I0ca13d6de41da8b0ce9bfc66685476b42a6991e6
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Jun 2, 2017
1 parent 5fb91b5 commit c8785e3
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 0 deletions.
90 changes: 90 additions & 0 deletions common/tools/configtxlator/rest/configtxlator_handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rest

import (
"fmt"
"io/ioutil"
"net/http"

"github.com/hyperledger/fabric/common/tools/configtxlator/update"

cb "github.com/hyperledger/fabric/protos/common"

"github.com/golang/protobuf/proto"
)

func fieldBytes(fieldName string, r *http.Request) ([]byte, error) {
fieldFile, _, err := r.FormFile(fieldName)
if err != nil {
return nil, err
}
defer fieldFile.Close()

return ioutil.ReadAll(fieldFile)
}

func fieldConfigProto(fieldName string, r *http.Request) (*cb.Config, error) {
fieldBytes, err := fieldBytes(fieldName, r)
if err != nil {
return nil, fmt.Errorf("error reading field bytes: %s", err)
}

config := &cb.Config{}
err = proto.Unmarshal(fieldBytes, config)
if err != nil {
return nil, fmt.Errorf("error unmarshaling field bytes: %s", err)
}

return config, nil
}

func ComputeUpdateFromConfigs(w http.ResponseWriter, r *http.Request) {
originalConfig, err := fieldConfigProto("original", r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error with field 'original': %s\n", err)
return
}

updatedConfig, err := fieldConfigProto("updated", r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error with field 'updated': %s\n", err)
return
}

configUpdate, err := update.Compute(originalConfig, updatedConfig)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Error computing update: %s\n", err)
return
}

configUpdate.ChannelId = r.FormValue("channel")

encoded, err := proto.Marshal(configUpdate)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error marshaling config update: %s\n", err)
return
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(encoded)
}
159 changes: 159 additions & 0 deletions common/tools/configtxlator/rest/configtxlator_handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package rest

import (
"bytes"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"

cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"

"github.com/stretchr/testify/assert"
)

func TestProtolatorComputeConfigUpdate(t *testing.T) {
originalConfig := utils.MarshalOrPanic(&cb.Config{
ChannelGroup: &cb.ConfigGroup{
ModPolicy: "foo",
},
})

updatedConfig := utils.MarshalOrPanic(&cb.Config{
ChannelGroup: &cb.ConfigGroup{
ModPolicy: "bar",
},
})

buffer := &bytes.Buffer{}
mpw := multipart.NewWriter(buffer)

ffw, err := mpw.CreateFormFile("original", "foo")
assert.NoError(t, err)
_, err = bytes.NewReader(originalConfig).WriteTo(ffw)
assert.NoError(t, err)

ffw, err = mpw.CreateFormFile("updated", "bar")
assert.NoError(t, err)
_, err = bytes.NewReader(updatedConfig).WriteTo(ffw)
assert.NoError(t, err)

err = mpw.Close()
assert.NoError(t, err)

req, err := http.NewRequest("POST", "/configtxlator/compute/update-from-configs", buffer)
assert.NoError(t, err)

req.Header.Set("Content-Type", mpw.FormDataContentType())
rec := httptest.NewRecorder()
r := NewRouter()
r.ServeHTTP(rec, req)

assert.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
}

func TestProtolatorMissingOriginal(t *testing.T) {
updatedConfig := utils.MarshalOrPanic(&cb.Config{
ChannelGroup: &cb.ConfigGroup{
ModPolicy: "bar",
},
})

buffer := &bytes.Buffer{}
mpw := multipart.NewWriter(buffer)

ffw, err := mpw.CreateFormFile("updated", "bar")
assert.NoError(t, err)
_, err = bytes.NewReader(updatedConfig).WriteTo(ffw)
assert.NoError(t, err)

err = mpw.Close()
assert.NoError(t, err)

req, err := http.NewRequest("POST", "/configtxlator/compute/update-from-configs", buffer)
assert.NoError(t, err)

req.Header.Set("Content-Type", mpw.FormDataContentType())
rec := httptest.NewRecorder()
r := NewRouter()
r.ServeHTTP(rec, req)

assert.Equal(t, http.StatusBadRequest, rec.Code)
}

func TestProtolatorMissingUpdated(t *testing.T) {
originalConfig := utils.MarshalOrPanic(&cb.Config{
ChannelGroup: &cb.ConfigGroup{
ModPolicy: "bar",
},
})

buffer := &bytes.Buffer{}
mpw := multipart.NewWriter(buffer)

ffw, err := mpw.CreateFormFile("original", "bar")
assert.NoError(t, err)
_, err = bytes.NewReader(originalConfig).WriteTo(ffw)
assert.NoError(t, err)

err = mpw.Close()
assert.NoError(t, err)

req, err := http.NewRequest("POST", "/configtxlator/compute/update-from-configs", buffer)
assert.NoError(t, err)

req.Header.Set("Content-Type", mpw.FormDataContentType())
rec := httptest.NewRecorder()
r := NewRouter()
r.ServeHTTP(rec, req)

assert.Equal(t, http.StatusBadRequest, rec.Code)
}

func TestProtolatorCorruptProtos(t *testing.T) {
originalConfig := []byte("Garbage")
updatedConfig := []byte("MoreGarbage")

buffer := &bytes.Buffer{}
mpw := multipart.NewWriter(buffer)

ffw, err := mpw.CreateFormFile("original", "bar")
assert.NoError(t, err)
_, err = bytes.NewReader(originalConfig).WriteTo(ffw)
assert.NoError(t, err)

ffw, err = mpw.CreateFormFile("updated", "bar")
assert.NoError(t, err)
_, err = bytes.NewReader(updatedConfig).WriteTo(ffw)
assert.NoError(t, err)

err = mpw.Close()
assert.NoError(t, err)

req, err := http.NewRequest("POST", "/configtxlator/compute/update-from-configs", buffer)
assert.NoError(t, err)

req.Header.Set("Content-Type", mpw.FormDataContentType())
rec := httptest.NewRecorder()
r := NewRouter()
r.ServeHTTP(rec, req)

assert.Equal(t, http.StatusBadRequest, rec.Code)
}
3 changes: 3 additions & 0 deletions common/tools/configtxlator/rest/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func NewRouter() *mux.Router {
router.
HandleFunc("/protolator/decode/{msgName}", Decode).
Methods("POST")
router.
HandleFunc("/configtxlator/compute/update-from-configs", ComputeUpdateFromConfigs).
Methods("POST")

return router
}

0 comments on commit c8785e3

Please sign in to comment.