Skip to content

Commit

Permalink
WDAPI-645 add device posture integration api (cloudflare#749)
Browse files Browse the repository at this point in the history
* WDAPI-645 add device posture integration api

* Update device_posture_rule.go

Co-authored-by: Jacob Bednarz <[email protected]>

Co-authored-by: andrew <[email protected]>
Co-authored-by: Jacob Bednarz <[email protected]>
  • Loading branch information
3 people authored Dec 21, 2021
1 parent 8d7b587 commit beb29bf
Show file tree
Hide file tree
Showing 2 changed files with 372 additions and 11 deletions.
158 changes: 147 additions & 11 deletions device_posture_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,140 @@ import (
"github.com/pkg/errors"
)

// DevicePostureIntegrationConfig contains authentication information
// for a device posture integration.
type DevicePostureIntegrationConfig struct {
ClientID string `json:"client_id,omitempty"`
ClientSecret string `json:"client_secret,omitempty"`
AuthUrl string `json:"auth_url,omitempty"`
ApiUrl string `json:"api_url,omitempty"`
}

// DevicePosturIntegration represents a device posture integration.
type DevicePostureIntegration struct {
IntegrationID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Interval string `json:"interval,omitempty"`
Config DevicePostureIntegrationConfig `json:"config,omitempty"`
}

// DevicePostureIntegrationResponse represents the response from the get
// device posture integrations endpoint.
type DevicePostureIntegrationResponse struct {
Result DevicePostureIntegration `json:"result"`
Response
ResultInfo `json:"result_info"`
}

// DevicePostureIntegrationListResponse represents the response from the list
// device posture integrations endpoint.
type DevicePostureIntegrationListResponse struct {
Result []DevicePostureIntegration `json:"result"`
Response
ResultInfo `json:"result_info"`
}

// CreateDevicePostureIntegration creates a device posture integration within an account.
//
// API reference: https://api.cloudflare.com/#device-posture-integrations-create-device-posture-integration
func (api *API) CreateDevicePostureIntegration(ctx context.Context, accountID string, integration DevicePostureIntegration) (DevicePostureIntegration, error) {
uri := fmt.Sprintf("/%s/%s/devices/posture/integration", AccountRouteRoot, accountID)

res, err := api.makeRequestContext(ctx, http.MethodPost, uri, integration)
if err != nil {
fmt.Printf("err:%+v res:%+v\n", err, res)
return DevicePostureIntegration{}, err
}

var devicePostureIntegrationResponse DevicePostureIntegrationResponse
err = json.Unmarshal(res, &devicePostureIntegrationResponse)
if err != nil {
return DevicePostureIntegration{}, errors.Wrap(err, errUnmarshalError)
}

return devicePostureIntegrationResponse.Result, nil
}

// UpdateDevicePostureIntegration updates a device posture integration within an account.
//
// API reference: https://api.cloudflare.com/#device-posture-integrations-update-device-posture-integration
func (api *API) UpdateDevicePostureIntegration(ctx context.Context, accountID string, integration DevicePostureIntegration) (DevicePostureIntegration, error) {
uri := fmt.Sprintf("/%s/%s/devices/posture/integration/%s", AccountRouteRoot, accountID, integration.IntegrationID)

res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, integration)
if err != nil {
return DevicePostureIntegration{}, err
}

var devicePostureIntegrationResponse DevicePostureIntegrationResponse
err = json.Unmarshal(res, &devicePostureIntegrationResponse)
if err != nil {
return DevicePostureIntegration{}, errors.Wrap(err, errUnmarshalError)
}

return devicePostureIntegrationResponse.Result, nil
}

// DevicePostureIntegration returns a specific device posture integrations within an account.
//
// API reference: https://api.cloudflare.com/#device-posture-integrations-device-posture-integration-details
func (api *API) DevicePostureIntegration(ctx context.Context, accountID, integrationID string) (DevicePostureIntegration, error) {
uri := fmt.Sprintf("/%s/%s/devices/posture/integration/%s", AccountRouteRoot, accountID, integrationID)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return DevicePostureIntegration{}, err
}

var devicePostureIntegrationResponse DevicePostureIntegrationResponse
err = json.Unmarshal(res, &devicePostureIntegrationResponse)
if err != nil {
return DevicePostureIntegration{}, errors.Wrap(err, errUnmarshalError)
}

return devicePostureIntegrationResponse.Result, nil
}

// DevicePostureIntegrations returns all device posture integrations within an account.
//
// API reference: https://api.cloudflare.com/#device-posture-integrations-list-device-posture-integrations
func (api *API) DevicePostureIntegrations(ctx context.Context, accountID string) ([]DevicePostureIntegration, ResultInfo, error) {
uri := fmt.Sprintf("/%s/%s/devices/posture/integration", AccountRouteRoot, accountID)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []DevicePostureIntegration{}, ResultInfo{}, err
}

var devicePostureIntegrationListResponse DevicePostureIntegrationListResponse
err = json.Unmarshal(res, &devicePostureIntegrationListResponse)
if err != nil {
return []DevicePostureIntegration{}, ResultInfo{}, errors.Wrap(err, errUnmarshalError)
}

return devicePostureIntegrationListResponse.Result, devicePostureIntegrationListResponse.ResultInfo, nil
}

// DeleteDevicePostureIntegration deletes a device posture integration.
//
// API reference: https://api.cloudflare.com/#device-posture-integrations-delete-device-posture-integration
func (api *API) DeleteDevicePostureIntegration(ctx context.Context, accountID, ruleID string) error {
uri := fmt.Sprintf(
"/%s/%s/devices/posture/integration/%s",
AccountRouteRoot,
accountID,
ruleID,
)

_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}

return nil
}

// DevicePostureRule represents a device posture rule.
type DevicePostureRule struct {
ID string `json:"id,omitempty"`
Expand All @@ -27,17 +161,19 @@ type DevicePostureRuleMatch struct {

// DevicePostureRuleInput represents the value to be checked against.
type DevicePostureRuleInput struct {
ID string `json:"id,omitempty"`
Path string `json:"path,omitempty"`
Exists bool `json:"exists,omitempty"`
Thumbprint string `json:"thumbprint,omitempty"`
Sha256 string `json:"sha256,omitempty"`
Running bool `json:"running,omitempty"`
RequireAll bool `json:"requireAll,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Version string `json:"version,omitempty"`
Operator string `json:"operator,omitempty"`
Domain string `json:"domain,omitempty"`
ID string `json:"id,omitempty"`
Path string `json:"path,omitempty"`
Exists bool `json:"exists,omitempty"`
Thumbprint string `json:"thumbprint,omitempty"`
Sha256 string `json:"sha256,omitempty"`
Running bool `json:"running,omitempty"`
RequireAll bool `json:"requireAll,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Version string `json:"version,omitempty"`
Operator string `json:"operator,omitempty"`
Domain string `json:"domain,omitempty"`
ComplianceStatus string `json:"compliance_status,omitempty"`
ConnectionID string `json:"connection_id,omitempty"`
}

// DevicePostureRuleListResponse represents the response from the list
Expand Down
Loading

0 comments on commit beb29bf

Please sign in to comment.