Skip to content

Commit

Permalink
feat: implement GET /registration_requests and /registration_requests…
Browse files Browse the repository at this point in the history
…/{id}

Signed-off-by: James Ramirez <[email protected]>
  • Loading branch information
ramirezj committed May 11, 2024
1 parent 36018b1 commit b5e4df0
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ using the interactive API documentation at https://kolideapi.readme.io/reference
| GET | /exemption_requests/{id} | #37 | ? | ? | Ok |
| PATCH | /exemption_requests/{id} | :no_entry_sign: | | | |
| PUT | /exemption_requests/{id} | :no_entry_sign: | | | |
| GET | /registration_requests | #38 | ? | ? | Ok |
| GET | /registration_requests/{id} | #39 | ? | ? | Ok |
| GET | /registration_requests | :question: | ? | ? | Ok |
| GET | /registration_requests/{id} | :question: | ? | ? | Ok |
| PATCH | /registration_requests/{id} | :no_entry_sign: | | | |
| PUT | /registration_requests/{id} | :no_entry_sign: | | | |
| GET | /reporting/tables/{tableName}/table_records | #42 | ? | ? | [^1] |
Expand Down
51 changes: 51 additions & 0 deletions docs/tables/kolide_registration_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Table: kolide_registration_request

Lists the registration requests made when attempting to register a device with Kolide. These can be approved or denied by admins.

## Examples

### Basic info

```sql
select
id,
status,
requested_at,
requester_message,
requester_id,
device_id
from
kolide_registration_request;
```

### List all unresolved requests

```sql
select
id,
status,
requested_at,
requester_message,
requester_id,
device_id
from
kolide_registration_request
where
status != 'approved';
```

### List all recent requests

```sql
select
id,
status,
requested_at,
requester_message,
requester_id,
device_id
from
kolide_registration_request
where
requested_at > date_trunc('day', current_date) - interval '4 weeks';
```
31 changes: 31 additions & 0 deletions kolide/client/registration_requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kolide_client

import "time"

type RegistrationRequestListResponse struct {
RegistrationRequests []RegistrationRequest `json:"data"`
Pagination Pagination `json:"pagination"`
}
type RegistrationRequest struct {
Id string `json:"id"`
Status string `json:"status"`
RequesterMessage string `json:"requester_message,omitempty"`
InternalDenialNote string `json:"internal_denial_note,omitempty"`
RequestedAt time.Time `json:"requested_at,omitempty"`
EndUserDenialNote string `json:"end_user_denial_note,omitempty"`
RequesterInformation RequesterInformation `json:"requester_information,omitempty"`
DeviceInformation DeviceInformation `json:"device_information,omitempty"`
}

type RequesterInformation struct {
// Whilst the Kolide API readme entry references this as a "string", the returned value encountered during implementation MAY BE an "int"
Identifier int32 `json:"identifier,omitempty"`
}

func (c *Client) GetRegistrationRequests(cursor string, limit int32, searches ...Search) (interface{}, error) {
return c.fetchCollection("/registration_request/", cursor, limit, searches, new(RegistrationRequestListResponse))
}

func (c *Client) GetRegistrationRequestById(id string) (interface{}, error) {
return c.fetchResource("/registration_request/", id, new(RegistrationRequest))
}
1 change: 1 addition & 0 deletions kolide/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"kolide_person_group": tableKolidePersonGroup(ctx),
"kolide_person_open_issue": tableKolidePersonOpenIssue(ctx),
"kolide_person_registered_device": tableKolidePersonRegisteredDevice(ctx),
"kolide_registration_request": tableKolideRegistrationRequest(ctx),
},
}
return p
Expand Down
66 changes: 66 additions & 0 deletions kolide/table_kolide_registration_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package kolide

import (
"context"

kolide "github.com/grendel-consulting/steampipe-plugin-kolide/kolide/client"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

//// TABLE DEFINITION

func tableKolideRegistrationRequest(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "kolide_registration_request",
Description: "Created when someone requests admin approval to register a device with Kolide. Registration requests can be approved or denied by admins",
Columns: []*plugin.Column{
// Filterable "top" columns
{Name: "id", Description: "Canonical identifier for this registration request.", Type: proto.ColumnType_STRING},
{Name: "status", Description: "Current status of this registration request; one of: pending, approved or denied", Type: proto.ColumnType_STRING},
{Name: "requester_message", Description: "Any message the person provided when requesting approval for this registration.", Type: proto.ColumnType_STRING},
{Name: "requested_at", Description: "When this registration approval was requested.", Type: proto.ColumnType_TIMESTAMP},
// Other columns
{Name: "end_user_denial_note", Description: "Any explanation the admin provided for the requester when denying this request, for internal documentation. It is meant to be shown to the requester. It will be blank if the registration has not been denied.", Type: proto.ColumnType_STRING},
{Name: "internal_denial_note", Description: "Any internal explanation the admin provided when denying this request, for internal documentation. It is not shown to the requester. It will be blank if the registration has not been denied.", Type: proto.ColumnType_STRING},
{Name: "requester_id", Description: "Canonical identifier for the person who requested this registration.", Type: proto.ColumnType_STRING, Transform: transform.FromField("RequesterInformation.Identifier")},
{Name: "device_id", Description: "Canonical identifier for the device the person is trying to register.", Type: proto.ColumnType_STRING, Transform: transform.FromField("DeviceInformation.Identifier")},
// Steampipe standard columns
{Name: "title", Description: "Display name for this registration request.", Type: proto.ColumnType_STRING, Transform: transform.FromField("DeviceInformation.Identifier")},
},
List: &plugin.ListConfig{
KeyColumns: []*plugin.KeyColumn{
// Using Kolide API query feature, can be combined with AND (and OR)
{Name: "status", Require: plugin.Optional, Operators: []string{"=", "~~"}},
{Name: "requested_at", Require: plugin.Optional, Operators: []string{"=", ">", "<"}},
{Name: "requester_message", Require: plugin.Optional, Operators: []string{"=", "~~"}},
},
Hydrate: listRegistrationRequests,
},
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("id"),
Hydrate: getRegistrationRequest,
},
}
}

