Пример #1
0
func requiredValidation(route db.Route) *routing_api.Error {
	err := validateRouteUrl(route.Route)
	if err != nil {
		return err
	}

	err = validateRouteServiceUrl(route.RouteServiceUrl)
	if err != nil {
		return err
	}

	if route.Port <= 0 {
		err := routing_api.NewError(routing_api.RouteInvalidError, "Each route request requires a port greater than 0")
		return &err
	}

	if route.Route == "" {
		err := routing_api.NewError(routing_api.RouteInvalidError, "Each route request requires a valid route")
		return &err
	}

	if route.IP == "" {
		err := routing_api.NewError(routing_api.RouteInvalidError, "Each route request requires an IP")
		return &err
	}

	return nil
}
Пример #2
0
func validateTcpRouteMapping(tcpRouteMapping db.TcpRouteMapping) *routing_api.Error {
	if tcpRouteMapping.TcpRoute.RouterGroupGuid == "" {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
			"Each tcp mapping requires a non empty router group guid. RouteMapping=["+tcpRouteMapping.String()+"]")
		return &err
	}

	if tcpRouteMapping.TcpRoute.ExternalPort <= 0 {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
			"Each tcp mapping requires a positive external port. RouteMapping=["+tcpRouteMapping.String()+"]")
		return &err
	}

	if tcpRouteMapping.HostIP == "" {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
			"Each tcp mapping requires a non empty backend ip. RouteMapping=["+tcpRouteMapping.String()+"]")
		return &err
	}

	if tcpRouteMapping.HostPort <= 0 {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
			"Each tcp mapping requires a positive backend port. RouteMapping=["+tcpRouteMapping.String()+"]")
		return &err
	}

	return nil
}
Пример #3
0
func handleUnauthorizedError(w http.ResponseWriter, err error, log lager.Logger) {
	log.Error("error", err)
	metrics.IncrementTokenError()
	retErr, _ := json.Marshal(routing_api.NewError(routing_api.UnauthorizedError, err.Error()))

	w.WriteHeader(http.StatusUnauthorized)
	w.Write(retErr)
}
Пример #4
0
func handleProcessRequestError(w http.ResponseWriter, procErr error, log lager.Logger) {
	log.Error("error", procErr)

	retErr, _ := json.Marshal(routing_api.NewError(routing_api.ProcessRequestError, "Cannot process request: "+procErr.Error()))

	w.WriteHeader(http.StatusBadRequest)
	w.Write(retErr)
}
Пример #5
0
func handleDBCommunicationError(w http.ResponseWriter, err error, log lager.Logger) {
	log.Error("error", err)

	retErr, _ := json.Marshal(routing_api.NewError(routing_api.DBCommunicationError, err.Error()))

	w.WriteHeader(http.StatusInternalServerError)
	w.Write(retErr)
}
Пример #6
0
func handleDBConflictError(w http.ResponseWriter, err error, log lager.Logger) {
	log.Error("error", err)

	retErr, _ := json.Marshal(routing_api.NewError(routing_api.DBConflictError, err.Error()))

	w.WriteHeader(http.StatusConflict)
	w.Write(retErr)
}
Пример #7
0
func validateRouteServiceUrl(routeService string) *routing_api.Error {
	if routeService == "" {
		return nil
	}

	if !strings.HasPrefix(routeService, "https://") {
		err := routing_api.NewError(routing_api.RouteServiceUrlInvalidError, "Route service url must use HTTPS.")
		return &err
	}

	err := validateUrl(routeService)
	if err != nil {
		err := routing_api.NewError(routing_api.RouteServiceUrlInvalidError, err.Error())
		return &err
	}

	return nil
}
Пример #8
0
func validateRouteUrl(route string) *routing_api.Error {
	err := validateUrl(route)
	if err != nil {
		err := routing_api.NewError(routing_api.RouteInvalidError, err.Error())
		return &err
	}

	return nil
}
Пример #9
0
func (v Validator) ValidateCreate(routes []db.Route, maxTTL int) *routing_api.Error {
	for _, route := range routes {
		err := requiredValidation(route)
		if err != nil {
			return err
		}

		if route.TTL > maxTTL {
			err := routing_api.NewError(routing_api.RouteInvalidError, fmt.Sprintf("Max ttl is %d", maxTTL))
			return &err
		}

		if route.TTL <= 0 {
			err := routing_api.NewError(routing_api.RouteInvalidError, "Request requires a ttl greater than 0")
			return &err
		}
	}
	return nil
}
Пример #10
0
func (v Validator) ValidateCreateTcpRouteMapping(tcpRouteMappings []db.TcpRouteMapping) *routing_api.Error {
	for _, tcpRouteMapping := range tcpRouteMappings {
		err := validateTcpRouteMapping(tcpRouteMapping)
		if err != nil {
			return err
		}
		if tcpRouteMapping.TcpRoute.RouterGroupGuid != helpers.DefaultRouterGroupGuid {
			err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
				"router_group_guid: "+tcpRouteMapping.TcpRoute.RouterGroupGuid+" not found")
			return &err
		}
	}
	return nil
}
Пример #11
0
func validateTcpRouteMapping(tcpRouteMappings db.TcpRouteMapping) *routing_api.Error {

	if tcpRouteMappings.TcpRoute.RouterGroupGuid == "" {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError, "Each tcp mapping requires a valid router group guid")
		return &err
	}

	if tcpRouteMappings.TcpRoute.ExternalPort <= 0 {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError, "Each tcp mapping requires a positive external port")
		return &err
	}

	if tcpRouteMappings.HostIP == "" {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError, "Each tcp mapping requires a non empty host ip")
		return &err
	}

	if tcpRouteMappings.HostPort <= 0 {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError, "Each tcp mapping requires a positive host port")
		return &err
	}

	return nil
}
Пример #12
0
func (v Validator) ValidateCreateTcpRouteMapping(tcpRouteMappings []models.TcpRouteMapping, routerGroups models.RouterGroups, maxTTL uint16) *routing_api.Error {
	for _, tcpRouteMapping := range tcpRouteMappings {
		err := validateTcpRouteMapping(tcpRouteMapping, true, maxTTL)
		if err != nil {
			return err
		}

		validGuid := false
		for _, routerGroup := range routerGroups {
			if tcpRouteMapping.TcpRoute.RouterGroupGuid == routerGroup.Guid {
				validGuid = true
				break
			}
		}

		if !validGuid {
			err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
				"router_group_guid: "+tcpRouteMapping.TcpRoute.RouterGroupGuid+" not found")
			return &err
		}
	}
	return nil
}
Пример #13
0
func validateTcpRouteMapping(tcpRouteMapping models.TcpRouteMapping, checkTTL bool, maxTTL uint16) *routing_api.Error {
	if tcpRouteMapping.TcpRoute.RouterGroupGuid == "" {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
			"Each tcp mapping requires a non empty router group guid. RouteMapping=["+tcpRouteMapping.String()+"]")
		return &err
	}

	if tcpRouteMapping.TcpRoute.ExternalPort <= 0 {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
			"Each tcp mapping requires a positive external port. RouteMapping=["+tcpRouteMapping.String()+"]")
		return &err
	}

	if tcpRouteMapping.HostIP == "" {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
			"Each tcp mapping requires a non empty backend ip. RouteMapping=["+tcpRouteMapping.String()+"]")
		return &err
	}

	if tcpRouteMapping.HostPort <= 0 {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
			"Each tcp mapping requires a positive backend port. RouteMapping=["+tcpRouteMapping.String()+"]")
		return &err
	}

	if checkTTL && tcpRouteMapping.TTL > maxTTL {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
			"Each tcp mapping requires TTL to be less than or equal to "+strconv.Itoa(int(maxTTL))+". RouteMapping=["+tcpRouteMapping.String()+"]")
		return &err
	}

	if checkTTL && tcpRouteMapping.TTL <= 0 {
		err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError,
			"Each tcp route mapping requires a ttl greater than 0")
		return &err
	}

	return nil
}
					request = handlers.NewTestRequest(`[{"route":{"router_group_guid": "tcp-default", "external_port": 5200}, "host_ip": "10.1.1.12", "host_port": 65537}]`)

					tcpRouteMappingsHandler.Upsert(responseRecorder, request)

					Expect(responseRecorder.Code).To(Equal(http.StatusBadRequest))
					Expect(responseRecorder.Body.String()).To(ContainSubstring("cannot unmarshal number 65537 into Go value of type uint16"))
					Expect(database.SaveRouteCallCount()).To(Equal(0))
					Expect(logger.Logs()[0].Message).To(ContainSubstring("error"))

				})

			})

			Context("when validator returns error", func() {
				BeforeEach(func() {
					err := routing_api.NewError(routing_api.TcpRouteMappingInvalidError, "Each tcp mapping requires a valid router group guid")
					validator.ValidateTcpRouteMappingReturns(&err)
				})

				It("returns error", func() {
					request = handlers.NewTestRequest(`[{"route":{"router_group_guid": "", "external_port": 52000}, "host_ip": "10.1.1.12", "host_port": 60000}]`)
					tcpRouteMappingsHandler.Upsert(responseRecorder, request)
					Expect(responseRecorder.Code).To(Equal(http.StatusBadRequest))
					Expect(responseRecorder.Body.String()).To(ContainSubstring("Each tcp mapping requires a valid router group guid"))
					Expect(database.SaveRouteCallCount()).To(Equal(0))
					Expect(logger.Logs()[1].Message).To(ContainSubstring("error"))
				})
			})

			Context("when the UAA token is not valid", func() {
				BeforeEach(func() {