-
Notifications
You must be signed in to change notification settings - Fork 4
/
agent.go
97 lines (88 loc) · 2.13 KB
/
agent.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
85
86
87
88
89
90
91
92
93
94
95
96
97
// ts - golang ts api client
// agents.go: list all your agents, or just one of them
//
// Copyright 2019-2022 F5 Inc.
// Licensed under the BSD 3-clause license; see LICENSE for more information.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
tsapi "github.com/threatstack/ts/api"
"github.com/urfave/cli"
)
func getAgents(c *cli.Context, online bool) {
var agents []tsapi.Agent
var tokenString string
client := &http.Client{}
agentEndpoint := "/v2/agents?status=online"
if !online {
agentEndpoint = "/v2/agents?status=offline"
}
for {
var response tsapi.AgentResponseRaw
req, err := tsBuildHTTPReq(c, "GET", agentEndpoint+tokenString, nil)
if err != nil {
log.Fatalln(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
if err := json.Unmarshal(body, &response); err != nil {
log.Fatalln(err)
}
agents = append(agents, response.Agents...)
if response.Token != "" {
tokenString = fmt.Sprintf("&token=%s", response.Token)
} else {
break
}
} else {
fmt.Printf("Unable to query %s - API responded with a %d", agentEndpoint+tokenString, resp.StatusCode)
os.Exit(1)
}
}
ser, err := json.Marshal(agents)
if err != nil {
log.Fatalln(err)
}
fmt.Println(string(ser))
}
func getAgent(c *cli.Context) {
if c.Args().Get(0) == "" {
cli.ShowSubcommandHelp(c)
fmt.Printf("\nERROR: Specify the Agent ID you want to look up as an argument.\n")
os.Exit(1)
}
client := &http.Client{}
agentEndpoint := fmt.Sprintf("/v2/agents/%s", c.Args().Get(0))
req, err := tsBuildHTTPReq(c, "GET", agentEndpoint, nil)
if err != nil {
log.Fatalln(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%s\n", body)
} else {
fmt.Printf("Unable to query %s - API responded with an HTTP/%d", agentEndpoint, resp.StatusCode)
os.Exit(1)
}
}