Skip to content
This repository has been archived by the owner on Mar 3, 2024. It is now read-only.

Commit

Permalink
Allow configuration to be read from file
Browse files Browse the repository at this point in the history
  • Loading branch information
labkode committed Sep 27, 2017
1 parent 6e95ef7 commit cec7a3e
Showing 1 changed file with 55 additions and 41 deletions.
96 changes: 55 additions & 41 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/cernbox/cboxgroupd/pkg/redisgrouplooker"
gh "github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/zap"
"log"
"net/http"
Expand All @@ -24,34 +26,41 @@ var (
)

var fVersion bool
var fPort int
var fLDAPHostname string
var fLDAPPort int
var fLDAPPageLimit uint
var fRedisHostname string
var fRedisPort int
var fRedisDatabase int
var fRedisTTL int
var fAppLog string
var fHTTPLog string
var fSecret string
var fLDAPMaxConcurrency int

func init() {
viper.SetDefault("port", 2002)
viper.SetDefault("ldaphostname", "xldap.cern.ch")
viper.SetDefault("ldapport", 389)
viper.SetDefault("ldappagelimit", 1000)
viper.SetDefault("redishostname", "localhost")
viper.SetDefault("redisport", 6379)
viper.SetDefault("redisdb", 0)
viper.SetDefault("redisttl", 60)
viper.SetDefault("applog", "stderr")
viper.SetDefault("httplog", "stderr")
viper.SetDefault("secret", "change_me!!!")
viper.SetDefault("ldapmaxconcurrency", 10)

viper.SetConfigName("cboxgroupd")
viper.AddConfigPath("/etc/cboxgroupd/")

flag.BoolVar(&fVersion, "version", false, "Show version")
flag.IntVar(&fPort, "port", 2002, "Port to listen for connections")
flag.StringVar(&fLDAPHostname, "ldaphostname", "xldap.cern.ch", "Hostname of the LDAP server")
flag.IntVar(&fLDAPPort, "ldapport", 389, "Port of LDAP server")
flag.UintVar(&fLDAPPageLimit, "ldappagelimit", 1000, "Page limit for paged searchs")
flag.StringVar(&fRedisHostname, "redishostname", "localhost", "Hostname of the Redis server")
flag.IntVar(&fRedisPort, "redisport", 6379, "Port of Redis server")
flag.IntVar(&fRedisDatabase, "redisdb", 0, "Redis number database for keys isolation (0-15)")
flag.IntVar(&fRedisTTL, "redisttl", 60, "Number of seconds to expire cached entries in Redis")
flag.StringVar(&fAppLog, "applog", "stderr", "File to log application data")
flag.StringVar(&fHTTPLog, "httplog", "stderr", "File to log HTTP requests")
flag.StringVar(&fSecret, "secret", "changeme!!!", "Share secret between services to authenticate requests")
flag.IntVar(&fLDAPMaxConcurrency, "ldapmaxconcurrency", 100, "Number of concurrent connections to LDAP for update operations")
flag.Parse()
flag.Int("port", 2002, "Port to listen for connections")
flag.String("ldaphostname", "xldap.cern.ch", "Hostname of the LDAP server")
flag.Int("ldapport", 389, "Port of LDAP server")
flag.Uint("ldappagelimit", 1000, "Page limit for paged searchs")
flag.String("redishostname", "localhost", "Hostname of the Redis server")
flag.Int("redisport", 6379, "Port of Redis server")
flag.Int("redisdb", 0, "Redis number database for keys isolation (0-15)")
flag.Int("redisttl", 60, "Number of seconds to expire cached entries in Redis")
flag.String("applog", "stderr", "File to log application data")
flag.String("httplog", "stderr", "File to log HTTP requests")
flag.String("secret", "changeme!!!", "Share secret between services to authenticate requests")
flag.Int("ldapmaxconcurrency", 100, "Number of concurrent connections to LDAP for update operations")

pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
}

func main() {
Expand All @@ -60,26 +69,31 @@ func main() {
showVersion()
}

err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}

config := zap.NewProductionConfig()
config.OutputPaths = []string{fAppLog}
config.OutputPaths = []string{viper.GetString("applog")}
logger, _ := config.Build()

lgl := ldapgrouplooker.New(fLDAPHostname, fLDAPPort, uint32(fLDAPPageLimit))
rgl := redisgrouplooker.New(fRedisHostname, fRedisPort, fRedisDatabase, fRedisTTL, lgl)
lgl := ldapgrouplooker.New(viper.GetString("ldaphostname"), viper.GetInt("ldapport"), uint32(viper.GetInt("ldappagelimit")))
rgl := redisgrouplooker.New(viper.GetString("redishostname"), viper.GetInt("redisport"), viper.GetInt("redisdb"), viper.GetInt("redisttl"), lgl)

