generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 102
Seed #2546
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
Merged
Merged
Seed #2546
Changes from 71 commits
Commits
Show all changes
82 commits
Select commit
Hold shift + click to select a range
e3900e3
seed setup and seed secrets
8260a2e
skeleton of sandbox seed command
8935019
merge main
31c6da1
seed policy generation
bf92de4
grants for storage
6db8fbc
changed seed policy name to amplifySeedPolicy
889e903
test for secrets
1dd9c54
removed managed seed policy
ec798d4
set secret and seed command
0747b04
generate seed policy command
a996f26
auth APIs - incomplete
e1a2bc7
created folders, getting data from generateConfig
0f1bd95
removed static, changed name of AuthOutputsReader to ConfigReader
5dd262d
fixed seed commands, figured out callback for TOTP
f548f3f
conditional typing
b5d18f5
mfa updates and seed command test
47123a9
adjustments and tests added
c55f39e
update to user group type
e9991c4
merge main into branch
51f47c7
changeset
8ce4880
update api
b34532e
unit tests
c395733
adjust MFA type
8b3dc92
Merge branch 'main' into seed-feat
c447235
remove edit to construct container
892628b
removed unnecessary changeset
5485bd6
fixed some tests
7c2287c
fix dependencies tests
7828ae0
adjust seed command test
f98fee9
fix dep inconsistencies
ef27369
updated challenge response to use secretValue instead of input
8212b46
PR feedback
9ace2f2
more PR feedback
7f9915d
Merge branch 'main' into seed-feat
5852d7b
even more PR feedback
a189d11
testing something
446018b
reverted
0b5910c
e2e tests
7a7b2f9
e2e-tests
c7defa8
revert some package-lock changes
587586d
e2e tests
d89dd6c
removed user check from seed test
5949f2e
remove describeUserpoolCommand
c9755a7
update test
725ef86
sts package-lock update
9505ba1
e2e tests
909813c
fix package versions
32a77d9
fixing permissions
682b22a
import crypto
72bc973
appeasing json
9f8c5ab
fixing json
c05e494
cleaned up e2e file
b1dcca9
crpyto polyfill in e2e test seed script
10d4717
added context
2dd52a5
Merge branch 'main' into seed-feat
20cee9f
fix dependency issues
a0682e0
added spinner and success message to seed command
241d6e6
Merge branch 'main' into seed-feat
0f2ae13
updated success message
9949198
lint fixes from prettier update
bfea93a
updated some tests
e6de640
Merge branch 'main' into seed-feat
ShadowCat567 da3d96b
adjustments
ShadowCat567 47836d8
fixes after prettier sandbox merge
99d4ff2
Merge branch 'main' into seed-feat
3cf5bec
small api adjustment
31d458e
Merge branch 'main' into seed-feat
8488ca1
seed version bump changed to major
a40f891
reverted some package changes
5941f88
PR feedback
cfe281d
Merge branch 'main' into seed-feat
83a15e0
make codeQL happy
a1ee08d
Potential fix for code scanning alert no. 327: Insecure randomness
ShadowCat567 8ef43dc
Merge branch 'seed-feat' of github.com:aws-amplify/amplify-backend in…
252b150
e2e tests did not like readFile, using import
55a5253
fixed seed e2e tests
07adffb
Merge branch 'main' into seed-feat
dbde683
removed outputs import
a81efe3
Merge branch 'main' into seed-feat
b7decfa
Update packages/cli/src/commands/sandbox/sandbox-seed/sandbox_seed_co…
ShadowCat567 15b5277
fixed tests
2bf0970
Merge branch 'main' into seed-feat
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@aws-amplify/seed': major | ||
'@aws-amplify/backend-cli': minor | ||
--- | ||
|
||
adding seed feature |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
packages/cli/src/commands/sandbox/sandbox-seed/sandbox_seed_command.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { after, before, describe, it, mock } from 'node:test'; | ||
import fsp from 'fs/promises'; | ||
import * as path from 'path'; | ||
import { SandboxSeedCommand } from './sandbox_seed_command.js'; | ||
import yargs, { CommandModule } from 'yargs'; | ||
import { | ||
TestCommandError, | ||
TestCommandRunner, | ||
} from '../../../test-utils/command_runner.js'; | ||
import { createSandboxSecretCommand } from '../sandbox-secret/sandbox_secret_command_factory.js'; | ||
import { EventHandler, SandboxCommand } from '../sandbox_command.js'; | ||
import { SandboxSingletonFactory } from '@aws-amplify/sandbox'; | ||
import { SandboxDeleteCommand } from '../sandbox-delete/sandbox_delete_command.js'; | ||
import { ClientConfigGeneratorAdapter } from '../../../client-config/client_config_generator_adapter.js'; | ||
import { format, printer } from '@aws-amplify/cli-core'; | ||
import { CommandMiddleware } from '../../../command_middleware.js'; | ||
import { SandboxSeedGeneratePolicyCommand } from './sandbox_seed_policy_command.js'; | ||
import assert from 'node:assert'; | ||
import { BackendIdentifier } from '@aws-amplify/plugin-types'; | ||
import { SandboxBackendIdResolver } from '../sandbox_id_resolver.js'; | ||
|
||
const seedFileContents = 'console.log(`seed has been run`);'; | ||
|
||
const testBackendNameSpace = 'testSandboxId'; | ||
const testSandboxName = 'testSandboxName'; | ||
|
||
const testBackendId: BackendIdentifier = { | ||
namespace: testBackendNameSpace, | ||
name: testSandboxName, | ||
type: 'sandbox', | ||
}; | ||
|
||
void describe('sandbox seed command', () => { | ||
let commandRunner: TestCommandRunner; | ||
|
||
const clientConfigGenerationMock = mock.fn<EventHandler>(); | ||
const clientConfigDeletionMock = mock.fn<EventHandler>(); | ||
|
||
const clientConfigGeneratorAdapterMock = { | ||
generateClientConfigToFile: clientConfigGenerationMock, | ||
} as unknown as ClientConfigGeneratorAdapter; | ||
|
||
const commandMiddleware = new CommandMiddleware(printer); | ||
const mockHandleProfile = mock.method( | ||
commandMiddleware, | ||
'ensureAwsCredentialAndRegion', | ||
() => null, | ||
); | ||
|
||
const mockProfileResolver = mock.fn(); | ||
|
||
let amplifySeedDir: string; | ||
let fullPath: string; | ||
|
||
const sandboxIdResolver: SandboxBackendIdResolver = { | ||
resolve: () => Promise.resolve(testBackendId), | ||
} as SandboxBackendIdResolver; | ||
|
||
before(async () => { | ||
const sandboxFactory = new SandboxSingletonFactory( | ||
() => Promise.resolve(testBackendId), | ||
mockProfileResolver, | ||
printer, | ||
format, | ||
); | ||
|
||
const sandboxSeedCommand = new SandboxSeedCommand(sandboxIdResolver, [ | ||
new SandboxSeedGeneratePolicyCommand(sandboxIdResolver), | ||
]); | ||
|
||
const sandboxCommand = new SandboxCommand( | ||
sandboxFactory, | ||
[ | ||
new SandboxDeleteCommand(sandboxFactory), | ||
createSandboxSecretCommand(), | ||
sandboxSeedCommand, | ||
], | ||
clientConfigGeneratorAdapterMock, | ||
commandMiddleware, | ||
() => ({ | ||
successfulDeployment: [clientConfigGenerationMock], | ||
successfulDeletion: [clientConfigDeletionMock], | ||
failedDeployment: [], | ||
}), | ||
); | ||
const parser = yargs().command(sandboxCommand as unknown as CommandModule); | ||
commandRunner = new TestCommandRunner(parser); | ||
mockHandleProfile.mock.resetCalls(); | ||
}); | ||
|
||
void describe('seed script exists', () => { | ||
before(async () => { | ||
await fsp.mkdir(path.join(process.cwd(), 'amplify', 'seed'), { | ||
recursive: true, | ||
}); | ||
amplifySeedDir = path.join(process.cwd(), 'amplify'); | ||
fullPath = path.join(process.cwd(), 'amplify', 'seed', 'seed.ts'); | ||
await fsp.writeFile(fullPath, seedFileContents, 'utf8'); | ||
}); | ||
|
||
after(async () => { | ||
await fsp.rm(amplifySeedDir, { recursive: true, force: true }); | ||
if (process.env.AMPLIFY_BACKEND_IDENTIFIER) { | ||
delete process.env.AMPLIFY_BACKEND_IDENTIFIER; | ||
} | ||
}); | ||
|
||
void it('runs seed if seed script is found', async () => { | ||
const output = await commandRunner.runCommand('sandbox seed'); | ||
|
||
const cleanedOutput = output.trimEnd().trimStart(); | ||
|
||
assert.ok(output !== undefined); | ||
assert.deepStrictEqual( | ||
cleanedOutput, | ||
'✔ seed has successfully completed', | ||
); | ||
assert.strictEqual(mockHandleProfile.mock.callCount(), 1); | ||
}); | ||
}); | ||
|
||
void describe('seed script does not exist', () => { | ||
before(async () => { | ||
await fsp.mkdir(path.join(process.cwd(), 'amplify', 'seed'), { | ||
recursive: true, | ||
}); | ||
amplifySeedDir = path.join(process.cwd(), 'amplify'); | ||
}); | ||
|
||
after(async () => { | ||
await fsp.rm(amplifySeedDir, { recursive: true, force: true }); | ||
if (process.env.AMPLIFY_BACKEND_IDENTIFIER) { | ||
delete process.env.AMPLIFY_BACKEND_IDENTIFIER; | ||
} | ||
}); | ||
|
||
void it('throws error if seed script does not exist', async () => { | ||
await assert.rejects( | ||
() => commandRunner.runCommand('sandbox seed'), | ||
(err: TestCommandError) => { | ||
assert.match(err.output, /SeedScriptNotFoundError/); | ||
assert.match(err.output, /There is no file that corresponds to/); | ||
assert.match( | ||
err.output, | ||
/Please make a file that corresponds to (.*) and put your seed logic in it/, | ||
); | ||
return true; | ||
}, | ||
); | ||
}); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
packages/cli/src/commands/sandbox/sandbox-seed/sandbox_seed_command.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Argv, CommandModule } from 'yargs'; | ||
import path from 'path'; | ||
import { existsSync } from 'fs'; | ||
import { execa } from 'execa'; | ||
import { SandboxBackendIdResolver } from '../sandbox_id_resolver.js'; | ||
import { AmplifyUserError } from '@aws-amplify/platform-core'; | ||
import { SandboxCommandGlobalOptions } from '../option_types.js'; | ||
import { format, printer } from '@aws-amplify/cli-core'; | ||
|
||
/** | ||
* Command that runs seed in sandbox environment | ||
*/ | ||
export class SandboxSeedCommand implements CommandModule<object> { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
readonly command: string; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
readonly describe: string; | ||
|
||
/** | ||
* Seeds sandbox environment. | ||
*/ | ||
constructor( | ||
private readonly backendIDResolver: SandboxBackendIdResolver, | ||
private readonly seedSubCommands: CommandModule[], | ||
) { | ||
this.command = 'seed'; | ||
this.describe = 'Seeds sandbox environment'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
handler = async (): Promise<void> => { | ||
printer.startSpinner(''); | ||
const backendID = await this.backendIDResolver.resolve(); | ||
const seedPath = path.join('amplify', 'seed', 'seed.ts'); | ||
try { | ||
await execa('tsx', [seedPath], { | ||
cwd: process.cwd(), | ||
stdio: 'inherit', | ||
env: { | ||
AMPLIFY_BACKEND_IDENTIFIER: JSON.stringify(backendID), | ||
}, | ||
}); | ||
} finally { | ||
printer.stopSpinner(); | ||
} | ||
printer.printNewLine(); | ||
printer.print(`${format.success('✔')} seed has successfully completed`); | ||
}; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
builder = (yargs: Argv): Argv<SandboxCommandGlobalOptions> => { | ||
return yargs.command(this.seedSubCommands).check(() => { | ||
const seedPath = path.join(process.cwd(), 'amplify', 'seed', 'seed.ts'); | ||
if (!existsSync(seedPath)) { | ||
throw new AmplifyUserError('SeedScriptNotFoundError', { | ||
message: `There is no file that corresponds to ${seedPath}`, | ||
resolution: `Please make a file that corresponds to ${seedPath} and put your seed logic in it`, | ||
}); | ||
} | ||
return true; | ||
}); | ||
}; | ||
} |
43 changes: 43 additions & 0 deletions
43
packages/cli/src/commands/sandbox/sandbox-seed/sandbox_seed_policy_command.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Argv, CommandModule } from 'yargs'; | ||
import { printer } from '@aws-amplify/cli-core'; | ||
import { SandboxBackendIdResolver } from '../sandbox_id_resolver.js'; | ||
import { generateSeedPolicyTemplate } from '../../../seed-policy-generation/generate_seed_policy_template.js'; | ||
|
||
/** | ||
* Command that generates policy template with permissions to be able to run seed in sandbox environment | ||
*/ | ||
export class SandboxSeedGeneratePolicyCommand implements CommandModule<object> { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
readonly command: string; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
readonly describe: string; | ||
|
||
/** | ||
* Generates policy to run seed, is a subcommand of seed | ||
*/ | ||
constructor(private readonly backendIdResolver: SandboxBackendIdResolver) { | ||
this.command = 'generate-policy'; | ||
this.describe = 'Generates policy for seeding'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
handler = async (): Promise<void> => { | ||
const backendId = await this.backendIdResolver.resolve(); | ||
const policyDocument = await generateSeedPolicyTemplate(backendId); | ||
printer.print(JSON.stringify(policyDocument.toJSON(), null, 2)); | ||
}; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
builder = (yargs: Argv) => { | ||
return yargs; | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.