-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expose grpc as http endpoint #18221
Merged
Merged
expose grpc as http endpoint #18221
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
183298e
init commit
wangxinyi7 eaf297d
pass x-consul-token to the grpc server
wangxinyi7 97dacbc
query params and add logger
wangxinyi7 7062429
log message
wangxinyi7 e69aa2d
change log message
wangxinyi7 4975291
fix unit test
wangxinyi7 f5e9707
remove read
wangxinyi7 f1bfb5d
add detailed error handling message
wangxinyi7 7c19794
Merge branch 'main' into NET-2678/xw-http-api-write
wangxinyi7 42d6ccf
fix unit tests
wangxinyi7 595ac3e
fix unit tests
wangxinyi7 eb96780
general refactor
wangxinyi7 1c2efc2
add more tests
wangxinyi7 884b79d
Merge branch 'main' into NET-2678/xw-http-api-write
wangxinyi7 dacbda0
refactor test
wangxinyi7 b5edf4a
add add owner and remove extra check
wangxinyi7 8db104c
add more tests
wangxinyi7 30938f1
Merge branch 'main' into NET-2678/xw-http-api-write
wangxinyi7 388a465
write the error msg back
wangxinyi7 c3775a7
add owner test
wangxinyi7 996f9e8
add tests regarding the version query parameter
wangxinyi7 c043f45
code refactor
wangxinyi7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package http | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"path" | ||
"strings" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/metadata" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/types/known/anypb" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
|
||
"github.com/hashicorp/consul/internal/resource" | ||
"github.com/hashicorp/consul/proto-public/pbresource" | ||
) | ||
|
||
func NewHandler( | ||
client pbresource.ResourceServiceClient, | ||
registry resource.Registry, | ||
parseToken func(req *http.Request, token *string), | ||
logger hclog.Logger) http.Handler { | ||
mux := http.NewServeMux() | ||
for _, t := range registry.Types() { | ||
// Individual Resource Endpoints. | ||
prefix := strings.ToLower(fmt.Sprintf("/%s/%s/%s/", t.Type.Group, t.Type.GroupVersion, t.Type.Kind)) | ||
logger.Info("Registered resource endpoint", "endpoint", prefix) | ||
mux.Handle(prefix, http.StripPrefix(prefix, &resourceHandler{t, client, parseToken, logger})) | ||
} | ||
|
||
return mux | ||
} | ||
|
||
type writeRequest struct { | ||
Metadata map[string]string `json:"metadata"` | ||
Data json.RawMessage `json:"data"` | ||
Owner *pbresource.ID `json:"owner"` | ||
} | ||
|
||
type resourceHandler struct { | ||
reg resource.Registration | ||
client pbresource.ResourceServiceClient | ||
parseToken func(req *http.Request, token *string) | ||
logger hclog.Logger | ||
} | ||
|
||
func (h *resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
var token string | ||
h.parseToken(r, &token) | ||
ctx := metadata.AppendToOutgoingContext(r.Context(), "x-consul-token", token) | ||
switch r.Method { | ||
case http.MethodPut: | ||
h.handleWrite(w, r, ctx) | ||
default: | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
} | ||
|
||
func (h *resourceHandler) handleWrite(w http.ResponseWriter, r *http.Request, ctx context.Context) { | ||
var req writeRequest | ||
// convert req body to writeRequest | ||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte("Request body didn't follow schema.")) | ||
} | ||
// convert data struct to proto message | ||
data := h.reg.Proto.ProtoReflect().New().Interface() | ||
if err := protojson.Unmarshal(req.Data, data); err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte("Request body didn't follow schema.")) | ||
} | ||
// proto message to any | ||
anyProtoMsg, err := anypb.New(data) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
h.logger.Error("Failed to convert proto message to any type", "error", err) | ||
return | ||
} | ||
|
||
tenancyInfo, resourceName, version := checkURL(r) | ||
|
||
rsp, err := h.client.Write(ctx, &pbresource.WriteRequest{ | ||
Resource: &pbresource.Resource{ | ||
Id: &pbresource.ID{ | ||
Type: h.reg.Type, | ||
Tenancy: tenancyInfo, | ||
Name: resourceName, | ||
}, | ||
Owner: req.Owner, | ||
Version: version, | ||
Metadata: req.Metadata, | ||
Data: anyProtoMsg, | ||
}, | ||
}) | ||
if err != nil { | ||
handleResponseError(err, w, h) | ||
return | ||
} | ||
|
||
output, err := jsonMarshal(rsp.Resource) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
h.logger.Error("Failed to unmarshal GRPC resource response", "error", err) | ||
return | ||
} | ||
w.Write(output) | ||
} | ||
|
||
func checkURL(r *http.Request) (tenancy *pbresource.Tenancy, resourceName string, version string) { | ||
params := r.URL.Query() | ||
tenancy = &pbresource.Tenancy{ | ||
Partition: params.Get("partition"), | ||
PeerName: params.Get("peer_name"), | ||
Namespace: params.Get("namespace"), | ||
} | ||
resourceName = path.Base(r.URL.Path) | ||
if resourceName == "." || resourceName == "/" { | ||
resourceName = "" | ||
} | ||
version = params.Get("version") | ||
|
||
return | ||
} | ||
|
||
func jsonMarshal(res *pbresource.Resource) ([]byte, error) { | ||
output, err := protojson.Marshal(res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var stuff map[string]any | ||
if err := json.Unmarshal(output, &stuff); err != nil { | ||
return nil, err | ||
} | ||
|
||
delete(stuff["data"].(map[string]any), "@type") | ||
return json.MarshalIndent(stuff, "", " ") | ||
} | ||
|
||
func handleResponseError(err error, w http.ResponseWriter, h *resourceHandler) { | ||
if e, ok := status.FromError(err); ok { | ||
switch e.Code() { | ||
case codes.InvalidArgument: | ||
w.WriteHeader(http.StatusBadRequest) | ||
h.logger.Info("User has mal-formed request", "error", err) | ||
case codes.NotFound: | ||
w.WriteHeader(http.StatusNotFound) | ||
h.logger.Info("Failed to write to GRPC resource: Not found", "error", err) | ||
case codes.PermissionDenied: | ||
w.WriteHeader(http.StatusForbidden) | ||
h.logger.Info("Failed to write to GRPC resource: User not authenticated", "error", err) | ||
case codes.Aborted: | ||
w.WriteHeader(http.StatusConflict) | ||
h.logger.Info("Failed to write to GRPC resource: the request conflict with the current state of the target resource", "error", err) | ||
default: | ||
w.WriteHeader(http.StatusInternalServerError) | ||
h.logger.Error("Failed to write to GRPC resource", "error", err) | ||
} | ||
} else { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
h.logger.Error("Failed to write to GRPC resource: not able to parse error returned", "error", err) | ||
} | ||
w.Write([]byte(err.Error())) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice: I moved the version here instead of retrieving the info from request
data
struct based on the description from the ticket. It's documented to get the version info from query parameter