-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsalt.go
84 lines (71 loc) · 1.79 KB
/
salt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"crypto/tls"
"errors"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"github.com/google/go-querystring/query"
)
// Request represents a single request made to the Salt API
type Request struct {
Client string `url:"client"`
Target string `url:"tgt"`
Function string `url:"fun"`
Arguments string `url:"arg,omitempty"`
ExpressionForm string `url:"expr_form,omitempty"`
}
// Salt represents a connection to the Salt API
type Salt struct {
Hostname string
client http.Client
}
// NewSalt helps create a new Salt object
func NewSalt(hostname string) *Salt {
s := new(Salt)
s.Hostname = hostname
cookieJar, _ := cookiejar.New(nil)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
s.client = http.Client{
Jar: cookieJar,
Transport: tr,
}
return s
}
// Login does a POST request against the Salt API to get an authentication cookie.
func (s *Salt) Login(username string, password string, eauth string) error {
values := url.Values{
"username": {username},
"password": {password},
"eauth": {eauth},
}
resp, err := s.client.PostForm(s.Hostname+"/login", values)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == 401 {
return errors.New("Could not authenticate using provided credentials")
}
return nil
}
// Run sends a Request to the Salt API
func (s *Salt) Run(target string, function string, arguments string) (string, error) {
request := Request{
Client: "local",
Target: target,
Function: function,
Arguments: arguments,
}
values, _ := query.Values(request)
resp, err := s.client.PostForm(s.Hostname, values)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return string(body), nil
}