//// LIST FUNCTION

func listRegistrationRequests(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
var visitor ListPredicate = func(client *kolide.Client, cursor string, limit int32, searches ...kolide.Search) (interface{}, error) {
return client.GetRegistrationRequests(cursor, limit, searches...)
}

return listAnything(ctx, d, h, "kolide_registration_request.listRegistrationRequests", visitor, "RegistrationRequests")
}

//// GET FUNCTION

func getRegistrationRequest(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
var visitor GetPredicate = func(client *kolide.Client, id string) (interface{}, error) {
return client.GetRegistrationRequestById(id)
}

return getAnything(ctx, d, h, "kolide_registration_request.getRegistrationRequest", "id", visitor)
}
11 changes: 11 additions & 0 deletions test/end-to-end/_query/kolide_registration_request.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
select
id,
status,
requested_at,
requester_message,
requester_id,
device_id
from
kolide_registration_request
order by
id asc;
11 changes: 11 additions & 0 deletions test/end-to-end/_query/kolide_registration_request_by_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
select
id,
status,
requested_at,
requester_message,
requester_id,
device_id
from
kolide_registration_request
where
id = '12345';
11 changes: 11 additions & 0 deletions test/end-to-end/_results/kolide_registration_request.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

define_test_results(){
export EXPECTED_COUNT="0"
# export ID=""
# export STATUS=""
# export REQUESTED_AT=""
# export REQUESTER_MESSAGE=""
# export REQUESTER_ID=""
# export DEVICE_ID=""
}
69 changes: 69 additions & 0 deletions test/end-to-end/kolide_registration_request.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# bats file_tags=table:kolide_registration_request, output:registration_request

setup_file() {
load "${BATS_TEST_DIRNAME}/_support/globals.bash"
define_file_globals

define_common_test_results

if [[ -f $EXPECTED_RESULTS ]]; then
load $EXPECTED_RESULTS
fi
}

setup() {
load "${BATS_TEST_DIRNAME}/_support/extensions.bash"
load_helpers
}

#bats test_tags=scope:smoke
@test "can_execute_query_via_steampipe" {
steampipe query $QUERY_UNDER_TEST --output json > $QUERY_RESULTS
assert_exists $QUERY_RESULTS
}

@test "has_expected_number_of_results" {
run bash -c "cat $QUERY_RESULTS | jq -r '.rows | length'"

if [[ -z "$EXPECTED_COUNT" ]]; then assert_output $EXPECTED_COUNT ; else assert [ "$output" -ge "1" ] ; fi
}

@test "has_expected_id" {
run bash -c "cat $QUERY_RESULTS | jq -r '.rows.[0].id'"
if [[ -z "$ID" ]]; then assert_output $ID ; else assert_success ; fi
}

@test "has_expected_status" {
run bash -c "cat $QUERY_RESULTS | jq -r '.rows.[0].status'"
if [[ -z "$STATUS" ]]; then assert_output --partial $STATUS ; else assert_success ; fi
}

#bats test_tags=exactness:fuzzy
@test "has_expected_requested_at" {
run bash -c "cat $QUERY_RESULTS | jq -r '.rows.[0].requested_at'"
if [[ -z "$REQUESTED_AT" ]]; then assert_output $REQUESTED_AT ; else assert_success ; fi
}

#bats test_tags=exactness:fuzzy
@test "has_expected_requester_message" {
run bash -c "cat $QUERY_RESULTS | jq -r '.rows.[0].requester_message'"
if [[ -z "$REQUESTER_MESSAGE" ]]; then assert_output --partial $REQUESTER_MESSAGE ; else assert_success ; fi
}

#bats test_tags=exactness:fuzzy
@test "has_expected_requester_id" {
run bash -c "cat $QUERY_RESULTS | jq -r '.rows.[0].requester_id'"
if [[ -z "$REQUESTER_ID" ]]; then assert_output --partial $REQUESTER_ID ; else assert_success ; fi
}

#bats test_tags=exactness:fuzzy
@test "has_expected_device_id" {
run bash -c "cat $QUERY_RESULTS | jq -r '.rows.[0].device_id'"
if [[ -z "$DEVICE_ID" ]]; then assert_output --partial $DEVICE_ID ; else assert_success ; fi
}

teardown_file(){
if [[ -f $QUERY_RESULTS ]]; then
rm -f $QUERY_RESULTS
fi
}
30 changes: 30 additions & 0 deletions test/end-to-end/kolide_registration_request_by_id.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# bats file_tags=table:kolide_registration_request, output:registration_request

setup_file() {
load "${BATS_TEST_DIRNAME}/_support/globals.bash"
define_file_globals
}

setup() {
load "${BATS_TEST_DIRNAME}/_support/extensions.bash"
load_helpers
}

#bats test_tags=scope:smoke
@test "can_execute_query_via_steampipe" {
steampipe query $QUERY_UNDER_TEST --output json > $QUERY_RESULTS
assert_exists $QUERY_RESULTS
}

@test "has_no_more_than_one_result" {
run bash -c "cat $QUERY_RESULTS | jq -r '.rows | length'"
assert [ "$output" -le "1" ]
}

# Remaining functionality covered in kolide_registration_request.bats

teardown_file(){
if [[ -f $QUERY_RESULTS ]]; then
rm -f $QUERY_RESULTS
fi
}

0 comments on commit b5e4df0

Please sign in to comment.