summaryrefslogtreecommitdiff
path: root/monitor/josef_reader.py
blob: d2435851c390cccac0b6a083b9ee86f65ffd892b (plain)
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
#!/usr/bin/python
# -*- coding: utf-8 -*-     

import sys
import time
import datetime
from josef_lib import *
import leveldb
import argparse
import json
import base64
import subprocess
from josef_leveldb import *
from datetime import datetime as dt
import ast
from monitor_conf import DB_PATH, MONITORED_DOMAINS, OUTPUT_DIR



class monitored_domain:
    def __init__(self, url):
        self.url = url
        self.entries = []

    def __eq__(self, other):
        return self.url == other.url

    def log(self, string):
        s = time.strftime('%Y-%m-%d, %H:%M:%S') + " " + string
        with open(OUTPUT_DIR + "monitor.log", 'a') as f:
            f.write(s + "\n")
            f.close()

    def set(self):
        self.entries = db_monitor_domain(self.url, None, True, None)
        self.log("Got " + str(len(self.entries)) + " certs for " + self.url)

    def update(self):
        new = db_monitor_domain(self.url, None, True, None)
        self.compare_and_log_entries(new, self.entries)
        self.entries = new

    def to_dict(self):
        d = {}
        d["url"] = self.url
        entries = []
        for e in self.entries:
            entries.append(e.to_dict())
        d["entries"] = entries
        return d

    def load_entries(self, l):
        entries = []
        for item in l:
            entries.append(monitored_entry(item["subject"],item["issuer"],item["log"],item["status"],item["leaf_hash"]))
        self.entries = entries

    def compare_and_log_entries(self, new, old):
        added_items  = []
        removed_items  = []

        for item in new:
            if not item in old:
                added_items.append(item)

        for item in old:
            if not item in new:
                removed_items.append(item)

        if len(added_items) != 0:
            self.log(str(len(added_items)) + " new item(s):")
            for item in added_items:
                self.log(str(item))
        
        if len(removed_items) != 0:
            self.log(str(len(removed_items)) + " removed item(s):")
            for item in removed_items:
                self.log(str(item))


class monitored_entry:
    def __init__(self, subject, issuer, log, status, leaf_hash):
        self.issuer = issuer
        self.subject = subject
        self.log = log
        self.status = status
        self.leaf_hash = leaf_hash

    def __eq__(self, other):
        return self.leaf_hash == other.leaf_hash 
        # return self.leaf_hash == other.leaf_hash and self.status == other.status

    def __str__(self):
        s = self.subject + \
        " certified by " + self.issuer + \
        " (" + self.log + ") "
        if self.status:
            return "(VALID) " + s
        else:
            return "(NOT VALID) " + s

    def to_dict(self):
        d = {}
        d["issuer"] = self.issuer
        d["subject"] = self.subject
        d["log"] = self.log
        d["status"] = self.status
        d["leaf_hash"] = self.leaf_hash
        return d



def db_monitor_domain(domain, log=None, exclude_invalid=None, get_cert=None):
    # print domain
    raw = db_lookup_domain(DB_PATH, domain)

    cur_time = dt.now()
    count_valid = 0
    count_expired = 0
    count_not_yet_valid = 0
    count_all = 0
    res = []
    for item in raw:
        try:
            entry = ast.literal_eval(item)
        except:
            print "Failed to parse item: " + item
            continue

        success = True
        not_after_time = dt.strptime(entry["not_after"], "%b %d %H:%M:%S %Y GMT")
        not_before_time = dt.strptime(entry["not_before"], "%b %d %H:%M:%S %Y GMT")


        if log:
            if log in entry["log"]:
                pass
            else:
                success = False

        if issuer:
            if issuer in entry["issuer"]:
                pass
            else:
                success = False

        if cur_time > not_after_time:
            valid = False
            expired = True
        elif cur_time < not_before_time:
            valid = False
            expired = False
        else:
            expired = False
            valid = True

        # Exclude expired
        if exclude_invalid and not valid:
            success = False
            
        
        # Set count matches
        if success:
            count_all += 1
            if valid:
                count_valid += 1
            elif expired:
                count_expired += 1
            else:
                count_not_yet_valid += 1

        # Print matching
        if success:
            me = monitored_entry(entry["subject"].split("CN=")[1], \
                entry["issuer"].split("CN=")[1], \
                entry["log"], \
                valid, \
                entry["leaf_hash"])
            # print str(me)

            if get_cert:
                print get_full_cert(entry)
                if "index" in entry:
                    print "INDEX:", index
            if me not in res:
                res.append(me)


    print str(count_all) + " matches found. " \
    + str(count_valid) + " valid, " \
    + str(count_expired) + " expired and " \
    + str(count_not_yet_valid) + " not yet valid for " \
    + domain
    return res

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description="")
    parser.add_argument('--domain', default=None)
    parser.add_argument('--log', default=None)
    parser.add_argument('--issuer', default=None)
    parser.add_argument('--exclude-invalid', action='store_true')
    parser.add_argument('--get-cert', action='store_true')

    args = parser.parse_args()

    monitored_domains = []
    for md in MONITORED_DOMAINS:
        monitored_domains.append(monitored_domain(md))

    if args.domain:
        db_monitor_domain(args.domain, args.log, args.exclude_invalid, args.get_cert)
    else:
        print "Running on " + str(len(monitored_domains)) + " monitored domains."
        for d in monitored_domains:
            d.set()
        for d in monitored_domains:
            print d.to_dict()