-
Notifications
You must be signed in to change notification settings - Fork 773
Expand file tree
/
Copy pathzt_risk_behaviors.go
More file actions
126 lines (107 loc) · 3 KB
/
Copy pathzt_risk_behaviors.go
File metadata and controls
126 lines (107 loc) · 3 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cloudflare
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/goccy/go-json"
)
// Behavior represents a single zt risk behavior config.
type Behavior struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
RiskLevel RiskLevel `json:"risk_level"`
Enabled *bool `json:"enabled"`
}
// Wrapper used to have full-fidelity repro of json structure.
type Behaviors struct {
Behaviors map[string]Behavior `json:"behaviors"`
}
// BehaviorResponse represents the response from the zt risk scoring endpoint
// and contains risk behaviors for an account.
type BehaviorResponse struct {
Success bool `json:"success"`
Result Behaviors `json:"result"`
Errors []string `json:"errors"`
Messages []string `json:"messages"`
}
// Behaviors returns all zero trust risk scoring behaviors for the provided account
//
// API reference: https://developers.cloudflare.com/api/operations/dlp-zt-risk-score-get-behaviors
func (api *API) Behaviors(ctx context.Context, accountID string) (Behaviors, error) {
uri := fmt.Sprintf("/accounts/%s/zt_risk_scoring/behaviors", accountID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return Behaviors{}, err
}
var r BehaviorResponse
err = json.Unmarshal(res, &r)
if err != nil {
return Behaviors{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// UpdateBehaviors returns all zero trust risk scoring behaviors for the provided account
// NOTE: description/name updates are no-ops, risk_level [low medium high] and enabled [true/false] results in modifications
//
// API reference: https://developers.cloudflare.com/api/operations/dlp-zt-risk-score-put-behaviors
func (api *API) UpdateBehaviors(ctx context.Context, accountID string, behaviors Behaviors) (Behaviors, error) {
uri := fmt.Sprintf("/accounts/%s/zt_risk_scoring/behaviors", accountID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, behaviors)
if err != nil {
return Behaviors{}, err
}
var r BehaviorResponse
err = json.Unmarshal(res, &r)
if err != nil {
return Behaviors{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
type RiskLevel int
const (
_ RiskLevel = iota
Low
Medium
High
)
func (p RiskLevel) MarshalJSON() ([]byte, error) {
return json.Marshal(p.String())
}
func (p RiskLevel) String() string {
return [...]string{"low", "medium", "high"}[p-1]
}
func (p *RiskLevel) UnmarshalJSON(data []byte) error {
var (
s string
err error
)
err = json.Unmarshal(data, &s)
if err != nil {
return err
}
v, err := RiskLevelFromString(s)
if err != nil {
return err
}
*p = *v
return nil
}
func RiskLevelFromString(s string) (*RiskLevel, error) {
s = strings.ToLower(s)
var v RiskLevel
switch s {
case "low":
v = Low
case "medium":
v = Medium
case "high":
v = High
default:
return nil, fmt.Errorf("unknown variant for risk level: %s", s)
}
return &v, nil
}
func (p RiskLevel) IntoRef() *RiskLevel {
return &p
}