Пример #1
0
//curl localhost:9090/sshKeys/123456
func (this *SshKeysController) Get() {
	uid := this.Ctx.Input.Params[":objectId"]
	if uid == "" {
		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("db init error!", err, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	sshKeysM := new(models.SshKeysManage)
	sshKeysOb, err := sshKeysM.GetAll(dbconn, uid, 0)
	if err != nil {
		logs.Error("sshkeysM getall error:", err, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}
	this.Data["json"] = sshKeysOb
	this.ServeJson()
}
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()
}
Пример #3
0
//curl -i -X GET 'localhost:9090/sshPort[/2200]'
func (this *SshPortController) Get() {
	portNum := this.Ctx.Input.Params[":objectId"]
	dbconn, err := models.InitDbConn(0)
	if err != nil {
		logs.Error("db init error:", err, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	sshPortM := new(models.SshPortManage)
	//当没有传入具体的port值时,输出全部sshport对象
	if portNum == "" {
		sshPortObs, _ := sshPortM.QueryAll(dbconn, 0)
		this.Data["json"] = sshPortObs
		this.ServeJson()
		this.StopRun()
	}

	//输出portNum对应的sshPort信息
	sshPortOb, err := sshPortM.Query(dbconn, portNum, 0)
	if err != nil {
		logs.Error("ssh port query error:", err, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}
	this.Data["json"] = sshPortOb
	this.ServeJson()
}
Пример #4
0
//curl -i -X GET localhost:9090/sshRules/9999
func (this *SshRulesController) GetByUid() {
	uid := this.Ctx.Input.Params[":uid"]
	if uid == "" {
		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, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"init db conn error"}`))
		this.StopRun()
	}
	defer dbconn.Close()

	sshRulesM := new(models.SshRuleManage)
	rulelist, err := sshRulesM.QueryByUid(dbconn, uid, 0)
	if err != nil {
		logs.Error("ssh rule query by uid error:", err, 0)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":"` + err.Error() + `"}`))
		this.StopRun()
	}
	this.Data["json"] = rulelist
	this.ServeJson()
}
Пример #5
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
}
Пример #6
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()
}
Пример #7
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()
}
Пример #8
0
//curl -i -X POST localhost:9090/sshRules/9999 -d '{"name":"aiaiia","port":4242,"rule":"~~~~","uid":9999}'
func (this *SshRulesController) Post() {
	var sshRulesOb models.SshRule
	requestBody := string(this.Ctx.Input.RequestBody)

	err := json.Unmarshal(this.Ctx.Input.RequestBody, &sshRulesOb)
	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:", string(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)

	sshRulesM := new(models.SshRuleManage)
	err = sshRulesM.Insert(dbconn, sshRulesOb, logid)
	if err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("ssh rules insert error:", err, "logid:", logid)
		this.Ctx.Output.SetStatus(500)
		this.Ctx.Output.Body([]byte(`{"result":1,"error":` + err.Error() + `}`))
		this.StopRun()
	}

	if err = this.reloadRules(dbconn, sshRulesOb, logid); err != nil {
		dbconn.Exec("ROLLBACK")
		logs.Normal("ROLLBACK", "logid:", logid)
		logs.Error("reload rules 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("post OK!", "logid:", logid)
	this.Ctx.Output.Body([]byte(`{"result":0}`))
}
Пример #9
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
}
Пример #10
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
}
Пример #11
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
}
Пример #12
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
}
Пример #13
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
}
Пример #14
0
func (this *SshRulesController) reloadRules(conn *sql.DB, sshRulesOb models.SshRule, logid int64) error {
	sshKeyOb := new(models.SshKeysManage)
	keylist, err := sshKeyOb.GetAll(conn, strconv.Itoa(sshRulesOb.Uid), logid)
	if err != nil {
		logs.Error("ssh key get all error:", err, "logid:", logid)
		return err
	}
	if len(keylist) <= 0 {
		return nil
	}
	err = models.UpdateRule([]models.SshRule{sshRulesOb}, keylist, logid)
	if err != nil {
		logs.Error("update rule error:", err, "rule list:", sshRulesOb, "key list:", keylist, "logid:", logid)
	}
	return err
}
Пример #15
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
}
Пример #16
0
//初始化数据库
func InitDbConn(logid int64) (*sql.DB, error) {
	mysql_conn := beego.AppConfig.String("mysql_conn")
	dbconn, err := sql.Open("mysql", mysql_conn)
	if err != nil {
		logs.Error("db connect Error!", err, "logid:", logid)
	}
	return dbconn, err
}
Пример #17
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
}
Пример #18
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
}
Пример #19
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
}
Пример #20
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
}
Пример #21
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
}
Пример #22
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
}
Пример #23
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
}
Пример #24
0
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()
}
Пример #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
}
Пример #26
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
}
Пример #27
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
}
Пример #28
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
}
Пример #29
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
}
Пример #30
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()
}