Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FE-5990 Set argv.secret to 'secret' when --local passed in and --secret is not. Always respect argv.secret when getting a secret with FaunaClient.getSecret. #449

Merged
merged 8 commits into from
Dec 3, 2024
2 changes: 1 addition & 1 deletion src/commands/query.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function queryCommand(argv) {

// get the query handler and run the query
try {
const secret = await getSecret();
const secret = await getSecret({ secretArg: argv.secret });
const { url, timeout, typecheck, extra, json, apiVersion } = argv;
const results = await container.resolve("runQueryFromString")(expression, {
apiVersion,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/shell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async function buildCustomEval(argv) {

let res;
try {
const secret = await getSecret();
const secret = await getSecret({ secretArg: argv.secret });
const { url, timeout, typecheck } = argv;
res = await runQueryFromString(cmd, {
apiVersion,
Expand Down
6 changes: 3 additions & 3 deletions src/lib/auth/accountKeys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class AccountKeys {
// Let users know if the creds they provided are invalid (empty)
if (!key && keySource !== "credentials-file") {
throw new Error(
`The account key provided by ${keySource} is invalid. Please provide an updated value.`,
`The account key provided by '${keySource}' is invalid. Please provide an updated value.`,
);
}
}
Expand Down Expand Up @@ -56,7 +56,7 @@ export class AccountKeys {
*/
promptLogin() {
throw new Error(
`The requested user ${this.user || ""} is not signed in or has expired.\nPlease re-authenticate\n\n
`The requested user '${this.user || ""}' is not signed in or has expired.\nPlease re-authenticate\n\n
To sign in, run:\n\nfauna login\n
`,
);
Expand All @@ -69,7 +69,7 @@ export class AccountKeys {
async onInvalidCreds() {
if (this.keySource !== "credentials-file") {
throw new Error(
`Account key provided by ${this.keySource} is invalid. Please provide an updated account key.`,
`Account key provided by '${this.keySource}' is invalid. Please provide an updated account key.`,
);
}
await this.refreshKey();
Expand Down
6 changes: 5 additions & 1 deletion src/lib/command-helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,18 @@ function yargsWithCommonOptions(yargs, options) {
return yargs
.options({ ...options, })
.check((argv) => {
// If --local is provided and --url is not, set the default URL for local
// If --local is provided and --url is not, set argv.url to "http://localhost:8443"
if (!argv.url) {
if (argv.local) {
argv.url = 'http://localhost:8443';
} else {
argv.url = 'https://db.fauna.com';
}
}
// if --local is provided and --secret is not set argv.secret to "secret"
if (!argv.secret && argv.local) {
argv.secret = 'secret';
}
return true; // Validation passed
});
}
12 changes: 11 additions & 1 deletion src/lib/fauna-client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ export default class FaunaClient {
}
}

export const getSecret = async () => {
/**
* Gets a secret. Defaults to the secretArg. If the secretArg is undefined
* fetches a secret for the current credentials.
* @param opts {Object}
* @param opts.secretArg {string} The secret passed as an argument, if any.
* @return {Promise<string>} the secret
*/
export async function getSecret({ secretArg }) {
if (secretArg !== undefined) {
return secretArg;
}
const credentials = container.resolve("credentials");
if (!credentials.databaseKeys.key) {
return await credentials.databaseKeys.getOrRefreshKey();
Expand Down
34 changes: 32 additions & 2 deletions test/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe("configuration file", function () {
});
});

it("--local arg sets the url to http://localhost:8443 if no URL is given", async function () {
it("--local arg sets the argv.url to http://localhost:8443 if no --url is given", async function () {
fs.readdirSync.withArgs(process.cwd()).returns([]);
await runBasicTest({
cmd: `eval "Database.all()" --secret "no-config" --local`,
Expand All @@ -213,7 +213,7 @@ describe("configuration file", function () {
});
});

it("--url arg takes precedence over --local arg for the URL", async function () {
it("--url arg takes precedence over --local arg for the argv.url", async function () {
fs.readdirSync.withArgs(process.cwd()).returns([]);
await runBasicTest({
cmd: `eval "Database.all()" --secret "no-config" --local --url http://localhost:hibob`,
Expand All @@ -228,6 +228,36 @@ describe("configuration file", function () {
});
});

it("--local sets the argv.secret to 'secret' if no --secret is given", async function () {
fs.readdirSync.withArgs(process.cwd()).returns([]);
await runBasicTest({
cmd: `eval "Database.all()" --local`,
argvMatcher: sinon.match({
apiVersion: "10",
secret: "secret",
url: "http://localhost:8443",
timeout: 5000,
typecheck: undefined,
}),
objectToReturn: databaseObject,
});
});

it("--secret arg takes precedence over --local arg for the argv.secret", async function () {
fs.readdirSync.withArgs(process.cwd()).returns([]);
await runBasicTest({
cmd: `eval "Database.all()" --local --secret "sauce"`,
argvMatcher: sinon.match({
apiVersion: "10",
secret: "sauce",
url: "http://localhost:8443",
timeout: 5000,
typecheck: undefined,
}),
objectToReturn: databaseObject,
});
});

it("exits with an error if multiple default files exist", async function () {
fs.readdirSync
.withArgs(process.cwd())
Expand Down
Loading