From c0f70e3485fd4d76aaadd64caac3a6959c23a21a Mon Sep 17 00:00:00 2001 From: Yuto Kimura Date: Mon, 23 Oct 2017 12:33:46 +0900 Subject: [PATCH] Add exec script function to sesha3 * add exec get request * request json: add script_name * exec: change responce * change func name --- mobingi/sesha3/sesha3.go | 143 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) diff --git a/mobingi/sesha3/sesha3.go b/mobingi/sesha3/sesha3.go index 29101ae..d0a969b 100644 --- a/mobingi/sesha3/sesha3.go +++ b/mobingi/sesha3/sesha3.go @@ -60,6 +60,149 @@ type GetSessionUrlInput struct { Timeout int64 } +type ExecScriptInput struct { + StackId string + Target string + Script string + ScriptName string + InstUser string + Flag string +} + +type ScriptRes struct { + Out string `json:"stdout"` + Err string `json:"stderr"` +} + +func (s *sesha3) ExecScript(in *ExecScriptInput) (*client.Response, []byte, ScriptRes, error) { + var sresp ScriptRes + + if in == nil { + return nil, nil, sresp, errors.New("input cannot be nil") + } + if in.Target == "" { + return nil, nil, sresp, errors.New("target cannot be empty") + } + if in.Script == "" { + return nil, nil, sresp, errors.New("script cannot be empty") + } + if in.ScriptName == "" { + return nil, nil, sresp, errors.New("script cannot be empty") + } + + if s.session.Config.ApiVersion >= 3 { + if in.Flag == "" { + return nil, nil, sresp, errors.New("flag cannot be empty") + } + } + + if in.InstUser == "" { + return nil, nil, sresp, errors.New("instance username cannot be empty") + } + + // get pem url from stack id + almsvc := alm.New(s.session) + inpem := alm.GetPemInput{ + StackId: in.StackId, + } + + if s.session.Config.ApiVersion >= 3 { + inpem.Flag = in.Flag + } + + resp, body, _, err := almsvc.GetPem(&inpem) + if err != nil { + return resp, body, sresp, errors.Wrap(err, "get pem failed") + } + + type rsaurl struct { + Status string `json:"status"` + Data string `json:"data"` + } + + var ru rsaurl + err = json.Unmarshal(body, &ru) + if err != nil { + return resp, body, sresp, errors.Wrap(err, "url body unmarshal failed") + } + + pemurl := strings.Replace(ru.Data, "\\", "", -1) + + //get sesha3 token + type payload_token_t struct { + Username string `json:"username"` + Passwd string `json:"passwd"` + } + payloadToken := payload_token_t{ + Username: s.session.Config.Username, + Passwd: s.session.Config.Password, + } + + b, err := json.Marshal(payloadToken) + if err != nil { + return resp, body, sresp, errors.Wrap(err, "payload token marshal failed") + } + + ep := s.session.Sesha3Endpoint() + "/token" + req, err := http.NewRequest(http.MethodGet, ep, bytes.NewBuffer(b)) + req.Header.Add("Content-Type", "application/json") + resp, body, err = s.client.Do(req) + if err != nil { + return resp, body, sresp, errors.Wrap(err, "client do failed") + } + + var m map[string]string + err = json.Unmarshal(body, &m) + + if err != nil { + return resp, body, sresp, errors.Wrap(err, "token reply unmarshal failed") + } + + token, ok := m["key"] + if !ok { + return resp, body, sresp, errors.Wrap(err, "can't find token") + } + + type payload_t struct { + Pem string `json:"pem"` + StackId string `json:"stackid"` + Target string `json:"target"` + Script string `json:"script"` + ScriptName string `json:"script_name"` + User string `json:"user"` + } + + payload := payload_t{ + Pem: pemurl, + StackId: in.StackId, + Target: in.Target, + Script: in.Script, + ScriptName: in.ScriptName, + User: in.InstUser, + } + + b, err = json.Marshal(payload) + if err != nil { + return resp, body, sresp, errors.Wrap(err, "payload marshal failed") + } + + ep = s.session.Sesha3Endpoint() + "/exec" + req, err = http.NewRequest(http.MethodGet, ep, bytes.NewBuffer(b)) + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Authorization", "Bearer "+token) + resp, body, err = s.client.Do(req) + if err != nil { + return resp, body, sresp, errors.Wrap(err, "client do failed") + } + + err = json.Unmarshal(body, &sresp) + if err != nil { + return resp, body, sresp, errors.Wrap(err, "reply unmarshal failed") + } + + return resp, body, sresp, nil +} + func (s *sesha3) GetSessionUrl(in *GetSessionUrlInput) (*client.Response, []byte, string, error) { var u string