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

Updating a secret with --asm will return an error, but appears to work #2084

Closed
wesbillman opened this issue Jul 16, 2024 · 5 comments · Fixed by #2107
Closed

Updating a secret with --asm will return an error, but appears to work #2084

wesbillman opened this issue Jul 16, 2024 · 5 comments · Fixed by #2107
Assignees
Labels

Comments

@wesbillman
Copy link
Collaborator

When attempting to update an ASM secret, we get the following errors.

❯ ftl --endpoint=https://[redacted] secret set --asm [redacted].FTL_DSN_[redacted]_[redacted]
Secret:
ftl: error: unknown: failed to set secret: failed to set secret URL: could not set secret URL: duplicate key value violates unique constraint "module_secrets_module_name_key": conflict

❯ ftl --endpoint=https://[redacted] secret unset --asm [redacted].FTL_DSN_[redacted]_[redacted]

❯ ftl --endpoint=https://[redacted] secret set --asm [redacted].FTL_DSN_[redacted]_[redacted]
Secret:
ftl: error: unknown: failed to set secret: asm/leader: unable to store secret in ASM: operation error Secrets Manager: CreateSecret, https response error StatusCode: 400, RequestID: 498ad9de-f3d5-466d-a0b1-733ee040cb03, InvalidRequestException: You can't create this secret because a secret with this name is already scheduled for deletion.

❯ ftl --endpoint=https://[redacted] secret set --asm [redacted].FTL_DSN_[redacted]_[redacted]
Secret:
ftl: error: unknown: failed to set secret: failed to set secret URL: could not set secret URL: duplicate key value violates unique constraint "module_secrets_module_name_key": conflict

Likely coming from here when a secret already exists:

https://github.com/TBD54566975/ftl/blob/main/common/configuration/asm_leader.go#L139-L164

@github-actions github-actions bot added the triage Issue needs triaging label Jul 16, 2024
@ftl-robot ftl-robot mentioned this issue Jul 16, 2024
@github-actions github-actions bot removed the triage Issue needs triaging label Jul 16, 2024
@matt2e matt2e added the P1 label Jul 17, 2024
@matt2e matt2e removed their assignment Jul 17, 2024
@matt2e matt2e added the next Work that will be be picked up next label Jul 17, 2024
@matt2e
Copy link
Collaborator

matt2e commented Jul 17, 2024

Putting this back into the next pile because I didn't get to it today

@deniseli
Copy link
Contributor

deniseli commented Jul 17, 2024

Looks like duplicate key value violates unique constraint "module_secrets_module_name_key": conflict comes from the module_secrets table's uniqueness constraint coupled with the query SetModuleSecretURL just running INSERT INTO without an ON CONFLICT clause. This logic was largely copied from the module_config, so we'll need to fix that too (though lower priority). (Totally my fault since I introduced the module_config bug...)

@deniseli
Copy link
Contributor

If setting the secret appears to work despite the error, then I'm guessing that postgres reports an error when the uniqueness constraint is violated, but it doesn't actually kill or revert the operation. So if the postgresql engine executes lookups by scanning starting from the most recent rows instead of scanning from the oldest, then it would pick up the most recently set value.

@deniseli
Copy link
Contributor

Notes on the ASM issue:

The premise behind You can't create this secret because a secret with this name is already scheduled for deletion. is a bit flawed. If someone is trying to recreate a secret that they previously executed a deletion for, they're almost certainly just trying to replace the value, so instead of erroring for that case, we should instead remove the deletion request from the queue and instead treat the insertion as a replace operation. Since secrets are held to the uniqueness constraint anyway, so we should always replace if one already exists anyway, it would be sufficient to just remove the deletion request from the queue. This is something we should fix.

That said:

  • Fixing the above probably isn't urgent enough to prioritize now because the only reason we even hit that issue was because we were trying to work around the other issue: the postgresql error. So it probably makes the most sense to just fix the sql query bug and stop there for now.
  • I couldn't find the "You can't create this secret because a secret with this name is already scheduled for deletion" error anywhere in the FTL codebase by grepping for it... did we already remove/fix this, or is it somehow coming from somewhere else?

@deniseli
Copy link
Contributor

Another thought - if we've been inserting duplicate key pairs into the module_secrets table this whole time, then we'll need to clear out the duplicates (unless we're already wiping everything)... the duplicate clearing would need to keep only the last-inserted record for each (module, name) pair and delete all prior ones to get rid of the duplicates.

@deniseli deniseli self-assigned this Jul 17, 2024
@github-actions github-actions bot removed the next Work that will be be picked up next label Jul 17, 2024
deniseli added a commit that referenced this issue Jul 17, 2024
… on conflict (#2105)

Addresses part of #2084

Postgresql way to handle upserts:
https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-upsert/

This PR fixes the issue for secrets since that's causing issues for the
client team, but not for config, which has the same issue. I confirmed
the test fails before this change but passes with it.
deniseli added a commit that referenced this issue Jul 17, 2024
…erting on conflict (#2107)

Fixes #2084

This PR addresses the config side, whereas
#2105 fixed it for secrets.

I confirmed the test fails before this change but passes with it:

```
$ go test ./common/configuration/dal/... -run TestModuleConfiguration
debug:migrate: Applying: 20231103205514_init.sql
debug:migrate: Applied: 20231103205514_init.sql in 134.088875ms
debug:migrate: Applying: 20240704103403_create_module_secrets.sql
debug:migrate: Applied: 20240704103403_create_module_secrets.sql in 5.793958ms
--- FAIL: TestModuleConfiguration (0.30s)
    --- FAIL: TestModuleConfiguration/HandlesConflicts (0.00s)
        dal_test.go:108: Did not expect an error but got:
            duplicate key value violates unique constraint "module_configuration_module_name_key": conflict
FAIL
FAIL	github.com/TBD54566975/ftl/common/configuration/dal	0.760s
FAIL
$ go test ./common/configuration/dal/... -run TestModuleConfiguration
ok  	github.com/TBD54566975/ftl/common/configuration/dal	0.775s
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants