mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
Add json-response flag to porter
Provides a response that includes a body and a method. This response will enable a client (e2e test) to confirm that a proxy did not alter the http method.
This commit is contained in:
parent
ededd08ba1
commit
f005b3a5f7
@ -1,7 +1,7 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
# agnhost: bump this one first
|
# agnhost: bump this one first
|
||||||
- name: "agnhost"
|
- name: "agnhost"
|
||||||
version: "2.24"
|
version: "2.25"
|
||||||
refPaths:
|
refPaths:
|
||||||
- path: test/images/agnhost/VERSION
|
- path: test/images/agnhost/VERSION
|
||||||
match: \d.\d
|
match: \d.\d
|
||||||
|
@ -1 +1 @@
|
|||||||
2.24
|
2.25
|
||||||
|
@ -51,7 +51,7 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
rootCmd := &cobra.Command{
|
rootCmd := &cobra.Command{
|
||||||
Use: "app",
|
Use: "app",
|
||||||
Version: "2.24",
|
Version: "2.25",
|
||||||
}
|
}
|
||||||
|
|
||||||
rootCmd.AddCommand(auditproxy.CmdAuditProxy)
|
rootCmd.AddCommand(auditproxy.CmdAuditProxy)
|
||||||
|
@ -23,6 +23,7 @@ limitations under the License.
|
|||||||
package porter
|
package porter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -55,6 +56,18 @@ To use a different cert/key, mount them into the pod and set the "CERT_FILE" and
|
|||||||
Run: main,
|
Run: main,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JSONResponse enables --json-response flag
|
||||||
|
var JSONResponse bool
|
||||||
|
|
||||||
|
type jsonResponse struct {
|
||||||
|
Method string
|
||||||
|
Body string
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
CmdPorter.Flags().BoolVar(&JSONResponse, "json-response", false, "Responds to requests with a json response that includes the default value with the http.Request Method")
|
||||||
|
}
|
||||||
|
|
||||||
func main(cmd *cobra.Command, args []string) {
|
func main(cmd *cobra.Command, args []string) {
|
||||||
for _, vk := range os.Environ() {
|
for _, vk := range os.Environ() {
|
||||||
// Put everything before the first = sign in parts[0], and
|
// Put everything before the first = sign in parts[0], and
|
||||||
@ -81,10 +94,23 @@ func main(cmd *cobra.Command, args []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func servePort(port, value string) {
|
func servePort(port, value string) {
|
||||||
|
|
||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Addr: "0.0.0.0:" + port,
|
Addr: "0.0.0.0:" + port,
|
||||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprint(w, value)
|
body := value
|
||||||
|
if JSONResponse {
|
||||||
|
j, err := json.Marshal(&jsonResponse{
|
||||||
|
Method: r.Method,
|
||||||
|
Body: value})
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Internal Server Error: %v", err), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
body = string(j)
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, body)
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
log.Printf("server on port %q failed: %v", port, s.ListenAndServe())
|
log.Printf("server on port %q failed: %v", port, s.ListenAndServe())
|
||||||
@ -94,7 +120,19 @@ func serveTLSPort(port, value string) {
|
|||||||
s := &http.Server{
|
s := &http.Server{
|
||||||
Addr: "0.0.0.0:" + port,
|
Addr: "0.0.0.0:" + port,
|
||||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprint(w, value)
|
body := value
|
||||||
|
if JSONResponse {
|
||||||
|
j, err := json.Marshal(&jsonResponse{
|
||||||
|
Method: r.Method,
|
||||||
|
Body: value})
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("Internal Server Error: %v", err), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
body = string(j)
|
||||||
|
}
|
||||||
|
fmt.Fprint(w, body)
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
certFile := os.Getenv("CERT_FILE")
|
certFile := os.Getenv("CERT_FILE")
|
||||||
|
Loading…
Reference in New Issue
Block a user