Skip to content

Commit

Permalink
starting off with a database bonanza (#126)
Browse files Browse the repository at this point in the history
* starting off with a database bonanza

* readme updates

* Delete .eslintrc.js

a test, yes

* Create .eslintrc.js

* Update .eslintrc.js

* Delete .eslintrc.js

* updates

* updates for #127 and #130

* more of #130 changes

* updates for #128

* removing graphs temporarily for #30

* more for #128, #129
new tests required

* shrinkwraps v4.6.0
  • Loading branch information
jjjpanda authored Dec 30, 2021
1 parent 41f0ace commit d73eca5
Show file tree
Hide file tree
Showing 30 changed files with 2,288 additions and 886 deletions.
30 changes: 7 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,24 @@ Massive Dependencies:
2. [ffmpeg](https://ffmpeg.org) - should be installed on same machine as [livestream](livestream) and [storage](storage).
3. [heartbeat](https://github.com/jjjpanda/heartbeat) - used to confirm server is still up.
4. [object](https://github.com/jjjpanda/object) - used to detect objects (still not implemented within start script).
5. [postgres](https://www.postgresql.org) - the database

## Quick Start

### 1. Install motion and ffmpeg
### 1. Install motion, ffmpeg, and postgres
```
sudo apt-get install motion ffmpeg
sudo apt-get install motion ffmpeg postgresql
```
Then, set up a conf for motion with all of your cameras.

*Or however you need to download it on your machine*

Then, set up a conf for **motion** with all of your cameras. Then, set up **postgres** with a database, port, user, and password of your choosing. **Motion** will also need postgres details in it's conf as well. [*See storage for details.*](storage)
### 2. Installing NPM dependencies

If running all services on one machine:
```
npm install --no-optional
```

If splitting services, you can install each service with:
```
npm run install:<service>
```
or
```
cd <service> && npm install
```
### 3. Create Environment Variables File

Copy the example env into an .env dotfile:
Expand All @@ -50,17 +45,6 @@ cp env.example .env

Fill in the .env with all the info listed ( for optional fields, leave blank after the "=" ).

Then, if running any service separate run:
```
npm run validate:env && npm run validate:pass
```
This is will split the .env into multiple .env's for the respective services, then create the hash file for passwords.

If running gateway and using https run:
```
npm run validate:acme
```

### 4. Start Chimera

If running all or more than one service(s)
Expand Down
19 changes: 0 additions & 19 deletions chimera/generateEmptyPassFile.js

This file was deleted.

36 changes: 36 additions & 0 deletions chimera/prepareDatabase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require("dotenv").config()
const Pool = require('pg').Pool
const pool = new Pool({
user: process.env.database_USER,
host: process.env.database_HOST,
database: process.env.database_NAME,
password: process.env.database_PASSWORD,
port: process.env.database_PORT,
})

const creationTasks = [
{
query: `CREATE TABLE frame_files(ID SERIAL PRIMARY KEY, timestamp TIMESTAMP, camera NUMERIC(10), name VARCHAR, size NUMERIC);`,
description: "frame files table"
},
{
query: `CREATE TABLE frame_deletes(ID SERIAL PRIMARY KEY, timestamp TIMESTAMP, camera NUMERIC(10), size NUMERIC, count NUMERIC);`,
description: "frame deletions table"
},
{
query: `CREATE TABLE auth(ID SERIAL PRIMARY KEY, username VARCHAR(10) UNIQUE, hash VARCHAR);`,
description: "authorization table"
}
]

Promise.allSettled(creationTasks.map(({query}) => {
return pool.query(query)
})).then(values => {
let issues = false
values.forEach((value, index) => {
const tableExists = value.status == "fulfilled" || (value.status == "rejected" && value.reason && value.reason.code == `42P07`)
if(!tableExists) issues = true
console.log(`${creationTasks[index].description} ${tableExists ? `✔️` : `❌`}`)
})
process.exit(issues ? 1 : 0)
})
19 changes: 11 additions & 8 deletions chimera/register.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
require("dotenv").config()
const { auth } = require("lib")

const Pool = require('pg').Pool
const pool = new Pool({
user: process.env.database_USER,
host: process.env.database_HOST,
database: process.env.database_NAME,
password: process.env.database_PASSWORD,
port: process.env.database_PORT,
})

const stdout = process.stdout
const stdin = process.stdin

Expand Down Expand Up @@ -60,7 +69,7 @@ const exit = (retyped) => {
}

const closeOut = (bypass=false) => {
auth.register(bypass ? "" : input, () => {
auth.register(bypass ? "" : input, pool, () => {
process.exit(0)
}, (msg) => {
console.log(msg)
Expand Down Expand Up @@ -89,13 +98,7 @@ const backspace = (retyped) => {
}
}

const seconds = 30
setInterval(() => {
enter(null, true)
}, seconds * 1000)

stdout.write(`Proceeding with password file in ${seconds} seconds`)
stdout.write("\nPassword:")
stdout.write("\nCreate Chimera Password:")
stdin.setRawMode(true)
stdin.resume()
stdin.setEncoding("utf-8")
Expand Down
19 changes: 15 additions & 4 deletions chimera/splitAndValidateEnvVars.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ env.storage += writeVarLine("cameras")
env.lib += writeVarLine("alert_URL")
env.lib += writeVarLine("admin_alert_URL")

env.command += writeVarLine("password_FILEPATH")
env.lib += writeVarLine("password_FILEPATH")
confirmPath("password_FILEPATH")

env.command += writeVarLine("templink_PIN")
env.lib += writeVarLine("templink_PIN")

Expand Down Expand Up @@ -147,6 +143,21 @@ env.memory += writeVarLine("memory_PORT")
env.memory += writeVarLine("memory_HOST")
env.memory += writeVarLine("memory_AUTH_TOKEN")

env.storage += writeVarLine("database_NAME")
env.command += writeVarLine("database_NAME")

env.storage += writeVarLine("database_USER")
env.command += writeVarLine("database_USER")

env.storage += writeVarLine("database_PASSWORD")
env.command += writeVarLine("database_PASSWORD")

env.storage += writeVarLine("database_HOST")
env.command += writeVarLine("database_HOST")

env.storage += writeVarLine("database_PORT")
env.command += writeVarLine("database_PORT")

if(allEnvPresent){
Promise.all(Object.entries(env)
.map(([key, value]) => new Promise((resolve, reject) => {
Expand Down
20 changes: 20 additions & 0 deletions command/__mocks__/pg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const bcrypt = jest.requireActual("bcryptjs")
const hashedMockedPassword = bcrypt.hashSync('mockedPassword', bcrypt.genSaltSync(10))

const queryObject = {
query: (str, callback) => callback(null, {rows: [{hash: hashedMockedPassword}]})
}
const mockedPool = {
connect: () => {
return queryObject
},
...queryObject,
end: jest.fn(),
on: jest.fn()
}

const pg = {
Pool: jest.fn(() => mockedPool)
}

module.exports = pg
18 changes: 13 additions & 5 deletions command/backend/routes/lib/auth.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
const secretKey = process.env.SECRETKEY
const jwt = require("jsonwebtoken")
const bcrypt = require("bcryptjs")
const fs = require("fs")
const path = require("path")
const {randomID, webhookAlert} = require('lib')

const Pool = require('pg').Pool
const pool = new Pool({
user: process.env.database_USER,
host: process.env.database_HOST,
database: process.env.database_NAME,
password: process.env.database_PASSWORD,
port: process.env.database_PORT,
})

const client = require("memory").client("AUTHORIZATION")

module.exports = {
Expand All @@ -17,8 +24,9 @@ module.exports = {
next()
}
else{
fs.readFile(path.join(process.env.password_FILEPATH), {encoding:"utf8", flag:"r"}, (err, hash) => {
if(!err){
pool.query(`SELECT hash FROM auth WHERE username = 'admin'`, (err, values) => {
if (!err && values.rows.length > 0 && values.rows[0].hash) {
const {hash} = values.rows[0]
bcrypt.compare(password == undefined ? "" : password, hash, (err, success) => {
if(!err && success){
next()
Expand All @@ -29,7 +37,7 @@ module.exports = {
})
}
else{
console.log(`NO HASH FILE AT ${process.env.password_FILEPATH}`)
console.log(`NO HASH`)
res.status(400).json({ error: true, errors: "Password Unable to Be Verified" })
}
})
Expand Down
4 changes: 2 additions & 2 deletions command/frontend/app/FileStats.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class FileStats extends React.Component {

{/* <ServerProcess key={`server${this.state.lastUpdated}`}/> */}

<ResponsiveContainer width="100%" height={300}>
{/* <ResponsiveContainer width="100%" height={300}>
<LineChart width={730} height={250} data={this.state.countStats}
margin={{ top: 5, right: 30, left: 20, bottom: 5 }}>
<CartesianGrid strokeDasharray="3 3" />
Expand All @@ -221,7 +221,7 @@ class FileStats extends React.Component {
return <Line type="monotone" dataKey={name} stroke={colorArray[index]} />
})}
</LineChart>
</ResponsiveContainer>
</ResponsiveContainer> */}

<List >
{this.state.cameras.map(cam => {
Expand Down
Loading

0 comments on commit d73eca5

Please sign in to comment.