fix(wise): refuse an SCA private key when encryption is unavailable

Merged
HO
3mo ago
+112-0

First of the stacked PRs split out of #3402, per the suggestion there. This one
is unrelated to the MCP work and stands entirely on its own, so it goes first.

The problem

WiseItem wraps its encrypts declarations in if encryption_ready?:

if encryption_ready?
  encrypts :token, deterministic: true
  encrypts :raw_payload
  encrypts :sca_private_key
end

encryption_ready? is ActiveRecordEncryptionConfig.explicitly_configured?, so on
any install that has not configured Active Record encryption the declaration never
runs and generate_sca_keypair! writes the PEM into the column verbatim.

That is a tolerable degraded mode for a display name. It is not one for the key that
signs Wise balance-statement requests, and nothing in the flow told the user it had
happened: the panel reported a keypair as generated either way.

The fix

generate_sca_keypair! raises SCAEncryptionUnavailable instead of writing, and a
validation refuses the attribute on every other write path. The exception is raised
rather than returned so no caller can read "not stored" as "stored".
WiseItemsController#generate_sca_keypair already rescues broadly, so the user sees
the same panel error as any other keypair failure rather than a 500. Error messages
are localized in EN and FR.

Tests

Both new tests fail without the fix. The controller one fails by storing the
plaintext PEM, which the failure output prints in full. The three existing tests that
generate a keypair now stub encryption_ready? to true: the test environment
configures no encryption keys, so without the stub they would be exercising the
refused path rather than the one they describe.

All 47 Wise tests pass, including wise_sca_localization_test.rb from #3410.
Rubocop clean, Brakeman reports no warnings.

Scope

token and raw_payload sit in the same conditional block and have the same
exposure on an unencrypted install. This PR deliberately does not touch them: a
signing key is the one worth refusing outright, and changing the token's behaviour
would lock existing users out of a working integration. Worth a separate look.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Prevented SCA private keys from being stored without encryption.
    • Added clear validation errors when encryption is unavailable.
    • SCA key generation now reports an error instead of proceeding when encryption is not configured.
    • Existing SCA private keys remain usable during unrelated updates.
    • Added English and French messages explaining the encryption requirement.
  • Tests

    • Added coverage for encrypted key generation, unavailable-encryption scenarios, and preserving existing keys.