-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitems.js
More file actions
170 lines (148 loc) · 6.04 KB
/
items.js
File metadata and controls
170 lines (148 loc) · 6.04 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
const dotenv = require('dotenv')
const child_process = require('child_process')
const crypto = require('crypto')
const fs = require('fs')
const log = require('./logger')(__filename)
exports.getItem = ({ items, name }) => {
for (let i = 0; i < items.length; i++) {
const item = items[i]
if (item.name === name) {
return item
}
}
throw Error(`getItem: Could not find any item named '${name}'`)
}
exports.getNotes = ({ items, name, systemdEscape }) => {
try {
const item = exports.getItem({ items, name })
if (systemdEscape === true) {
return JSON.stringify(item.notes)
}
return item.notes
} catch (error) {
throw Error(`getItem: Could not get item.notes from item name '${name}'`)
}
}
exports.getItems = ({ variables }) => {
const env = Object.assign({}, process.env, {
BW_CLIENTID: variables[0],
BW_CLIENTSECRET: variables[1],
BW_PASSWORD: variables[2],
})
try {
child_process.execSync('bw logout', { stdio: 'ignore' })
} catch (error) {
// Ignoring this error, 'bw logout' is run only to prevent a login attempt
// below on an already logged in Bitwarden client.
}
let items = []
try {
child_process.execSync('bw login --apikey', { env, encoding: 'utf8' })
env.BW_SESSION = child_process.execSync('bw unlock --raw --passwordenv BW_PASSWORD', { env, encoding: 'utf8' })
child_process.execSync('bw sync', { env, encoding: 'utf8' })
const result = child_process.execSync('bw list items', { env, encoding: 'utf8' })
items = JSON.parse(result)
child_process.execSync('bw logout')
} catch (error) {
log.error({ error, channel: 'operations' })
child_process.execSync('bw logout')
throw Error(`getItems: Could not get items for '${variables?.[0]}'`)
}
if (items.length === 0) {
throw Error(`getItems: items.length === 0 for '${variables?.[0]}'`)
}
return items
}
exports.setItems = ({ index, items }) => {
const variables = JSON.parse(exports.getNotes({ items: exports.items.operations, name: 'variables' }))[index]
const env = Object.assign({}, process.env, {
BW_CLIENTID: variables[0],
BW_CLIENTSECRET: variables[1],
BW_PASSWORD: variables[2],
})
try {
child_process.execSync('bw logout', { stdio: 'ignore' })
} catch (error) {
// Ignoring this error, 'bw logout' is run only to prevent a login attempt
// below on an already logged in Bitwarden client.
}
let item
try {
child_process.execSync('bw login --apikey', { env, encoding: 'utf8' })
env.BW_SESSION = child_process.execSync('bw unlock --raw --passwordenv BW_PASSWORD', { env, encoding: 'utf8' })
for (let i = 0; i < items.length; i++) {
item = items[i]
child_process.execSync( `bw edit item ${item.id} ${Buffer.from(JSON.stringify(item)).toString('base64')}`, { env, encoding: 'utf8' })
}
child_process.execSync('bw logout')
} catch (error) {
const message = `setItems: Could not set items. Current item name '${item?.name}'`
log.error({ error, message, channel: 'operations' })
child_process.execSync('bw logout')
throw Error(message)
}
}
exports.items = {}
exports.validations = {
certificates: {},
development: {},
operations: {},
}
function backup({ data }) {
const key = crypto.createHash('sha512').update(process.env.ALIAJS_VARIABLE_2).digest('hex').substring(0, 32)
const encryptionIV = crypto.randomBytes(16)
const cipher = crypto.createCipheriv('aes-256-cbc', key, encryptionIV)
const final = `${encryptionIV.toString('hex')}:${cipher.update(data, 'utf8', 'hex')}${cipher.final('hex')}`
fs.writeFileSync(`../.aliajs-sauce-backup-9f6e7ec1`, final)
fs.writeFileSync(`../.aliajs-sauce-backup-9f6e7ec1-${Date.now()}`, final)
validate()
}
function restore() {
const data = fs.readFileSync(`../.aliajs-sauce-backup-9f6e7ec1`).toString('utf8').split(':')
const key = crypto.createHash('sha512').update(process.env.ALIAJS_VARIABLE_2).digest('hex').substring(0, 32)
const encryptionIV = Buffer.from(data[0], 'hex')
const decipher = crypto.createDecipheriv('aes-256-cbc', key, encryptionIV)
const final = `${decipher.update(data[1], 'hex', 'utf8')}${decipher.final('utf8')}`
exports.items = JSON.parse(final)
validate()
}
function validate() {
for (const name in exports.validations) {
if (exports.getNotes({ items: exports.items[name], name: 'restore_validation' }) !== process.env.RESTORE_VALIDATION) {
throw Error(`restore: Validations failed, could not validate 'restore_validation' notes for ${name}`)
}
}
}
function variables() {
const parsed = dotenv.parse(fs.readFileSync(`${__dirname}/../.env`))
for (let key in parsed) {
if (process.env[key] === '') {
process.env[key] = exports.getItem({ items: exports.items.operations, name: key }).notes
}
}
}
try {
// Development items
// restore()
// exports.items.operations.variables = JSON.parse(exports.getNotes({ items: exports.items.operations, name: 'variables' }))
// console.log(exports.items)
// Production items
exports.items.operations = exports.getItems({ variables: [process.env.ALIAJS_VARIABLE_0, process.env.ALIAJS_VARIABLE_1, process.env.ALIAJS_VARIABLE_2] })
exports.items.operations.variables = JSON.parse(exports.getNotes({ items: exports.items.operations, name: 'variables' }))
exports.items.development = exports.getItems({ variables: exports.items.operations.variables[1] })
exports.items.certificates = exports.getItems({ variables: exports.items.operations.variables[2] })
backup({ data: JSON.stringify(exports.items) })
// console.log(exports.items)
variables()
} catch (error) {
const message = '<!channel> items: Error exporting items'
log.error({ error, message, syncF2f8844b: true, channel: 'operations' })
throw Error(message)
// Uncomment the restore() lines below only in case of a catastrophic event.
// try {
// restore()
// } catch (error) {
// log.error({ error, message: 'Fatal error! Could not get the sauce. Followed by a critical error: Could not restore the sauce. Process will exit in 1600 milliseconds.', channel: 'operations' })
// process.exit(1)
// }
}