Skip to content

Commit f47924f

Browse files
authored
Fix influxdb output database quoting (influxdata#2851)
1 parent a96f85c commit f47924f

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

plugins/outputs/influxdb/client/http.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ var (
1616
defaultRequestTimeout = time.Second * 5
1717
)
1818

19-
//
2019
func NewHTTP(config HTTPConfig, defaultWP WriteParams) (Client, error) {
2120
// validate required parameters:
2221
if len(config.URL) == 0 {

plugins/outputs/influxdb/influxdb.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import (
1616
"github.com/influxdata/telegraf/plugins/outputs/influxdb/client"
1717
)
1818

19+
var (
20+
// Quote Ident replacer.
21+
qiReplacer = strings.NewReplacer("\n", `\n`, `\`, `\\`, `"`, `\"`)
22+
)
23+
1924
// InfluxDB struct is the primary data structure for the plugin
2025
type InfluxDB struct {
2126
// URL is only for backwards compatability
@@ -133,7 +138,7 @@ func (i *InfluxDB) Connect() error {
133138
}
134139
i.clients = append(i.clients, c)
135140

136-
err = c.Query("CREATE DATABASE " + i.Database)
141+
err = c.Query(fmt.Sprintf(`CREATE DATABASE "%s"`, qiReplacer.Replace(i.Database)))
137142
if err != nil {
138143
if !strings.Contains(err.Error(), "Status Code [403]") {
139144
log.Println("I! Database creation failed: " + err.Error())
@@ -191,7 +196,8 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
191196
if _, e := i.clients[n].WriteStream(r, bufsize); e != nil {
192197
// If the database was not found, try to recreate it:
193198
if strings.Contains(e.Error(), "database not found") {
194-
if errc := i.clients[n].Query("CREATE DATABASE " + i.Database); errc != nil {
199+
errc := i.clients[n].Query(fmt.Sprintf(`CREATE DATABASE "%s"`, qiReplacer.Replace(i.Database)))
200+
if errc != nil {
195201
log.Printf("E! Error: Database %s not found and failed to recreate\n",
196202
i.Database)
197203
}

plugins/outputs/influxdb/influxdb_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,44 @@ import (
88

99
"github.com/influxdata/telegraf/testutil"
1010

11+
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
1213
)
1314

15+
func TestIdentQuoting(t *testing.T) {
16+
var testCases = []struct {
17+
database string
18+
expected string
19+
}{
20+
{"x-y", `CREATE DATABASE "x-y"`},
21+
{`x"y`, `CREATE DATABASE "x\"y"`},
22+
{"x\ny", `CREATE DATABASE "x\ny"`},
23+
{`x\y`, `CREATE DATABASE "x\\y"`},
24+
}
25+
26+
for _, tc := range testCases {
27+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
28+
r.ParseForm()
29+
q := r.Form.Get("q")
30+
assert.Equal(t, tc.expected, q)
31+
32+
w.WriteHeader(http.StatusOK)
33+
w.Header().Set("Content-Type", "application/json")
34+
fmt.Fprintln(w, `{"results":[{}]}`)
35+
}))
36+
defer ts.Close()
37+
38+
i := InfluxDB{
39+
URLs: []string{ts.URL},
40+
Database: tc.database,
41+
}
42+
43+
err := i.Connect()
44+
require.NoError(t, err)
45+
require.NoError(t, i.Close())
46+
}
47+
}
48+
1449
func TestUDPInflux(t *testing.T) {
1550
i := InfluxDB{
1651
URLs: []string{"udp://localhost:8089"},

0 commit comments

Comments
 (0)