Beispiel #1
0
func downstreamFn(chain ChainSpec, ctx context.ContextSpec) handleStateFn {
	mods := chain.GetModules()
	pin := chain.GetCursor()
	for i := pin; i >= 0; i-- {
		resp, err := mods[i].ProcessResponse(ctx)
		if resp != nil {
			//log.Printf("Module: %s writing response ...", mods[i].GetId())
			ctx.SetHttpResponse(resp)
		}
		if err != nil {
			ctx.SetError(err)
		}
	}
	return finishedFn
}
Beispiel #2
0
func (a *HttpLoadbalancer) ProcessRequest(req context.ContextSpec) (*http.Response, error) {
	// None of the modules in the pipeline has intercepted the request, so lets hit the endpoint now!
	// TODO: - Transport should be configurable via options
	//       - HTTP Header to be added: `X-Forwarded-Host`

	// Note that we rewrite request each time we proxy it to the
	// endpoint, so that each try gets a fresh start
	req.SetHttpRequest(copyRequest(req.GetHttpRequest(), req.GetBody(), a.url))

	return http.DefaultTransport.RoundTrip(req.GetHttpRequest())
}
Beispiel #3
0
func (ba *BasicAuth) ProcessRequest(c context.ContextSpec) (*http.Response, error) {
	authHeaderValue := c.GetHttpRequest().Header.Get("Authorization")
	if authHeaderValue == "" {
		logutils.FileLogger.Error("Attempted access with malformed header, no auth header found. Path: %s, Origin: %s", c.GetHttpRequest().URL, c.GetHttpRequest().Referer())
		return nil, errors.FromStatus(http.StatusUnauthorized) // 401
	}
	// Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
	parts := strings.Fields(authHeaderValue)
	if len(parts) != 2 {
		logutils.FileLogger.Error("Attempted access with malformed header, header not in basic auth format.")
		return nil, errors.FromStatus(http.StatusBadRequest) //400
	}

	// Decode the username:password string
	authvaluesStr, err := base64.StdEncoding.DecodeString(parts[1])
	if err != nil {
		logutils.FileLogger.Error("Base64 Decoding failed of basic auth data: %s", err)
		return nil, errors.FromStatus(http.StatusBadRequest) //400
	}

	authValues := strings.Split(string(authvaluesStr), ":")
	if len(authValues) != 2 {
		// Header malformed
		logutils.FileLogger.Error("Attempted access with malformed header, values not in basic auth format.")
		return nil, errors.FromStatus(http.StatusBadRequest) //400
	}

	// CHANGELOG.md - check session and identity for valid key

	// Ensure that username and password match up
	if ba.username != authValues[0] || ba.password != authValues[1] {
		logutils.FileLogger.Error("User not authorized")
		return nil, errors.FromStatus(http.StatusForbidden) //403
	}

	return nil, nil // all good
}
Beispiel #4
0
func (r *Rewrite) ProcessRequest(c context.ContextSpec) (*http.Response, error) {
	//fmt.Printf("[mod_rewrite] Old URL.String() is: %s \n", c.GetHttpRequest().URL.String())
	//c.GetHttpRequest().URL.Path = c.Eval(r.path)
	//fmt.Printf("[mod_rewrite] New URL.String() is: %s \n", c.GetHttpRequest().URL.String())
	operation := r.path[0]
	newPath := FuncMapOps[operation](c.GetHttpRequest().URL.Path, r.path[1])
	fmt.Printf("[mod-rewrite] Old Path was: %s. New Path is: %s ...\n", c.GetHttpRequest().URL.Path, newPath)
	c.GetHttpRequest().URL.Path = newPath
	return nil, nil
}
Beispiel #5
0
func upstreamFn(chain ChainSpec, ctx context.ContextSpec) handleStateFn {
	mods := chain.GetModules()
	for i, mod := range mods {
		resp, err := mod.ProcessRequest(ctx)
		chain.SetCursor(i)
		if resp != nil {
			//log.Printf("Module: %s writing response ...", mods[i].GetId())
			ctx.SetHttpResponse(resp)
			return downstreamFn
		}
		if err != nil {
			//log.Printf("Module: %s found error ...", mods[i].GetId())
			ctx.SetError(err)
			return errorFoundFn
		}
	}
	log.Printf("Error. At least one module must to respond")
	ctx.SetError(errors.FromStatus(http.StatusNoContent))
	return errorFoundFn
}
Beispiel #6
0
func ProcessChain(chain ChainSpec, ctx context.ContextSpec) (*http.Response, error) {
	for stateHandler := upstreamFn; stateHandler != nil; {
		stateHandler = stateHandler(chain, ctx)
	}
	return ctx.GetHttpResponse(), ctx.GetError()
}