router := mux.NewRouter()

protectedUsersInGroup := handlers.CheckSharedSecret(logger, fSecret, handlers.UsersInGroup(logger, rgl))
protectedUsersInComputingGroup := handlers.CheckSharedSecret(logger, fSecret, handlers.UsersInComputingGroup(logger, rgl))
protectedUserGroups := handlers.CheckSharedSecret(logger, fSecret, handlers.UserGroups(logger, rgl))
protectedUserComputingGroups := handlers.CheckSharedSecret(logger, fSecret, handlers.UserComputingGroups(logger, rgl))
protectedUsersInGroupTTL := handlers.CheckSharedSecret(logger, fSecret, handlers.UsersInGroupTTL(logger, rgl))
protectedUsersInComputingGroupTTL := handlers.CheckSharedSecret(logger, fSecret, handlers.UsersInComputingGroupTTL(logger, rgl))
protectedUserGroupsTTL := handlers.CheckSharedSecret(logger, fSecret, handlers.UserGroupsTTL(logger, rgl))
protectedUserComputingGroupsTTL := handlers.CheckSharedSecret(logger, fSecret, handlers.UserComputingGroupsTTL(logger, rgl))
protectedUsersInGroup := handlers.CheckSharedSecret(logger, viper.GetString("secret"), handlers.UsersInGroup(logger, rgl))
protectedUsersInComputingGroup := handlers.CheckSharedSecret(logger, viper.GetString("secret"), handlers.UsersInComputingGroup(logger, rgl))
protectedUserGroups := handlers.CheckSharedSecret(logger, viper.GetString("secret"), handlers.UserGroups(logger, rgl))
protectedUserComputingGroups := handlers.CheckSharedSecret(logger, viper.GetString("secret"), handlers.UserComputingGroups(logger, rgl))
protectedUsersInGroupTTL := handlers.CheckSharedSecret(logger, viper.GetString("secret"), handlers.UsersInGroupTTL(logger, rgl))
protectedUsersInComputingGroupTTL := handlers.CheckSharedSecret(logger, viper.GetString("secret"), handlers.UsersInComputingGroupTTL(logger, rgl))
protectedUserGroupsTTL := handlers.CheckSharedSecret(logger, viper.GetString("secret"), handlers.UserGroupsTTL(logger, rgl))
protectedUserComputingGroupsTTL := handlers.CheckSharedSecret(logger, viper.GetString("secret"), handlers.UserComputingGroupsTTL(logger, rgl))

protectedUpdateUsersInGroup := handlers.CheckSharedSecret(logger, fSecret, handlers.UpdateUsersInGroup(logger, rgl, fLDAPMaxConcurrency))
protectedUpdateUserGroups := handlers.CheckSharedSecret(logger, fSecret, handlers.UpdateUserGroups(logger, rgl, fLDAPMaxConcurrency))
protectedUpdateUsersInGroup := handlers.CheckSharedSecret(logger, viper.GetString("secret"), handlers.UpdateUsersInGroup(logger, rgl, viper.GetInt("ldapmaxconcurrency")))
protectedUpdateUserGroups := handlers.CheckSharedSecret(logger, viper.GetString("secret"), handlers.UpdateUserGroups(logger, rgl, viper.GetInt("ldapmaxconcurrency")))

router.Handle("/api/v1/membership/usersingroup/{gid}", protectedUsersInGroup).Methods("GET")
router.Handle("/api/v1/membership/usersincomputinggroup/{gid}", protectedUsersInComputingGroup).Methods("GET")
Expand All @@ -94,11 +108,11 @@ func main() {
router.Handle("/api/v1/update/usersingroup", protectedUpdateUsersInGroup).Methods("POST")
router.Handle("/api/v1/update/usergroups", protectedUpdateUserGroups).Methods("POST")

out := getHTTPLoggerOut(fHTTPLog)
out := getHTTPLoggerOut(viper.GetString("httplog"))
loggedRouter := gh.LoggingHandler(out, router)

logger.Info("server is listening", zap.Int("port", fPort))
logger.Warn("server stopped", zap.Error(http.ListenAndServe(fmt.Sprintf(":%d", fPort), loggedRouter)))
logger.Info("server is listening", zap.Int("port", viper.GetInt("port")))
logger.Warn("server stopped", zap.Error(http.ListenAndServe(fmt.Sprintf(":%d", viper.GetInt("port")), loggedRouter)))
}

func getHTTPLoggerOut(filename string) *os.File {
Expand Down

0 comments on commit cec7a3e

Please sign in to comment.