Change how Firebase scripts auth (#87)

Now the admin key path is loaded from enviroment variables instead
of being hardcoded in script-init.ts.
This commit is contained in:
Marshall Polaris 2022-04-19 21:31:46 -07:00 committed by GitHub
parent 987e36c357
commit 5dbe16ec8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 76 additions and 44 deletions

View File

@ -1,15 +1,8 @@
import * as admin from 'firebase-admin'
import * as _ from 'lodash'
// Generate your own private key, and set the path below:
// https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
// const serviceAccount = require('../../../../../../Downloads/dev-mantic-markets-firebase-adminsdk-sir5m-b2d27f8970.json')
const serviceAccount = require('../../../../../../Downloads/mantic-markets-firebase-adminsdk-1ep46-351a65eca3.json')
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
})
import { initAdmin } from './script-init'
initAdmin()
import { getUserByUsername } from '../utils'
import { changeUser } from '../change-user-info'

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('stephen')
initAdmin()
import { Bet } from '../../../common/bet'
import { getDpmProbability } from '../../../common/calculate-dpm'

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('stephen')
initAdmin()
import { PrivateUser, STARTING_BALANCE, User } from '../../../common/user'

View File

@ -3,7 +3,7 @@ import * as _ from 'lodash'
import * as fs from 'fs'
import { initAdmin } from './script-init'
initAdmin('james')
initAdmin()
import { Bet } from '../../../common/bet'
import { Contract } from '../../../common/contract'

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('james')
initAdmin()
import { getValues } from '../utils'
import { Fold } from '../../../common/fold'

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('james')
initAdmin()
import { Contract } from '../../../common/contract'

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('james')
initAdmin()
import { Bet } from '../../../common/bet'
import { Contract } from '../../../common/contract'

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('stephenDev')
initAdmin()
import {
Binary,

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('stephenDev')
initAdmin()
import { Binary, Contract, DPM, FullContract } from '../../../common/contract'
import { Bet } from '../../../common/bet'

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('james')
initAdmin()
import { Bet } from '../../../common/bet'
import { Contract } from '../../../common/contract'

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('james')
initAdmin()
import { Bet } from '../../../common/bet'
import { Contract } from '../../../common/contract'

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('james')
initAdmin()
import { Bet } from '../../../common/bet'
import { Contract } from '../../../common/contract'

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('stephenDev')
initAdmin()
import { Contract } from '../../../common/contract'
import { getValues } from '../utils'

View File

@ -1,32 +1,71 @@
import * as path from 'path'
import * as fs from 'fs'
import * as os from 'os'
import * as admin from 'firebase-admin'
// Generate your own private key, and set the path below:
// Prod:
// https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
// Dev:
// https://console.firebase.google.com/u/0/project/dev-mantic-markets/settings/serviceaccounts/adminsdk
// First, generate a private key from the Google service account management page:
// Prod: https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
// Dev: https://console.firebase.google.com/u/0/project/dev-mantic-markets/settings/serviceaccounts/adminsdk
// Then set GOOGLE_ACCOUNT_CREDENTIALS_PROD or GOOGLE_ACCOUNT_CREDENTIALS_DEV to the path of the key.
const pathsToPrivateKey = {
james:
'/Users/jahooma/mantic-markets-firebase-adminsdk-1ep46-820891bb87.json',
jamesDev:
'/Users/jahooma/dev-mantic-markets-firebase-adminsdk-sir5m-f38cdbee37.json',
stephen:
'../../../../../../Downloads/mantic-markets-firebase-adminsdk-1ep46-351a65eca3.json',
stephenDev:
'../../../../../../Downloads/dev-mantic-markets-firebase-adminsdk-sir5m-b2d27f8970.json',
// Then, to run a script, make sure you are pointing at the Firebase you intend to:
// $ firebase use dev (or prod)
//
// Followed by, if you have https://github.com/TypeStrong/ts-node installed (recommended):
// $ ts-node my-script.ts
//
// Or compile it and run the compiled version:
// $ yarn build && ../../lib/functions/scripts/src/my-script.js
const getFirebaseProjectRoot = (cwd: string) => {
// see https://github.com/firebase/firebase-tools/blob/master/src/detectProjectRoot.ts
let dir = cwd
while (!fs.existsSync(path.resolve(dir, './firebase.json'))) {
const parentDir = path.dirname(dir)
if (parentDir === dir) {
return null
}
dir = parentDir
}
return dir
}
export const initAdmin = (who: keyof typeof pathsToPrivateKey) => {
const serviceAccount = require(pathsToPrivateKey[who])
const getFirebaseActiveProject = (cwd: string) => {
// firebase uses this configstore package https://github.com/yeoman/configstore/blob/main/index.js#L9
const projectRoot = getFirebaseProjectRoot(cwd)
if (projectRoot == null) {
return null
}
const xdgConfig =
process.env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config')
const configPath = path.join(xdgConfig, 'configstore', 'firebase-tools.json')
try {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'))
return config['activeProjects'][projectRoot]
} catch (e) {
return null
}
}
export const initAdmin = (env?: string) => {
env = env || getFirebaseActiveProject(process.cwd())
if (env == null) {
console.error(
"Couldn't find active Firebase project; did you do `firebase use <alias>?`"
)
return
}
const envVar = `GOOGLE_AUTHENTICATION_CREDENTIALS_${env.toUpperCase()}`
const keyPath = process.env[envVar]
if (keyPath == null) {
console.error(
`Please set the ${envVar} environment variable to contain the path to your ${env} environment key file.`
)
return
}
console.log(`Initializing connection to ${env} Firebase...`)
const serviceAccount = require(keyPath)
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
})
}
// Then:
// yarn watch (or yarn build)
// firebase use dev (or firebase use prod)
// Run script:
// node lib/functions/src/scripts/update-contract-tags.js

View File

@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
import * as _ from 'lodash'
import { initAdmin } from './script-init'
initAdmin('jamesDev')
initAdmin()
import { Contract } from '../../../common/contract'
import { parseTags } from '../../../common/util/parse'