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:
Stephen Heywood 2020-10-22 16:25:46 +13:00
parent ededd08ba1
commit f005b3a5f7
4 changed files with 43 additions and 5 deletions

View File

@ -1,7 +1,7 @@
dependencies:
# agnhost: bump this one first
- name: "agnhost"
version: "2.24"
version: "2.25"
refPaths:
- path: test/images/agnhost/VERSION
match: \d.\d

View File

@ -1 +1 @@
2.24
2.25

View File

@ -51,7 +51,7 @@ import (
func main() {
rootCmd := &cobra.Command{
Use: "app",
Version: "2.24",
Version: "2.25",
}
rootCmd.AddCommand(auditproxy.CmdAuditProxy)

View File

@ -23,6 +23,7 @@ limitations under the License.
package porter
import (
"encoding/json"
"fmt"
"log"
"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,
}
// 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) {
for _, vk := range os.Environ() {
// 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) {
s := &http.Server{
Addr: "0.0.0.0:" + port,
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())
@ -94,7 +120,19 @@ func serveTLSPort(port, value string) {
s := &http.Server{
Addr: "0.0.0.0:" + port,
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")