Repository navigation
[AI-3632] Open line and model secrets bound to their row, keep sealing them customer-only #2573
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7b4064a
fix(router): bind sealed channel line and custom model secrets to the…
kanat d497320
Merge commit '24f671e7d' into ai-3632-channel-model-aad
kanat e60c11b
fix(store): reseal a custom model's key in a detached write
kanat 3530bc9
test(router): a line's and a model key's AAD parts do not run together
kanat 0cc6cd5
refactor(store): a line's reseal relies on the bytes check alone
kanat 7246e8d
AI-3632: merge origin/main
kanat 82bdc3e
AI-3632: open both forms of a line's and a model key's seal, keep sea…
kanat 062041e
AI-3632: pin the record label an old-form open is counted under
kanat 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
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
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
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
193 changes: 193 additions & 0 deletions
193
acceleration/internal/channels/secrets_integration_test.go
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,193 @@ | ||
| //go:build integration | ||
|
|
||
| package channels | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "log/slog" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/stretchr/testify/suite" | ||
|
|
||
| "github.com/GetStream/Vision-Agents/acceleration/internal/auth" | ||
| "github.com/GetStream/Vision-Agents/acceleration/internal/store" | ||
| "github.com/GetStream/Vision-Agents/acceleration/internal/testenv" | ||
| ) | ||
|
|
||
| // LineSecretsSuite is how a line's sealed credentials are read back: bound to its customer, as | ||
| // lines are sealed, or bound to the line, as a later release seals them. | ||
| type LineSecretsSuite struct { | ||
| suite.Suite | ||
| ctx context.Context | ||
| store *store.Store | ||
| sealer *auth.Sealer | ||
| service *Service | ||
| customerID string | ||
| } | ||
|
|
||
| func TestLineSecretsSuite(t *testing.T) { suite.Run(t, new(LineSecretsSuite)) } | ||
|
|
||
| func (s *LineSecretsSuite) SetupSuite() { | ||
| dsn := os.Getenv("ROUTER_POSTGRES_DSN") | ||
| if dsn == "" { | ||
| s.T().Skip("ROUTER_POSTGRES_DSN not set") | ||
| } | ||
| s.ctx = context.Background() | ||
| opened, err := store.Open(testenv.Database(dsn, "channels_secrets")) | ||
| s.Require().NoError(err) | ||
| s.Require().NoError(opened.Migrate(s.ctx)) | ||
| s.T().Cleanup(func() { s.Require().NoError(opened.Close()) }) | ||
| s.store = opened | ||
| s.sealer, err = auth.NewSealer("line-secrets-kek") | ||
| s.Require().NoError(err) | ||
| } | ||
|
|
||
| func (s *LineSecretsSuite) SetupTest() { | ||
| s.customerID = "line-secrets-" + uuid.NewString() | ||
| s.service = s.serviceSealedBy(s.sealer) | ||
| } | ||
|
|
||
| func (s *LineSecretsSuite) serviceSealedBy(sealer *auth.Sealer) *Service { | ||
| return &Service{store: s.store, secrets: sealer, logger: slog.New(slog.DiscardHandler)} | ||
| } | ||
|
|
||
| // number is a line number no other test connects. | ||
| func (s *LineSecretsSuite) number() string { | ||
| return "+1555" + uuid.NewString()[:7] | ||
| } | ||
|
|
||
| // connected stores the SMS line e164 holding sealed as its credentials and returns the row. | ||
| func (s *LineSecretsSuite) connected(e164 string, sealed []byte, version int) store.ChannelAccount { | ||
| account := store.ChannelAccount{ | ||
| CustomerID: s.customerID, Kind: string(SMS), E164: e164, | ||
| Token: "line-" + uuid.NewString(), SecretsSealed: sealed, SecretsKEKVersion: version, | ||
| } | ||
| s.Require().NoError(s.store.ChannelAccounts.Upsert(s.ctx, &account)) | ||
| return account | ||
| } | ||
|
|
||
| // legacy is a line's credentials sealed the way connect seals them: bound to the customer only. | ||
| func (s *LineSecretsSuite) legacy(sealer *auth.Sealer) []byte { | ||
| sealed, err := sealer.SealWithAAD(`{"token":"telnyx-key","signing":"a-public-key"}`, []byte(s.customerID)) | ||
| s.Require().NoError(err) | ||
| return sealed | ||
| } | ||
|
|
||
| func (s *LineSecretsSuite) stored(account store.ChannelAccount) store.ChannelAccount { | ||
| held, err := s.store.ChannelAccounts.GetByToken(s.ctx, account.Token) | ||
| s.Require().NoError(err) | ||
| return held | ||
| } | ||
|
|
||
| // bound is a line's credentials sealed bound to the line of customerID, kind and e164, as the | ||
| // release after this one seals them. | ||
| func (s *LineSecretsSuite) bound(customerID string, kind Kind, e164 string, credentials Secrets) []byte { | ||
| raw, err := json.Marshal(credentials) | ||
| s.Require().NoError(err) | ||
| sealed, err := s.sealer.SealWithAAD(string(raw), secretsAAD(customerID, string(kind), e164)) | ||
| s.Require().NoError(err) | ||
| return sealed | ||
| } | ||
|
|
||
| // legacyOpensOf reads router_sealed_legacy_opens_total for one kind of record. | ||
| func (s *LineSecretsSuite) legacyOpensOf(record string) float64 { | ||
| families, err := prometheus.DefaultGatherer.Gather() | ||
| s.Require().NoError(err) | ||
| for _, family := range families { | ||
| if family.GetName() != "router_sealed_legacy_opens_total" { | ||
| continue | ||
| } | ||
| for _, metric := range family.GetMetric() { | ||
| for _, pair := range metric.GetLabel() { | ||
| if pair.GetName() == "record" && pair.GetValue() == record { | ||
| return metric.GetCounter().GetValue() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return 0 | ||
| } | ||
|
|
||
| // Catches the line's own AAD naming only the customer, or a fallback that opens any blob of | ||
| // the customer's: credentials sealed for one line, put in another line's row, do not open. | ||
| func (s *LineSecretsSuite) TestCredentialsMovedFromAnotherLineDoNotOpen() { | ||
| sealed := s.bound(s.customerID, SMS, s.number(), Secrets{Token: "telnyx-key"}) | ||
| second := s.connected(s.number(), sealed, s.sealer.CurrentVersion()) | ||
|
|
||
| _, _, err := s.service.line(s.ctx, second.Token) | ||
|
|
||
| s.ErrorContains(err, "cannot be opened") | ||
| } | ||
|
|
||
| // Catches the kind or the customer missing from the line's AAD: the same number on another | ||
| // channel, or another app's line on the same number, is another line. | ||
| func (s *LineSecretsSuite) TestCredentialsSealedForTheSameNumberOnAnotherChannelOrAppDoNotOpen() { | ||
| number := s.number() | ||
| line := store.ChannelAccount{CustomerID: s.customerID, Kind: string(SMS), E164: number, | ||
| SecretsKEKVersion: s.sealer.CurrentVersion()} | ||
|
|
||
| line.SecretsSealed = s.bound(s.customerID, WhatsApp, number, Secrets{Token: "meta-token"}) | ||
| _, err := Open(s.sealer, line) | ||
| s.Error(err, "a WhatsApp line's credentials on the SMS line of the same number") | ||
|
|
||
| line.SecretsSealed = s.bound("another-app", SMS, number, Secrets{Token: "telnyx-key"}) | ||
| _, err = Open(s.sealer, line) | ||
| s.Error(err, "another app's line of the same number") | ||
| } | ||
|
|
||
| // Catches a line the next release seals bound to itself not opening here, which would make | ||
| // that release impossible to roll back. | ||
| func (s *LineSecretsSuite) TestALineSealedBoundToItselfOpens() { | ||
| number := s.number() | ||
| account := s.connected(number, s.bound(s.customerID, SMS, number, Secrets{Token: "telnyx-key"}), | ||
| s.sealer.CurrentVersion()) | ||
|
|
||
| line, _, err := s.service.line(s.ctx, account.Token) | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("telnyx-key", line.Token) | ||
| } | ||
|
|
||
| // Catches a line sealed bound only to its customer, as every line is, going dark, being | ||
| // written back on a read, or counted under another kind of record than a channel account: the | ||
| // count is what says when that form can stop being opened. | ||
| func (s *LineSecretsSuite) TestALineSealedBoundOnlyToItsCustomerOpensIsLeftAsItIsAndIsCounted() { | ||
| sealed := s.legacy(s.sealer) | ||
| account := s.connected(s.number(), sealed, s.sealer.CurrentVersion()) | ||
| before := s.legacyOpensOf("channel_account") | ||
|
|
||
| line, _, err := s.service.line(s.ctx, account.Token) | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("telnyx-key", line.Token) | ||
| s.Equal("a-public-key", line.Signing) | ||
| s.Equal(sealed, s.stored(account).SecretsSealed, "a read writes nothing") | ||
| s.GreaterOrEqual(s.legacyOpensOf("channel_account"), before+1) | ||
| } | ||
|
|
||
| // Catches a line bound only to its customer being opened under the current key rather than | ||
| // the one it was sealed under. | ||
| func (s *LineSecretsSuite) TestALegacyLineUnderAnOlderKeyOpensWithItsStoredVersion() { | ||
| before, err := auth.NewSealer("old-key") | ||
| s.Require().NoError(err) | ||
| account := s.connected(s.number(), s.legacy(before), 1) | ||
| rotated, err := auth.NewSealerWithKeyring(2, map[int]string{1: "old-key", 2: "new-key"}) | ||
| s.Require().NoError(err) | ||
|
|
||
| line, _, err := s.serviceSealedBy(rotated).line(s.ctx, account.Token) | ||
|
|
||
| s.Require().NoError(err) | ||
| s.Equal("telnyx-key", line.Token) | ||
| s.Equal(1, s.stored(account).SecretsKEKVersion) | ||
| } | ||
|
|
||
| // Catches the parts of a line's AAD running together: a colon inside one part cannot make two | ||
| // lines give the same bytes. | ||
| func (s *LineSecretsSuite) TestTheLinesAADPartsDoNotRunTogether() { | ||
| s.NotEqual(secretsAAD("app:sms", "whatsapp", "+15550100"), secretsAAD("app", "sms:whatsapp", "+15550100")) | ||
| s.NotEqual(secretsAAD("app", "sms:+1555", "0100"), secretsAAD("app", "sms", "+1555:0100")) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Should fix] R1.3 No test checks the
recordlabel at either caller, and the follow-up deletes each fallback by this label. Two mutants keep every suite green: this line counting under"custom_model", andllmrouter/custom.go:63counting under"channel_account".Test rows, checked to fail under their mutant and pass on head:
LineSecretsSuite: connect a legacy line, then callservice.line.router_sealed_legacy_opens_total{record="channel_account"}, read throughprometheus.DefaultGathereras ininternal/vendorevent/vendorevent_test.go:110, must be>= before+1.CustomModelKeySuite: create a legacy model, then callCustomModel.{record="custom_model"}must be>= before+1.Use
>=, not==: the label is fixed, so other tests in the package can move it. The mutant moves the count to the other label, so>=still catches it.The counter stays in step 1, so this applies under R1.1 too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 062041e:
LineSecretsSuite.TestALineSealedBoundOnlyToItsCustomerOpensIsLeftAsItIsAndIsCountedreadsrouter_sealed_legacy_opens_total{record="channel_account"}andCustomModelKeySuite.TestAKeySealedBoundOnlyToItsCustomerOpensIsLeftAsItIsAndIsCountedreads{record="custom_model"}, each >= before+1. Swapping the two labels fails each test.