Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bdickson latest edits - fix issue with generated test data #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,67 @@ const (
TableCENReport = "CENReport"

// Default Conn String
DefaultConnString = "[change this to your server]"
DefaultConnString = "root:2Nemesis234@/conn"
)

// Backend holds a client to connect to the BigTable backend
type Backend struct {
db *sql.DB
}

//
// Brian Dickson's added comments for understanding the SQL elements used/required
//
// OLD DEFINITIONS, WRONG SIZE FOR cenKey
//
// CREATE TABLE `CENKeys` (
// `cenKey` varchar(32) DEFAULT "",
// `reportID` varchar(64) DEFAULT "",
// `reportTS` int,
// PRIMARY KEY (`cenKey`, `reportID`),
// KEY (`reportID`),
// KEY (`reportTS`),
// KEY (`cenKey`)
// );
//
// CREATE TABLE `CENReport` (
// `reportID` varchar(64) DEFAULT "",
// `report` varchar(4000) DEFAULT "",
// `reportMimeType` varchar(64) DEFAULT "",
// `reportTS` int,
// PRIMARY KEY (`reportID`),
// KEY (`reportTS`)
// );
//
//
// NEW DEFINITIONS, RIGHT SIZE FOR cenKey
//
// CREATE TABLE `CENKeys` (
// `cenKey` varchar(64) DEFAULT "",
// `reportID` varchar(64) DEFAULT "",
// `reportTS` int,
// PRIMARY KEY (`cenKey`, `reportID`),
// KEY (`reportID`),
// KEY (`reportTS`),
// KEY (`cenKey`)
// );
//
// CREATE TABLE `CENReport` (
// `reportID` varchar(64) DEFAULT "",
// `report` varchar(4000) DEFAULT "",
// `reportMimeType` varchar(64) DEFAULT "",
// `reportTS` int,
// PRIMARY KEY (`reportID`),
// KEY (`reportTS`)
// );
//
//

// CENReport payload is sent by client to /cenreport when user reports symptoms
type CENReport struct {
ReportID string `json:"reportID,omitempty"`
Report []byte `json:"report,omitempty"` // this is expected to be a JSON blob but the server doesn't need to parse it
CENKeys string `json:"cenKeys,omitempty"` // comma separated list of base64 AES Keys
CENKeys string `json:"cenKeys,omitempty"` // comma separated list of hex AES-128 Keys (COMMENT WAS: base64 AES Keys)
brian-peter-dickson marked this conversation as resolved.
Show resolved Hide resolved
ReportMimeType string `json:"reportMimeType,omitempty"`
ReportTimeStamp uint64 `json:"reportTimeStamp,omitempty"`
}
Expand Down Expand Up @@ -72,22 +120,21 @@ func (backend *Backend) ProcessCENReport(cenReport *CENReport) (err error) {
return err
}

curTS := uint64(time.Now().Unix())
reportID := fmt.Sprintf("%x", Computehash(reportData))
cenKeys := strings.Split(cenReport.CENKeys, ",")
// store the cenreportID in cenkeys table, one row per key
for _, cenKey := range cenKeys {
cenKey := strings.Trim(cenKey, " \n")
if len(cenKey) > 30 && len(cenKey) <= 32 {
_, err = stmtKeys.Exec(cenKey, reportID, curTS)
if len(cenKey) > 62 && len(cenKey) <= 64 {
_, err = stmtKeys.Exec(cenKey, reportID, cenReport.ReportTimeStamp)
if err != nil {
return err
}
}
}

// store the cenreportID in cenReport table, one row per key
_, err = stmtReport.Exec(reportID, cenReport.Report, cenReport.ReportMimeType, curTS)
_, err = stmtReport.Exec(reportID, cenReport.Report, cenReport.ReportMimeType, cenReport.ReportTimeStamp)
if err != nil {
panic(5)
return err
Expand Down Expand Up @@ -161,7 +208,7 @@ func Computehash(data ...[]byte) []byte {
}

func makeCENKeyString() string {
key := make([]byte, 16)
key := make([]byte, 32)
rand.Read(key)
encoded := fmt.Sprintf("%x", key)
return encoded
Expand All @@ -174,9 +221,11 @@ func GetSampleCENReportAndCENKeys(nKeys int) (cenReport *CENReport, cenKeys []st
cenKeys[i] = makeCENKeyString()
}
CENKeys := fmt.Sprintf("%s,%s", cenKeys[0], cenKeys[1])
curTS := uint64(time.Now().Unix())
cenReport = new(CENReport)
cenReport.ReportID = "1"
cenReport.Report = []byte("severe fever,coughing,hard to breathe")
cenReport.CENKeys = CENKeys
cenReport.ReportTimeStamp = curTS
return cenReport, cenKeys
}
23 changes: 23 additions & 0 deletions backend/cen2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
DROP TABLE IF EXISTS CENKeys;
brian-peter-dickson marked this conversation as resolved.
Show resolved Hide resolved
DROP TABLE IF EXISTS CENReport;

CREATE TABLE `CENKeys` (
`cenKey` varchar(64) DEFAULT "",
`reportID` varchar(64) DEFAULT "",
`reportTS` int,
PRIMARY KEY (`cenKey`, `reportID`),
KEY (`reportID`),
KEY (`reportTS`),
KEY (`cenKey`)
);

CREATE TABLE `CENReport` (
`reportID` varchar(64) DEFAULT "",
`report` varchar(4000) DEFAULT "",
`reportMimeType` varchar(64) DEFAULT "",
`reportTS` int,
PRIMARY KEY (`reportID`),
KEY (`reportTS`)
);