Ejemplo n.º 1
0
func (s *MySQLTestSuite) TestMakeGrant(t *C) {
	user := "******"
	pass := "******"
	dsn := mysql.DSN{
		Username: "******",
		Password: "******",
	}

	dsn.Hostname = "localhost"
	maxOpenConnections := int64(1)
	got := i.MakeGrant(dsn, user, pass, maxOpenConnections)
	expect := []string{
		"SET SESSION old_passwords=0",
		"GRANT SUPER, PROCESS, USAGE, SELECT ON *.* TO 'new-user'@'localhost' IDENTIFIED BY 'some pass' WITH MAX_USER_CONNECTIONS 1",
		"GRANT UPDATE, DELETE, DROP ON performance_schema.* TO 'new-user'@'localhost' IDENTIFIED BY 'some pass' WITH MAX_USER_CONNECTIONS 1",
	}
	t.Check(got, DeepEquals, expect)

	dsn.Hostname = "127.0.0.1"
	got = i.MakeGrant(dsn, user, pass, maxOpenConnections)
	expect = []string{
		"SET SESSION old_passwords=0",
		"GRANT SUPER, PROCESS, USAGE, SELECT ON *.* TO 'new-user'@'127.0.0.1' IDENTIFIED BY 'some pass' WITH MAX_USER_CONNECTIONS 1",
		"GRANT UPDATE, DELETE, DROP ON performance_schema.* TO 'new-user'@'127.0.0.1' IDENTIFIED BY 'some pass' WITH MAX_USER_CONNECTIONS 1",
	}
	t.Check(got, DeepEquals, expect)

	dsn.Hostname = "10.1.1.1"
	got = i.MakeGrant(dsn, user, pass, maxOpenConnections)
	expect = []string{
		"SET SESSION old_passwords=0",
		"GRANT SUPER, PROCESS, USAGE, SELECT ON *.* TO 'new-user'@'%' IDENTIFIED BY 'some pass' WITH MAX_USER_CONNECTIONS 1",
		"GRANT UPDATE, DELETE, DROP ON performance_schema.* TO 'new-user'@'%' IDENTIFIED BY 'some pass' WITH MAX_USER_CONNECTIONS 1",
	}
	t.Check(got, DeepEquals, expect)

	dsn.Hostname = ""
	dsn.Socket = "/var/lib/mysql.sock"
	got = i.MakeGrant(dsn, user, pass, maxOpenConnections)
	expect = []string{
		"SET SESSION old_passwords=0",
		"GRANT SUPER, PROCESS, USAGE, SELECT ON *.* TO 'new-user'@'localhost' IDENTIFIED BY 'some pass' WITH MAX_USER_CONNECTIONS 1",
		"GRANT UPDATE, DELETE, DROP ON performance_schema.* TO 'new-user'@'localhost' IDENTIFIED BY 'some pass' WITH MAX_USER_CONNECTIONS 1",
	}
	t.Check(got, DeepEquals, expect)
}
Ejemplo n.º 2
0
func (i *Installer) getDSNFromUser(dsn *mysql.DSN) error {
	// Ask for username
	username, err := i.term.PromptString("MySQL username", dsn.Username)
	if err != nil {
		return err
	}
	dsn.Username = username

	// Ask for password
	var password string
	if i.flags.Bool["plain-passwords"] {
		password, err = i.term.PromptString("MySQL password", "")
	} else {
		password, err = gopass.GetPass("MySQL password: "******"MySQL host[:port] or socket file",
		dsn.To(),
	)
	if err != nil {
		return err
	}
	if filepath.IsAbs(hostname) {
		dsn.Socket = hostname
		dsn.Hostname = ""
	} else {
		f := strings.Split(hostname, ":")
		dsn.Hostname = f[0]
		if len(f) > 1 {
			dsn.Port = f[1]
		} else {
			dsn.Port = "3306"
		}
		dsn.Socket = ""
	}
	return nil
}
Ejemplo n.º 3
0
func (s *MySQLTestSuite) TestMakeGrant(t *C) {
	user := "******"
	pass := "******"
	dsn := mysql.DSN{
		Username: "******",
		Password: "******",
	}

	dsn.Hostname = "localhost"
	t.Check(i.MakeGrant(dsn, user, pass), Equals, "GRANT SUPER, PROCESS, USAGE ON *.* TO 'new-user'@'localhost' IDENTIFIED BY 'some pass'")

	dsn.Hostname = "127.0.0.1"
	t.Check(i.MakeGrant(dsn, user, pass), Equals, "GRANT SUPER, PROCESS, USAGE ON *.* TO 'new-user'@'127.0.0.1' IDENTIFIED BY 'some pass'")

	dsn.Hostname = "10.1.1.1"
	t.Check(i.MakeGrant(dsn, user, pass), Equals, "GRANT SUPER, PROCESS, USAGE ON *.* TO 'new-user'@'%' IDENTIFIED BY 'some pass'")

	dsn.Hostname = ""
	dsn.Socket = "/var/lib/mysql.sock"
	t.Check(i.MakeGrant(dsn, user, pass), Equals, "GRANT SUPER, PROCESS, USAGE ON *.* TO 'new-user'@'localhost' IDENTIFIED BY 'some pass'")
}
Ejemplo n.º 4
0
func (i *Installer) autodetectDSN(dsn *mysql.DSN) error {
	params := []string{}
	if i.flags.String["mysql-defaults-file"] != "" {
		params = append(params, "--defaults-file="+i.flags.String["mysql-defaults-file"])
	}
	// --print-defaults needs to be last param
	params = append(params, "--print-defaults")
	cmd := exec.Command("mysql", params...)
	byteOutput, err := cmd.Output()
	if err != nil {
		return err
	}
	output := string(byteOutput)
	if i.flags.Bool["debug"] {
		log.Println(output)
	}
	autoDSN := ParseMySQLDefaults(output)
	if i.flags.Bool["debug"] {
		log.Printf("autoDSN: %#v\n", autoDSN)
	}
	// Fill in the given DSN with auto-detected options.
	if dsn.Username == "" {
		dsn.Username = autoDSN.Username
	}
	if dsn.Password == "" {
		dsn.Password = autoDSN.Password
	}
	if dsn.Hostname == "" {
		dsn.Hostname = autoDSN.Hostname
	}
	if dsn.Port == "" {
		dsn.Port = autoDSN.Port
	}
	if dsn.Socket == "" {
		dsn.Socket = autoDSN.Socket
	}
	if dsn.Username == "" {
		user, err := user.Current()
		if err == nil {
			dsn.Username = user.Username
		}
	}
	if i.flags.Bool["debug"] {
		log.Printf("dsn: %#v\n", dsn)
	}
	return nil
}
Ejemplo n.º 5
0
func (i *Installer) connectMySQL(def *mysql.DSN, creating bool) (mysql.DSN, error) {
	dsn := mysql.DSN{}
	if creating {
		fmt.Println("Specify a root/super MySQL user to create a user for the agent")
	} else if def != nil {
		fmt.Println("Verify the existing MySQL user to use for the agent")
		dsn = *def
		if dsn.Username == "" {
			user, _ := user.Current()
			if user != nil {
				dsn.Username = user.Username
			}
		}
	} else {
		fmt.Println("Specify an existing MySQL user to use for the agent")
	}

CONNECT_MYSQL:
	for {
		username, err := i.term.PromptStringRequired("MySQL username", dsn.Username)
		if err != nil {
			return dsn, err
		}
		dsn.Username = username

		var password string
		if creating {
			password, err = gopass.GetPass("MySQL password: "******"MySQL password", dsn.Password)
		}
		if err != nil {
			return dsn, err
		}
		dsn.Password = password

		hostname, err := i.term.PromptStringRequired("MySQL host[:port] or socket file", dsn.To())
		if err != nil {
			return dsn, err
		}
		if filepath.IsAbs(hostname) {
			dsn.Socket = hostname
			dsn.Hostname = ""
		} else {
			f := strings.Split(hostname, ":")
			dsn.Hostname = f[0]
			if len(f) > 1 {
				dsn.Port = f[1]
			} else {
				dsn.Port = "3306"
			}
			dsn.Socket = ""
		}

		if err := TestMySQLConnection(dsn); err != nil {
			fmt.Printf("Error connecting to MySQL %s: %s\n", dsn, err)
			again, err := i.term.PromptBool("Try again?", "Y")
			if err != nil {
				return dsn, err
			}
			if !again {
				return dsn, fmt.Errorf("Failed to connect to MySQL")
			}
			continue CONNECT_MYSQL
		}
		fmt.Printf("MySQL connection OK\n")
		break
	}
	return dsn, nil
}