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

correctly handle stack argument for generate schema command #2028

Merged
merged 5 commits into from
Sep 20, 2024

Conversation

rtpascual
Copy link
Contributor

Problem

When passing stack option for generate schema-from-database command, secret client getSecret is expecting backend identifier to be like https://github.com/aws-amplify/amplify-backend/blob/main/packages/plugin-types/src/backend_identifier.ts#L17 but it is resolved to { stackName: '<stack>' }. So we fail to convert the backend identifier to parameter path strings for connection URI and SSL Cert secrets.

Issue number, if available:

Changes

If stack is passed, we correctly convert to BackendIdentifier before getting secrets.

Corresponding docs PR, if applicable:

Validation

Updated test.

Checklist

  • If this PR includes a functional change to the runtime behavior of the code, I have added or updated automated test coverage for this change.
  • If this PR requires a change to the Project Architecture README, I have included that update in this PR.
  • If this PR requires a docs update, I have linked to that docs PR above.
  • If this PR modifies E2E tests, makes changes to resource provisioning, or makes SDK calls, I have run the PR checks with the run-e2e label set.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@rtpascual rtpascual requested a review from a team as a code owner September 19, 2024 00:47
Copy link

changeset-bot bot commented Sep 19, 2024

🦋 Changeset detected

Latest commit: 46dc9b3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@aws-amplify/backend-cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines 73 to 77
const backendIdentifierIsStack = 'stackName' in backendIdentifier;
const sanitizedBackendId = backendIdentifierIsStack
? BackendIdentifierConversions.fromStackName(backendIdentifier.stackName)
: backendIdentifier;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be turned into a convertDeployedBackendIdentifierToBackendIdentifier? Because we have two types DeployedBackendIdentifier and BackendIdentifier this confusion or issue may happen else where as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or perhaps we should refactor this and have.
BackendIdentifierResolver.resolveDeployedBackendIdentifier
and
BackendIdentifierResolver.resolveBackendIdentifier
so that this conversion logic stays in one place.

these are all internal types to cli package so we could do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaning towards keeping it all in BackendIdentifierResolver for now, we can determine later if there is a need to separate this logic.

const connectionUriSecret = await this.secretClient.getSecret(
backendIdentifier as BackendIdentifier,
sanitizedBackendId as BackendIdentifier,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove this force cast once we have an instance of BackendIdentifier

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like type system was trying to warn about the problem in original implementation.

Comment on lines 16 to 18
resolveDeployedBackendIdToBackendId: (
deployedBackendId?: DeployedBackendIdentifier
) => Promise<BackendIdentifier | undefined>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
resolveDeployedBackendIdToBackendId: (
deployedBackendId?: DeployedBackendIdentifier
) => Promise<BackendIdentifier | undefined>;
resolveDeployedBackendIdToBackendId: (
deployedBackendId: DeployedBackendIdentifier
) => Promise<BackendIdentifier>;

Should we have caller handle undefined ?

Or perhaps should we have

resolveBackendIdentifier: (
    args: BackendIdentifierParameters
  ) => Promise<BackendIdentifier | undefined>;

as additional API ?

The thing is that if callers give this type BackendIdentifierParameters they should be getting final product instead of passing return value from one API to the other one - that solution is not much beneficial over just calling "Conversions".

I like the later more as I type this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm all for the latter, it just makes me question the use of the original resolve for other commands but looks like those are different and do require DeployedBackendIdentifier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for other commands but looks like those are different and do require DeployedBackendIdentifier.

Yep. that's why that api has to stay. Consider renaming it though to make it less confusing.

args
);
const backendIdentifier =
await this.backendIdentifierResolver.resolveBackendIdentifier(args);

if (!backendIdentifier) {
throw new AmplifyFault('BackendIdentifierFault', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a fault if the input is coming from the user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should stay as a fault because if we can't resolve using input from the user we fallback to resolving to sandbox backend ID. The only thing really controlled by the user at that point is if they have the name field in their package.json which is covered here https://github.com/aws-amplify/amplify-backend/blob/main/packages/cli/src/backend-identifier/local_namespace_resolver.ts#L27.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is concerning. if a user provided a --stack argument that doesn't result in a valid identifier, why would we fallback to sandbox backend identifier? This seems like a wrong fallback when customer intention is clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True... this fallback is there for all generate commands unfortunately

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can follow up with a PR to not fallback if user provided stack or app ID and branch then change this to an error

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I think in some context it might makes sense to have this fallback but maybe not in the cli commands.

@rtpascual rtpascual merged commit 9e11e5d into main Sep 20, 2024
38 checks passed
@rtpascual rtpascual deleted the stack-schema-command branch September 20, 2024 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants