vs4vijay/newman-reporter-influxdb

Sweep: Connection with InfluxDB needs Authorization API key, not user/password authentification

Opened this issue · 1 comments

Details

Hello
As it is said in the official doc :
"All requests to the InfluxDB v2 API must include an InfluxDB API token."
Therefore, i'm unable to connect with this reporter to my DB.

Is there a workaround?

Thx

Checklist
  • Modify src/http.service.jseaf726b Edit
  • Running GitHub Actions for src/http.service.jsEdit
  • Modify src/http.service.js4fa97d4 Edit
  • Running GitHub Actions for src/http.service.jsEdit
  • Modify src/http.service.jsa3dc928 Edit
  • Running GitHub Actions for src/http.service.jsEdit

🚀 Here's the PR! #46

See Sweep's progress at the progress dashboard!
Sweep Basic Tier: I'm using GPT-4. You have 5 GPT-4 tickets left for the month and 3 for the day. (tracking ID: 5263eb8f83)

For more GPT-4 tickets, visit our payment portal. For a one week free trial, try Sweep Pro (unlimited GPT-4 tickets).

Tip

I can email you next time I complete a pull request if you set up your email here!


Actions (click)

  • ↻ Restart Sweep

GitHub Actions✓

Here are the GitHub Actions logs prior to making any changes:

Sandbox logs for adbb171
Checking src/http.service.js for syntax errors... ✅ src/http.service.js has no syntax errors! 1/1 ✓
Checking src/http.service.js for syntax errors...
✅ src/http.service.js has no syntax errors!

Sandbox passed on the latest develop, so sandbox checks will be enabled for this issue.


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description.

