-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add pprof.enabled configuration flag for /debug/pprof APIs (#505)…
… (#509) In order to disable access in production environments, a new filter was added to disable access when the configuration is set to any non true value. When changing the value the filter will pass and go tool pprof can be used directly
- Loading branch information
Showing
11 changed files
with
258 additions
and
19 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
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,64 @@ | ||
/* | ||
Copyright 2023 The Katanomi Authors. | ||
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 config | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
restful "github.com/emicklei/go-restful/v3" | ||
"github.com/katanomi/pkg/errors" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"knative.dev/pkg/logging" | ||
) | ||
|
||
// ConfigKeyExpectedValueFunc is a helper function to check if configmap has expected value | ||
// If the value is not as expected, an error is expected to be returned | ||
type ConfigKeyExpectedValueFunc func(ctx context.Context, req *restful.Request, key string, value FeatureValue) (err error) | ||
|
||
// ConfigFilter adds a restful filter to manager to watch configmap and and custom validation | ||
// according a specific key value pair. | ||
func ConfigFilter(ctx context.Context, manager *Manager, configKey string, expectedKeyValueFunc ConfigKeyExpectedValueFunc) func(*restful.Request, *restful.Response, *restful.FilterChain) { | ||
return func(req *restful.Request, res *restful.Response, chain *restful.FilterChain) { | ||
featureValue := manager.GetFeatureFlag(configKey) | ||
if err := expectedKeyValueFunc(ctx, req, configKey, featureValue); err != nil { | ||
log := logging.FromContext(ctx) | ||
log.Debugw("Error in ConfigFilter, will return", "err", err, "code", res.StatusCode()) | ||
errors.HandleError(req, res, err) | ||
return | ||
} | ||
chain.ProcessFilter(req, res) | ||
} | ||
} | ||
|
||
// ConfigFilterNotFoundWhenNotTrue is a helper ConfigKeyExpectedValue implementation that checks if the value is a boolean true | ||
// value, if not true will return a standard 404 not found error | ||
func ConfigFilterNotFoundWhenNotTrue(ctx context.Context, req *restful.Request, key string, value FeatureValue) (err error) { | ||
if ok, _ := value.AsBool(); !ok { | ||
return apierrors.NewGenericServerResponse( | ||
http.StatusNotFound, | ||
req.Request.Method, | ||
errors.RESTAPIGroupResource, | ||
req.Request.URL.String(), | ||
fmt.Sprintf("%s Not Found", req.Request.URL.String()), | ||
0, | ||
false, | ||
) | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
Copyright 2023 The Katanomi Authors. | ||
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 config | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strings" | ||
|
||
restful "github.com/emicklei/go-restful/v3" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"knative.dev/pkg/logging" | ||
) | ||
|
||
var _ = Describe("ConfigFilter", func() { | ||
|
||
var ( | ||
manager *Manager | ||
ctx context.Context | ||
request *restful.Request | ||
response *restful.Response | ||
chain *restful.FilterChain | ||
recorder *httptest.ResponseRecorder | ||
key string | ||
expectedKeyValueFunc ConfigKeyExpectedValueFunc | ||
) | ||
|
||
BeforeEach(func() { | ||
ctx = context.Background() | ||
ctx = logging.WithLogger(ctx, log) | ||
req := &http.Request{ | ||
Header: map[string][]string{ | ||
restful.HEADER_AcceptEncoding: []string{restful.MIME_JSON}, | ||
}, | ||
} | ||
testUrl, _ := url.Parse("http://test.example/some/path") | ||
req.URL = testUrl | ||
request = &restful.Request{Request: req} | ||
recorder = httptest.NewRecorder() | ||
response = &restful.Response{ResponseWriter: recorder} | ||
response.SetRequestAccepts(restful.MIME_JSON) | ||
chain = &restful.FilterChain{ | ||
Filters: []restful.FilterFunction{}, | ||
Target: func(req *restful.Request, resp *restful.Response) { | ||
resp.WriteHeader(http.StatusOK) | ||
}, | ||
} | ||
|
||
manager = &Manager{Config: &Config{Data: map[string]string{"test": "test"}}} | ||
key = "test" | ||
}) | ||
|
||
JustBeforeEach(func() { | ||
request.Request = request.Request.WithContext(ctx) | ||
|
||
ConfigFilter(ctx, manager, key, expectedKeyValueFunc)(request, response, chain) | ||
}) | ||
|
||
Context("Uses a \"test\" key with \"test\" value using some basic ConfigKeyExpectedValueFunc implementation", func() { | ||
BeforeEach(func() { | ||
expectedKeyValueFunc = func(ctx context.Context, req *restful.Request, key string, value FeatureValue) (err error) { | ||
ok, err := value.AsBool() | ||
if err != nil { | ||
return err | ||
} else if !ok { | ||
return fmt.Errorf("value is not true: %v", value) | ||
} | ||
return nil | ||
} | ||
}) | ||
It("should have a internal error as status code with api error in response body", func() { | ||
Expect(recorder.Code).To(Equal(http.StatusInternalServerError)) | ||
Expect(strings.TrimSpace(recorder.Body.String())).To(Equal(`{"metadata":{},"status":"Failure","message":"Internal error occurred: failed parsing feature flags config \"test\": strconv.ParseBool: parsing \"test\": invalid syntax","reason":"InternalError","details":{"causes":[{"message":"failed parsing feature flags config \"test\": strconv.ParseBool: parsing \"test\": invalid syntax"}]},"code":500}`)) | ||
}) | ||
|
||
Context("Uses a \"test\" key with \"true\" value using some basic ConfigKeyExpectedValueFunc implementation", func() { | ||
BeforeEach(func() { | ||
manager.Config.Data["test"] = "true" | ||
}) | ||
It("should pass filter", func() { | ||
Expect(recorder.Code).To(Equal(http.StatusOK)) | ||
}) | ||
}) | ||
}) | ||
|
||
Context("Uses ConfigFilterNotFoundWhenNotTrue with false value", func() { | ||
BeforeEach(func() { | ||
expectedKeyValueFunc = ConfigFilterNotFoundWhenNotTrue | ||
manager.Config.Data["test"] = "false" | ||
}) | ||
|
||
It("should have a not found error as status code with api error in response body", func() { | ||
Expect(recorder.Code).To(Equal(http.StatusNotFound)) | ||
Expect(strings.TrimSpace(recorder.Body.String())).To(Equal(`{"metadata":{},"status":"Failure","message":"the server could not find the requested resource ( API.katanomi.dev http://test.example/some/path)","reason":"NotFound","details":{"name":"http://test.example/some/path","group":"katanomi.dev","kind":"API"},"code":404}`)) | ||
}) | ||
|
||
}) | ||
|
||
Context("Uses ConfigFilterNotFoundWhenNotTrue with true value", func() { | ||
BeforeEach(func() { | ||
expectedKeyValueFunc = ConfigFilterNotFoundWhenNotTrue | ||
manager.Config.Data["test"] = "true" | ||
}) | ||
|
||
It("should pass filter", func() { | ||
Expect(recorder.Code).To(Equal(http.StatusOK)) | ||
}) | ||
|
||
}) | ||
}) |
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
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