Skip to content

Commit

Permalink
Merge pull request #686 from RedHatInsights/handle_tenant_refresh
Browse files Browse the repository at this point in the history
Handle tenant-refresh flow on the users table
  • Loading branch information
lpichler authored Mar 6, 2024
2 parents 7b67cf2 + 2958248 commit 3bac714
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type SourcesApiConfig struct {
SecretStore string
TenantTranslatorUrl string
Env string
HandleTenantRefresh bool

SecretsManagerAccessKey string
SecretsManagerSecretKey string
Expand Down Expand Up @@ -97,6 +98,7 @@ func (s SourcesApiConfig) String() string {
fmt.Fprintf(&b, "%s=%v ", "SecretStore", parsedConfig.SecretStore)
fmt.Fprintf(&b, "%s=%v ", "TenantTranslatorUrl", parsedConfig.TenantTranslatorUrl)
fmt.Fprintf(&b, "%s=%v ", "Env", parsedConfig.Env)
fmt.Fprintf(&b, "%s=%v ", "HandleTenantRefresh", parsedConfig.HandleTenantRefresh)
fmt.Fprintf(&b, "%s=%v ", "SecretsManagerPrefix", parsedConfig.SecretsManagerPrefix)
fmt.Fprintf(&b, "%s=%v ", "LocalStackURL", parsedConfig.LocalStackURL)
return b.String()
Expand Down Expand Up @@ -217,6 +219,9 @@ func Get() *SourcesApiConfig {

options.SetDefault("Env", os.Getenv("SOURCES_ENV"))

handleTenantRefresh, _ := strconv.ParseBool(os.Getenv("HANDLE_TENANT_REFRESH"))
options.SetDefault("HandleTenantRefresh", handleTenantRefresh)

options.SetDefault("FeatureFlagsService", os.Getenv("FEATURE_FLAGS_SERVICE"))

if os.Getenv("SOURCES_ENV") == "prod" {
Expand Down Expand Up @@ -327,6 +332,7 @@ func Get() *SourcesApiConfig {
SecretStore: options.GetString("SecretStore"),
TenantTranslatorUrl: options.GetString("TenantTranslatorUrl"),
Env: options.GetString("Env"),
HandleTenantRefresh: options.GetBool("HandleTenantRefresh"),
SecretsManagerAccessKey: options.GetString("SecretsManagerAccessKey"),
SecretsManagerSecretKey: options.GetString("SecretsManagerSecretKey"),
SecretsManagerPrefix: options.GetString("SecretsManagerPrefix"),
Expand Down
15 changes: 14 additions & 1 deletion dao/user_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package dao
import (
"fmt"

"github.com/RedHatInsights/sources-api-go/config"
m "github.com/RedHatInsights/sources-api-go/model"
"gorm.io/gorm/clause"
)

var GetUserDao func(*int64) UserDao
Expand Down Expand Up @@ -40,7 +42,18 @@ func (u *userDaoImpl) FindOrCreate(userID string) (*m.User, error) {
if err != nil {
user.TenantID = *u.TenantID
user.UserID = userID
err = DB.Debug().Create(&user).Error

stmt := DB.Debug()

if config.Get().HandleTenantRefresh {
// tenant refresh = we run into conflicts where one user_id is tied to another tenant (since the org_id changes), therefore we tack an `ON CONFLICT UPDATE` if we're in an environment where this can happen
stmt = stmt.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "user_id"}},
DoUpdates: clause.AssignmentColumns([]string{"tenant_id"}),
})
}

err = stmt.Create(&user).Error
if err != nil {
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions deploy/clowdapp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ objects:
name: sources-secrets-manager
key: localstack_url
optional: true
- name: HANDLE_TENANT_REFRESH
value: ${HANDLE_TENANT_REFRESH}
readinessProbe:
tcpSocket:
port: 8000
Expand Down Expand Up @@ -451,3 +453,6 @@ parameters:
- description: Which secret-store should sources use for authentications
name: SECRET_STORE
value: "database"
- description: Whether to handle conflicts on user_ids during a tenant refresh
name: HANDLE_TENANT_REFRESH
value: "false"

0 comments on commit 3bac714

Please sign in to comment.