Skip to content

Commit

Permalink
feat: add Edge authentication support (#111)
Browse files Browse the repository at this point in the history
Co-authored-by: Jakub Bednář <[email protected]>
  • Loading branch information
alespour and bednar authored Aug 5, 2024
1 parent c0e42c8 commit 9fba8a2
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 25 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

1. [#101](https://github.com/InfluxCommunity/influxdb3-csharp/pull/101): Add standard `user-agent` header to all calls.
1. [#110](https://github.com/InfluxCommunity/influxdb3-csharp/pull/110): InfluxDB Edge (OSS) error handling.
1. [#111](https://github.com/InfluxCommunity/influxdb3-csharp/pull/111): Add InfluxDB Edge (OSS) authentication support.

### Migration Notice

- `InfluxDBClient` constructor with connection options has new option `authScheme` with `null` default value:

```diff
- public InfluxDBClient(string host, string token, string? organization = null, string? database = null);
+ public InfluxDBClient(string host, string token, string? organization = null, string? database = null, string? authScheme = null)
```

This new option is used for Edge (OSS) authentication.

## 0.6.0 [2024-04-16]

Expand Down
37 changes: 37 additions & 0 deletions Client.Test/Config/ClientConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public void CreateFromConnectionStringBasic()
});
}

[Test]
public void CreateFromConnectionStringWithAuthScheme()
{
var cfg = new ClientConfig("http://localhost:8086?token=my-token&org=my-org&authScheme=my-scheme");
Assert.That(cfg, Is.Not.Null);
cfg.Validate();
Assert.Multiple(() =>
{
Assert.That(cfg.Host, Is.EqualTo("http://localhost:8086/"));
Assert.That(cfg.Token, Is.EqualTo("my-token"));
Assert.That(cfg.AuthScheme, Is.EqualTo("my-scheme"));
Assert.That(cfg.WriteOptions, Is.EqualTo(null));
});
}

[Test]
public void CreateFromConnectionStringWithWriteOptions()
{
Expand Down Expand Up @@ -143,6 +158,28 @@ public void CreateFromEnvBasic()
});
}

[Test]
public void CreateFromEnvWithAuthScheme()
{
var env = new Dictionary<String, String>
{
{"INFLUX_HOST", "http://localhost:8086"},
{"INFLUX_TOKEN", "my-token"},
{"INFLUX_AUTH_SCHEME", "my-scheme"},
};
SetEnv(env);
var cfg = new ClientConfig(env);
Assert.That(cfg, Is.Not.Null);
cfg.Validate();
Assert.Multiple(() =>
{
Assert.That(cfg.Host, Is.EqualTo("http://localhost:8086/"));
Assert.That(cfg.Token, Is.EqualTo("my-token"));
Assert.That(cfg.AuthScheme, Is.EqualTo("my-scheme"));
Assert.That(cfg.WriteOptions, Is.EqualTo(null));
});
}

[Test]
public void CreateFromEnvWithWriteOptions()
{
Expand Down
16 changes: 16 additions & 0 deletions Client.Test/Internal/RestClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ public async Task Authorization()
Assert.That(requests[0].RequestMessage.Headers?["Authorization"][0], Is.EqualTo("Token my-token"));
}

[Test]
public async Task AuthorizationCustomScheme()
{
CreateAndConfigureRestClient(new ClientConfig
{
Host = MockServerUrl,
Token = "my-token",
AuthScheme = "my-scheme"
});
await DoRequest();

var requests = MockServer.LogEntries.ToList();

Assert.That(requests[0].RequestMessage.Headers?["Authorization"][0], Is.EqualTo("my-scheme my-token"));
}

[Test]
public async Task UserAgent()
{
Expand Down
9 changes: 9 additions & 0 deletions Client/Config/ClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace InfluxDB3.Client.Config;
/// You can configure following options:
/// - Host: The URL of the InfluxDB server.
/// - Token: The authentication token for accessing the InfluxDB server.
/// - AuthScheme: Token authentication scheme. Default is 'null' for Cloud access. Set to 'Bearer' for Edge access.
/// - Organization: The organization to be used for operations.
/// - Database: The database to be used for InfluxDB operations.
/// - Headers: The set of HTTP headers to be included in requests.
Expand Down Expand Up @@ -44,6 +45,7 @@ public class ClientConfig
{
internal const string EnvInfluxHost = "INFLUX_HOST";
internal const string EnvInfluxToken = "INFLUX_TOKEN";
internal const string EnvInfluxAuthScheme = "INFLUX_AUTH_SCHEME";
internal const string EnvInfluxOrg = "INFLUX_ORG";
internal const string EnvInfluxDatabase = "INFLUX_DATABASE";
internal const string EnvInfluxPrecision = "INFLUX_PRECISION";
Expand All @@ -67,6 +69,7 @@ internal ClientConfig(string connectionString)
Host = uri.GetLeftPart(UriPartial.Path);
var values = HttpUtility.ParseQueryString(uri.Query);
Token = values.Get("token");
AuthScheme = values.Get("authScheme");
Organization = values.Get("org");
Database = values.Get("database");
ParsePrecision(values.Get("precision"));
Expand All @@ -80,6 +83,7 @@ internal ClientConfig(IDictionary env)
{
Host = (string)env[EnvInfluxHost];
Token = env[EnvInfluxToken] as string;
AuthScheme = env[EnvInfluxAuthScheme] as string;
Organization = env[EnvInfluxOrg] as string;
Database = env[EnvInfluxDatabase] as string;
ParsePrecision(env[EnvInfluxPrecision] as string);
Expand All @@ -100,6 +104,11 @@ public string Host
/// </summary>
public string? Token { get; set; }

/// <summary>
/// Token authentication scheme.
/// </summary>
public string? AuthScheme { get; set; }

/// <summary>
/// The organization to be used for operations.
/// </summary>
Expand Down
Loading

0 comments on commit 9fba8a2

Please sign in to comment.