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:
parent
987e36c357
commit
5dbe16ec8d
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
|||
import * as _ from 'lodash'
|
||||
|
||||
import { initAdmin } from './script-init'
|
||||
initAdmin('stephenDev')
|
||||
initAdmin()
|
||||
|
||||
import {
|
||||
Binary,
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Reference in New Issue
Block a user