Skip to content

Commit

Permalink
[FAB-6842] Change rc for some endpoints to 201
Browse files Browse the repository at this point in the history
The HTTP success return code for /register, /enroll and
/reenroll API endpoints should be 201 as they create an artifact
for the identity being registered, enrolled, reenrolled, respectively.
Currently, a 200 is returned.

Change-Id: Ib0f005551fc664ea23aa56cc925f98ef702500d1
Signed-off-by: Anil Ambati <[email protected]>
  • Loading branch information
Anil Ambati committed Nov 6, 2017
1 parent 5b815c8 commit 626f943
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 18 deletions.
9 changes: 9 additions & 0 deletions cmd/fabric-ca-client/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,15 @@ func testRegisterCommandLine(t *testing.T, srv *lib.Server) {
assert.NoError(t, err)
assert.Equal(t, "user", user.Type, "Identity type for '%s' should have been 'user'", userName)

// Register an identity with a space in its name
userName = "Test Register5"
err = RunMain([]string{cmdName, "register", "-d", "--id.name", userName,
"--id.affiliation", "hyperledger.org1"})
assert.NoError(t, err, "Failed to register identity "+userName)
user, err = db.GetUserInfo(userName)
assert.NoError(t, err)
assert.Equal(t, "user", user.Type, "Identity type for '%s' should have been 'user'", userName)

os.Remove(defYaml) // Delete default config file

err = RunMain([]string{cmdName, "register", "-u", "http://localhost:7091"})
Expand Down
2 changes: 1 addition & 1 deletion lib/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (i *Identity) Register(req *api.RegistrationRequest) (rr *api.RegistrationR
return nil, err
}

log.Debug("The register request completely successfully")
log.Debug("The register request completed successfully")
return resp, nil
}

Expand Down
3 changes: 2 additions & 1 deletion lib/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const (
attrIntermediateCA = "hf.IntermediateCA"
attrGenCRL = "hf.GenCRL"
attrRegistrarAttr = "hf.Registrar.Attributes"
apiPathPrefix = "/api/v1/"
)

// Server is the fabric-ca server
Expand Down Expand Up @@ -479,7 +480,7 @@ func (s *Server) registerHandlers() {
// Register a handler
func (s *Server) registerHandler(path string, se *serverEndpoint) {
s.mux.Handle("/"+path, se)
s.mux.Handle("/api/v1/"+path, se)
s.mux.Handle(apiPathPrefix+path, se)
}

// Starting listening and serving
Expand Down
17 changes: 13 additions & 4 deletions lib/serverendpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
type serverEndpoint struct {
// The HTTP methods ("GET", "POST", etc) which the function will handle
Methods []string
// The HTTP return code for success responses
successRC int
// Handler is the handler function for this endpoint
Handler func(ctx *serverRequestContext) (interface{}, error)
// Server which hosts this endpoint
Expand All @@ -50,16 +52,16 @@ func (se *serverEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(he.scode)
log.Infof(`%s %s %s %d %d "%s"`, r.RemoteAddr, r.Method, r.URL, he.scode, he.lcode, he.lmsg)
} else {
w.WriteHeader(200)
log.Infof(`%s %s %s 200 0 "OK"`, r.RemoteAddr, r.Method, r.URL)
w.WriteHeader(se.getSuccessRC())
log.Infof(`%s %s %s %d 0 "OK"`, r.RemoteAddr, r.Method, r.URL, se.getSuccessRC())
}
} else if err == nil {
w.WriteHeader(200)
w.WriteHeader(se.getSuccessRC())
err = api.SendResponse(w, resp)
if err != nil {
log.Warning("Failed to send response for %s: %+v", url, err)
}
log.Infof(`%s %s %s 200 0 "OK"`, r.RemoteAddr, r.Method, r.URL)
log.Infof(`%s %s %s %d 0 "OK"`, r.RemoteAddr, r.Method, r.URL, se.getSuccessRC())
} else {
he := getHTTPErr(err)
he.writeResponse(w)
Expand All @@ -68,6 +70,13 @@ func (se *serverEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func (se *serverEndpoint) getSuccessRC() int {
if se.successRC == 0 {
return 200
}
return se.successRC
}

// Validate that the HTTP method is supported for this endpoint
func (se *serverEndpoint) validateMethod(r *http.Request) error {
for _, m := range se.Methods {
Expand Down
14 changes: 8 additions & 6 deletions lib/serverenroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,19 @@ type enrollmentResponseNet struct {

func newEnrollEndpoint(s *Server) *serverEndpoint {
return &serverEndpoint{
Methods: []string{"POST"},
Handler: enrollHandler,
Server: s,
Methods: []string{"POST"},
Handler: enrollHandler,
Server: s,
successRC: 201,
}
}

func newReenrollEndpoint(s *Server) *serverEndpoint {
return &serverEndpoint{
Methods: []string{"POST"},
Handler: reenrollHandler,
Server: s,
Methods: []string{"POST"},
Handler: reenrollHandler,
Server: s,
successRC: 201,
}
}

Expand Down
11 changes: 7 additions & 4 deletions lib/serverregister.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package lib

import (
"fmt"
"net/url"
"strings"

"github.com/pkg/errors"
Expand All @@ -31,9 +32,10 @@ import (

func newRegisterEndpoint(s *Server) *serverEndpoint {
return &serverEndpoint{
Methods: []string{"POST"},
Handler: registerHandler,
Server: s,
Methods: []string{"POST"},
Handler: registerHandler,
Server: s,
successRC: 201,
}
}

Expand Down Expand Up @@ -106,7 +108,8 @@ func registerUser(req *api.RegistrationRequestNet, registrar string, ca *CA, ctx
if err != nil {
return "", errors.WithMessage(err, fmt.Sprintf("Registration of '%s' failed", req.Name))
}

// Set the location header to the URI of the identity that was created by the registration request
ctx.resp.Header().Set("Location", fmt.Sprintf("%sidentities/%s", apiPathPrefix, url.PathEscape(req.Name)))
return secret, nil
}

Expand Down
4 changes: 2 additions & 2 deletions swagger/swagger-fabric-ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
}
],
"responses": {
"200": {
"201": {
"description": "Successfully enrolled a new identity",
"schema": {
"type": "object",
Expand Down Expand Up @@ -347,7 +347,7 @@
}
],
"responses": {
"200": {
"201": {
"description": "Successfully reenrolled identity",
"schema": {
"type": "object",
Expand Down

0 comments on commit 626f943

Please sign in to comment.