func main() { configPath := flag.String("agentConfig", "", "Agent config yaml") flag.Parse() logger := lager.NewLogger("redis-agent") logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG)) logger.RegisterSink(lager.NewWriterSink(os.Stderr, lager.ERROR)) config, err := agentconfig.Load(*configPath) if err != nil { logger.Fatal("Error loading config file", err, lager.Data{ "path": *configPath, }) } templateRedisConf(config, logger) redisResetter := resetter.New( config.DefaultConfPath, config.ConfPath, portChecker{}, commandRunner{}, config.MonitExecutablePath, ) handler := auth.NewWrapper( config.AuthConfiguration.Username, config.AuthConfiguration.Password, ).Wrap( agentapi.New(redisResetter, config.ConfPath), ) http.Handle("/", handler) logger.Fatal("http-listen", http.ListenAndServe("localhost:"+config.Port, nil)) }
JustBeforeEach(func() { log = gbytes.NewBuffer() logger := lager.NewLogger("provider") logger.RegisterSink(lager.NewWriterSink(log, lager.DEBUG)) idLocator = id.DedicatedInstanceIDLocator(endpoint, clientUsername, clientPassword, logger) actualInstanceID, instanceIDErr = idLocator.LocateID("", nodeIP) expectedURL = fmt.Sprintf("%s?host=%s", endpoint, nodeIP) }) Context("when the broker responds", func() { BeforeEach(func() { nodeIP = "8.8.8.8" authWrapper := auth.NewWrapper(brokerUsername, brokerPassword) instanceHandler := authWrapper.WrapFunc(redisinstance.NewHandler(&instanceFinder{ InstanceID: expectedInstanceID, InstanceIP: nodeIP, })) server = httptest.NewServer(instanceHandler) endpoint = server.URL clientUsername = brokerUsername clientPassword = brokerPassword }) AfterEach(func() { server.Close()
func New(serviceBroker ServiceBroker, logger lager.Logger, brokerCredentials BrokerCredentials) http.Handler { router := mux.NewRouter() AttachRoutes(router, serviceBroker, logger) return auth.NewWrapper(brokerCredentials.Username, brokerCredentials.Password).Wrap(router) }
func main() { brokerConfigPath := configPath() brokerLogger := lager.NewLogger("redis-broker") brokerLogger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG)) brokerLogger.RegisterSink(lager.NewWriterSink(os.Stderr, lager.ERROR)) brokerLogger.Info("Starting CF Redis broker") brokerLogger.Info("Config File: " + brokerConfigPath) config, err := brokerconfig.ParseConfig(brokerConfigPath) if err != nil { brokerLogger.Fatal("Loading config file", err, lager.Data{ "broker-config-path": brokerConfigPath, }) } commandRunner := system.OSCommandRunner{ Logger: brokerLogger, } localRepo := redis.NewLocalRepository(config.RedisConfiguration, brokerLogger) localRepo.AllInstancesVerbose() processController := &redis.OSProcessController{ CommandRunner: commandRunner, InstanceInformer: localRepo, Logger: brokerLogger, ProcessChecker: &process.ProcessChecker{}, ProcessKiller: &process.ProcessKiller{}, PingFunc: redis.PingServer, WaitUntilConnectableFunc: availability.Check, } localCreator := &redis.LocalInstanceCreator{ FindFreePort: system.FindFreePort, RedisConfiguration: config.RedisConfiguration, ProcessController: processController, LocalInstanceRepository: localRepo, } agentClient := &redis.RemoteAgentClient{ HttpAuth: config.AuthConfiguration, } remoteRepo, err := redis.NewRemoteRepository(agentClient, config, brokerLogger) if err != nil { brokerLogger.Fatal("Error initializing remote repository", err) } sigChannel := make(chan os.Signal, 1) signal.Notify(sigChannel, syscall.SIGTERM) go func() { <-sigChannel brokerLogger.Info("Starting Redis Broker shutdown") localRepo.AllInstancesVerbose() remoteRepo.StateFromFile() os.Exit(0) }() serviceBroker := &broker.RedisServiceBroker{ InstanceCreators: map[string]broker.InstanceCreator{ "shared": localCreator, "dedicated": remoteRepo, }, InstanceBinders: map[string]broker.InstanceBinder{ "shared": localRepo, "dedicated": remoteRepo, }, Config: config, } brokerCredentials := brokerapi.BrokerCredentials{ Username: config.AuthConfiguration.Username, Password: config.AuthConfiguration.Password, } brokerAPI := brokerapi.New(serviceBroker, brokerLogger, brokerCredentials) authWrapper := auth.NewWrapper(brokerCredentials.Username, brokerCredentials.Password) debugHandler := authWrapper.WrapFunc(debug.NewHandler(remoteRepo)) instanceHandler := authWrapper.WrapFunc(redisinstance.NewHandler(remoteRepo)) http.HandleFunc("/instance", instanceHandler) http.HandleFunc("/debug", debugHandler) http.Handle("/", brokerAPI) brokerLogger.Fatal("http-listen", http.ListenAndServe(config.Host+":"+config.Port, nil)) }
} BeforeEach(func() { username = "******" password = "******" httpRecorder = httptest.NewRecorder() }) Describe("wrapped handler", func() { var wrappedHandler http.Handler BeforeEach(func() { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) }) wrappedHandler = auth.NewWrapper(username, password).Wrap(handler) }) It("works when the credentials are correct", func() { request := newRequest(username, password) wrappedHandler.ServeHTTP(httpRecorder, request) Expect(httpRecorder.Code).To(Equal(http.StatusCreated)) }) It("fails when the username is empty", func() { request := newRequest("", password) wrappedHandler.ServeHTTP(httpRecorder, request) Expect(httpRecorder.Code).To(Equal(http.StatusUnauthorized)) }) It("fails when the password is empty", func() {
func wrapAuth(router httpRouter, credentials BrokerCredentials) http.Handler { return auth.NewWrapper(credentials.Username, credentials.Password).Wrap(router) }