Skip to content

Commit

Permalink
Add env cors & env origins commands
Browse files Browse the repository at this point in the history
  • Loading branch information
2can authored and zaiste committed Oct 12, 2023
1 parent bcfa7d9 commit 3926ef7
Show file tree
Hide file tree
Showing 14 changed files with 563 additions and 19 deletions.
66 changes: 66 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ for more information, find the documentation at https://saleor.io
* [environment](#environment)
* [environment auth](#environment-auth)
* [environment clear](#environment-clear)
* [environment cors](#environment-cors)
* [environment create](#environment-create)
* [environment list](#environment-list)
* [environment maintenance](#environment-maintenance)
* [environment origins](#environment-origins)
* [environment populate](#environment-populate)
* [environment promote](#environment-promote)
* [environment remove](#environment-remove)
Expand Down Expand Up @@ -455,9 +457,11 @@ saleor environment [command]
Commands:
saleor environment auth [key|environment] Manage basic auth for a specific environment
saleor environment clear <key|environment> Clear database for environment
saleor environment cors [key|environment] Manage environment's CORS
saleor environment create [name] Create a new environment
saleor environment list List environments
saleor environment maintenance [key|environment] Enable or disable maintenance mode in a specific environment
saleor environment origins [key|environment] Manage environment's trusted client origins
saleor environment populate [key|environment] Populate database for environment
saleor environment promote [key|environment] Promote environment to production
saleor environment remove [key|environment] Delete an environment
Expand Down Expand Up @@ -531,6 +535,39 @@ Options:
-h, --help Show help [boolean]
```

#### environment cors

```sh
$ saleor environment cors --help
```

Help output:

```
saleor environment cors [key|environment]
Manage environment's CORS
Positionals:
key, environment key of the environment [string]
Options:
--json Output the data as JSON [boolean] [default: false]
--short Output data as text [boolean] [default: false]
-u, --instance, --url Saleor instance to work with [string]
--all All origins are allowed [boolean]
--dashboard Only dashboard is allowed [boolean]
--selected Only specified origins are allowed [array]
-V, --version Show version number [boolean]
-h, --help Show help [boolean]
Examples:
saleor env cors
saleor env cors my-environment --all
saleor env cors my-environment --dashboard
saleor env cors my-environment --selected="https://example.com"
```

#### environment create

```sh
Expand Down Expand Up @@ -622,6 +659,35 @@ Examples:
saleor env maintenance my-environment --disable
```

#### environment origins

```sh
$ saleor environment origins --help
```

Help output:

```
saleor environment origins [key|environment]
Manage environment's trusted client origins
Positionals:
key, environment key of the environment [string]
Options:
--json Output the data as JSON [boolean] [default: false]
--short Output data as text [boolean] [default: false]
-u, --instance, --url Saleor instance to work with [string]
--origin Allowed domains [array]
-V, --version Show version number [boolean]
-h, --help Show help [boolean]
Examples:
saleor env origins
saleor env origins my-environment --origin=https://trusted-origin.com
```

#### environment populate

```sh
Expand Down
206 changes: 206 additions & 0 deletions src/cli/env/cors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import Debug from 'debug';
import type { Arguments, CommandBuilder } from 'yargs';

import Enquirer from 'enquirer';
import { ar } from 'date-fns/locale';
import { error } from 'console';
import chalk from 'chalk';
import { API, PATCH } from '../../lib/index.js';
import { obfuscateArgv, println, printlnSuccess } from '../../lib/util.js';
import {
useBlockingTasksChecker,
useEnvironment,
} from '../../middleware/index.js';
import { Options } from '../../types.js';
import { getEnvironment, promptOrigin } from '../../lib/environment.js';

const debug = Debug('saleor-cli:env:auth');

export const command = 'cors [key|environment]';
export const desc = 'Manage environment\'s CORS';

export const builder: CommandBuilder = (_) =>
_.positional('key', {
type: 'string',
demandOption: false,
desc: 'key of the environment',
})
.option('all', {
type: 'boolean',
demandOption: false,
desc: 'All origins are allowed',
})
.option('dashboard', {
type: 'boolean',
demandOption: false,
desc: 'Only dashboard is allowed',
})
.option('selected', {
type: 'array',
demandOption: false,
desc: 'Only specified origins are allowed',
})
.example('saleor env cors', '')
.example('saleor env cors my-environment --all', '')
.example('saleor env cors my-environment --dashboard', '')
.example(
'saleor env cors my-environment --selected="https://example.com"',
'',
);

export const handler = async (argv: Arguments<Options>) => {
debug('command arguments: %O', obfuscateArgv(argv));

const { allowed_cors_origins: allowedCorsOrigins } =
await getEnvironment(argv);

if (argv.all) {
if (allowedCorsOrigins === '*') {
println('All origins are already allowed');
return;
}

await PATCH(API.Environment, argv, {
json: {
allowed_cors_origins: '*',
},
});

printlnSuccess('All origins are allowed');
return;
}

if (argv.dashboard) {
if (!allowedCorsOrigins) {
println('Only dashboard is already allowed');
return;
}

await PATCH(API.Environment, argv, {
json: {
allowed_cors_origins: null,
},
});

printlnSuccess('Only dashboard is allowed');
return;
}

if (((argv.selected as undefined | string[]) || []).length > 0) {
await PATCH(API.Environment, argv, {
json: {
allowed_cors_origins: argv.selected,
},
});

printlnSuccess('Selected Origins are allowed');
return;
}

// First form to check current
const { kind } = await Enquirer.prompt<{
kind: string;
}>([
{
type: 'select',
name: 'kind',
message: 'Choose allowed API origins ',
choices: [
{
name: 'Allow all origins',
value: 'all',
},
{
name: 'Selected Origins',
value: 'selected',
},
{
name: 'Dashboard only',
value: 'dashboard',
},
],
initial: () => {
if (allowedCorsOrigins === '*') {
return 1;
}
if (allowedCorsOrigins == null) {
return 2;
}
if (Array.isArray(allowedCorsOrigins)) {
return 3;
}

return 1;
},
},
]);

// Trigger an update for all and dashboard
if (['all', 'dashboard'].includes(kind)) {
await PATCH(API.Environment, argv, {
json: {
allowed_cors_origins: kind === 'all' ? '*' : null,
},
});

printlnSuccess(
kind === 'all' ? 'All origins are allowed' : 'Only dashboard is allowed',
);
return;
}

// Handle selected origins
const selected: string[] =
(argv.selected as string[]) || (allowedCorsOrigins as string[]) || [];
let addMore = true;

const { origins } = await Enquirer.prompt<{
origins: string;
}>([
{
type: 'multiselect',
name: 'origins',
message:
'Define Selected Origins\n (use the arrows to navigate and the space bar to select)',
choices: [...selected, 'Add a new origin'],
initial: selected,
},
]);

do {
if (origins.length === 0) {
return;
}
if (origins.includes('Add a new CORS origin')) {
const form = await promptOrigin();
selected.push(form.origin);
addMore = form.addMore;
} else {
addMore = false;
}
} while (addMore);

const { proceed } = await Enquirer.prompt<{
proceed: boolean;
}>([
{
type: 'confirm',
name: 'proceed',
message: `Do you want to set the following CORS origins?\n ${selected.join(
'\n ',
)}`,
},
]);

if (proceed) {
await PATCH(API.Environment, argv, {
json: {
allowed_cors_origins: selected,
},
});

printlnSuccess('Specified CORS origins are allowed');
}
};

export const middlewares = [useEnvironment, useBlockingTasksChecker];
4 changes: 4 additions & 0 deletions src/cli/env/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useOrganization, useToken } from '../../middleware/index.js';
import * as auth from './auth.js';
import * as cleardb from './clear.js';
import * as cors from './cors.js';
import * as create from './create.js';
import * as list from './list.js';
import * as maintenance from './maintenance.js';
import * as origins from './origins.js';
import * as populatedb from './populate.js';
import * as promote from './promote.js';
import * as remove from './remove.js';
Expand All @@ -16,9 +18,11 @@ export default function (_: any) {
_.command([
auth,
cleardb,
cors,
create,
list,
maintenance,
origins,
populatedb,
promote,
remove,
Expand Down
Loading

0 comments on commit 3926ef7

Please sign in to comment.