Skip to content

Commit

Permalink
Merge pull request #156 from launchdarkly/eb/ch83954/enterprise-3-aut…
Browse files Browse the repository at this point in the history
…oconf-stream

(RPE - #3) implement auto-config stream client
  • Loading branch information
eli-darkly authored Aug 6, 2020
2 parents 41bf9c5 + 823c909 commit d090a4d
Show file tree
Hide file tree
Showing 10 changed files with 1,407 additions and 0 deletions.
56 changes: 56 additions & 0 deletions enterprise/autoconfig/message_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package autoconfig

import (
"time"

"github.com/launchdarkly/ld-relay/v6/core/config"
)

// MessageHandler defines the methods that StreamManager will call when it receives messages
// from the auto-configuration stream.
type MessageHandler interface {
// AddEnvironment is called when the stream has provided a configuration for an environment
// that StreamManager has not seen before. This can happen due to either a "put" or a "patch".
AddEnvironment(params EnvironmentParams)

// UpdateEnvironment is called when the stream has provided a new configuration for an
// existing environment. This can happen due to either a "put" or a "patch".
UpdateEnvironment(params EnvironmentParams)

// DeleteEnvironment is called when an environment should be removed, due to either a "delete"
// message, or a "put" that no longer contains that environment.
DeleteEnvironment(id config.EnvironmentID)

// KeyExpired is called when a key that was previously provided in EnvironmentParams.ExpiringSDKKey
// has now expired. Relay should disconnect any clients currently using that key and reject any
// future requests that use it.
KeyExpired(id config.EnvironmentID, oldKey config.SDKKey)
}

// EnvironmentParams contains information that MessageHandler passes to StreamHandler to describe
// an environment. This is not the same type that we use for the environment representation in the
// auto-config stream, because not all of the properties there are relevant to Relay.
type EnvironmentParams struct {
// ID is the environment ID.
ID config.EnvironmentID

// Name is the human-readable unique name, which is always in the format "ProjectName EnvName".
Name string

// SDKKey is the environment's SDK key; if there is more than one active key, it is the latest.
SDKKey config.SDKKey

// MobileKey is the environment's mobile key.
MobileKey config.MobileKey

// DeprecatedSDKKey is an additional SDK key that should also be allowed (but not surfaced as
// the canonical one), or "" if none. StreamManager is responsible for knowing when the key
// will expire.
ExpiringSDKKey config.SDKKey

// TTL is the cache TTL for PHP clients.
TTL time.Duration

// SecureMode is true if secure mode is required for this environment.
SecureMode bool
}
5 changes: 5 additions & 0 deletions enterprise/autoconfig/package_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Package autoconfig contains a client for the auto-configuration streaming service.
//
// This abstracts away the protocol details so that the RelayEnterprise implementation only
// needs to know what environments it should add, update, or remove.
package autoconfig
33 changes: 33 additions & 0 deletions enterprise/autoconfig/reps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package autoconfig

import (
"github.com/launchdarkly/ld-relay/v6/core/config"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldtime"
)

// Representation of an environment that is being added or updated (in either a "put" or a
// "patch" message).
type environmentRep struct {
EnvID config.EnvironmentID `json:"envID"`
EnvKey string `json:"envKey"`
EnvName string `json:"envName"`
MobKey config.MobileKey `json:"mobKey"`
ProjKey string `json:"projKey"`
ProjName string `json:"projName"`
SDKKey sdkKeyRep `json:"sdkKey"`
DefaultTTL int `json:"defaultTtl"`
SecureMode bool `json:"secureMode"`
Version int `json:"version"`
}

// Description of an SDK key optionally accompanied by an old expiring key.
type sdkKeyRep struct {
Value config.SDKKey `json:"value"`
Expiring expiringKeyRep
}

// An old key that will expire at the specified date/time.
type expiringKeyRep struct {
Value config.SDKKey `json:"value"`
Timestamp ldtime.UnixMillisecondTime `json:"timestamp"`
}
45 changes: 45 additions & 0 deletions enterprise/autoconfig/reps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package autoconfig

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/launchdarkly/ld-relay/v6/core/config"
"gopkg.in/launchdarkly/go-sdk-common.v2/ldtime"
)

func TestEnvironmentRepJSONFormat(t *testing.T) {
jsonStr := `{
"envID": "envid1",
"envKey": "envkey",
"envName": "envname",
"mobKey": "mobkey",
"projKey": "projkey",
"projName": "projname",
"sdkKey": { "value": "sdkkey", "expiring": { "value": "oldkey", "timestamp": 10000 } },
"defaultTtl": 2,
"secureMode": true
}`
var rep environmentRep
require.NoError(t, json.Unmarshal([]byte(jsonStr), &rep))
assert.Equal(t, environmentRep{
EnvID: config.EnvironmentID("envid1"),
EnvKey: "envkey",
EnvName: "envname",
MobKey: config.MobileKey("mobkey"),
ProjKey: "projkey",
ProjName: "projname",
SDKKey: sdkKeyRep{
Value: config.SDKKey("sdkkey"),
Expiring: expiringKeyRep{
Value: config.SDKKey("oldkey"),
Timestamp: ldtime.UnixMillisecondTime(10000),
},
},
DefaultTTL: 2,
SecureMode: true,
}, rep)
}
Loading

0 comments on commit d090a4d

Please sign in to comment.