mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-12-26 15:29:04 +01:00
Add basic authentication support
This commit is contained in:
parent
f6b5f7fb07
commit
94d5822426
@ -28,6 +28,11 @@ type HttpReceiverConfig struct {
|
|||||||
// should be larger than the measurement interval to keep the connection open
|
// should be larger than the measurement interval to keep the connection open
|
||||||
IdleTimeout string `json:"idle_timeout"`
|
IdleTimeout string `json:"idle_timeout"`
|
||||||
idleTimeout time.Duration
|
idleTimeout time.Duration
|
||||||
|
|
||||||
|
// Basic authentication
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
useBasicAuth bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type HttpReceiver struct {
|
type HttpReceiver struct {
|
||||||
@ -58,6 +63,8 @@ func (r *HttpReceiver) Init(name string, config json.RawMessage) error {
|
|||||||
if len(r.config.Port) == 0 {
|
if len(r.config.Port) == 0 {
|
||||||
return errors.New("not all configuration variables set required by HttpReceiver")
|
return errors.New("not all configuration variables set required by HttpReceiver")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check idle timeout config
|
||||||
if len(r.config.IdleTimeout) > 0 {
|
if len(r.config.IdleTimeout) > 0 {
|
||||||
t, err := time.ParseDuration(r.config.IdleTimeout)
|
t, err := time.ParseDuration(r.config.IdleTimeout)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -65,6 +72,18 @@ func (r *HttpReceiver) Init(name string, config json.RawMessage) error {
|
|||||||
r.config.idleTimeout = t
|
r.config.idleTimeout = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check basic authentication config
|
||||||
|
if len(r.config.Username) > 0 || len(r.config.Password) > 0 {
|
||||||
|
r.config.useBasicAuth = true
|
||||||
|
}
|
||||||
|
if r.config.useBasicAuth && len(r.config.Username) == 0 {
|
||||||
|
return errors.New("basic authentication requires username")
|
||||||
|
}
|
||||||
|
if r.config.useBasicAuth && len(r.config.Password) == 0 {
|
||||||
|
return errors.New("basic authentication requires password")
|
||||||
|
}
|
||||||
|
|
||||||
r.meta = map[string]string{"source": r.name}
|
r.meta = map[string]string{"source": r.name}
|
||||||
p := r.config.Path
|
p := r.config.Path
|
||||||
if !strings.HasPrefix(p, "/") {
|
if !strings.HasPrefix(p, "/") {
|
||||||
@ -100,11 +119,22 @@ func (r *HttpReceiver) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *HttpReceiver) ServerHttp(w http.ResponseWriter, req *http.Request) {
|
func (r *HttpReceiver) ServerHttp(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
|
// Check request method, only post method is handled
|
||||||
if req.Method != http.MethodPost {
|
if req.Method != http.MethodPost {
|
||||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check basic authentication
|
||||||
|
if r.config.useBasicAuth {
|
||||||
|
username, password, ok := req.BasicAuth()
|
||||||
|
if !ok || username != r.config.Username || password != r.config.Password {
|
||||||
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
d := influx.NewDecoder(req.Body)
|
d := influx.NewDecoder(req.Body)
|
||||||
for d.Next() {
|
for d.Next() {
|
||||||
|
|
||||||
|
@ -10,7 +10,10 @@ The `http` receiver can be used receive metrics through HTTP POST requests.
|
|||||||
"type": "http",
|
"type": "http",
|
||||||
"address" : "",
|
"address" : "",
|
||||||
"port" : "8080",
|
"port" : "8080",
|
||||||
"path" : "/write"
|
"path" : "/write",
|
||||||
|
"idle_timeout": "120s",
|
||||||
|
"username": "myUser",
|
||||||
|
"password": "myPW"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -19,6 +22,9 @@ The `http` receiver can be used receive metrics through HTTP POST requests.
|
|||||||
- `address`: Listen address
|
- `address`: Listen address
|
||||||
- `port`: Listen port
|
- `port`: Listen port
|
||||||
- `path`: URL path for the write endpoint
|
- `path`: URL path for the write endpoint
|
||||||
|
- `idle_timeout`: Maximum amount of time to wait for the next request when keep-alives are enabled should be larger than the measurement interval to keep the connection open
|
||||||
|
- `username`: username for basic authentication
|
||||||
|
- `password`: password for basic authentication
|
||||||
|
|
||||||
The HTTP endpoint listens to `http://<address>:<port>/<path>`
|
The HTTP endpoint listens to `http://<address>:<port>/<path>`
|
||||||
|
|
||||||
@ -28,7 +34,9 @@ The HTTP endpoint listens to `http://<address>:<port>/<path>`
|
|||||||
- Use curl to send message to `http` receiver
|
- Use curl to send message to `http` receiver
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl http://localhost:8080/write --data \
|
curl http://localhost:8080/write \
|
||||||
|
--user "myUser:myPW" \
|
||||||
|
--data \
|
||||||
"myMetric,hostname=myHost,type=hwthread,type-id=0,unit=Hz value=400000i 1694777161164284635
|
"myMetric,hostname=myHost,type=hwthread,type-id=0,unit=Hz value=400000i 1694777161164284635
|
||||||
myMetric,hostname=myHost,type=hwthread,type-id=1,unit=Hz value=400001i 1694777161164284635"
|
myMetric,hostname=myHost,type=hwthread,type-id=1,unit=Hz value=400001i 1694777161164284635"
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user