Commit ff668f96 authored by Eric's avatar Eric

[update] validate or register access key

parent f1da5e13
...@@ -2,6 +2,7 @@ package main ...@@ -2,6 +2,7 @@ package main
import ( import (
"cocogo/pkg/config" "cocogo/pkg/config"
"cocogo/pkg/service"
"cocogo/pkg/sshd" "cocogo/pkg/sshd"
) )
...@@ -10,5 +11,6 @@ func init() { ...@@ -10,5 +11,6 @@ func init() {
} }
func main() { func main() {
service.Initial()
sshd.StartServer() sshd.StartServer()
} }
...@@ -9,7 +9,7 @@ type Terminal struct { ...@@ -9,7 +9,7 @@ type Terminal struct {
AccessKey struct { AccessKey struct {
Id string `json:"id"` Id string `json:"id"`
Secret string `json:"secret"` Secret string `json:"secret"`
} } `json:"access_key"`
} `json:"service_account"` } `json:"service_account"`
} }
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"strings" "strings"
"cocogo/pkg/common" "cocogo/pkg/common"
"cocogo/pkg/config"
"cocogo/pkg/logger" "cocogo/pkg/logger"
) )
...@@ -60,10 +61,31 @@ func (ak *AccessKey) LoadAccessKeyFromFile(keyPath string) error { ...@@ -60,10 +61,31 @@ func (ak *AccessKey) LoadAccessKeyFromFile(keyPath string) error {
} }
func (ak *AccessKey) SaveToFile() error { func (ak *AccessKey) SaveToFile() error {
return nil f, err := os.Create(ak.Path)
defer f.Close()
if err != nil {
return err
}
_, err = f.WriteString(fmt.Sprintf("%s:%s", ak.Id, ak.Secret))
if err != nil {
logger.Error(err)
}
return err
} }
func (ak *AccessKey) Register(times int) error { func (ak *AccessKey) Register(times int) error {
name := config.Conf.Name
token := config.Conf.BootstrapToken
comment := "Coco"
res := RegisterTerminal(name, token, comment)
if res.Name != name {
msg := "register access key failed"
logger.Error(msg)
os.Exit(1)
}
ak.Id = res.ServiceAccount.AccessKey.Id
ak.Secret = res.ServiceAccount.AccessKey.Secret
return nil return nil
} }
......
package service package service
import ( import (
"cocogo/pkg/common" "os"
"cocogo/pkg/config"
"path" "path"
"path/filepath" "path/filepath"
"time"
"cocogo/pkg/common"
"cocogo/pkg/config"
"cocogo/pkg/logger"
) )
var client = common.NewClient(30, "") var client = common.NewClient(30, "")
...@@ -21,4 +25,24 @@ func Initial() { ...@@ -21,4 +25,24 @@ func Initial() {
ak := AccessKey{Value: config.Conf.AccessKey, Path: keyPath} ak := AccessKey{Value: config.Conf.AccessKey, Path: keyPath}
_ = ak.Load() _ = ak.Load()
authClient.Auth = ak authClient.Auth = ak
ValidateAccessAuth()
}
func ValidateAccessAuth() {
count := 0
for count < 100 {
user := getTerminalProfile()
if user.Id != "" {
break
}
msg := `Connect server error or access key is invalid,
remove %s run again`
logger.Errorf(msg, config.Conf.AccessKeyFile)
time.Sleep(3 * time.Second)
count++
if count >= 3 {
os.Exit(1)
}
}
} }
...@@ -13,8 +13,18 @@ func RegisterTerminal(name, token, comment string) (res model.Terminal) { ...@@ -13,8 +13,18 @@ func RegisterTerminal(name, token, comment string) (res model.Terminal) {
} }
client.Headers["Authorization"] = fmt.Sprintf("BootstrapToken %s", token) client.Headers["Authorization"] = fmt.Sprintf("BootstrapToken %s", token)
data := map[string]string{"name": name, "comment": comment} data := map[string]string{"name": name, "comment": comment}
Url := client.ParseUrlQuery(TerminalRegisterURL, nil)
err := client.Post(Url, data, &res)
if err != nil {
logger.Error(err)
}
return
}
func getTerminalProfile() (user model.User) {
Url := authClient.ParseUrlQuery(UserProfileURL, nil)
err := client.Post(TerminalRegisterURL, data, &res) err := authClient.Get(Url, &user)
if err != nil { if err != nil {
logger.Error(err) logger.Error(err)
} }
......
...@@ -245,11 +245,11 @@ func (i *InteractiveHandler) refreshAssetsAndNodesData() { ...@@ -245,11 +245,11 @@ func (i *InteractiveHandler) refreshAssetsAndNodesData() {
} }
func (i *InteractiveHandler) loadUserAssets() { func (i *InteractiveHandler) loadUserAssets() {
i.assets = service.GetUserAssets(i.user.Id) i.assets = service.GetUserAssets(i.user.Id, "1")
} }
func (i *InteractiveHandler) loadUserAssetNodes() { func (i *InteractiveHandler) loadUserAssetNodes() {
i.nodes = service.GetUserNodes(i.user.Id) i.nodes = service.GetUserNodes(i.user.Id, "1")
} }
func (i *InteractiveHandler) changeLanguage() { func (i *InteractiveHandler) changeLanguage() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment