Esempio n. 1
0
func constructApiServer(cfg config.Config, database db.DB, statsdClient statsd.Statter, stopChan chan struct{}, logger lager.Logger) ifrit.Runner {
	var token authentication.Token

	if *devMode {
		token = authentication.NullToken{}
	} else {
		token = authentication.NewAccessToken(cfg.UAAPublicKey)
		err := token.CheckPublicToken()
		if err != nil {
			logger.Error("failed to check public token", err)
			os.Exit(1)
		}
	}

	validator := handlers.NewValidator()
	routesHandler := handlers.NewRoutesHandler(token, *maxTTL, validator, database, logger)
	eventStreamHandler := handlers.NewEventStreamHandler(token, database, logger, statsdClient, stopChan)

	actions := rata.Handlers{
		"Upsert":      route(routesHandler.Upsert),
		"Delete":      route(routesHandler.Delete),
		"List":        route(routesHandler.List),
		"EventStream": route(eventStreamHandler.EventStream),
	}

	handler, err := rata.NewRouter(routing_api.Routes, actions)
	if err != nil {
		logger.Error("failed to create router", err)
		os.Exit(1)
	}

	handler = handlers.LogWrap(handler, logger)
	return http_server.New(":"+strconv.Itoa(*port), handler)
}
Esempio n. 2
0
func constructApiServer(cfg config.Config, database db.DB, statsdClient statsd.Statter, stopChan chan struct{}, logger lager.Logger) ifrit.Runner {
	var tokenValidator authentication.TokenValidator

	if *devMode {
		tokenValidator = authentication.NullTokenValidator{}
	} else {
		uaaKeyFetcher := authentication.NewUaaKeyFetcher(logger, cfg.UAAEndpoint+"/token_key")
		uaaPublicKey, err := uaaKeyFetcher.FetchKey()
		if err != nil {
			logger.Error("Failed to get verification key from UAA", err)
			os.Exit(1)
		}
		tokenValidator = authentication.NewAccessTokenValidator(logger, uaaPublicKey, uaaKeyFetcher)
		err = tokenValidator.CheckPublicToken()
		if err != nil {
			logger.Error("Failed to check public token", err)
			os.Exit(1)
		}
	}

	validator := handlers.NewValidator()
	routesHandler := handlers.NewRoutesHandler(tokenValidator, *maxTTL, validator, database, logger)
	eventStreamHandler := handlers.NewEventStreamHandler(tokenValidator, database, logger, statsdClient, stopChan)
	routeGroupsHandler := handlers.NewRouteGroupsHandler(tokenValidator, logger)
	tcpMappingsHandler := handlers.NewTcpRouteMappingsHandler(tokenValidator, validator, database, logger)

	actions := rata.Handlers{
		routing_api.UpsertRoute:           route(routesHandler.Upsert),
		routing_api.DeleteRoute:           route(routesHandler.Delete),
		routing_api.ListRoute:             route(routesHandler.List),
		routing_api.EventStreamRoute:      route(eventStreamHandler.EventStream),
		routing_api.ListRouterGroups:      route(routeGroupsHandler.ListRouterGroups),
		routing_api.UpsertTcpRouteMapping: route(tcpMappingsHandler.Upsert),
		routing_api.DeleteTcpRouteMapping: route(tcpMappingsHandler.Delete),
		routing_api.ListTcpRouteMapping:   route(tcpMappingsHandler.List),
		routing_api.EventStreamTcpRoute:   route(eventStreamHandler.TcpEventStream),
	}

	handler, err := rata.NewRouter(routing_api.Routes, actions)
	if err != nil {
		logger.Error("failed to create router", err)
		os.Exit(1)
	}

	handler = handlers.LogWrap(handler, logger)
	return http_server.New(":"+strconv.Itoa(int(*port)), handler)
}
Esempio n. 3
0
func constructApiServer(cfg config.Config, database db.DB, statsdClient statsd.Statter, logger lager.Logger) ifrit.Runner {

	uaaClient, err := newUaaClient(logger, cfg)
	if err != nil {
		logger.Error("Failed to create uaa client", err)
		os.Exit(1)
	}

	_, err = uaaClient.FetchKey()
	if err != nil {
		logger.Error("Failed to get verification key from UAA", err)
		os.Exit(1)
	}

	validator := handlers.NewValidator()
	routesHandler := handlers.NewRoutesHandler(uaaClient, int(maxTTL.Seconds()), validator, database, logger)
	eventStreamHandler := handlers.NewEventStreamHandler(uaaClient, database, logger, statsdClient)
	routeGroupsHandler := handlers.NewRouteGroupsHandler(uaaClient, logger, database)
	tcpMappingsHandler := handlers.NewTcpRouteMappingsHandler(uaaClient, validator, database, int(maxTTL.Seconds()), logger)

	actions := rata.Handlers{
		routing_api.UpsertRoute:           route(routesHandler.Upsert),
		routing_api.DeleteRoute:           route(routesHandler.Delete),
		routing_api.ListRoute:             route(routesHandler.List),
		routing_api.EventStreamRoute:      route(eventStreamHandler.EventStream),
		routing_api.ListRouterGroups:      route(routeGroupsHandler.ListRouterGroups),
		routing_api.UpsertTcpRouteMapping: route(tcpMappingsHandler.Upsert),
		routing_api.DeleteTcpRouteMapping: route(tcpMappingsHandler.Delete),
		routing_api.ListTcpRouteMapping:   route(tcpMappingsHandler.List),
		routing_api.EventStreamTcpRoute:   route(eventStreamHandler.TcpEventStream),
	}

	handler, err := rata.NewRouter(routing_api.Routes, actions)
	if err != nil {
		logger.Error("failed to create router", err)
		os.Exit(1)
	}

	handler = handlers.LogWrap(handler, logger)
	return http_server.New(":"+strconv.Itoa(int(*port)), handler)
}
)

