Skip to content

Commit 9bb26f3

Browse files
author
Jason Yellick
committed
[FAB-4469] Add REST endpoint for sanity check
The sanity checking code added in FAB-4468 currently has no external interface. In order to satisfy the requirements of FAB-3831, it is necessary to expose an external interface to this sanity checking ability. This CR adds a REST endpoint at /configtxlator/sanitycheck/config which the user may POST a marshaled common.Config to. The REST target then replies with a JSON document, indicating the error types and paths, as reported by the sanity checking code. Change-Id: I97616eec67aebd77ea8191b8ae39808716330aec Signed-off-by: Jason Yellick <[email protected]>
1 parent 9577fb7 commit 9bb26f3

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

common/tools/configtxlator/rest/configtxlator_handlers.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ limitations under the License.
1717
package rest
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
2122
"io/ioutil"
2223
"net/http"
2324

25+
"github.com/hyperledger/fabric/common/tools/configtxlator/sanitycheck"
2426
"github.com/hyperledger/fabric/common/tools/configtxlator/update"
25-
2627
cb "github.com/hyperledger/fabric/protos/common"
2728

2829
"github.com/golang/protobuf/proto"
@@ -88,3 +89,37 @@ func ComputeUpdateFromConfigs(w http.ResponseWriter, r *http.Request) {
8889
w.Header().Set("Content-Type", "application/octet-stream")
8990
w.Write(encoded)
9091
}
92+
93+
func SanityCheckConfig(w http.ResponseWriter, r *http.Request) {
94+
buf, err := ioutil.ReadAll(r.Body)
95+
if err != nil {
96+
w.WriteHeader(http.StatusBadRequest)
97+
fmt.Fprintln(w, err)
98+
return
99+
}
100+
101+
config := &cb.Config{}
102+
err = proto.Unmarshal(buf, config)
103+
if err != nil {
104+
w.WriteHeader(http.StatusBadRequest)
105+
fmt.Fprintf(w, "Error unmarshaling data to common.Config': %s\n", err)
106+
return
107+
}
108+
109+
fmt.Printf("Sanity checking %+v\n", config)
110+
sanityCheckMessages, err := sanitycheck.Check(config)
111+
if err != nil {
112+
w.WriteHeader(http.StatusInternalServerError)
113+
fmt.Fprintf(w, "Error performing sanity check: %s\n", err)
114+
return
115+
}
116+
117+
resBytes, err := json.Marshal(sanityCheckMessages)
118+
if err != nil {
119+
w.WriteHeader(http.StatusInternalServerError)
120+
fmt.Fprintf(w, "Error marshaling result to JSON: %s\n", err)
121+
return
122+
}
123+
w.WriteHeader(http.StatusOK)
124+
w.Write(resBytes)
125+
}

common/tools/configtxlator/rest/configtxlator_handlers_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ package rest
1818

1919
import (
2020
"bytes"
21+
"encoding/json"
2122
"mime/multipart"
2223
"net/http"
2324
"net/http/httptest"
2425
"testing"
2526

27+
"github.com/hyperledger/fabric/common/tools/configtxlator/sanitycheck"
2628
cb "github.com/hyperledger/fabric/protos/common"
2729
"github.com/hyperledger/fabric/protos/utils"
2830

@@ -157,3 +159,26 @@ func TestProtolatorCorruptProtos(t *testing.T) {
157159

158160
assert.Equal(t, http.StatusBadRequest, rec.Code)
159161
}
162+
163+
func TestConfigtxlatorSanityCheckConfig(t *testing.T) {
164+
req, _ := http.NewRequest("POST", "/configtxlator/config/verify", bytes.NewReader(utils.MarshalOrPanic(&cb.Config{})))
165+
rec := httptest.NewRecorder()
166+
r := NewRouter()
167+
r.ServeHTTP(rec, req)
168+
169+
assert.Equal(t, http.StatusOK, rec.Code)
170+
171+
outputMsg := &sanitycheck.Messages{}
172+
173+
err := json.Unmarshal(rec.Body.Bytes(), outputMsg)
174+
assert.NoError(t, err)
175+
}
176+
177+
func TestConfigtxlatorSanityCheckMalformedConfig(t *testing.T) {
178+
req, _ := http.NewRequest("POST", "/configtxlator/config/verify", bytes.NewReader([]byte("Garbage")))
179+
rec := httptest.NewRecorder()
180+
r := NewRouter()
181+
r.ServeHTTP(rec, req)
182+
183+
assert.Equal(t, http.StatusBadRequest, rec.Code)
184+
}

common/tools/configtxlator/rest/router.go

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ func NewRouter() *mux.Router {
3535
router.
3636
HandleFunc("/configtxlator/compute/update-from-configs", ComputeUpdateFromConfigs).
3737
Methods("POST")
38+
router.
39+
HandleFunc("/configtxlator/config/verify", SanityCheckConfig).
40+
Methods("POST")
3841

3942
return router
4043
}

0 commit comments

Comments
 (0)