Create a device posture rule

Creates a new device posture rule.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Create a device posture rule
8
 * Creates a new device posture rule.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  identifier: string,
13
  body: {
14
    description?: string;
15
    expiration?: string;
16
    input?:
17
      | {
18
          exists?: boolean;
19
          operating_system: "windows" | "linux" | "mac";
20
          path: string;
21
          sha256?: string;
22
          thumbprint?: string;
23
          [k: string]: unknown;
24
        }
25
      | {
26
          id: string;
27
          operating_system: "android" | "ios" | "chromeos";
28
          [k: string]: unknown;
29
        }
30
      | { domain?: string; operating_system: "windows"; [k: string]: unknown }
31
      | {
32
          operating_system: "windows";
33
          operator: "<" | "<=" | ">" | ">=" | "==";
34
          os_distro_name?: string;
35
          os_distro_revision?: string;
36
          os_version_extra?: string;
37
          version: string;
38
          [k: string]: unknown;
39
        }
40
      | {
41
          enabled: boolean;
42
          operating_system: "windows" | "mac";
43
          [k: string]: unknown;
44
        }
45
      | {
46
          operating_system: "windows" | "linux" | "mac";
47
          path: string;
48
          sha256?: string;
49
          thumbprint?: string;
50
          [k: string]: unknown;
51
        }
52
      | {
53
          operating_system: "windows" | "linux" | "mac";
54
          path: string;
55
          sha256?: string;
56
          thumbprint?: string;
57
          [k: string]: unknown;
58
        }
59
      | { checkDisks?: string[]; requireAll?: boolean; [k: string]: unknown }
60
      | {
61
          operating_system: "windows" | "linux" | "mac";
62
          path: string;
63
          sha256?: string;
64
          thumbprint?: string;
65
          [k: string]: unknown;
66
        }
67
      | { certificate_id: string; cn: string; [k: string]: unknown }
68
      | {
69
          compliance_status: "compliant" | "noncompliant" | "unknown";
70
          connection_id: string;
71
          [k: string]: unknown;
72
        }
73
      | {
74
          connection_id: string;
75
          operator?: "<" | "<=" | ">" | ">=" | "==";
76
          os?: string;
77
          overall?: string;
78
          sensor_config?: string;
79
          version?: string;
80
          versionOperator?: "<" | "<=" | ">" | ">=" | "==";
81
          [k: string]: unknown;
82
        }
83
      | {
84
          compliance_status:
85
            | "compliant"
86
            | "noncompliant"
87
            | "unknown"
88
            | "notapplicable"
89
            | "ingraceperiod"
90
            | "error";
91
          connection_id: string;
92
          [k: string]: unknown;
93
        }
94
      | {
95
          connection_id: string;
96
          countOperator: "<" | "<=" | ">" | ">=" | "==";
97
          issue_count: string;
98
          [k: string]: unknown;
99
        }
100
      | {
101
          connection_id: string;
102
          eid_last_seen?: string;
103
          operator?: "<" | "<=" | ">" | ">=" | "==";
104
          risk_level?: "low" | "medium" | "high" | "critical";
105
          scoreOperator?: "<" | "<=" | ">" | ">=" | "==";
106
          total_score?: number;
107
          [k: string]: unknown;
108
        }
109
      | {
110
          active_threats?: number;
111
          connection_id: string;
112
          infected?: boolean;
113
          is_active?: boolean;
114
          network_status?:
115
            | "connected"
116
            | "disconnected"
117
            | "disconnecting"
118
            | "connecting";
119
          operator?: "<" | "<=" | ">" | ">=" | "==";
120
          [k: string]: unknown;
121
        };
122
    match?: {
123
      platform?: "windows" | "mac" | "linux" | "android" | "ios";
124
      [k: string]: unknown;
125
    }[];
126
    name: string;
127
    schedule?: string;
128
    type:
129
      | "file"
130
      | "application"
131
      | "tanium"
132
      | "gateway"
133
      | "warp"
134
      | "disk_encryption"
135
      | "sentinelone"
136
      | "carbonblack"
137
      | "firewall"
138
      | "os_version"
139
      | "domain_joined"
140
      | "client_certificate"
141
      | "unique_client_id"
142
      | "kolide"
143
      | "tanium_s2s"
144
      | "crowdstrike_s2s"
145
      | "intune"
146
      | "workspace_one"
147
      | "sentinelone_s2s";
148
    [k: string]: unknown;
149
  }
150
) {
151
  const url = new URL(
152
    `https://api.cloudflare.com/client/v4/accounts/${identifier}/devices/posture`
153
  );
154

155
  const response = await fetch(url, {
156
    method: "POST",
157
    headers: {
158
      "X-AUTH-EMAIL": auth.email,
159
      "X-AUTH-KEY": auth.key,
160
      "Content-Type": "application/json",
161
      Authorization: "Bearer " + auth.token,
162
    },
163
    body: JSON.stringify(body),
164
  });
165
  if (!response.ok) {
166
    const text = await response.text();
167
    throw new Error(`${response.status} ${text}`);
168
  }
169
  return await response.json();
170
}
171