Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 69 additions & 46 deletions backend-config/account_association.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package backendconfig

import "github.com/rudderlabs/rudder-go-kit/logger"
import (
"github.com/rudderlabs/rudder-go-kit/logger"
obskit "github.com/rudderlabs/rudder-observability-kit/go/labels"
)

// processAccountAssociations processes account configurations and merges them with their corresponding
// account definitions. It then associates these merged accounts with destinations based on
Expand All @@ -15,69 +18,89 @@ func (c *ConfigT) processAccountAssociations() {
for i := range c.Sources {
for j := range c.Sources[i].Destinations {
dest := &c.Sources[i].Destinations[j]
c.setDestinationAccounts(dest)
c.enrichDestinationWithAccounts(dest)
}
}
}
}

// setDestinationAccounts assigns accounts to a destination based on its configuration.
// getAccountDefinition returns a pointer to the account definition for the given account definition name.
// If the account definition name is empty or the account definition doesn't exist, it returns nil.
//
// Parameters:
// - accountDefinitionName: The name of the account definition to look up
// - accountAssociationLogger: The logger instance to use for logging
//
// Returns:
// - *AccountDefinition: A pointer to the account definition or nil if not found
func (c *ConfigT) getAccountDefinition(accountDefinitionName string, accountAssociationLogger logger.Logger) *AccountDefinition {
if accountDefinition, exists := c.AccountDefinitions[accountDefinitionName]; exists {
return &accountDefinition
}
// Log error if account definition not found
accountAssociationLogger.Errorn("Account definition not found in configured accountDefinitions")
Comment thread
utsabc marked this conversation as resolved.
Resolved
return nil
}

// enrichAccountWithDefinition populates an account to a destination field based on the account ID.
// It creates an Account object by combining account details with its definition.
//
// Parameters:
// - accountID: The ID of the account to populate
// - accountAssociationLogger: The logger instance to use for logging
//
// Returns:
// - *Account: The populated account object or nil if the account doesn't exist
func (c *ConfigT) enrichAccountWithDefinition(accountID string, accountAssociationLogger logger.Logger) *Account {
if account, exists := c.Accounts[accountID]; exists {
var accountDefinitionPtr *AccountDefinition
if account.AccountDefinitionName != "" {
accountDefinitionPtr = c.getAccountDefinition(
account.AccountDefinitionName,
accountAssociationLogger,
)
}
account.AccountDefinition = accountDefinitionPtr
account.ID = accountID
return &account
}
accountAssociationLogger.Errorn("Account not found in configured accounts")
Comment thread
utsabc marked this conversation as resolved.
Resolved
return nil
}

// enrichDestinationWithAccounts assigns accounts to a destination based on its configuration.
// It handles two types of account associations:
// 1. Regular account (rudderAccountId) - Used for normal event delivery flow
// 2. Delete account (rudderDeleteAccountId) - Used for data deletion/regulation flow
//
// For each account type, it:
// 1. Checks if the corresponding account ID exists in the destination config
// 2. Verifies the account exists in the accounts map
// 3. Creates an AccountWithDefinition by combining account details with its definition
// 3. Creates an account by combining account details with its definition
// 4. Assigns it to the appropriate field in the destination
//
// Parameters:
// - dest: Pointer to the destination being configured
func (c *ConfigT) setDestinationAccounts(dest *DestinationT) {
// Check and set the regular account if specified in the destination config
if accountID, ok := dest.Config["rudderAccountId"].(string); ok {
if account, exists := c.Accounts[accountID]; exists {
accountDefinition, exists := c.AccountDefinitions[account.AccountDefinitionName]
if !exists {
pkgLogger.Warnn("Account definition not found in configured accountDefinitions for delivery flow",
logger.NewStringField("rudderAccountId", accountID),
logger.NewStringField("accountDefinitionName", account.AccountDefinitionName),
logger.NewStringField("destinationId", dest.ID))
}
dest.DeliveryAccount = &AccountWithDefinition{
Id: accountID,
Options: account.Options,
Secret: account.Secret,
AccountDefinition: accountDefinition,
}
} else {
pkgLogger.Warnn("Account not found in configured accounts for delivery flow",
logger.NewStringField("rudderAccountId", accountID),
logger.NewStringField("destinationId", dest.ID))
}
func (c *ConfigT) enrichDestinationWithAccounts(dest *DestinationT) {
accountAssociationLogger := pkgLogger.Withn(
obskit.DestinationID(dest.ID),
obskit.DestinationType(dest.DestinationDefinition.Name),
)
// Check and set the delivery account if specified in the destination config
if accountID, ok := dest.Config["rudderAccountId"].(string); ok && accountID != "" {
accountAssociationLogger = accountAssociationLogger.Withn(
logger.NewStringField("rudderAccountId", accountID),
logger.NewStringField("flowType", "delivery"),
)
dest.DeliveryAccount = c.enrichAccountWithDefinition(accountID, accountAssociationLogger)
}

// Check and set the delete account if specified in the destination config
if deleteAccountID, ok := dest.Config["rudderDeleteAccountId"].(string); ok {
if account, exists := c.Accounts[deleteAccountID]; exists {
accountDefinition, exists := c.AccountDefinitions[account.AccountDefinitionName]
if !exists {
pkgLogger.Warnn("Account definition not found in configured accountDefinitions for regulation flow",
logger.NewStringField("rudderDeleteAccountId", deleteAccountID),
logger.NewStringField("accountDefinitionName", account.AccountDefinitionName),
logger.NewStringField("destinationId", dest.ID))
}
dest.DeleteAccount = &AccountWithDefinition{
Id: deleteAccountID,
Options: account.Options,
Secret: account.Secret,
AccountDefinition: accountDefinition,
}
} else {
pkgLogger.Warnn("Account not found in configured accounts for regulation flow",
logger.NewStringField("rudderDeleteAccountId", deleteAccountID),
logger.NewStringField("destinationId", dest.ID))
}
if deleteAccountID, ok := dest.Config["rudderDeleteAccountId"].(string); ok && deleteAccountID != "" {
accountAssociationLogger = accountAssociationLogger.Withn(
logger.NewStringField("rudderAccountId", deleteAccountID),
logger.NewStringField("flowType", "regulation"),
)
dest.DeleteAccount = c.enrichAccountWithDefinition(deleteAccountID, accountAssociationLogger)
}
}
177 changes: 170 additions & 7 deletions backend-config/account_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/rudderlabs/rudder-go-kit/jsonrs"
)

func TestAccountAssociations(t *testing.T) {
Expand Down Expand Up @@ -45,7 +47,7 @@ func TestAccountAssociations(t *testing.T) {

c.processAccountAssociations()

require.Equal(t, "acc-1", c.Sources[0].Destinations[0].DeliveryAccount.Id)
require.Equal(t, "acc-1", c.Sources[0].Destinations[0].DeliveryAccount.ID)
require.Equal(t, map[string]interface{}{"key1": "value1"}, c.Sources[0].Destinations[0].DeliveryAccount.Options)
require.Equal(t, map[string]interface{}{"secret1": "secretValue1"}, c.Sources[0].Destinations[0].DeliveryAccount.Secret)
require.Equal(t, map[string]interface{}{
Expand Down Expand Up @@ -96,12 +98,12 @@ func TestAccountAssociations(t *testing.T) {
c.processAccountAssociations()

for _, dest := range c.Sources[0].Destinations {
require.Equal(t, "acc-1", dest.DeliveryAccount.Id)
require.Equal(t, "acc-1", dest.DeliveryAccount.ID)
require.Equal(t, AccountDefinition{
Name: "oauth-def",
Config: map[string]interface{}{"oauth": true},
AuthenticationType: "OAuth",
}, dest.DeliveryAccount.AccountDefinition)
}, *dest.DeliveryAccount.AccountDefinition)
}
})

Expand Down Expand Up @@ -136,11 +138,11 @@ func TestAccountAssociations(t *testing.T) {

c.processAccountAssociations()

require.Equal(t, "acc-1", c.Sources[0].Destinations[0].DeleteAccount.Id)
require.Equal(t, "acc-1", c.Sources[0].Destinations[0].DeleteAccount.ID)
require.Equal(t, AccountDefinition{
Name: "oauth-def",
Config: map[string]interface{}{"oauth": true},
}, c.Sources[0].Destinations[0].DeleteAccount.AccountDefinition)
}, *c.Sources[0].Destinations[0].DeleteAccount.AccountDefinition)
})

