check_policy.py 1.36 KB
import hashlib
from firebase_admin import db
from firebase_admin import credentials
import firebase_admin
import csv


cred = credentials.Certificate('wello_firebase_SDKKey.json')
firebase_admin.initialize_app(cred, {'databaseURL': 'https://capstone-vip.firebaseio.com/'})
ref = db.reference()
policies = ref.get()

## compare dict
policy_dict = dict()

def policy_hash_writer(policies):
    with open('./hash_policy.csv', 'w', encoding='utf-8', newline='') as csvfile:
        wr = csv.writer(csvfile)
        
        for policy in policies:
            sha = hashlib.sha256()
            sha.update(str(policy).encode('utf-8'))
            wr.writerow([policy["Value"], sha.hexdigest()])


def policy_db_reader():
    for policy in policies:
        if policy["Value"] in policy_dict.keys():
            sha = hashlib.sha256()
            sha.update(str(policy).encode('utf-8'))
            if policy_dict[policy["Value"]] != sha.hexdigest():
                print("change contents : ", policy["Value"], ".", policy["Policy"])
        else:
            print("new policy : ", policy["Value"], ". ", policy["Policy"])
            

def policy_hash_reader():
    with open('./hash_policy.csv', 'r', encoding='utf-8') as csvfile:
        rdr = csv.reader(csvfile)

        for r in rdr:
            policy_dict[r[0]] = r[1]

#policy_hash_writer(policies)
policy_hash_reader()
policy_db_reader()