## Getting Started
1. Install `newman`
2. Install `newman-reporter-influxdb`
3. Install InfluxDB (Get the server address, port, database name, etc)
### Prerequisites
1. `node` and `npm`
2. `newman` - `npm install -g newman`
3. [InfluxDB](https://github.com/influxdata/influxdb)
---
## Installation
```console
npm install -g newman-reporter-influxdb
```
> Installation should be done globally if newman is installed globally, otherwise install without `-g` option
---
## Usage
Specify `-r influxdb` option while running the collection
```bash
newman run <collection-url> -r influxdb \
--reporter-influxdb-server <server-ip> \
--reporter-influxdb-port <server-port> \
--reporter-influxdb-name <database-name> \
--reporter-influxdb-measurement <measurement-name>
```
- By default, reporter consider influxdb version 1.x (i.e 1.7, 1.8)
- In case of InfluxDB version 2, specify version, org and bucket name as well
- `--reporter-influxdb-version 2`
- `--reporter-influxdb-org <org-name>`
- `--reporter-influxdb-name <bucket-name>`
Example:
```
# For InfluxDB version 1.x
newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv -r influxdb \
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name newman_reports \
--reporter-influxdb-measurement api_results
# For InfluxDB version 2.x
newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv -r influxdb \
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-org viz \
--reporter-influxdb-version 2 \
--reporter-influxdb-username viz \
--reporter-influxdb-password db123456 \
--reporter-influxdb-name viz \
--reporter-influxdb-measurement api_results
```
### Options:
**Option** | **Remarks**
--- | ---
`--reporter-influxdb-server` | IP Address or Host of InfluxDB
`--reporter-influxdb-port` | Port no. (Usually `8086`)
`--reporter-influxdb-version` | InfluxDB Version `1`, `2` (default `1`)
`--reporter-influxdb-org` | InfluxDB Org (For InfluxDB version 2.x)
`--reporter-influxdb-port` | Port no. (Usually `8086`)
`--reporter-influxdb-name` | Database name (or Bucket name for InfluxDB version 2.x)
`--reporter-influxdb-measurement` | Measurement Point name (If not provided, then reporter will create measurement with prefix `newman_results-<timestamp>`)
`--reporter-influxdb-username` (*Optional*) | Username created for InfluxDB (e.g. `newman_user`)
`--reporter-influxdb-password` (*Optional*) | Password of the user (e.g. `p@ssw0rd`)
`--reporter-influxdb-identifier` (*Optional*) | An identifier to be passed to InfluxDB (default: `run-${Date.now()}`)
`--reporter-influxdb-mode` | Transmission Mode `http`, `https`, `udp` (default: `http`)
`--reporter-debug` | Enable debug mode (default: `false`)
---
## Compatibility
**newman-reporter-influxdb** | **InfluxDB**
--- | ---
v1.0.0+ | v1.7
v2.0.0+ | v1.8, v2.x
#### Notes:
- This reporter currently uses InfluxDB HTTP APIs / TCP Protocol to send data
---
## To Do
- [x] Convert to ES6 based version
- [x] Folder Structure
- [x] Username and Password support
- [x] Include UDP Reporter as well
- [x] HTTPS Support
- [ ] Add batch operation
- [ ] ESLint / StandardJS
- [x] CI/CD with Github Actions
- [ ] HealthCheck to InfluxDB
- [ ] Remove axios to make it lightweight
- [x] Compatibility with InfluxDB 2.x
- [ ] Tests
---
## Development
- `npm pack`
- `npm i -g newman-reporter-<name>.<version>.tgz`
- OR `make local-install`
- `make test-v1` / `make test-v2`
---
### Development Notes
```
npm publish --access public
- name: npm publish
run: |
LATEST=`npm view . version`
CURRENT=`cat package.json | jq -r .version`
if [ "$LATEST" != "$CURRENT" ]
then
npm ci
npm publish
fi
{
"scripts": {
"postpublish" : "PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]') && git tag $PACKAGE_VERSION && git push --tags"
}
}
PACKAGE_VERSION=$(cat package.json \
| grep version \
| head -1 \
| awk -F: '{ print $2 }' \
| sed 's/[",]//g' \
| tr -d '[[:space:]]')
echo $PACKAGE_VERSION
https://img.shields.io/npm/v/newman-reporter-influxdb.svg
<a href="https://www.npmjs.com/package/newman-reporter-influxdb"><img src="https://img.shields.io/npm/v/newman-reporter-influxdb.svg" alt="npm version"></a>
<a href="https://www.npmjs.com/package/newman-reporter-influxdb"><img src="https://img.shields.io/npm/dm/newman-reporter-influxdb.svg" alt="npm downloads"></a>
<img alt="GitHub All Releases" src="https://img.shields.io/github/downloads/vs4vijay/newman-reporter-influxdb/total">
https://nodejs.org/api/http.html#http_http_request_url_options_callback
https://nodejs.dev/making-http-requests-with-nodejs
https://github.com/influxdata/influxdb/blob/1.7/services/udp/README.md
https://docs.influxdata.com/influxdb/v1.7/supported_protocols/udp/
[udp]
enabled = true
bind-address = ":8086"
database = "newman_reports_udp"
batch-size = 1000
batch-timeout = “1s”
To write, just send newline separated line protocol over UDP. Can send one point at a time (not very performant) or send batches.
$ echo "newman_results value=1" > /dev/udp/localhost/8086
$ echo "select * from newman_results" | influx -database newman_reports_udp
Connected to http://localhost:8086 version 1.7
InfluxDB shell 0.9
name: newman_results
---------
time value
2020-26-06T11:25:15.321527811Z 1
curl -G http://localhost:8086/query --data-urlencode "q=CREATE DATABASE newman_reports"

constructor(context) {
this.context = context;
const axiosOptions = {
baseURL: `${this.context.mode === "https" ? "https" : "http"}://${this.context.server}:${this.context.port}`
};
if(this.context.version == 2) {
// For InfluxDB version 2.x
axiosOptions.auth = {
username: this.context.username,
password: this.context.password,
}
}
this.client = axios.create(axiosOptions);
if(this.context.version == 2) {
this.signIn();
}
}
_buildInfluxDBUrl(path='write') {
const url = `http://${this.context.server}:${this.context.port}/${path}`;
const params = {
db: this.context.name,
u: this.context.username,
p: this.context.password,
};
const paramsQuerystring = querystring.stringify(params);
const connectionUrl = `${url}?${paramsQuerystring}`;
return connectionUrl;
}
async signIn() {
console.log('[+] Signing In to InfluxDB');
try {
const res = await this.client.post('/api/v2/signin');
this.client.defaults.headers.common['cookie'] = res.headers['set-cookie'];
} catch (error) {
console.log('[-] ERROR: while signing in to InfluxDB', this.context.debug ? error : error.message);
}
}
async signOut() {
console.log('[+] Signing Out from InfluxDB');
try {
await this.client.post('/api/v2/signout');
} catch (error) {
console.log('[-] ERROR: while signing out to InfluxDB', this.context.debug ? error : error.message);
}
}
async healthCheck() {
let connectionUrl = this._buildInfluxDBUrl('ping');
try {
const data = await axios.get(connectionUrl);
} catch (error) {
console.log('[-] ERROR: not able to connect to InfluxDB', this.context.debug ? error : error.message);
}
}
async sendData(data) {
let url;
if(this.context.version == 1) {
const params = {
db: this.context.name,
u: this.context.username,
p: this.context.password,
};
const paramsQuerystring = querystring.stringify(params);
url = `/write?${paramsQuerystring}`;
} else {
// For InfluxDB version 2
const params = {
bucket: this.context.name,
org: this.context.org,
};
const paramsQuerystring = querystring.stringify(params);
url = `/api/v2/write?${paramsQuerystring}`;
}
try {
await this.client.post(url, data);
} catch (error) {
console.log('[-] ERROR: while sending data to InfluxDB', this.context.debug ? error : error.message);
}
}
disconnect() {
if(this.context.version == 2) {
this.signOut();
}
}
};

