diff --git a/common/tools/configtxlator/rest/configtxlator_handlers.go b/common/tools/configtxlator/rest/configtxlator_handlers.go index d614c506578..ae7712eacc5 100644 --- a/common/tools/configtxlator/rest/configtxlator_handlers.go +++ b/common/tools/configtxlator/rest/configtxlator_handlers.go @@ -17,12 +17,13 @@ limitations under the License. package rest import ( + "encoding/json" "fmt" "io/ioutil" "net/http" + "github.com/hyperledger/fabric/common/tools/configtxlator/sanitycheck" "github.com/hyperledger/fabric/common/tools/configtxlator/update" - cb "github.com/hyperledger/fabric/protos/common" "github.com/golang/protobuf/proto" @@ -88,3 +89,37 @@ func ComputeUpdateFromConfigs(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/octet-stream") w.Write(encoded) } + +func SanityCheckConfig(w http.ResponseWriter, r *http.Request) { + buf, err := ioutil.ReadAll(r.Body) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintln(w, err) + return + } + + config := &cb.Config{} + err = proto.Unmarshal(buf, config) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(w, "Error unmarshaling data to common.Config': %s\n", err) + return + } + + fmt.Printf("Sanity checking %+v\n", config) + sanityCheckMessages, err := sanitycheck.Check(config) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "Error performing sanity check: %s\n", err) + return + } + + resBytes, err := json.Marshal(sanityCheckMessages) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "Error marshaling result to JSON: %s\n", err) + return + } + w.WriteHeader(http.StatusOK) + w.Write(resBytes) +} diff --git a/common/tools/configtxlator/rest/configtxlator_handlers_test.go b/common/tools/configtxlator/rest/configtxlator_handlers_test.go index 395a0160662..108614b6750 100644 --- a/common/tools/configtxlator/rest/configtxlator_handlers_test.go +++ b/common/tools/configtxlator/rest/configtxlator_handlers_test.go @@ -18,11 +18,13 @@ package rest import ( "bytes" + "encoding/json" "mime/multipart" "net/http" "net/http/httptest" "testing" + "github.com/hyperledger/fabric/common/tools/configtxlator/sanitycheck" cb "github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/utils" @@ -157,3 +159,26 @@ func TestProtolatorCorruptProtos(t *testing.T) { assert.Equal(t, http.StatusBadRequest, rec.Code) } + +func TestConfigtxlatorSanityCheckConfig(t *testing.T) { + req, _ := http.NewRequest("POST", "/configtxlator/config/verify", bytes.NewReader(utils.MarshalOrPanic(&cb.Config{}))) + rec := httptest.NewRecorder() + r := NewRouter() + r.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + + outputMsg := &sanitycheck.Messages{} + + err := json.Unmarshal(rec.Body.Bytes(), outputMsg) + assert.NoError(t, err) +} + +func TestConfigtxlatorSanityCheckMalformedConfig(t *testing.T) { + req, _ := http.NewRequest("POST", "/configtxlator/config/verify", bytes.NewReader([]byte("Garbage"))) + rec := httptest.NewRecorder() + r := NewRouter() + r.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusBadRequest, rec.Code) +} diff --git a/common/tools/configtxlator/rest/router.go b/common/tools/configtxlator/rest/router.go index d4984f6b36b..3e7c2dfd11f 100644 --- a/common/tools/configtxlator/rest/router.go +++ b/common/tools/configtxlator/rest/router.go @@ -35,6 +35,9 @@ func NewRouter() *mux.Router { router. HandleFunc("/configtxlator/compute/update-from-configs", ComputeUpdateFromConfigs). Methods("POST") + router. + HandleFunc("/configtxlator/config/verify", SanityCheckConfig). + Methods("POST") return router }