Esempio n. 1
0
func RegisterHandlers(mux httpMux.Mux, containerManager manager.Manager, httpAuthFile, httpAuthRealm, httpDigestFile, httpDigestRealm, prometheusEndpoint string) error {
	// Basic health handler.
	if err := healthz.RegisterHandler(mux); err != nil {
		return fmt.Errorf("failed to register healthz handler: %s", err)
	}

	// Validation/Debug handler.
	mux.HandleFunc(validate.ValidatePage, func(w http.ResponseWriter, r *http.Request) {
		err := validate.HandleRequest(w, containerManager)
		if err != nil {
			fmt.Fprintf(w, "%s", err)
		}
	})

	// Register API handler.
	if err := api.RegisterHandlers(mux, containerManager); err != nil {
		return fmt.Errorf("failed to register API handlers: %s", err)
	}

	// Redirect / to containers page.
	mux.Handle("/", http.RedirectHandler(pages.ContainersPage, http.StatusTemporaryRedirect))

	var authenticated bool = false

	// Setup the authenticator object
	if httpAuthFile != "" {
		glog.Infof("Using auth file %s", httpAuthFile)
		secrets := auth.HtpasswdFileProvider(httpAuthFile)
		authenticator := auth.NewBasicAuthenticator(httpAuthRealm, secrets)
		mux.HandleFunc(static.StaticResource, authenticator.Wrap(staticHandler))
		if err := pages.RegisterHandlersBasic(mux, containerManager, authenticator); err != nil {
			return fmt.Errorf("failed to register pages auth handlers: %s", err)
		}
		authenticated = true
	}
	if httpAuthFile == "" && httpDigestFile != "" {
		glog.Infof("Using digest file %s", httpDigestFile)
		secrets := auth.HtdigestFileProvider(httpDigestFile)
		authenticator := auth.NewDigestAuthenticator(httpDigestRealm, secrets)
		mux.HandleFunc(static.StaticResource, authenticator.Wrap(staticHandler))
		if err := pages.RegisterHandlersDigest(mux, containerManager, authenticator); err != nil {
			return fmt.Errorf("failed to register pages digest handlers: %s", err)
		}
		authenticated = true
	}

	// Change handler based on authenticator initalization
	if !authenticated {
		mux.HandleFunc(static.StaticResource, staticHandlerNoAuth)
		if err := pages.RegisterHandlersBasic(mux, containerManager, nil); err != nil {
			return fmt.Errorf("failed to register pages handlers: %s", err)
		}
	}

	collector := metrics.NewPrometheusCollector(containerManager)
	prometheus.MustRegister(collector)
	http.Handle(prometheusEndpoint, prometheus.Handler())

	return nil
}
Esempio n. 2
0
func main() {
	flag.Parse()

	flag.VisitAll(func(f *flag.Flag) {
		if fmt.Sprintf("%s", f.Value) == f.DefValue {
			log.Println("Arguments invalid")
			os.Exit(0)
		}
	})

	absRoot, err := filepath.Abs(*root)
	if err != nil {
		log.Fatal(err)
	}

	absKey, err := filepath.Abs(*keyFile)
	if err != nil {
		log.Fatal(err)
	}

	absCert, err := filepath.Abs(*certFile)
	if err != nil {
		log.Fatal(err)
	}

	secret := auth.HtdigestFileProvider(*htdigest)
	authenticator := auth.NewDigestAuthenticator(*realm, secret)

	log.Printf("Starting server on port %d\n", *port)

	hostport := fmt.Sprintf(":%d", *port)
	authHandler := makeAuthHandler(http.FileServer(http.Dir(absRoot)))
	handler := authenticator.Wrap(authHandler)
	log.Fatal(http.ListenAndServeTLS(hostport, absCert, absKey, handler))
}
Esempio n. 3
0
func loadDigestAuth(realm string) auth.DigestAuth {
	file := loadOrCreateDigestFile(settings.DigestPath)
	info, _ := file.Stat()
	if info.Size() == 0 {
		if *debug {
			log.Println("Digest File is empty, requesting user and pass")
		}
		fmt.Println("You specified a digest file but it's empty, please choose a user & pass to login to your BTSyncInator site.")
		// Get Username
		fmt.Println("Username:"******"Error with bufferedIO.ReadString(\"\n\"): %v", err)
		}
		username = strings.Replace(username, "\n", "", -1)
		// Get Password
		fmt.Println("Password:"******"Error with bufferedIO.ReadString(\"\n\"): %v", err)
		}
		password = strings.Replace(password, "\n", "", -1)
		// Setup string to hash through md5
		byteContents := []byte(username)
		byteContents = append(byteContents, []byte(":")...)
		byteContents = append(byteContents, []byte(realm)...)
		byteContents = append(byteContents, []byte(":")...)
		byteContents = append(byteContents, []byte(password)...)
		encryptedBytePassArray := md5.Sum(byteContents)
		encryptedBytePass := encryptedBytePassArray[:]
		// Setup bytes to write to htdigest file
		byteContents = []byte(username)
		byteContents = append(byteContents, []byte(":")...)
		byteContents = append(byteContents, []byte(realm)...)
		byteContents = append(byteContents, []byte(":")...)
		byteContents = append(byteContents, []byte(fmt.Sprintf("%x", encryptedBytePass))...)
		byteContents = append(byteContents, []byte("\n")...)
		_, err = file.Write(byteContents)
		if err != nil {
			log.Fatalf("Error with file.Write(): %v", err)
		}
		file.Close()
	} else if *debug {
		log.Printf("Digest File %s loaded", file.Name())
	}
	secretProvider := auth.HtdigestFileProvider(file.Name())
	digestAuth := auth.NewDigestAuthenticator(realm, secretProvider)
	return *digestAuth
}
Esempio n. 4
0
func main() {
	defer glog.Flush()
	flag.Parse()

	if *versionFlag {
		fmt.Printf("cAdvisor version %s\n", info.VERSION)
		os.Exit(0)
	}

	setMaxProcs()

	storageDriver, err := NewStorageDriver(*argDbDriver)
	if err != nil {
		glog.Fatalf("Failed to connect to database: %s", err)
	}

	sysFs, err := sysfs.NewRealSysFs()
	if err != nil {
		glog.Fatalf("Failed to create a system interface: %s", err)
	}

	containerManager, err := manager.New(storageDriver, sysFs)
	if err != nil {
		glog.Fatalf("Failed to create a Container Manager: %s", err)
	}

	// Register Docker.
	if err := docker.Register(containerManager); err != nil {
		glog.Errorf("Docker registration failed: %v.", err)
	}

	// Register the raw driver.
	if err := raw.Register(containerManager); err != nil {
		glog.Fatalf("Raw registration failed: %v.", err)
	}

	// Basic health handler.
	if err := healthz.RegisterHandler(); err != nil {
		glog.Fatalf("Failed to register healthz handler: %s", err)
	}

	// Validation/Debug handler.
	http.HandleFunc(validate.ValidatePage, func(w http.ResponseWriter, r *http.Request) {
		err := validate.HandleRequest(w, containerManager)
		if err != nil {
			fmt.Fprintf(w, "%s", err)
		}
	})

	// Register API handler.
	if err := api.RegisterHandlers(containerManager); err != nil {
		glog.Fatalf("Failed to register API handlers: %s", err)
	}

	// Redirect / to containers page.
	http.Handle("/", http.RedirectHandler(pages.ContainersPage, http.StatusTemporaryRedirect))

	var authenticated bool = false

	// Setup the authenticator object
	if *httpAuthFile != "" {
		glog.Infof("Using auth file %s", *httpAuthFile)
		secrets := auth.HtpasswdFileProvider(*httpAuthFile)
		authenticator := auth.NewBasicAuthenticator(*httpAuthRealm, secrets)
		http.HandleFunc(static.StaticResource, authenticator.Wrap(staticHandler))
		if err := pages.RegisterHandlersBasic(containerManager, authenticator); err != nil {
			glog.Fatalf("Failed to register pages auth handlers: %s", err)
		}
		authenticated = true
	}
	if *httpAuthFile == "" && *httpDigestFile != "" {
		glog.Infof("Using digest file %s", *httpDigestFile)
		secrets := auth.HtdigestFileProvider(*httpDigestFile)
		authenticator := auth.NewDigestAuthenticator(*httpDigestRealm, secrets)
		http.HandleFunc(static.StaticResource, authenticator.Wrap(staticHandler))
		if err := pages.RegisterHandlersDigest(containerManager, authenticator); err != nil {
			glog.Fatalf("Failed to register pages digest handlers: %s", err)
		}
		authenticated = true
	}

	// Change handler based on authenticator initalization
	if !authenticated {
		http.HandleFunc(static.StaticResource, staticHandlerNoAuth)
		if err := pages.RegisterHandlersBasic(containerManager, nil); err != nil {
			glog.Fatalf("Failed to register pages handlers: %s", err)
		}
	}

	// Start the manager.
	if err := containerManager.Start(); err != nil {
		glog.Fatalf("Failed to start container manager: %v", err)
	}

	// Install signal handler.
	installSignalHandler(containerManager)

	glog.Infof("Starting cAdvisor version: %q on port %d", info.VERSION, *argPort)

	addr := fmt.Sprintf("%s:%d", *argIp, *argPort)
	glog.Fatal(http.ListenAndServe(addr, nil))
}