I also found the following external resources that might be helpful:

Summaries of links found in the content:

https://docs.influxdata.com/influxdb/v2/admin/tokens:

The page provides documentation on how to manage API tokens in InfluxDB. API tokens are used for secure interaction between InfluxDB and external tools. There are different types of API tokens, including Operator API tokens, All-Access API tokens, and Read/Write tokens. The page explains how to create, view, update, and delete API tokens using the InfluxDB UI, the influx CLI, or the InfluxDB API. It also provides information on how to use API tokens in the InfluxDB UI, the influx CLI, or the InfluxDB API. The page does not mention any specific workaround for connecting to the database without using API tokens.

https://docs.influxdata.com/influxdb/v2/api-guide:

The page is the documentation for the InfluxDB OSS v2 API. It provides information on how to use the API to interact with InfluxDB. The API can be accessed using the /api/v2/ endpoint. The page also mentions that all requests to the API must include an InfluxDB API token. There are developer guides available for using the API, as well as client libraries for different programming languages. The page also mentions that there is InfluxDB v1 compatibility API documentation available for working with InfluxDB 1.x client libraries and third-party integrations. There is a section on how to view the API documentation locally. The page also includes information on how to get support and provide feedback. There is a section on InfluxDB Clustered, which is a highly available InfluxDB 3.0 cluster. It is mentioned that Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. The page also mentions that the documentation for InfluxDB Cloud Serverless is a work in progress.


Step 2: ⌨️ Coding

