Skip to content

Commit

Permalink
temp working trying to get key roration working
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld committed Jun 5, 2024
1 parent e0efbad commit c0dabe2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
46 changes: 45 additions & 1 deletion relay/filedata_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,23 @@ func (a *relayFileDataActions) AddEnvironment(ae filedata.ArchiveEnvironment) {
return config
}
envConfig := envfactory.NewEnvConfigFactoryForOfflineMode(a.r.config.OfflineMode).MakeEnvironmentConfig(ae.Params)
_, _, err := a.r.addEnvironment(ae.Params.Identifiers, envConfig, transformConfig)
env, _, err := a.r.addEnvironment(ae.Params.Identifiers, envConfig, transformConfig)
if err != nil {
a.r.loggers.Errorf(logMsgAutoConfEnvInitError, ae.Params.Identifiers.GetDisplayName(), err)
return
}

// Note: the following lines (until end marker) are
// copied from autoconfig_actions.go.
if ae.Params.ExpiringSDKKey != "" {
if foundEnvWithOldKey, _ := a.r.getEnvironment(ae.Params.ExpiringSDKKey); foundEnvWithOldKey == nil {
env.AddCredential(ae.Params.ExpiringSDKKey)
env.DeprecateCredential(ae.Params.ExpiringSDKKey)
a.r.addedEnvironmentCredential(env, ae.Params.ExpiringSDKKey)
}
}
// End.

select {
case updates := <-updatesCh:
if a.envUpdates == nil {
Expand Down Expand Up @@ -83,6 +95,38 @@ func (a *relayFileDataActions) UpdateEnvironment(ae filedata.ArchiveEnvironment)
env.SetTTL(ae.Params.TTL)
env.SetSecureMode(ae.Params.SecureMode)

// Note: the following lines (until end marker) are
// copied from autoconfig_actions.go.
var oldSDKKey config.SDKKey
var oldMobileKey config.MobileKey
for _, c := range env.GetCredentials() {
switch c := c.(type) {
case config.SDKKey:
oldSDKKey = c
case config.MobileKey:
oldMobileKey = c
}
}

if ae.Params.SDKKey != oldSDKKey {
env.AddCredential(ae.Params.SDKKey)
a.r.addedEnvironmentCredential(env, ae.Params.SDKKey)
if ae.Params.ExpiringSDKKey == oldSDKKey {
env.DeprecateCredential(oldSDKKey)
} else {
a.r.removingEnvironmentCredential(oldSDKKey)
env.RemoveCredential(oldSDKKey)
}
}

if ae.Params.MobileKey != oldMobileKey {
env.AddCredential(ae.Params.MobileKey)
a.r.addedEnvironmentCredential(env, ae.Params.MobileKey)
a.r.removingEnvironmentCredential(oldMobileKey)
env.RemoveCredential(oldMobileKey)
}
// End.

// SDKData will be non-nil only if the flag/segment data for the environment has actually changed.
if ae.SDKData != nil {
updates.Init(ae.SDKData)
Expand Down
28 changes: 28 additions & 0 deletions relay/filedata_actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,31 @@ func TestOfflineModeEventsAreAcceptedAndDiscardedIfSendEventsIsTrue(t *testing.T
})
})
}

func TestOfflineModeDeprecateSDKKeyIsAccepted(t *testing.T) {
offlineModeTest(t, config.Config{}, func(p offlineModeTestParams) {
p.updateHandler.AddEnvironment(testFileDataExpiringSDKKey)

client1 := p.awaitClient()
assert.Equal(t, testFileDataExpiringSDKKey.Params.SDKKey, client1.Key)

env := p.awaitEnvironment(testFileDataEnv1.Params.EnvID)

var sdkKey config.SDKKey
for _, c := range env.GetCredentials() {
if c, ok := c.(config.SDKKey); ok {
sdkKey = c
break
}
}
var expiringSDKKey config.SDKKey
for _, c := range env.GetDeprecatedCredentials() {
if c, ok := c.(config.SDKKey); ok {
expiringSDKKey = c
break
}
}
assert.Equal(t, testFileDataExpiringSDKKey.Params.SDKKey, sdkKey)
assert.Equal(t, testFileDataExpiringSDKKey.Params.ExpiringSDKKey, expiringSDKKey)
})
}
22 changes: 22 additions & 0 deletions relay/filedata_testdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,25 @@ var testFileDataEnv2 = filedata.ArchiveEnvironment{
},
},
}

var testFileDataExpiringSDKKey = filedata.ArchiveEnvironment{
Params: envfactory.EnvironmentParams{
EnvID: config.EnvironmentID("env1"),
SDKKey: config.SDKKey("sdkkey2"),
ExpiringSDKKey: config.SDKKey("sdkkey1"),
Identifiers: relayenv.EnvIdentifiers{
ProjName: "Project",
ProjKey: "project",
EnvName: "Env1",
EnvKey: "env1",
},
},
SDKData: []ldstoretypes.Collection{
{
Kind: ldstoreimpl.Features(),
Items: []ldstoretypes.KeyedItemDescriptor{
{Key: testFileDataFlag1.Key, Item: sharedtest.FlagDesc(testFileDataFlag1)},
},
},
},
}

0 comments on commit c0dabe2

Please sign in to comment.