var _ = Describe("EventsHandler", func() {
	var (
		handler  handlers.EventStreamHandler
		database *fake_db.FakeDB
		logger   *lagertest.TestLogger
		token    *fake_token.FakeToken
		server   *httptest.Server
	)

	BeforeEach(func() {
		token = &fake_token.FakeToken{}
		database = &fake_db.FakeDB{}
		logger = lagertest.NewTestLogger("event-handler-test")
		handler = *handlers.NewEventStreamHandler(token, database, logger)
	})

	AfterEach(func(done Done) {
		if server != nil {
			go func() {
				server.CloseClientConnections()
				server.Close()
				close(done)
			}()
		} else {
			close(done)
		}
	})

	Describe(".EventStream", func() {
Esempio n. 5
0
func main() {
	logger := cf_lager.New("routing-api")

	flag.Parse()
	if *configPath == "" {
		logger.Error("failed to start", errors.New("No configuration file provided"))
		os.Exit(1)
	}

	cfg, err := config.NewConfigFromFile(*configPath)
	if err != nil {
		logger.Error("failed to start", err)
		os.Exit(1)
	}

	err = dropsonde.Initialize(cfg.MetronConfig.Address+":"+cfg.MetronConfig.Port, cfg.LogGuid)
	if err != nil {
		logger.Error("failed to initialize Dropsonde", err)
		os.Exit(1)
	}

	logger.Info("database", lager.Data{"etcd-addresses": flag.Args()})
	database := db.NewETCD(flag.Args())
	err = database.Connect()
	if err != nil {
		logger.Error("failed to connect to etcd", err)
		os.Exit(1)
	}
	defer database.Disconnect()

	var token authentication.Token

	if *devMode {
		token = authentication.NullToken{}
	} else {
		token = authentication.NewAccessToken(cfg.UAAPublicKey)
		err = token.CheckPublicToken()
		if err != nil {
			logger.Error("failed to check public token", err)
			os.Exit(1)
		}
	}

	validator := handlers.NewValidator()

	routesHandler := handlers.NewRoutesHandler(token, *maxTTL, validator, database, logger)
	eventStreamHandler := handlers.NewEventStreamHandler(token, database, logger)

	actions := rata.Handlers{
		"Upsert":      route(routesHandler.Upsert),
		"Delete":      route(routesHandler.Delete),
		"List":        route(routesHandler.List),
		"EventStream": route(eventStreamHandler.EventStream),
	}

	handler, err := rata.NewRouter(routing_api.Routes, actions)
	if err != nil {
		logger.Error("failed to create router", err)
		os.Exit(1)
	}

	handler = handlers.LogWrap(handler, logger)

	logger.Info("starting", lager.Data{"port": *port})
	err = http.ListenAndServe(":"+strconv.Itoa(*port), handler)
	if err != nil {
		panic(err)
	}
}
		handler  handlers.EventStreamHandler
		database *fake_db.FakeDB
		logger   *lagertest.TestLogger
		token    *fake_token.FakeToken
		server   *httptest.Server
		stats    *fake_statsd.FakePartialStatsdClient
		stopChan chan struct{}
	)

	BeforeEach(func() {
		token = &fake_token.FakeToken{}
		database = &fake_db.FakeDB{}
		logger = lagertest.NewTestLogger("event-handler-test")
		stats = new(fake_statsd.FakePartialStatsdClient)
		stopChan = make(chan struct{})
		handler = *handlers.NewEventStreamHandler(token, database, logger, stats, stopChan)
	})

	AfterEach(func(done Done) {
		if server != nil {
			go func() {
				server.CloseClientConnections()
				server.Close()
				close(done)
			}()
		} else {
			close(done)
		}
	})

	Describe(".EventStream", func() {
		fakeClient *fake_client.FakeClient
		server     *httptest.Server
		stats      *fake_statsd.FakePartialStatsdClient
	)

	var emptyCancelFunc = func() {}

	BeforeEach(func() {
		fakeClient = &fake_client.FakeClient{}

		database = &fake_db.FakeDB{}
		database.WatchRouteChangesReturns(nil, nil, emptyCancelFunc)

		logger = lagertest.NewTestLogger("event-handler-test")
		stats = new(fake_statsd.FakePartialStatsdClient)
		handler = *handlers.NewEventStreamHandler(fakeClient, database, logger, stats)
	})

	AfterEach(func(done Done) {
		if server != nil {
			go func() {
				server.CloseClientConnections()
				server.Close()
				close(done)
			}()
		} else {
			close(done)
		}
	})

	Describe("EventStream", func() {