forked from GoogleCloudPlatform/k8s-config-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mockgcp: improve iam serviceaccount to match golden tests
- Loading branch information
Showing
6 changed files
with
471 additions
and
9 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 httpmux | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
type wrappedStatus struct { | ||
Error *wrappedError `json:"error,omitempty"` | ||
} | ||
|
||
type wrappedError struct { | ||
Code int `json:"code,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
Status string `json:"status,omitempty"` | ||
Errors []errorDetails `json:"errors,omitempty"` | ||
} | ||
|
||
type errorDetails struct { | ||
Domain string `json:"domain,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
Reason string `json:"reason,omitempty"` | ||
} | ||
|
||
// customErrorHandler wraps errors in an error blockk | ||
func customErrorHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) { | ||
s := status.Convert(err) | ||
// pb := s.Proto() | ||
|
||
w.Header().Del("Trailer") | ||
w.Header().Del("Transfer-Encoding") | ||
|
||
w.Header().Set("Content-Type", "application/json; charset=UTF-8") | ||
|
||
httpStatusCode := runtime.HTTPStatusFromCode(s.Code()) | ||
wrapped := &wrappedStatus{ | ||
Error: &wrappedError{ | ||
Code: httpStatusCode, | ||
Message: s.Message(), | ||
}, | ||
} | ||
|
||
switch s.Code() { | ||
case codes.PermissionDenied: | ||
wrapped.Error.Status = "PERMISSION_DENIED" | ||
case codes.AlreadyExists: | ||
wrapped.Error.Status = "ALREADY_EXISTS" | ||
case codes.NotFound: | ||
wrapped.Error.Status = "NOT_FOUND" | ||
wrapped.Error.Errors = append(wrapped.Error.Errors, errorDetails{ | ||
Domain: "global", | ||
Message: wrapped.Error.Message, | ||
Reason: "notFound", | ||
}) | ||
} | ||
|
||
buf, merr := json.Marshal(wrapped) | ||
if merr != nil { | ||
klog.Warningf("Failed to marshal error message %q: %v", s, merr) | ||
runtime.DefaultHTTPErrorHandler(ctx, mux, marshaler, w, r, err) | ||
return | ||
} | ||
|
||
if err := addGCPHeaders(ctx, w, nil); err != nil { | ||
klog.Warningf("unexpected error from header filter: %v", err) | ||
} | ||
|
||
w.WriteHeader(httpStatusCode) | ||
if _, err := w.Write(buf); err != nil { | ||
klog.Warningf("Failed to write response: %v", err) | ||
} | ||
|
||
} |
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,79 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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 httpmux | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime" | ||
"google.golang.org/grpc" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/proto" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
// NewServeMux constructs an http server with our error handling etc | ||
func NewServeMux(ctx context.Context, conn *grpc.ClientConn, handlers ...func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error) (*runtime.ServeMux, error) { | ||
marshaler := &runtime.HTTPBodyMarshaler{ | ||
Marshaler: &runtime.JSONPb{ | ||
MarshalOptions: protojson.MarshalOptions{ | ||
EmitUnpopulated: false, | ||
}, | ||
UnmarshalOptions: protojson.UnmarshalOptions{ | ||
DiscardUnknown: true, | ||
}, | ||
}, | ||
} | ||
|
||
outgoingHeaderMatcher := func(key string) (string, bool) { | ||
switch key { | ||
case "content-type": | ||
return "", false | ||
default: | ||
klog.Warningf("unknown grpc metadata header %q", key) | ||
return "", false | ||
} | ||
} | ||
|
||
mux := runtime.NewServeMux( | ||
runtime.WithErrorHandler(customErrorHandler), | ||
runtime.WithMarshalerOption(runtime.MIMEWildcard, marshaler), | ||
runtime.WithOutgoingHeaderMatcher(outgoingHeaderMatcher), | ||
runtime.WithForwardResponseOption(addGCPHeaders), | ||
) | ||
|
||
for _, handler := range handlers { | ||
if err := handler(ctx, mux, conn); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return mux, nil | ||
} | ||
|
||
func addGCPHeaders(ctx context.Context, w http.ResponseWriter, resp proto.Message) error { | ||
if w.Header().Get("Content-Type") == "application/json" { | ||
w.Header().Set("Content-Type", "application/json; charset=UTF-8") | ||
} | ||
w.Header().Set("Cache-Control", "private") | ||
w.Header().Set("Server", "ESF") | ||
w.Header()["Vary"] = []string{"Origin", "X-Origin", "Referer"} | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
w.Header().Set("X-Frame-Options", "SAMEORIGIN") | ||
w.Header().Set("X-Xss-Protection", "0") | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.