Modify src/http.service.js with contents:
• Replace the current authentication setup in the constructor with an API token-based approach. Specifically, remove the conditional block that sets `axiosOptions.auth` for InfluxDB version 2.x.
• Instead, add a new condition to check if `this.context.apiToken` is provided. If so, set the `Authorization` header in `axiosOptions.headers` to `Token ${this.context.apiToken}`. This will ensure that all requests made using the `axios` client include the API token in the request headers.
• Example modification: ```javascript if(this.context.apiToken) { axiosOptions.headers = { Authorization: `Token ${this.context.apiToken}`, }; } ```
--- 
+++ 
@@ -13,10 +13,11 @@
     };
 
     if(this.context.version == 2) {
-      // For InfluxDB version 2.x
-      axiosOptions.auth = {
-        username: this.context.username,
-        password: this.context.password,
+      // Setting Authorization header for API token-based authentication
+      if(this.context.apiToken) {
+        axiosOptions.headers = {
+          Authorization: `Token ${this.context.apiToken}`,
+        };
       }
     }
 
  • Running GitHub Actions for src/http.service.jsEdit
Check src/http.service.js with contents:

Ran GitHub Actions for eaf726bac79d7debb73e7b6e67c91273bf04c94d:

Modify src/http.service.js with contents:
• Modify the `_buildInfluxDBUrl` method to include the API token in the query parameters only if the InfluxDB version is 1.x and username/password authentication is used. For InfluxDB version 2.x, the API token should not be included in the URL since it will be provided in the request headers.
• Remove the `u` (username) and `p` (password) parameters from the `params` object for InfluxDB version 2.x.
• Adjust the condition to check for InfluxDB version and whether username and password are provided for version 1.x. If not, do not include these parameters in the query string.
• Example modification: ```javascript if(this.context.version == 1 && this.context.username && this.context.password) { const params = { db: this.context.name, u: this.context.username, p: this.context.password, }; const paramsQuerystring = querystring.stringify(params); const connectionUrl = `${url}?${paramsQuerystring}`; return connectionUrl; } else { return url; } ```
--- 
+++ 
@@ -13,10 +13,11 @@
     };
 
     if(this.context.version == 2) {
-      // For InfluxDB version 2.x
-      axiosOptions.auth = {
-        username: this.context.username,
-        password: this.context.password,
+      // Setting Authorization header for API token-based authentication
+      if(this.context.apiToken) {
+        axiosOptions.headers = {
+          Authorization: `Token ${this.context.apiToken}`,
+        };
       }
     }
 
@@ -29,14 +30,19 @@
 
   _buildInfluxDBUrl(path='write') {
     const url = `http://${this.context.server}:${this.context.port}/${path}`;
-    const params = {
-      db: this.context.name,
-      u: this.context.username,
-      p: this.context.password,
-    };
-
+    let params = {};
+    if(this.context.version == 1 && this.context.username && this.context.password) {
+      params = {
+        db: this.context.name,
+        u: this.context.username,
+        p: this.context.password,
+      };
+    } else if(this.context.version == 2) {
+      params = {
+        db: this.context.name
+      };
+    }
     const paramsQuerystring = querystring.stringify(params);
-
     const connectionUrl = `${url}?${paramsQuerystring}`;
     return connectionUrl;
   }
  • Running GitHub Actions for src/http.service.jsEdit
Check src/http.service.js with contents:

Ran GitHub Actions for 4fa97d42efb2796e2d0630c410bcedd0a49b5cce:

Modify src/http.service.js with contents:
• Ensure that the `sendData` method does not attempt to include username and password in the URL for InfluxDB version 2.x, as authentication will be handled via the API token in the request headers.
• Remove the conditional block that appends username and password to the query string for InfluxDB version 2.x.
• This modification ensures that the API token provided in the headers is the sole method of authentication for InfluxDB version 2.x, aligning with the official documentation's requirements.
--- 
+++ 
@@ -13,10 +13,11 @@
     };
 
     if(this.context.version == 2) {
-      // For InfluxDB version 2.x
-      axiosOptions.auth = {
-        username: this.context.username,
-        password: this.context.password,
+      // Setting Authorization header for API token-based authentication
+      if(this.context.apiToken) {
+        axiosOptions.headers = {
+          Authorization: `Token ${this.context.apiToken}`,
+        };
       }
     }
 
@@ -29,14 +30,19 @@
 
   _buildInfluxDBUrl(path='write') {
     const url = `http://${this.context.server}:${this.context.port}/${path}`;
-    const params = {
-      db: this.context.name,
-      u: this.context.username,
-      p: this.context.password,
-    };
-
+    let params = {};
+    if(this.context.version == 1 && this.context.username && this.context.password) {
+      params = {
+        db: this.context.name,
+        u: this.context.username,
+        p: this.context.password,
+      };
+    } else if(this.context.version == 2) {
+      params = {
+        db: this.context.name
+      };
+    }
     const paramsQuerystring = querystring.stringify(params);
-
     const connectionUrl = `${url}?${paramsQuerystring}`;
     return connectionUrl;
   }
@@ -79,6 +85,11 @@
         u: this.context.username,
         p: this.context.password,
       };
+    } else if (this.context.version == 2) {
+      const params = {
+        bucket: this.context.name,
+        org: this.context.org,
+      };
       const paramsQuerystring = querystring.stringify(params);
       url = `/write?${paramsQuerystring}`;
     } else {
  • Running GitHub Actions for src/http.service.jsEdit
Check src/http.service.js with contents:

Ran GitHub Actions for a3dc92862ae95509c6538b0c7897d0aec7692971:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/connection_with_influxdb_needs_authoriza.


🎉 Latest improvements to Sweep:
  • New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
  • Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
  • Use the GitHub issues extension for creating Sweep issues directly from your editor.

💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.Something wrong? Let us know.

This is an automated message generated by Sweep AI.