Exemplo n.º 1
0
func DeleteContainerUserFromProxy(rulelist []SshRule, logid int64) error {
	logs.Normal("delete container from proxy:", rulelist, "logid:", logid)
	token := beego.AppConfig.String("proxy_url_token")
	//userdel
	for _, sshRuleOb := range rulelist {
		container := sshRuleOb.ContainerName
		proxyHost := sshRuleOb.ProxyHost
		//this url todo
		proxyDelUrl := proxyHost + ":9090/deleteContainer?container=" + container + "&logid=" + strconv.FormatInt(logid, 10) + "&token=" + token
		logs.Normal("curl delete container url:", proxyDelUrl, "logid:", logid)

		req := httplib.Get(proxyDelUrl)
		output := make(map[string]interface{})
		err := req.ToJson(&output)
		if err != nil {
			logs.Error("request from "+proxyDelUrl+" error:", err, logid)
			return err
		}

		if output["result"].(int) == 0 {
			logs.Normal(proxyDelUrl, "response ok!", logid)
			continue
		} else {
			logs.Error(proxyDelUrl+" error:", output["error"], logid)
		}
	}
	return nil
}
Exemplo n.º 2
0
func (u *SshRuleManage) Update(conn *sql.DB, ruleOb SshRule, logid int64) error {
	tableName := u.TableName()
	if ruleOb.ContainerName == "" {
		return errors.New("ssh rule object is not good! miss primary key!")
	}
	param := make([]interface{}, 0)
	param = append(param, ruleOb.ContainerName)
	sqlu := "update " + tableName + " set containerName = '" + ruleOb.ContainerName + "'"
	if ruleOb.ProxyHost != "" {
		sqlu += " ,proxyHost='" + ruleOb.ProxyHost + "'"
		param = append(param, ruleOb.ProxyHost)
	}
	if ruleOb.PublicPort != 0 {
		portstr := strconv.Itoa(ruleOb.PublicPort)
		sqlu += " ,sshPort=" + portstr
		param = append(param, ruleOb.PublicPort)
	}
	if ruleOb.UiIpPort != "" {
		sqlu += " ,uiIpPort='" + ruleOb.UiIpPort + "'"
		param = append(param, ruleOb.UiIpPort)
	}

	uidstr := strconv.Itoa(ruleOb.Uid)
	sqlu += " where containerName = '" + ruleOb.ContainerName + "' and uid = " + uidstr
	logs.Normal("sql update:", sqlu, "logid:", logid)
	param = append(param, ruleOb.ContainerName)
	_, err := conn.Exec(sqlu)
	if err != nil {
		logs.Error("conn exec error:", err, "logid:", logid)
	}
	return err
}
func (this *SshProxyServerController) Get() {
	host := this.Ctx.Input.Params[":objectId"]
	if host == "" {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}

	dbconn, err := models.InitDbConn(0)
	if err != nil {
		logs.Error("init db conn error:", err, "logid:", 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	sshProxyM := new(models.SshProxyManage)
	sshProxyOb, err := sshProxyM.Query(dbconn, host, 0)
	if err != nil {
		logs.Error("ssh proxy query error:", err, "logid:", 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}
	logs.Normal("ssh post ok!", sshProxyOb, "logid:", 0)
	this.Data["json"] = sshProxyOb
	this.ServeJson()
}
Exemplo n.º 4
0
//curl -i -X POST 'localhost:9090/sshPort' -d '{"port":2300,"host":"testinfo2","number":10,"avail":"true"}'
func (this *SshPortController) Post() {
	var sshPortOb models.SshPort
	requestBody := string(this.Ctx.Input.RequestBody)
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &sshPortOb)
	if err != nil {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}
	logid, err := models.GetLogId(this.Ctx.Input.RequestBody)
	if err != nil {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"logid param error"}`))
		this.StopRun()
	}
	logs.Normal("post data:", requestBody, "logid:", logid)

	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	dbconn.Exec("START TRANSACTION")
	logs.Normal("start transaction", "logid:", logid)

	sshPortM := new(models.SshPortManage)
	err = sshPortM.Insert(dbconn, sshPortOb, logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("ssh port insert error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":` + err.Error() + `}`))
		this.StopRun()
	}
	dbconn.Exec("COMMIT")
	logs.Normal("COMMIT", "logid:", logid)
	logs.Normal("ssh post OK!", "logid:", logid)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
}
Exemplo n.º 5
0
func (u *SshRuleManage) DeleteByUid(conn *sql.DB, uid string, logid int64) error {
	tableName := u.TableName()
	sqld := "delete from " + tableName + " where uid = " + uid
	logs.Normal("delete by uid sql:", sqld, "logid:", logid)
	if _, err := conn.Exec(sqld); err != nil {
		logs.Error("conn exec error:", err, "logid:", logid)
		return err
	}
	return nil
}
Exemplo n.º 6
0
func (u *SshKeysManage) DeleteByKey(conn *sql.DB, uid string, key string, logid int64) error {
	tableName := u.TableName()
	sqld := "delete from " + tableName + " where uid = " + uid + " and keyName='" + key + "'"
	if _, err := conn.Exec(sqld); err != nil {
		logs.Error("sql deleteByKey error!", err, sqld, "logid:", logid)
		return err
	}
	logs.Normal("sql deleteByKey OK!", sqld, "logid:", logid)
	return nil
}
Exemplo n.º 7
0
func (u *SshProxyManage) Delete(conn *sql.DB, host string, logid int64) error {
	tableName := u.TableName()
	sqld := "delete from " + tableName + " where machineHost = '" + host + "'"
	logs.Normal("sql delete:", sqld, "logid:", logid)
	if _, err := conn.Exec(sqld); err != nil {
		logs.Error("conn exec error:", err, "logid:", logid)
		return err
	}
	return nil
}
Exemplo n.º 8
0
func (u *SshRuleManage) Delete(conn *sql.DB, uid, cname string, logid int64) error {
	tableName := u.TableName()
	sqld := "delete from " + tableName + " where uid= " + uid + " and containerName = '" + cname + "'"
	logs.Normal("delete sql:", sqld, "logid:", logid)
	if _, err := conn.Exec(sqld); err != nil {
		logs.Error("conn exec error:", err, "logid:", logid)
		return err
	}
	return nil
}
func (this *SshProxyServerController) Delete() {
	host := this.Ctx.Input.Params[":objectId"]
	if host == "" {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}
	logid, _ := this.GetInt("logid")
	if logid == 0 {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"logid param error"}`))
		this.StopRun()
	}
	logs.Normal("delete param:", "host:", host, "logid:", logid)
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	dbconn.Exec("START TRANSACTION")
	logs.Normal("start transaction", "logid:", logid)

	sshProxyM := new(models.SshProxyManage)
	err = sshProxyM.Delete(dbconn, host, logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("ssh proxy delete err:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}
	dbconn.Exec("COMMIT")
	logs.Normal("COMMIT", "logid:", logid)
	logs.Normal("ssh proxy delete OK!", "logid:", logid)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
	this.StopRun()
}
Exemplo n.º 10
0
func (this *SshKeysController) reloadSshPublicKeys(dbconn *sql.DB, uid string, logid int64) error {
	sshKeysM := new(models.SshKeysManage)
	pubSshKeyMap, errall := sshKeysM.GetAll(dbconn, uid, logid)
	logs.Normal("reloadSshPublicKeys get sshkey map:", pubSshKeyMap, "logid:", logid)
	if errall != nil {
		logs.Error("sshKeysM.GetAll error!", errall, " uid:", uid, "logid:", logid)
		return errall
	}
	//check ssh rules,if the rules is null,then only change ssh keys;
	// if the rules is not null,then change the proxy server
	sshRuleM := new(models.SshRuleManage)
	rulelist, err := sshRuleM.QueryByUid(dbconn, uid, logid)
	if err != nil {
		logs.Error("sshRuleM.QueryByUid error!", err, " uid:", uid, "logid:", logid)
		return err
	}
	logs.Normal("sshRuleM.QueryByUid get rule list:", rulelist, "logid:", logid)
	if len(rulelist) > 0 {
		err = models.UpdateRule(rulelist, pubSshKeyMap, logid)
	}
	return err
}
Exemplo n.º 11
0
func (this *SshAgentController) system(s, logid string) (string, error) {
	cmd := exec.Command("/bin/sh", "-c", s)
	var out bytes.Buffer
	cmd.Stdout = &out
	err := cmd.Run()
	if err != nil {
		logs.Error("exec command error happened:", err, "the command is :", s, logid)
		return "", err
	}
	output := strings.TrimSpace(out.String())
	logs.Normal("exec command result is: ", output, "process state is :", cmd.ProcessState, logid)
	//fmt.Printf("%s\n", out.String(), cmd.ProcessState)
	return output, err
}
Exemplo n.º 12
0
func UpdateRule(rulelist []SshRule, keylist map[string]string, logid int64) error {
	logs.Normal("update rule list:", rulelist, "key list", keylist, "logid:", logid)
	token := beego.AppConfig.String("proxy_url_token")
	keylistStr, _ := json.Marshal(keylist)
	//useradd
	for _, sshRuleOb := range rulelist {
		container := sshRuleOb.ContainerName
		proxyHost := sshRuleOb.ProxyHost
		uiIpPort := sshRuleOb.UiIpPort
		uiArr := strings.Split(uiIpPort, ":")
		if len(uiArr) != 2 {
			logs.Error("update rule uiIpPort Error!", uiIpPort, container, proxyHost)
			continue
		}
		//this url todo
		proxyAddUrl := "http://" + proxyHost + ":9090/updateContainer?container=" + container + "&uiip=" + uiArr[0] + "&uiport=" + uiArr[1] + "&keylist=" + string(keylistStr) + "&logid=" + strconv.FormatInt(logid, 10) + "&token=" + token
		logs.Normal("curl add container url:", proxyAddUrl, "logid:", logid)

		req := httplib.Get(proxyAddUrl)
		output := make(map[string]interface{})
		err := req.ToJson(&output)
		if err != nil {
			logs.Error("request from "+proxyAddUrl+" error:", err, logid)
			return err
		}

		if output["result"].(int) == 0 {
			logs.Normal(proxyAddUrl, "response ok!", logid)
			continue
		} else {
			logs.Error(proxyAddUrl+" error:", output["error"], logid)
		}

	}
	return nil
}
Exemplo n.º 13
0
func (u *SshRuleManage) Insert(conn *sql.DB, sshRuleOb SshRule, logid int64) error {
	tableName := u.TableName()
	sqli := "INSERT INTO " + tableName + " (containerName,sshPort,proxyHost,uiIpPort,uid,addTime) VALUES (?,?,?,?,?,?)"
	logs.Normal("insert sql:", sqli, sshRuleOb, "logid:", logid)
	stmt, sterr := conn.Prepare(sqli)
	if sterr != nil {
		logs.Error("conn prepare error:", sterr, sqli, "logid:", logid)
		return sterr
	}
	if _, err := stmt.Exec(sshRuleOb.ContainerName, sshRuleOb.PublicPort, sshRuleOb.ProxyHost, sshRuleOb.UiIpPort, sshRuleOb.Uid, time.Now().Format("2006-01-02 15:04:05")); err != nil {
		logs.Error("sql exec error:", err, sqli, "logid:", logid)
		return err
	}
	return nil
}
Exemplo n.º 14
0
func (u *SshKeysManage) Insert(conn *sql.DB, sshKeysOb SshKeys, logid int64) error {
	tableName := u.TableName()
	sqli := "INSERT INTO " + tableName + " (uid,keyName,publicKey,addTime) VALUES (?,?,?,?)"
	stmt, sterr := conn.Prepare(sqli)
	if sterr != nil {
		logs.Error("sql prepare error:", sterr, "logid:", logid)
		return sterr
	}
	if _, err := stmt.Exec(sshKeysOb.Uid, sshKeysOb.KeyName, sshKeysOb.PublicKey, time.Now().Format("2006-01-02 15:04:05")); err != nil {
		logs.Error("sql exec error:", sterr, " the sql is:", sqli, sshKeysOb, "logid:", logid)
		return err
	}
	logs.Normal("sql insert OK!", sqli, sshKeysOb, "logid:", logid)
	return nil
}
Exemplo n.º 15
0
func (u *SshProxyManage) Insert(conn *sql.DB, sshProxyOb SshProxyMachine, logid int64) error {
	tableName := u.TableName()
	sqli := "INSERT INTO " + tableName + " (machineHost,userNumber,isAvail,addTime) VALUES (?,?,?,?)"
	logs.Normal("insert:", sqli, sshProxyOb, "logid:", logid)
	stmt, sterr := conn.Prepare(sqli)
	if sterr != nil {
		logs.Error("conn prepare error:", sterr, "logid:", logid)
		return sterr
	}
	if _, err := stmt.Exec(sshProxyOb.MachineHost, sshProxyOb.UserNumber, sshProxyOb.IsAvail, time.Now().Format("2006-01-02 15:04:05")); err != nil {
		logs.Error("stmt exec error:", err, "logid:", logid)
		return err
	}
	return nil
}
Exemplo n.º 16
0
func (this *SshPortController) GetBestUsePort(logid int64) (models.SshPort, error) {
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn err:", err, "logid:", logid)
		return models.SshPort{}, err
	}
	defer dbconn.Close()

	sshPortM := new(models.SshPortManage)
	portOb, err := sshPortM.GetBestUsePort(dbconn, logid)
	if err != nil {
		logs.Error("ssh port get best userport error:", err, "logid:", logid)
	}
	logs.Normal("get best use port ok!", portOb, "logid:", logid)
	return portOb, err
}
Exemplo n.º 17
0
//curl localhost:9090/sshKeys -X PUT -i -d '{"uid":5544,"keyname":"testliuyanglong","publickey":"d","logid":"444444"}'
func (this *SshKeysController) Put() {
	requestBody := string(this.Ctx.Input.CopyBody())
	var sshKeysOb models.SshKeys
	err := json.Unmarshal(this.Ctx.Input.CopyBody(), &sshKeysOb)
	if err != nil {
		//fmt.Println(err)
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}
	logid, err := models.GetLogId(this.Ctx.Input.CopyBody())
	if err != nil {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"logid param error"}`))
		this.StopRun()
	}
	logs.Normal("param is:", requestBody, "logid:", logid)
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()
	dbconn.Exec("START TRANSACTION")
	logs.Normal("start transaction", "logid:", logid)
	sshKeysM := new(models.SshKeysManage)
	err = sshKeysM.Update(dbconn, sshKeysOb, logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("ssh keys update error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}

	//reload ssh public key
	err = this.reloadSshPublicKeys(dbconn, strconv.Itoa(sshKeysOb.Uid), logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("reload ssh public key error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":` + err.Error() + `}`))
		this.StopRun()
	}
	dbconn.Exec("COMMIT")
	logs.Normal("COMMIT", "logid:", logid)
	logs.Normal("ssh key put ok", "logid:", logid)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
	this.StopRun()
}
Exemplo n.º 18
0
func (this *SshPortController) UpdateNumber(port, num string, isplus bool, logid int64) error {
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn err:", err, "logid:", logid)
		return err
	}
	defer dbconn.Close()

	sshPortM := new(models.SshPortManage)
	err = sshPortM.Update(dbconn, port, num, isplus, logid)
	if err != nil {
		logs.Error("ssh port update error:", err, "logid:", logid)
		return err
	}
	logs.Normal("ssh updatenumber OK", logid)
	return nil
}
Exemplo n.º 19
0
//curl -i -X GET localhost:9090/sshRules/9999/aiaiia
func (this *SshRulesController) DeleteByContainer() {
	uid := this.Ctx.Input.Params[":uid"]
	containerName := this.Ctx.Input.Params[":container"]
	if uid == "" {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}
	logid, _ := this.GetInt("logid")
	if logid == 0 {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"logid param error"}`))
		this.StopRun()
	}
	logs.Normal("delete param:", "uid:", uid, "containerName:", containerName, "logid:", logid)
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn err:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	dbconn.Exec("START TRANSACTION")
	logs.Normal("start transaction", "logid:", logid)

	sshRulesM := new(models.SshRuleManage)
	delrule, _ := sshRulesM.Query(dbconn, uid, containerName, logid)
	err = sshRulesM.Delete(dbconn, uid, containerName, logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("delete err:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}

	err = models.DeleteContainerUserFromProxy([]models.SshRule{delrule}, logid)
	if err != nil {
		logs.Error("Delete single Container From Proxy error:", err, "logid:", logid)
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", logid)
	}

	dbconn.Exec("COMMIT")
	logs.Normal("COMMIT", "logid:", logid)

	logs.Normal("delete OK!", "logid:", logid)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
	this.StopRun()
}
Exemplo n.º 20
0
func (u *SshKeysManage) Query(conn *sql.DB, uid string, keyname string, logid int64) (SshKeys, error) {
	tableName := u.TableName()
	sql := "select uid,keyName,publicKey from " + tableName + " where uid = " + uid + " and keyname = '" + keyname + "'"
	rows, err := conn.Query(sql)
	if err != nil {
		logs.Error("sql query error!", err, sql, "logid:", logid)
		return SshKeys{}, err
	}
	sshKeysOb := SshKeys{}
	for rows.Next() {
		if err = rows.Scan(&sshKeysOb.Uid, &sshKeysOb.KeyName, &sshKeysOb.PublicKey); err != nil {
			logs.Error("rows scan error!", err, sql, "logid:", logid)
			return SshKeys{}, err
		}
	}
	logs.Normal("query OK!", sql, sshKeysOb)
	return sshKeysOb, nil
}
Exemplo n.º 21
0
func (u *SshKeysManage) GetAll(conn *sql.DB, uid string, logid int64) (map[string]string, error) {
	tableName := u.TableName()
	keyMaps := make(map[string]string)
	sql := "select keyName,publicKey from " + tableName + " where uid = " + uid
	rows, err := conn.Query(sql)
	if err != nil {
		logs.Error("sql query error!", err, sql, "logid:", logid)
		return keyMaps, err
	}

	var keyname, publickey string
	for rows.Next() {
		if err = rows.Scan(&keyname, &publickey); err == nil {
			keyMaps[keyname] = publickey
		}
	}
	logs.Normal("getAll OK!", sql, keyMaps, "logid:", logid)
	return keyMaps, nil
}
Exemplo n.º 22
0
func (u *SshProxyManage) Query(conn *sql.DB, host string, logid int64) (SshProxyMachine, error) {
	tableName := u.TableName()
	sql := "select machineHost,userNumber,isAvail from " + tableName + " where machineHost = '" + host + "'"
	logs.Normal("query sql:", sql, "logid:", logid)
	rows, err := conn.Query(sql)
	if err != nil {
		logs.Error("query error:", err, sql, "logid:", logid)
		return SshProxyMachine{}, err
	}
	sshProxyOb := SshProxyMachine{}
	for rows.Next() {
		if err = rows.Scan(&sshProxyOb.MachineHost, &sshProxyOb.UserNumber, &sshProxyOb.IsAvail); err != nil {
			logs.Error("row scan error:", err, "logid:", logid)
			return SshProxyMachine{}, err
		}
	}

	return sshProxyOb, nil
}
Exemplo n.º 23
0
func (u *SshKeysManage) Update(conn *sql.DB, keysOb SshKeys, logid int64) error {
	tableName := u.TableName()
	if keysOb.Uid == 0 {
		return errors.New("ssh keys object is not good! miss primary key!")
	}
	param := make([]interface{}, 0)
	param = append(param, keysOb.Uid)
	sqlu := "update " + tableName + " set uid = " + strconv.Itoa(keysOb.Uid)

	if keysOb.PublicKey != "" {
		sqlu += " ,publicKey='" + keysOb.PublicKey + "'"
		param = append(param, keysOb.PublicKey)
	}
	sqlu += " where uid = " + strconv.Itoa(keysOb.Uid) + " and keyName='" + keysOb.KeyName + "'"
	logs.Normal("sql update sql is:", sqlu, "logid:", logid)
	param = append(param, keysOb.Uid)
	_, err := conn.Exec(sqlu)
	return err
}
Exemplo n.º 24
0
func (u *SshRuleManage) Query(conn *sql.DB, uid, cname string, logid int64) (SshRule, error) {
	tableName := u.TableName()
	sql := "select containerName,sshPort,proxyHost,uiIpPort,uid from " + tableName + " where uid = " + uid + " and containerName = '" + cname + "'"
	logs.Normal("sql query:", sql, "logid:", logid)
	rows, err := conn.Query(sql)
	if err != nil {
		logs.Error("conn query error:", err, sql, "logid:", logid)
		return SshRule{}, err
	}
	sshRuleOb := SshRule{}
	for rows.Next() {
		if err = rows.Scan(&sshRuleOb.ContainerName, &sshRuleOb.PublicPort, &sshRuleOb.ProxyHost, &sshRuleOb.UiIpPort, &sshRuleOb.Uid); err != nil {
			logs.Error("row scan error:", err, "logid:", logid)
			return SshRule{}, err
		}
	}

	return sshRuleOb, nil
}
Exemplo n.º 25
0
func (u *SshRuleManage) QueryByUid(conn *sql.DB, uid string, logid int64) ([]SshRule, error) {
	tableName := u.TableName()
	rulelist := make([]SshRule, 0)
	sql := "select containerName,sshPort,proxyHost,uiIpPort,uid from " + tableName + " where uid= " + uid
	logs.Normal("sql query by uid:", sql, "logid:", logid)
	rows, err := conn.Query(sql)
	if err != nil {
		logs.Error("conn query error:", err, sql, "logid:", logid)
		return rulelist, err
	}
	for rows.Next() {
		sshRuleOb := SshRule{}
		if err = rows.Scan(&sshRuleOb.ContainerName, &sshRuleOb.PublicPort, &sshRuleOb.ProxyHost, &sshRuleOb.UiIpPort, &sshRuleOb.Uid); err == nil {
			logs.Error("rows scan error:", err, "logid:", logid)
			rulelist = append(rulelist, sshRuleOb)
		}
	}

	return rulelist, nil
}
Exemplo n.º 26
0
//curl -i -X DELETE 'localhost:9090/sshKeys/4444444?logid=4223'
func (this *SshKeysController) Delete() {
	uid := this.Ctx.Input.Params[":objectId"]
	keyname := this.Ctx.Input.Params[":keyname"]
	if uid == "" {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}
	logid, _ := this.GetInt("logid")
	if logid == 0 {
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"logid param error"}`))
		this.StopRun()
	}
	logs.Normal("delete param:", "uid:", uid, "keyname:", keyname, "logid:", logid)
	dbconn, err := models.InitDbConn(logid)
	if err != nil {
		logs.Error("init db conn error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()
	dbconn.Exec("START TRANSACTION")
	logs.Normal("start transaction", "logid:", logid)
	sshKeysM := new(models.SshKeysManage)
	if keyname != "" {
		err = sshKeysM.DeleteByKey(dbconn, uid, keyname, logid)
		if err != nil {
			dbconn.Exec("ROLLBACK")
			logs.Normal("ROLLBACK", "logid:", logid)
			logs.Error("delete by key error:", err, "logid:", logid)
			this.Ctx.Output.SetStatus(500)
			this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
			this.StopRun()
		}
	} else {
		err = sshKeysM.Delete(dbconn, uid, logid)
		if err != nil {
			dbconn.Exec("ROLLBACK")
			logs.Normal("ROLLBACK", "logid:", logid)
			logs.Error("delete by uid error:", err, "logid:", logid)
			this.Ctx.Output.SetStatus(500)
			this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
			this.StopRun()
		}
	}

	//reload ssh public key
	err = this.reloadSshPublicKeys(dbconn, uid, logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("reload ssh public key error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":` + err.Error() + `}`))
		this.StopRun()
	}
	dbconn.Exec("COMMIT")
	logs.Normal("COMMIT", "logid:", logid)
	logs.Normal("delete over", "logid:", logid)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
	this.StopRun()
}
Exemplo n.º 27
0
func (this *SshAgentController) UpdateContainerRull() {
	container := this.GetString("container")
	if container == "" {
		this.Ctx.Output.SetStatus(400)
		this.Ctx.Output.Body([]byte(`{"result":1,"message":"container missing"}`))
		this.StopRun()
	}

	keylist := this.GetString("keylist")
	if keylist == "" {
		this.Ctx.Output.SetStatus(400)
		this.Ctx.Output.Body([]byte(`{"result":1,"message":"keylist missing"}`))
		this.StopRun()
	}

	logid := this.GetString("logid")
	if logid == "" {
		this.Ctx.Output.SetStatus(400)
		this.Ctx.Output.Body([]byte(`{"result":1,"message":"logid missing"}`))
		this.StopRun()
	}

	token := this.GetString("token")
	if token == "" {
		this.Ctx.Output.SetStatus(400)
		this.Ctx.Output.Body([]byte(`{"result":1,"message":"token missing"}`))
		this.StopRun()
	}

	uiip := this.GetString("uiip")
	if uiip == "" {
		this.Ctx.Output.SetStatus(400)
		this.Ctx.Output.Body([]byte(`{"result":1,"message":"uiip missing"}`))
		this.StopRun()
	}

	uiport := this.GetString("uiport")
	if uiport == "" {
		this.Ctx.Output.SetStatus(400)
		this.Ctx.Output.Body([]byte(`{"result":1,"message":"uiport missing"}`))
		this.StopRun()
	}

	if this.checkToken(token) == false {
		this.Ctx.Output.SetStatus(400)
		this.Ctx.Output.Body([]byte(`{"result":1,"message":"token error"}`))
		this.StopRun()
	}

	keylistMap := make(map[string]string)
	err := json.Unmarshal([]byte(keylist), &keylistMap)
	if err != nil {
		logs.Error("UpdateContainerRull keylist error", keylistMap, err, logid)
		this.Ctx.Output.SetStatus(403)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"param error"}`))
		this.StopRun()
	}
	logs.Normal("UpdateContainerRull params: ", container, keylistMap, token, logid, uiip, uiport, logid)

	//增加用户
	this.system("/usr/sbin/useradd "+container, logid)
	authKeyStr := ""
	for _, authkey := range keylistMap {
		authKeyStr += `command="ssh bae@` + uiip + ` -p ` + uiport + `" ` + authkey + "\n"
	}

	logs.Normal("UpdateContainerRull authkey is ", authKeyStr, logid)

	//将内容写入authorized_key文件
	authkeyfile := "/home/ssh/authorized_key_" + container
	logs.Normal("UpdateContainerRull authkey file is ", authkeyfile, logid)
	fout, err := os.Create(authkeyfile)
	defer fout.Close()
	if err != nil {
		logs.Error("authkeyfile is :", authkeyfile, err, logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"error happened"}`))
		this.StopRun()
	}

	//生成id_rsa&id_rsa.pub
	this.system("/bin/echo -e 'y\n' | /usr/bin/ssh-keygen -t rsa -f /home/bae/.ssh/id_rsa -N '' -q > /dev/null", logid)

	/*
	*
	*  todo 将生成的id_rsa.pub 通过调度传到container为authorized_keys;
	*	同时将container中/home/bae/.ssh的文件设置为root,bae不可读
	 */

	fout.WriteString(authKeyStr)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
	this.StopRun()
}