proxy interceptors implicitly modify arguments, instead of returning them
This commit is contained in:
1 parent
126d4f4079
commit
2f8c5d7bd9
4 files changed
+26
-26
No files matched your search
+3
-3
@@ -27,7 +27,7 @@ func newClient(dial func() (net.Conn, error), i interceptor) *client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (c *client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
req, err := c.InterceptRequest(r)
|
err := c.InterceptRequest(r)
|
||||||
if err == docker.ErrNoSuchImage {
|
if err == docker.ErrNoSuchImage {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
@@ -47,13 +47,13 @@ func (c *client) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
client := httputil.NewClientConn(conn, nil)
|
client := httputil.NewClientConn(conn, nil)
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(r)
|
||||||
if err != nil && err != httputil.ErrPersistEOF {
|
if err != nil && err != httputil.ErrPersistEOF {
|
||||||
http.Error(w, fmt.Sprintf("Could not make request to target: %v", err), http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("Could not make request to target: %v", err), http.StatusInternalServerError)
|
||||||
Warning.Print("Error forwarding request: ", err)
|
Warning.Print("Error forwarding request: ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resp, err = c.InterceptResponse(resp)
|
err = c.InterceptResponse(resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Unable to intercept response", http.StatusInternalServerError)
|
http.Error(w, "Unable to intercept response", http.StatusInternalServerError)
|
||||||
Warning.Print("Error intercepting response: ", err)
|
Warning.Print("Error intercepting response: ", err)
|
||||||
|
|||||||
@@ -21,36 +21,36 @@ type createContainerRequestBody struct {
|
|||||||
MacAddress string `json:"MacAddress,omitempty" yaml:"MacAddress,omitempty"`
|
MacAddress string `json:"MacAddress,omitempty" yaml:"MacAddress,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *createContainerInterceptor) InterceptRequest(r *http.Request) (*http.Request, error) {
|
func (i *createContainerInterceptor) InterceptRequest(r *http.Request) error {
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
r.Body.Close()
|
r.Body.Close()
|
||||||
|
|
||||||
container := createContainerRequestBody{}
|
container := createContainerRequestBody{}
|
||||||
if err := json.Unmarshal(body, &container); err != nil {
|
if err := json.Unmarshal(body, &container); err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := weaveCIDRsFromConfig(container.Config); ok {
|
if _, ok := weaveCIDRsFromConfig(container.Config); ok {
|
||||||
container.HostConfig.VolumesFrom = append(container.HostConfig.VolumesFrom, "weaveproxy")
|
container.HostConfig.VolumesFrom = append(container.HostConfig.VolumesFrom, "weaveproxy")
|
||||||
if err := i.setWeaveWaitEntrypoint(container.Config); err != nil {
|
if err := i.setWeaveWaitEntrypoint(container.Config); err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
if err := i.setWeaveDNS(&container); err != nil {
|
if err := i.setWeaveDNS(&container); err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newBody, err := json.Marshal(container)
|
newBody, err := json.Marshal(container)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
r.Body = ioutil.NopCloser(bytes.NewReader(newBody))
|
r.Body = ioutil.NopCloser(bytes.NewReader(newBody))
|
||||||
r.ContentLength = int64(len(newBody))
|
r.ContentLength = int64(len(newBody))
|
||||||
|
|
||||||
return r, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Config) error {
|
func (i *createContainerInterceptor) setWeaveWaitEntrypoint(container *docker.Config) error {
|
||||||
@@ -100,6 +100,6 @@ func (i *createContainerInterceptor) setWeaveDNS(container *createContainerReque
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *createContainerInterceptor) InterceptResponse(r *http.Response) (*http.Response, error) {
|
func (i *createContainerInterceptor) InterceptResponse(r *http.Response) error {
|
||||||
return r, nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -3,17 +3,17 @@ package proxy
|
|||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
type interceptor interface {
|
type interceptor interface {
|
||||||
InterceptRequest(*http.Request) (*http.Request, error)
|
InterceptRequest(*http.Request) error
|
||||||
InterceptResponse(*http.Response) (*http.Response, error)
|
InterceptResponse(*http.Response) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type nullInterceptor struct {
|
type nullInterceptor struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i nullInterceptor) InterceptRequest(r *http.Request) (*http.Request, error) {
|
func (i nullInterceptor) InterceptRequest(r *http.Request) error {
|
||||||
return r, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i nullInterceptor) InterceptResponse(r *http.Response) (*http.Response, error) {
|
func (i nullInterceptor) InterceptResponse(r *http.Response) error {
|
||||||
return r, nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -16,15 +16,15 @@ type startContainerInterceptor struct {
|
|||||||
withDNS bool
|
withDNS bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *startContainerInterceptor) InterceptRequest(r *http.Request) (*http.Request, error) {
|
func (i *startContainerInterceptor) InterceptRequest(r *http.Request) error {
|
||||||
return r, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *startContainerInterceptor) InterceptResponse(r *http.Response) (*http.Response, error) {
|
func (i *startContainerInterceptor) InterceptResponse(r *http.Response) error {
|
||||||
containerID := ""
|
containerID := ""
|
||||||
if subs := containerIDRegexp.FindStringSubmatch(r.Request.URL.Path); subs == nil {
|
if subs := containerIDRegexp.FindStringSubmatch(r.Request.URL.Path); subs == nil {
|
||||||
Warning.Printf("No container id found in request with path %s", r.Request.URL.Path)
|
Warning.Printf("No container id found in request with path %s", r.Request.URL.Path)
|
||||||
return r, nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
containerID = subs[1]
|
containerID = subs[1]
|
||||||
}
|
}
|
||||||
@@ -32,13 +32,13 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) (*http.R
|
|||||||
container, err := i.client.InspectContainer(containerID)
|
container, err := i.client.InspectContainer(containerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Warning.Printf("Error inspecting container %s: %v", containerID, err)
|
Warning.Printf("Error inspecting container %s: %v", containerID, err)
|
||||||
return r, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cidrs, ok := weaveCIDRsFromConfig(container.Config)
|
cidrs, ok := weaveCIDRsFromConfig(container.Config)
|
||||||
if !ok {
|
if !ok {
|
||||||
Debug.Print("No Weave CIDR, ignoring")
|
Debug.Print("No Weave CIDR, ignoring")
|
||||||
return r, nil
|
return nil
|
||||||
}
|
}
|
||||||
Info.Printf("Container %s was started with CIDR \"%s\"", containerID, strings.Join(cidrs, " "))
|
Info.Printf("Container %s was started with CIDR \"%s\"", containerID, strings.Join(cidrs, " "))
|
||||||
args := []string{"attach"}
|
args := []string{"attach"}
|
||||||
@@ -46,7 +46,7 @@ func (i *startContainerInterceptor) InterceptResponse(r *http.Response) (*http.R
|
|||||||
args = append(args, containerID)
|
args = append(args, containerID)
|
||||||
if _, err := callWeave(args...); err != nil {
|
if _, err := callWeave(args...); err != nil {
|
||||||
Warning.Printf("Attaching container %s to weave failed: %v", containerID, err)
|
Warning.Printf("Attaching container %s to weave failed: %v", containerID, err)
|
||||||
return r, nil
|
return nil
|
||||||
}
|
}
|
||||||
return r, nil
|
return nil
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user