Пример #1
0
func NewDbDriver() (*DbDriver, error) {

	lock.Lock()
	defer lock.Unlock()

	if dbc != nil {
		return dbc, nil
	}

	log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)

	dbc = &DbDriver{}

	var username string
	var password string
	var ip string
	var port uint
	var database string

	mflag.StringVar(&username, []string{"-mysql_username"}, "docker", "username for mysql")
	mflag.StringVar(&password, []string{"-mysql_password"}, "dockerdocker", "username for mysql")
	mflag.StringVar(&ip, []string{"-mysql_ip"}, "10.97.232.22", "ip for mysql")
	mflag.UintVar(&port, []string{"-mysql_port"}, 3306, "port for mysql")
	mflag.StringVar(&database, []string{"-mysql_database"}, "docker", "database name for mysql")

	dataSourceName := username + ":" + password + "@tcp(" + ip + ":" + strconv.Itoa(int(port)) + ")/" + database + "?charset=utf8"

	log.Printf("INFO: data source : %s", dataSourceName)

	db, err := sql.Open("mysql", dataSourceName)

	if err != nil {
		log.Printf("ERROR: %v", err)
	}

	dbc.db = db

	return dbc, nil
}
Пример #2
0
// TODO mmore log
func main() {

	var ListenAddr string
	var Port uint

	tokenAuth := &handler.TokenAuth{}

	// http
	mflag.StringVar(&ListenAddr, []string{"l", "-addr"}, "0.0.0.0", "Listening Address")
	mflag.UintVar(&Port, []string{"p", "-port"}, 9999, "Listening Port")

	// acl
	var aclDriverName string
	mflag.StringVar(&aclDriverName, []string{"-acl_driver"}, "", "ACL Driver for Docker Wicket")

	// token for v1 and v2
	mflag.StringVar(&tokenAuth.Issuer, []string{"-issuer"}, "docker-wicket", "Issuer of the token, MUST be same as what in registy2")
	mflag.StringVar(&tokenAuth.Service, []string{"-service"}, "registry", "Service of the token")
	mflag.Int64Var(&tokenAuth.Expiration, []string{"-expiration"}, 600, "how long the token can be treated as valid. (sec)")

	// cert and key for token
	var certPath string
	var certKeyPath string
	mflag.StringVar(&certPath, []string{"-cert"}, "", "Token certificate file path, MUST be in the bundle of registy2")
	mflag.StringVar(&certKeyPath, []string{"-key"}, "", "Key file path to token certificate")

	// v1 only
	var indexDriverName string
	var v1Endpoint string
	mflag.StringVar(&v1Endpoint, []string{"-v1_endpoint"}, "", "Endpoint of registry1")
	mflag.StringVar(&indexDriverName, []string{"-v1_index_driver"}, "", "Index driver of registry1")

	parseConf()

	err := tokenAuth.LoadCertAndKey(certPath, certKeyPath)
	if err != nil {
		log.Fatalf("Cannot load cert: %v", err)
	}

	acldriver, err := acl.Load(aclDriverName)
	if err != nil {
		log.Fatalf("Cannot load ACL Driver: %v", err)
	}

	indexdriver, err := index.Load(indexDriverName)
	if err != nil {
		log.Fatalf("Cannot load index Driver: %v", err)
	}

	router := web.New(handler.ShareWebContext{}).
		Middleware(web.LoggerMiddleware)

	v1.InstallHandler(router, &v1.RunningContext{
		RunningContext: handler.RunningContext{
			TokenAuth: tokenAuth,
			Acl:       acldriver,
		},
		// spec
		Endpoints: v1Endpoint,
		Index:     indexdriver,
	})

	v2.InstallHandler(router, &v2.RunningContext{
		RunningContext: handler.RunningContext{
			Acl:       acldriver,
			TokenAuth: tokenAuth,
		},
	})

	log.Printf("Docker wicket @ %v:%v", ListenAddr, Port)

	log.Fatal(http.ListenAndServe(fmt.Sprintf("%v:%v", ListenAddr, Port), router))
}
Пример #3
0
func initConfig() {
	configfile := mflag.String([]string{"-config"}, "/etc/sshpiperd.conf", "Config file path. Note: any option will be overwrite if it is set by commandline")

	mflag.StringVar(&config.ListenAddr, []string{"l", "-listen_addr"}, "0.0.0.0", "Listening Address")
	mflag.UintVar(&config.Port, []string{"p", "-port"}, 2222, "Listening Port")
	mflag.StringVar(&config.WorkingDir, []string{"w", "-working_dir"}, "/var/sshpiper", "Working Dir")
	mflag.StringVar(&config.PiperKeyFile, []string{"i", "-server_key"}, "/etc/ssh/ssh_host_rsa_key", "Key file for SSH Piper")
	mflag.StringVar(&config.Challenger, []string{"c", "-challenger"}, "", "Additional challenger name, e.g. pam, emtpy for no additional challenge")
	mflag.StringVar(&config.Logfile, []string{"-log"}, "", "Logfile path. Leave emtpy or any error occurs will fall back to stdout")
	mflag.BoolVar(&config.AllowBadUsername, []string{"-allow_bad_username"}, false, "disable username check while search the working dir")
	mflag.BoolVar(&config.ShowHelp, []string{"h", "-help"}, false, "Print help and exit")
	mflag.BoolVar(&config.ShowVersion, []string{"-version"}, false, "Print version and exit")

	mflag.Parse()

	if _, err := os.Stat(*configfile); os.IsNotExist(err) {
		if !mflag.IsSet("-config") {
			*configfile = ""
		} else {
			logger.Fatalf("config file %v not found", *configfile)
		}
	}

	gconf, err := globalconf.NewWithOptions(&globalconf.Options{
		Filename:  *configfile,
		EnvPrefix: "SSHPIPERD_",
	})

	if err != nil { // this error will happen only if file error
		logger.Fatalln("load config file error %v: %v", *configfile, err)
	}

	// build a dummy flag set for globalconf to parse
	fs := flag.NewFlagSet("", flag.ContinueOnError)

	ignoreSet := make(map[string]bool)
	mflag.Visit(func(f *mflag.Flag) {
		for _, n := range f.Names {
			ignoreSet[n] = true
		}
	})

	// should be ignored
	ignoreSet["-help"] = true
	ignoreSet["-version"] = true

	mflag.VisitAll(func(f *mflag.Flag) {
		for _, n := range f.Names {
			if len(n) < 2 {
				continue
			}

			if !ignoreSet[n] {
				n = strings.TrimPrefix(n, "-")
				fs.Var(f.Value, n, f.Usage)
			}
		}
	})

	gconf.ParseSet("", fs)
}