Skip to content

Commit

Permalink
Merge pull request #1 from weblinc/improvement/update-sys-association…
Browse files Browse the repository at this point in the history
…-for-v2

Improvement/update sys association for v2
  • Loading branch information
thomasv314 authored Nov 16, 2017
2 parents 278f088 + 1337a76 commit 7dc266a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.envrc
client.key
jcagent.conf
build
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ There is only one endpoint `/systems/<random-system-guid>` which accepts `GET`,
## Examples

```bash
# Get system Information
# Get System Information
$ jcsystool -X GET

# Tag a system w/ a Group Association (JC API v2 only)
$ jcsystool --associate-group=012345678

# Delete system from JumpCloud
$ jcsystool -X DELETE

Expand Down
46 changes: 32 additions & 14 deletions jc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,51 @@ import (
"os"
)

const version = "1.0.0"
const version = "2.0.0"

func main() {

fmt.Println("jcsystool - JumpCloud System Tool", version)

var opts struct {
HttpAction string `short:"X" long:"action" description:"HTTP method to use e.g. GET/PUT/DELETE" required:"true"`
JSONContent string `short:"J" long:"json" description:"JSON string to use for PUT actions to system API. Alternatively, use STDIN."`
AssociateGroup string `long:"associate-group" description:"Associate a group"`
HttpAction string `short:"X" long:"action" description:"HTTP method to use e.g. GET/PUT/DELETE"`
JSONContent string `short:"J" long:"json" description:"JSON string to use for PUT actions to system API. Alternatively, use STDIN."`
Endpoint string `short:"e" long:"endpoint" description:"The endpoint to hit."`
}

_, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}

// Create the client with config/client key overrides if neccessary
client, err := jc.NewSystemClient(
os.Getenv("JC_CONFIG_PATH"),
os.Getenv("JC_CLIENT_KEY_PATH"),
)

if err != nil {
exitWithError("Error creating client.", err)
}

if opts.AssociateGroup != "" {
associated, err := client.AssociateGroup(opts.AssociateGroup)
if err != nil {
exitWithError("Error associating group.", err)
}

fmt.Println("Associated:", associated)
os.Exit(0)
}

httpAction := opts.HttpAction

if httpAction == "" {
httpAction = "GET"
}

jsonContent := opts.JSONContent
endpoint := opts.Endpoint

// PUT requests are the only actions that need json to send
if httpAction == "PUT" {
Expand All @@ -41,17 +68,8 @@ func main() {
jsonContent = ""
}

// Create the client with config/client key overrides if neccessary
client, err := jc.NewSystemClient(
os.Getenv("JC_CONFIG_PATH"),
os.Getenv("JC_CLIENT_KEY_PATH"),
)

if err != nil {
exitWithError("Error creating client.", err)
}
resp, err := client.Do(endpoint, httpAction, jsonContent)

resp, err := client.Do(httpAction, jsonContent)
bodyBytes, _ := ioutil.ReadAll(resp.Body)
body := string(bodyBytes)
if resp.StatusCode == 200 {
Expand Down
24 changes: 18 additions & 6 deletions jc/system_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,28 @@ func NewSystemClient(configPath, clientKeyPath string) (client SystemClient, err
return
}

func (c *SystemClient) AssociateGroup(groupId string) (result string, err error) {
endpoint := "/api/v2/systemgroups/" + groupId + "/members"
json := "{ \"op\": \"add\", \"type\": \"system\", \"id\": \"" + c.SystemKey + "\" }"

_, err = c.Do(endpoint, "POST", json)

return "Successfully associated to " + groupId, err
}

// Sends an API request to the endpoint specified for the system key
func (c *SystemClient) Do(httpMethod, body string) (resp *http.Response, err error) {
urlPath := "/api/systems/" + c.SystemKey
url := c.url(urlPath)
func (c *SystemClient) Do(endpoint, httpMethod, body string) (resp *http.Response, err error) {
if endpoint == "" {
endpoint = "/api/systems/" + c.SystemKey
}

url := c.url(endpoint)

req, err := http.NewRequest(httpMethod, url, bytes.NewReader([]byte(body)))

time := getTime()

requestSigHeader, err := c.getAuthSignature(time, httpMethod, urlPath)
requestSigHeader, err := c.getAuthSignature(time, httpMethod, endpoint)

if err != nil {
log.Println("Error retrieving signature for request", err)
Expand All @@ -73,10 +85,10 @@ func (c *SystemClient) Do(httpMethod, body string) (resp *http.Response, err err
}

// Gets an Auth Signature header based off the client.key file and system key
func (c *SystemClient) getAuthSignature(time, httpMethod, urlPath string) (header string, err error) {
func (c *SystemClient) getAuthSignature(time, httpMethod, endpoint string) (header string, err error) {
header = "Signature "

requestSig, err := c.ClientKey.SignatureForRequest(time, httpMethod, urlPath)
requestSig, err := c.ClientKey.SignatureForRequest(time, httpMethod, endpoint)

if err != nil {
return
Expand Down

0 comments on commit 7dc266a

Please sign in to comment.