Skip to content

Commit

Permalink
Fix URL construction and API call with basic auth, fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
eablack committed Mar 29, 2024
1 parent 7df11c1 commit 76765b1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
17 changes: 10 additions & 7 deletions packages/cli/src/commands/pg/credentials/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ export default class Url extends Command {
const {app, name} = flags
const {database} = args
const db = await getAddon(this.heroku, app, database)
console.log('db', db)
if (legacyEssentialPlan(db) && name !== 'default') {
throw new Error('Legacy Essential-tier databases do not support named credentials.')
}

const {body: credInfo} = await this.heroku.get<CredentialsInfo>(
`/postgres/v0/databases/${db.name}/credentials/${encodeURIComponent(name)}`,
{hostname: pgHost()},
{
hostname: pgHost(),
headers: {
Authorization: `Basic ${Buffer.from(`:${this.heroku.auth}`).toString('base64')}`,
},
},
)
const activeCreds = credInfo.credentials.find(c => c.state === 'active')
if (!activeCreds) {
Expand All @@ -48,20 +52,19 @@ export default class Url extends Command {
}, {
user: activeCreds?.user, password: activeCreds?.password,
})
const connUrl = new URL(`/${creds.database}`)
connUrl.host = `${creds.host}:${creds.port}`
const connUrl = new URL(`postgres://${creds.host}/${creds.database}`)
connUrl.port = creds.port.toString()
if (creds.user && creds.password) {
connUrl.username = creds.user
connUrl.password = creds.password
}

connUrl.protocol = 'postgres:'
ux.log(heredoc(`
Connection information for ${color.yellow(name)} credential.
Connection info string:
"dbname=${creds.database} host=${creds.host} port=${creds.port} user=${creds.user} password=${creds.password} sslmode=require"
"dbname=${creds.database} host=${creds.host} port=${creds.port} user=${creds.user} password=${creds.password} sslmode=require"
Connection URL:
${connUrl}
${connUrl}
`))
}
}
16 changes: 7 additions & 9 deletions packages/cli/test/unit/commands/pg/credentials/url.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {stdout} from 'stdout-stderr'
import Cmd from '../../../../../src/commands/pg/credentials/destroy'
import Cmd from '../../../../../src/commands/pg/credentials/url'
import runCommand from '../../../../helpers/runCommand'
import * as nock from 'nock'
import expectOutput from '../../../../helpers/utils/expectOutput'
Expand Down Expand Up @@ -47,10 +47,9 @@ describe('pg:credentials:url', () => {
expectOutput(stdout.output, heredoc(`
Connection information for gandalf credential.
Connection info string:
"dbname=d123 host=localhost port=5442 user=gandalf
password=hunter2 sslmode=require"
"dbname=d123 host=localhost port=5442 user=gandalf password=hunter2 sslmode=require"
Connection URL:
postgres://gandalf:hunter2@localhost:5442/d123
postgres://gandalf:hunter2@localhost:5442/d123
`))
})

Expand Down Expand Up @@ -97,10 +96,9 @@ describe('pg:credentials:url', () => {
expectOutput(stdout.output, heredoc(`
Connection information for lucy credential.
Connection info string:
"dbname=d123 host=localhost
port=5442 user=lucy password=hunter2 sslmode=require"
"dbname=d123 host=localhost port=5442 user=lucy password=hunter2 sslmode=require"
Connection URL:
postgres://lucy:hunter2@localhost:5442/d123
postgres://lucy:hunter2@localhost:5442/d123
`))
})

Expand Down Expand Up @@ -129,9 +127,9 @@ describe('pg:credentials:url', () => {
expectOutput(stdout.output, heredoc(`
Connection information for default credential.
Connection info string:
"dbname=d123 host=localhost port=5442 user=abcdef password=hunter2 sslmode=require"
"dbname=d123 host=localhost port=5442 user=abcdef password=hunter2 sslmode=require"
Connection URL:
postgres://abcdef:hunter2@localhost:5442/d123
postgres://abcdef:hunter2@localhost:5442/d123
`))
})
})

0 comments on commit 76765b1

Please sign in to comment.