Skip to content

Commit bb5925e

Browse files
Merge pull request #492 from GridProtectionAlliance/influx-token-auth
InfluxDBAdapters: Add option for token authorization to InfluxDB output adapter
2 parents 025e5c4 + c3bea56 commit bb5925e

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

Source/Libraries/Adapters/InfluxDBAdapters/InfluxDBOutputAdapter.cs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ public class InfluxDBOutputAdapter : OutputAdapterBase
4848

4949
// Constants
5050

51-
/// <summary>
52-
/// Default value for <see cref="UserName"/>.
53-
/// </summary>
54-
public const string DefaultUserName = "root";
55-
56-
/// <summary>
57-
/// Default value for <see cref="Password"/>.
58-
/// </summary>
59-
public const string DefaultPassword = "root";
60-
6151
/// <summary>
6252
/// Default value for <see cref="UseParallelPosting"/>.
6353
/// </summary>
@@ -76,6 +66,7 @@ public class InfluxDBOutputAdapter : OutputAdapterBase
7666
private string m_databaseName; // Database name forInfluxDB connection
7767
private string m_userName; // Username for InfluxDB connection
7868
private string m_password; // Password for InfluxDB connection
69+
private string m_authorizationToken; // Token for InfluxDB authorization
7970
private bool m_useParallelPosting; // Enable parallel posting
8071
private int m_valuesPerPost; // Maximum values to send per post (when using parallel posting)
8172
private string m_connectionResponse; // Response from connection attempt
@@ -139,7 +130,7 @@ public string DatabaseName
139130
/// <summary>
140131
/// Gets or sets the user name for the InfluxDB connection.
141132
/// </summary>
142-
[ConnectionStringParameter, Description("Defines the the user name for the InfluxDB connection."), DefaultValue(DefaultUserName)]
133+
[ConnectionStringParameter, Description("Defines the the user name for the InfluxDB connection."), DefaultValue(null)]
143134
public string UserName
144135
{
145136
get
@@ -155,7 +146,7 @@ public string UserName
155146
/// <summary>
156147
/// Gets or sets the password for the InfluxDB connection.
157148
/// </summary>
158-
[ConnectionStringParameter, Description("Defines the password for the InfluxDB connection."), DefaultValue(DefaultPassword)]
149+
[ConnectionStringParameter, Description("Defines the password for the InfluxDB connection."), DefaultValue(null)]
159150
public string Password
160151
{
161152
get
@@ -168,6 +159,22 @@ public string Password
168159
}
169160
}
170161

162+
/// <summary>
163+
/// Gets or sets the password for the InfluxDB connection.
164+
/// </summary>
165+
[ConnectionStringParameter, Description("Defines the token for the InfluxDB authorization."), DefaultValue(null)]
166+
public string AuthorizationToken
167+
{
168+
get
169+
{
170+
return m_authorizationToken;
171+
}
172+
set
173+
{
174+
m_authorizationToken = value;
175+
}
176+
}
177+
171178
/// <summary>
172179
/// Gets or sets flag that determines if multiple posts to InfluxDB should be made in parallel.
173180
/// </summary>
@@ -310,13 +317,12 @@ public override void Initialize()
310317

311318
if (settings.TryGetValue("userName", out setting))
312319
m_userName = setting;
313-
else
314-
m_userName = DefaultUserName;
315320

316321
if (settings.TryGetValue("password", out setting))
317322
m_password = setting;
318-
else
319-
m_password = DefaultPassword;
323+
324+
if (settings.TryGetValue("authorizationToken", out setting))
325+
m_authorizationToken = setting;
320326

321327
if (settings.TryGetValue("useParallelPosting", out setting))
322328
m_useParallelPosting = setting.ParseBoolean();
@@ -333,7 +339,8 @@ public override void Initialize()
333339
// Define request URI
334340
UriBuilder requestUri = new UriBuilder(m_serverUri);
335341
requestUri.Path = string.Format("db/{0}/series", m_databaseName.UriEncode());
336-
requestUri.Query = string.Format("u={0}&p={1}", m_userName.UriEncode(), m_password.UriEncode());
342+
if (!string.IsNullOrEmpty(m_userName) && !string.IsNullOrEmpty(m_password))
343+
requestUri.Query = string.Format("u={0}&p={1}", m_userName.UriEncode(), m_password.UriEncode());
337344
m_requestUri = requestUri.Uri;
338345
}
339346

@@ -347,7 +354,8 @@ protected override void AttemptConnection()
347354
// Setup an authenticate request
348355
UriBuilder requestUri = new UriBuilder(m_serverUri);
349356
requestUri.Path = string.Format("db/{0}/authenticate", m_databaseName.UriEncode());
350-
requestUri.Query = string.Format("u={0}&p={1}", m_userName.UriEncode(), m_password.UriEncode());
357+
if (!string.IsNullOrEmpty(m_userName) && !string.IsNullOrEmpty(m_password))
358+
requestUri.Query = string.Format("u={0}&p={1}", m_userName.UriEncode(), m_password.UriEncode());
351359

352360
// Create a web request for authentication that will at least make sure the server is available
353361
HttpWebRequest request = WebRequest.Create(requestUri.Uri) as HttpWebRequest;
@@ -356,6 +364,9 @@ protected override void AttemptConnection()
356364
{
357365
request.ContentType = "application/json";
358366

367+
if (!string.IsNullOrEmpty(m_authorizationToken))
368+
request.Headers.Add(HttpRequestHeader.Authorization, $"Token {m_authorizationToken}");
369+
359370
// Attempt query - if this doesn't throw an exception, query succeeded
360371
using (WebResponse response = request.GetResponse())
361372
{
@@ -431,6 +442,9 @@ private void PostMeasurementsToArchive(IMeasurement[] measurements)
431442
request.AllowWriteStreamBuffering = true;
432443
request.ContentType = "application/json";
433444

445+
if (!string.IsNullOrEmpty(m_authorizationToken))
446+
request.Headers.Add(HttpRequestHeader.Authorization, $"Token {m_authorizationToken}");
447+
434448
// Build a JSON post expression with measurement values to use as post data
435449
StringBuilder jsonData = new StringBuilder();
436450

0 commit comments

Comments
 (0)