Skip to content

Commit b679ac3

Browse files
author
kavinli
committed
feat: add influxv1 auth.
1 parent 823e78f commit b679ac3

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

bulk_load/load.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ type BulkLoad interface {
4040

4141
type LoadRunner struct {
4242
DbName string
43+
User string
44+
Password string
45+
BasicAuthentication string
4346
Workers int
4447
ItemLimit int64
4548
BatchSize int
@@ -110,6 +113,9 @@ func (r *LoadRunner) notifyHandler(arg int) (int, error) {
110113

111114
func (r *LoadRunner) Init(defaultBatchSize int) {
112115
flag.StringVar(&r.DbName, "db", "benchmark_db", "Database name.")
116+
flag.StringVar(&r.User, "user", "", "User name, credentials as query parameters.")
117+
flag.StringVar(&r.Password, "password", "", "User password, credentials as query parameters.")
118+
flag.StringVar(&r.BasicAuthentication, "basic-authentication", "", "Authenticate with basic authentication. format [user:password].")
113119
flag.IntVar(&r.BatchSize, "batch-size", defaultBatchSize, "Batch size (1 line of input = 1 item).")
114120
flag.IntVar(&r.Workers, "workers", 1, "Number of parallel requests to make.")
115121
flag.Int64Var(&r.ItemLimit, "item-limit", -1, "Number of items to read from stdin before quitting. (1 item per 1 line of input.)")

cmd/bulk_load_influx/http_writer.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"log"
99
"time"
10+
"encoding/base64"
1011

1112
"github.com/valyala/fasthttp"
1213
)
@@ -32,6 +33,10 @@ type HTTPWriterConfig struct {
3233
// Name of the target database into which points will be written.
3334
Database string
3435

36+
User string
37+
Password string
38+
BasicAuthentication string
39+
3540
// Id of the target bucket into which points will be written. (InfluxDB v2)
3641
BucketId string
3742

@@ -86,6 +91,9 @@ func (w *HTTPWriter) WriteLineProtocol(body []byte, isGzip bool) (int64, error)
8691
if w.c.AuthToken != "" {
8792
req.Header.Add("Authorization", fmt.Sprintf("Token %s", w.c.AuthToken))
8893
}
94+
if w.c.BasicAuthentication != "" {
95+
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(w.c.BasicAuthentication))))
96+
}
8997
req.SetBody(body)
9098

9199
resp := fasthttp.AcquireResponse()

cmd/bulk_load_influx/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,16 @@ func (l *InfluxBulkLoad) PrepareProcess(i int) {
290290
DebugInfo: fmt.Sprintf("worker #%d, dest url: %s", i, l.configs[i].url),
291291
Host: l.configs[i].url,
292292
Database: bulk_load.Runner.DbName,
293+
User: bulk_load.Runner.User,
294+
Password: bulk_load.Runner.Password,
295+
BasicAuthentication: bulk_load.Runner.BasicAuthentication,
293296
BackingOffChan: l.configs[i].backingOffChan,
294297
BackingOffDone: l.configs[i].backingOffDone,
295298
}
296299
url = c.Host + "/write?consistency=" + l.consistency + "&db=" + neturl.QueryEscape(c.Database)
300+
if len(c.User) != 0 && len(c.Password) != 0 {
301+
url = url + "&u=" + neturl.QueryEscape(c.User) + "&p=" + neturl.QueryEscape(c.Password)
302+
}
297303
}
298304
l.configs[i].writer = NewHTTPWriter(*c, url)
299305
}

0 commit comments

Comments
 (0)