-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path__init__.py
More file actions
172 lines (133 loc) · 6.12 KB
/
__init__.py
File metadata and controls
172 lines (133 loc) · 6.12 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import logging
from enum import Enum
from typing import Dict, Optional, Union
from dataclasses import dataclass, asdict
log = logging.getLogger("socketdev")
class SecurityAction(str, Enum):
DEFER = "defer"
ERROR = "error"
WARN = "warn"
MONITOR = "monitor"
IGNORE = "ignore"
@dataclass
class SecurityPolicyRule:
action: SecurityAction
def __getitem__(self, key):
return getattr(self, key)
def to_dict(self):
return asdict(self)
@classmethod
def from_dict(cls, data: dict) -> "SecurityPolicyRule":
return cls(action=SecurityAction(data["action"]))
@dataclass
class OrgSecurityPolicyResponse:
success: bool
status: int
securityPolicyRules: Optional[Dict[str, SecurityPolicyRule]] = None
message: Optional[str] = None
def __getitem__(self, key):
return getattr(self, key)
def to_dict(self):
return asdict(self)
@classmethod
def from_dict(cls, data: dict) -> "OrgSecurityPolicyResponse":
return cls(
securityPolicyRules={k: SecurityPolicyRule.from_dict(v) for k, v in data["securityPolicyRules"].items()}
if data.get("securityPolicyRules")
else None,
success=data["success"],
status=data["status"],
message=data.get("message"),
)
class Settings:
def __init__(self, api):
self.api = api
def create_params_string(self, params: dict) -> str:
param_str = ""
for name, value in params.items():
if value:
if name == "committers" and isinstance(value, list):
# Handle committers specially - add multiple params
for committer in value:
param_str += f"&{name}={committer}"
else:
param_str += f"&{name}={value}"
param_str = "?" + param_str.lstrip("&")
return param_str
def get(
self, org_slug: str, custom_rules_only: bool = False, use_types: bool = False
) -> Union[dict, OrgSecurityPolicyResponse]:
path = f"orgs/{org_slug}/settings/security-policy"
params = {"custom_rules_only": custom_rules_only}
params_args = self.create_params_string(params) if custom_rules_only else ""
path += params_args
response = self.api.do_request(path=path, method="GET")
if response.status_code == 200:
rules = response.json()
if use_types:
return OrgSecurityPolicyResponse.from_dict(
{"securityPolicyRules": rules.get("securityPolicyRules", {}), "success": True, "status": 200}
)
return rules
error_message = response.json().get("error", {}).get("message", "Unknown error")
log.error(f"Failed to get security policy: {response.status_code}, message: {error_message}")
if use_types:
return OrgSecurityPolicyResponse.from_dict(
{"securityPolicyRules": {}, "success": False, "status": response.status_code, "message": error_message}
)
return {}
def integration_events(self, org_slug: str, integration_id: str) -> dict:
"""Get integration events for a specific integration.
Args:
org_slug: Organization slug
integration_id: Integration ID
"""
path = f"orgs/{org_slug}/settings/integrations/{integration_id}"
response = self.api.do_request(path=path)
if response.status_code == 200:
return response.json()
error_message = response.json().get("error", {}).get("message", "Unknown error")
log.error(f"Error getting integration events: {response.status_code}, message: {error_message}")
return {}
def get_license_policy(self, org_slug: str) -> dict:
"""Get license policy settings for an organization.
Args:
org_slug: Organization slug
"""
path = f"orgs/{org_slug}/settings/license-policy"
response = self.api.do_request(path=path)
if response.status_code == 200:
return response.json()
error_message = response.json().get("error", {}).get("message", "Unknown error")
log.error(f"Error getting license policy: {response.status_code}, message: {error_message}")
return {}
def update_security_policy(self, org_slug: str, body: dict, custom_rules_only: bool = False) -> dict:
"""Update security policy settings for an organization.
Args:
org_slug: Organization slug
body: Security policy configuration to update
custom_rules_only: Optional flag to update only custom rules
"""
path = f"orgs/{org_slug}/settings/security-policy"
if custom_rules_only:
path += "?custom_rules_only=true"
response = self.api.do_request(path=path, method="POST", payload=body)
if response.status_code == 200:
return response.json()
error_message = response.json().get("error", {}).get("message", "Unknown error")
log.error(f"Error updating security policy: {response.status_code}, message: {error_message}")
return {}
def update_license_policy(self, org_slug: str, body: dict, merge_update: bool = False) -> dict:
"""Update license policy settings for an organization.
Args:
org_slug: Organization slug
body: License policy configuration to update
merge_update: Optional flag to merge updates instead of replacing (defaults to False)
"""
path = f"orgs/{org_slug}/settings/license-policy?merge_update={str(merge_update).lower()}"
response = self.api.do_request(path=path, method="POST", payload=body)
if response.status_code == 200:
return response.json()
error_message = response.json().get("error", {}).get("message", "Unknown error")
log.error(f"Error updating license policy: {response.status_code}, message: {error_message}")
return {}