t.Run("destination with no account configuration", func(t *testing.T) {
Expand Down Expand Up @@ -238,7 +240,168 @@ func TestAccountAssociations(t *testing.T) {

c.processAccountAssociations()

require.Equal(t, "acc-1", c.Sources[0].Destinations[0].DeliveryAccount.Id)
require.Empty(t, c.Sources[0].Destinations[0].DeliveryAccount.AccountDefinition)
require.Equal(t, "acc-1", c.Sources[0].Destinations[0].DeliveryAccount.ID)
require.Nil(t, c.Sources[0].Destinations[0].DeliveryAccount.AccountDefinition)
})

t.Run("blank account definition name for an account which will result in nil accountDefinition for the account added to the destination", func(t *testing.T) {
// Set up test configuration
c := &ConfigT{
Sources: []SourceT{
{
ID: "source-1",
Destinations: []DestinationT{
{
ID: "dest-1",
Config: map[string]interface{}{
"rudderAccountId": "acc-1",
},
},
},
},
},
Accounts: map[string]Account{
"acc-1": {
AccountDefinitionName: "", // Blank account definition name
Options: map[string]interface{}{"key1": "value1"},
Secret: map[string]interface{}{"secret1": "secretValue1"},
},
},
AccountDefinitions: map[string]AccountDefinition{
"oauth-def": {
Name: "oauth-def",
Config: map[string]interface{}{},
},
},
}

c.processAccountAssociations()

// Verify the account is populated but AccountDefinition is nil
require.Equal(t, "acc-1", c.Sources[0].Destinations[0].DeliveryAccount.ID)
require.Equal(t, "", c.Sources[0].Destinations[0].DeliveryAccount.AccountDefinitionName)
require.Equal(t, map[string]interface{}{"key1": "value1"}, c.Sources[0].Destinations[0].DeliveryAccount.Options)
require.Equal(t, map[string]interface{}{"secret1": "secretValue1"}, c.Sources[0].Destinations[0].DeliveryAccount.Secret)
require.Nil(t, c.Sources[0].Destinations[0].DeliveryAccount.AccountDefinition)

// Note: We can't easily test that no warning was logged without mocking the logger,
// but the behavior is correct - no warning should be logged for blank AccountDefinitionName
})

t.Run("This test ensures JSON serialization/deserialization works correctly with the Account so that destination will contain deliveryAccount/deleteAccount", func(t *testing.T) {
dest := DestinationT{
ID: "dest-1",
DeliveryAccount: &Account{
ID: "acc-1",
AccountDefinitionName: "oauth-def",
Options: map[string]interface{}{"key1": "value1"},
Secret: map[string]interface{}{"secret1": "secretValue1"},
AccountDefinition: &AccountDefinition{
Name: "oauth-def",
Config: map[string]interface{}{"oauth": true},
AuthenticationType: "OAuth",
},
},
DeleteAccount: &Account{
ID: "acc-2",
AccountDefinitionName: "oauth-def",
Options: map[string]interface{}{"key2": "value2"},
Secret: map[string]interface{}{"secret2": "secretValue2"},
AccountDefinition: &AccountDefinition{
Name: "oauth-def",
Config: map[string]interface{}{"oauth": true},
AuthenticationType: "OAuth",
},
},
}

// Serialize to JSON
jsonBytes, err := jsonrs.Marshal(dest)
require.NoError(t, err)

// Verify JSON field names
jsonStr := string(jsonBytes)
require.Contains(t, jsonStr, `"deliveryAccount":`)
require.Contains(t, jsonStr, `"deleteAccount":`)

// Deserialize from JSON
var destFromJSON DestinationT
err = jsonrs.Unmarshal(jsonBytes, &destFromJSON)
require.NoError(t, err)

// Verify fields were correctly deserialized
require.Equal(t, "dest-1", destFromJSON.ID)
require.Equal(t, "acc-1", destFromJSON.DeliveryAccount.ID)
require.Equal(t, "acc-2", destFromJSON.DeleteAccount.ID)
require.Equal(t, map[string]interface{}{"key1": "value1"}, destFromJSON.DeliveryAccount.Options)
require.Equal(t, map[string]interface{}{"key2": "value2"}, destFromJSON.DeleteAccount.Options)
require.Equal(t, "OAuth", destFromJSON.DeliveryAccount.AccountDefinition.AuthenticationType)
require.Equal(t, "OAuth", destFromJSON.DeleteAccount.AccountDefinition.AuthenticationType)
})

t.Run("we should get nil deliveryAccount when rudderAccountId is an empty string for deliveryAccount", func(t *testing.T) {
c := &ConfigT{
Sources: []SourceT{
{
ID: "source-1",
Destinations: []DestinationT{
{
ID: "dest-1",
Config: map[string]interface{}{
"rudderAccountId": "",
},
},
},
},
},
Accounts: map[string]Account{
"acc-1": {
AccountDefinitionName: "non-existent-def",
},
},
AccountDefinitions: map[string]AccountDefinition{
"oauth-def": {
Name: "oauth-def",
Config: map[string]interface{}{},
},
},
}

c.processAccountAssociations()

require.Nil(t, c.Sources[0].Destinations[0].DeliveryAccount)
})

t.Run("we should get nil deleteAccount when rudderDeleteAccountId is an empty string", func(t *testing.T) {
c := &ConfigT{
Sources: []SourceT{
{
ID: "source-1",
Destinations: []DestinationT{
{
ID: "dest-1",
Config: map[string]interface{}{
"rudderDeleteAccountId": "",
},
},
},
},
},
Accounts: map[string]Account{
"acc-1": {
AccountDefinitionName: "non-existent-def",
},
},
AccountDefinitions: map[string]AccountDefinition{
"oauth-def": {
Name: "oauth-def",
Config: map[string]interface{}{},
},
},
}

c.processAccountAssociations()

require.Nil(t, c.Sources[0].Destinations[0].DeleteAccount)
})
}
Loading