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 admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
// Generate your own private key, and set the path below:
|
import { initAdmin } from './script-init'
|
||||||
// https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
|
initAdmin()
|
||||||
|
|
||||||
// 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 { getUserByUsername } from '../utils'
|
import { getUserByUsername } from '../utils'
|
||||||
import { changeUser } from '../change-user-info'
|
import { changeUser } from '../change-user-info'
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('stephen')
|
initAdmin()
|
||||||
|
|
||||||
import { Bet } from '../../../common/bet'
|
import { Bet } from '../../../common/bet'
|
||||||
import { getDpmProbability } from '../../../common/calculate-dpm'
|
import { getDpmProbability } from '../../../common/calculate-dpm'
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('stephen')
|
initAdmin()
|
||||||
|
|
||||||
import { PrivateUser, STARTING_BALANCE, User } from '../../../common/user'
|
import { PrivateUser, STARTING_BALANCE, User } from '../../../common/user'
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ import * as _ from 'lodash'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('james')
|
initAdmin()
|
||||||
|
|
||||||
import { Bet } from '../../../common/bet'
|
import { Bet } from '../../../common/bet'
|
||||||
import { Contract } from '../../../common/contract'
|
import { Contract } from '../../../common/contract'
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('james')
|
initAdmin()
|
||||||
|
|
||||||
import { getValues } from '../utils'
|
import { getValues } from '../utils'
|
||||||
import { Fold } from '../../../common/fold'
|
import { Fold } from '../../../common/fold'
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('james')
|
initAdmin()
|
||||||
|
|
||||||
import { Contract } from '../../../common/contract'
|
import { Contract } from '../../../common/contract'
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('james')
|
initAdmin()
|
||||||
|
|
||||||
import { Bet } from '../../../common/bet'
|
import { Bet } from '../../../common/bet'
|
||||||
import { Contract } from '../../../common/contract'
|
import { Contract } from '../../../common/contract'
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('stephenDev')
|
initAdmin()
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Binary,
|
Binary,
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('stephenDev')
|
initAdmin()
|
||||||
|
|
||||||
import { Binary, Contract, DPM, FullContract } from '../../../common/contract'
|
import { Binary, Contract, DPM, FullContract } from '../../../common/contract'
|
||||||
import { Bet } from '../../../common/bet'
|
import { Bet } from '../../../common/bet'
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('james')
|
initAdmin()
|
||||||
|
|
||||||
import { Bet } from '../../../common/bet'
|
import { Bet } from '../../../common/bet'
|
||||||
import { Contract } from '../../../common/contract'
|
import { Contract } from '../../../common/contract'
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('james')
|
initAdmin()
|
||||||
|
|
||||||
import { Bet } from '../../../common/bet'
|
import { Bet } from '../../../common/bet'
|
||||||
import { Contract } from '../../../common/contract'
|
import { Contract } from '../../../common/contract'
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('james')
|
initAdmin()
|
||||||
|
|
||||||
import { Bet } from '../../../common/bet'
|
import { Bet } from '../../../common/bet'
|
||||||
import { Contract } from '../../../common/contract'
|
import { Contract } from '../../../common/contract'
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as admin from 'firebase-admin'
|
||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('stephenDev')
|
initAdmin()
|
||||||
|
|
||||||
import { Contract } from '../../../common/contract'
|
import { Contract } from '../../../common/contract'
|
||||||
import { getValues } from '../utils'
|
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'
|
import * as admin from 'firebase-admin'
|
||||||
|
|
||||||
// Generate your own private key, and set the path below:
|
// First, generate a private key from the Google service account management page:
|
||||||
// Prod:
|
// Prod: https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
|
||||||
// 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
|
||||||
// Dev:
|
// Then set GOOGLE_ACCOUNT_CREDENTIALS_PROD or GOOGLE_ACCOUNT_CREDENTIALS_DEV to the path of the key.
|
||||||
// https://console.firebase.google.com/u/0/project/dev-mantic-markets/settings/serviceaccounts/adminsdk
|
|
||||||
|
|
||||||
const pathsToPrivateKey = {
|
// Then, to run a script, make sure you are pointing at the Firebase you intend to:
|
||||||
james:
|
// $ firebase use dev (or prod)
|
||||||
'/Users/jahooma/mantic-markets-firebase-adminsdk-1ep46-820891bb87.json',
|
//
|
||||||
jamesDev:
|
// Followed by, if you have https://github.com/TypeStrong/ts-node installed (recommended):
|
||||||
'/Users/jahooma/dev-mantic-markets-firebase-adminsdk-sir5m-f38cdbee37.json',
|
// $ ts-node my-script.ts
|
||||||
stephen:
|
//
|
||||||
'../../../../../../Downloads/mantic-markets-firebase-adminsdk-1ep46-351a65eca3.json',
|
// Or compile it and run the compiled version:
|
||||||
stephenDev:
|
// $ yarn build && ../../lib/functions/scripts/src/my-script.js
|
||||||
'../../../../../../Downloads/dev-mantic-markets-firebase-adminsdk-sir5m-b2d27f8970.json',
|
|
||||||
|
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 getFirebaseActiveProject = (cwd: string) => {
|
||||||
const serviceAccount = require(pathsToPrivateKey[who])
|
// 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({
|
admin.initializeApp({
|
||||||
credential: admin.credential.cert(serviceAccount),
|
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 * as _ from 'lodash'
|
||||||
|
|
||||||
import { initAdmin } from './script-init'
|
import { initAdmin } from './script-init'
|
||||||
initAdmin('jamesDev')
|
initAdmin()
|
||||||
|
|
||||||
import { Contract } from '../../../common/contract'
|
import { Contract } from '../../../common/contract'
|
||||||
import { parseTags } from '../../../common/util/parse'
|
import { parseTags } from '../../../common/util/parse'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user