forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain.py
More file actions
226 lines (177 loc) · 7.46 KB
/
domain.py
File metadata and controls
226 lines (177 loc) · 7.46 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""This module defines the Domain class, which models domains monitored by Tracker
and offers methods to get data about domains"""
import json
import formatting
import organization as org
import queries
class Domain:
"""Class that represents a domain in Tracker.
Attributes provide access to scalar fields for the domain in the GraphQL schema,
while methods return JSON data for non-scalar fields. Users should not typically
instantiate this class manually, instead use methods provided by
:class:`tracker_client.client.Client` to get Domains.
The naming irregularity between parameters and attributes is to match
parameter names to the keys contained in the API responses. This allows easy
use of dict unpacking when creating a Domain instance. Attribute names instead
adhere to Python convention
:param Client client: the :class:`tracker_client.client.Client` that created
this object. Provides a way for Domain methods to execute queries.
:param str domain: name of the domain
:param str lastRan: timestamp for last scan run on the domain
:param str dmarcPhase: DMARC implementation phase # TODO: Add ref. to relevant docs
:param list[str] selectors: DKIM selector strings associated with the domain # TODO: Add ref. to relevant docs
"""
def __init__(self, client, domain, lastRan, dmarcPhase, selectors):
self.client = client
self.domain_name = domain
self.last_ran = lastRan
self.dmarc_phase = dmarcPhase
self.dkim_selectors = selectors
def get_status(self):
"""Return pass/fail status information for this Domain
:return: formatted JSON data with the domain's status
:rtype: str
:Example:
>>> from tracker_client.client import Client
>>> client = Client()
>>> my_domains = client.get_domains()
>>> print(my_domains[0].get_status())
{
"foo.bar": {
"lastRan": "2021-01-23 22:33:26.921529",
"status": {
"https": "FAIL",
"ssl": "FAIL",
"dmarc": "PASS",
"dkim": "PASS",
"spf": "PASS"
}
}
}
"""
params = {"domain": self.domain_name}
result = self.client.execute_query(queries.DOMAIN_STATUS, params)
if "error" not in result:
result = formatting.format_domain_status(result)
return json.dumps(result, indent=4)
def get_monthly_dmarc_summary(self, month, year):
"""Get the DMARC summary for the specified month and year
:param str month: the full name of a month
:param int year: positive integer representing a year
:return: formatted JSON data with a DMARC summary
:rtype: str
:Example:
>>> from tracker_client.client import Client
>>> client = Client()
>>> foobar = client.get_domain("foo.bar")
>>> print(foobar.get_monthly_dmarc("september", 2020))
{
"foo.bar": {
"month": "SEPTEMBER",
"year": "2020",
"categoryPercentages": {
"fullPassPercentage": 87,
"passSpfOnlyPercentage": 0,
"passDkimOnlyPercentage": 6,
"failPercentage": 8,
"totalMessages": 10534
}
}
}
"""
params = {"domain": self.domain_name, "month": month.upper(), "year": str(year)}
result = self.client.execute_query(queries.DMARC_SUMMARY, params)
if "error" not in result:
result = formatting.format_dmarc_monthly(result)
return json.dumps(result, indent=4)
def get_yearly_dmarc_summaries(self):
"""Get yearly DMARC summaries for a domain
:return: formatted JSON data with yearly DMARC summaries
:rtype: str
:Example:
Output is truncated, you should expect more than 2 reports in the list
>>> from tracker_client.client import Client
>>> client = Client()
>>> foobar = client.get_domain("foo.bar")
>>> print(foobar.get_yearly_dmarc())
{
"foo.bar": [
{
"month": "AUGUST",
"year": "2020",
"categoryPercentages": {
"fullPassPercentage": 90,
"passSpfOnlyPercentage": 0,
"passDkimOnlyPercentage": 5,
"failPercentage": 5,
"totalMessages": 7045
}
},
{
"month": "JULY",
"year": "2020",
"categoryPercentages": {
"fullPassPercentage": 82,
"passSpfOnlyPercentage": 0,
"passDkimOnlyPercentage": 11,
"failPercentage": 8,
"totalMessages": 6647
}
},
]
}
"""
params = {"domain": self.domain_name}
result = self.client.execute_query(queries.DMARC_YEARLY_SUMMARIES, params)
if "error" not in result:
result = formatting.format_dmarc_yearly(result)
return json.dumps(result, indent=4)
def get_all_results(self):
"""Get all scan results for this Domain
:return: formatted JSON data with all scan results for the domain
:rtype: str
:Example:
Coming soon, function likely to change
"""
params = {"domain": self.domain_name}
result = self.client.execute_query(queries.ALL_RESULTS, params)
if "error" not in result:
result = formatting.format_all_results(result)
return json.dumps(result, indent=4)
def get_web_results(self):
"""Get web scan results for this Domain
:return: formatted JSON data with web scan results for the domain
:rtype: str
:Example:
"""
params = {"domain": self.domain_name}
result = self.client.execute_query(queries.WEB_RESULTS, params)
if "error" not in result:
result = formatting.format_web_results(result)
return json.dumps(result, indent=4)
def get_email_results(self):
"""Get email scan results for this Domain
:return: formatted JSON data with email scan results for the domain
:rtype: str
:Example:
"""
params = {"domain": self.domain_name}
result = self.client.execute_query(queries.EMAIL_RESULTS, params)
if "error" not in result:
result = formatting.format_email_results(result)
return json.dumps(result, indent=4)
def get_owners(self):
"""Get a list of Organizations that control this domain
:return: A list of one or more :class:`tracker_client.organization.Organization`s
responsible for this domain
:rtype: list[Organization]
"""
params = {"domain": self.domain_name}
result = self.client.execute_query(queries.GET_DOMAIN_OWNERS, params)
if "error" in result:
print("Server error: ", result)
raise ValueError("Unable to get owners for " + self.domain_name)
org_list = []
for edge in result["findDomainByDomain"]["organizations"]["edges"]:
org_list.append(org.Organization(self.client, **edge["node"]))
return org_list