Skip to content

Commit

Permalink
Merge pull request #21 from featurehub-io/prevent-bad-sdk-key
Browse files Browse the repository at this point in the history
Support failure for bad SDK key
  • Loading branch information
rvowles committed Apr 23, 2024
2 parents fe289b0 + 58daf3c commit fb5832b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
18 changes: 18 additions & 0 deletions FeatureHubSDK/EdgeFeatureHubConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public interface IFeatureHubConfig

void AddAnalyticCollector(IAnalyticsCollector collector);
}

public class FeatureHubKeyInvalidException : Exception
{
public FeatureHubKeyInvalidException(string message)
: base(message)
{
}

public FeatureHubKeyInvalidException(string message, Exception innerException)
: base(message, innerException)
{
}
}

public class EdgeFeatureHubConfig : IFeatureHubConfig
{
Expand All @@ -47,6 +60,11 @@ public EdgeFeatureHubConfig(string edgeUrl, string sdkKey)
{
_serverEvaluation = sdkKey != null && !sdkKey.Contains("*"); // two part keys are server evaluated

if (!sdkKey.Contains("/"))
{
throw new FeatureHubKeyInvalidException($"The SDK key `{sdkKey}` is invalid");
}

if (edgeUrl.EndsWith("/"))
{
edgeUrl = edgeUrl.Substring(0, edgeUrl.Length - 1);
Expand Down
22 changes: 20 additions & 2 deletions FeatureHubSDK/EventServiceListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,19 @@ public async Task ContextChange(string newHeader)

return headers;
}

private string DefaultEnvConfig(string envVar, string defaultValue)
{
return Environment.GetEnvironmentVariable(envVar) ?? defaultValue;
}

public void Init()
{
if (_closed) return;

var config = new Configuration(uri: new UriBuilder(_featureHost.Url).Uri,
backoffResetThreshold: TimeSpan.MaxValue,
delayRetryDuration: TimeSpan.Zero,
backoffResetThreshold: TimeSpan.FromSeconds(int.Parse(DefaultEnvConfig("FEATUREHUB_BACKOFF_RETRY_LIMIT", "100"))),
delayRetryDuration: TimeSpan.FromSeconds(int.Parse(DefaultEnvConfig("FEATUREHUB_DELAY_RETRY_MS", "10000"))),
requestHeaders: _featureHost.ServerEvaluation ? BuildContextHeader() : null);

if (FeatureLogging.InfoLogger != null)
Expand All @@ -122,6 +127,19 @@ public void Init()
}

_eventSource = new EventSource(config);
_eventSource.Error += (sender, ex) =>
{
if (ex.Exception is EventSourceServiceUnsuccessfulResponseException result)
{
if (result.StatusCode != 503)
{
_repository.Notify(SSEResultState.Failure, null);
FeatureLogging.ErrorLogger(this, "Server issued a failure, stopping.");
_closed = true;
_eventSource.Close();
}
}
};

// if (FeatureLogging.DebugLogger != null)
// {
Expand Down
6 changes: 3 additions & 3 deletions FeatureHubSDK/FeatureHubSDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<RepositoryUrl>https://github.com/featurehub-io/featurehub-dotnet-sdk</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>FeatureFlags FeatureToggles SDK Flags Toggles RemoteConfig</PackageTags>
<PackageReleaseNotes>2.2.0 - updates for caching support, update min dependencies.</PackageReleaseNotes>
<Version>2.2.0</Version>
<PackageVersion>2.2.0</PackageVersion>
<PackageReleaseNotes>2.3.0 - check valid keys and stop calling server on failure.</PackageReleaseNotes>
<Version>2.3.0</Version>
<PackageVersion>2.3.0</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 10 additions & 4 deletions FeatureHubTest/ConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ class ConfigTest
[Test]
public void EnsureConfigCorrectlyDeterminesUrl()
{
var cfg = new EdgeFeatureHubConfig("http://localhost:80/", "123*123");
var cfg = new EdgeFeatureHubConfig("http://localhost:80/", "id/123*123");
Assert.IsTrue(!cfg.ServerEvaluation);
Assert.AreEqual("http://localhost:80/features/123*123", cfg.Url);
Assert.AreEqual("http://localhost:80/features/id/123*123", cfg.Url);

cfg = new EdgeFeatureHubConfig("http://localhost:80", "123");
cfg = new EdgeFeatureHubConfig("http://localhost:80", "id/123");
Assert.IsTrue(cfg.ServerEvaluation);
Assert.AreEqual("http://localhost:80/features/123", cfg.Url);
Assert.AreEqual("http://localhost:80/features/id/123", cfg.Url);
}


[Test]
public void InvalidKeyStructureFails()
{
Assert.Throws<FeatureHubSDK.FeatureHubKeyInvalidException>(() =>
new EdgeFeatureHubConfig("http://localhost:80/", "123*123"));
}
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Details about what general features are available in FeatureHub SDKs are [availa

## Changelog

- 2.3.0 - Support for preventing badly formatted API keys from being passed in. Support for API keys and cause 4xx errors
to stop polling. Support for overriding the number of backoff attempts made (`FEATUREHUB_BACKOFF_RETRY_LIMIT` - defaults to 100),
and delay retry timeout (was zero, now 10s, controlled by `FEATUREHUB_DELAY_RETRY_MS`).
- 2.2.0 - FeatureHub 1.5.9 support - supporting Fastly integration, server side polling period control, stale environments.
We have upgraded to the 6.0.1 OpenAPI compiler, but gone no further because it generates code that does not work.
- 2.1.5 - FeatureHub 1.5.6 is not returning the name of the feature and this is causing the 2.1.4 to version to break.
Expand Down

0 comments on commit fb5832b

Please sign in to comment.