Skip to content

Commit e093518

Browse files
author
Lars Maier
authored
Merge pull request #5 from arangodb-helper/feature/jwt-file
JWT File
2 parents 423e828 + 87b1f72 commit e093518

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

exporter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ type Exporter struct {
102102
}
103103

104104
// NewExporter returns an initialized Exporter.
105-
func NewExporter(arangodbEndpoint, jwtSecret string, sslVerify bool, timeout time.Duration) (*Exporter, error) {
105+
func NewExporter(arangodbEndpoint, jwt string, sslVerify bool, timeout time.Duration) (*Exporter, error) {
106106
connCfg := driver_http.ConnectionConfig{
107107
Endpoints: []string{arangodbEndpoint},
108108
}
@@ -113,8 +113,8 @@ func NewExporter(arangodbEndpoint, jwtSecret string, sslVerify bool, timeout tim
113113
if err != nil {
114114
return nil, maskAny(err)
115115
}
116-
if jwtSecret != "" {
117-
hdr, err := CreateArangodJwtAuthorizationHeader(jwtSecret)
116+
if jwt != "" {
117+
hdr, err := CreateArangodJwtAuthorizationHeader(jwt)
118118
if err != nil {
119119
return nil, maskAny(err)
120120
}

jwt.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,20 @@ const (
3333
// CreateArangodJwtAuthorizationHeader calculates a JWT authorization header, for authorization
3434
// of a request to an arangod server, based on the given secret.
3535
// If the secret is empty, nothing is done.
36-
func CreateArangodJwtAuthorizationHeader(jwtSecret string) (string, error) {
36+
func CreateArangodJwtAuthorizationHeader(jwt string) (string, error) {
37+
return "bearer " + jwt, nil
38+
}
39+
40+
// CreateArangodJWT creates a superuser arangod jwt
41+
func CreateArangodJWT(jwtSecret string) (string, error) {
3742
if jwtSecret == "" {
3843
return "", nil
3944
}
4045
// Create a new token object, specifying signing method and the claims
4146
// you would like it to contain.
4247
token := jg.NewWithClaims(jg.SigningMethodHS256, jg.MapClaims{
4348
"iss": issArangod,
44-
"server_id": "foo",
49+
"server_id": "exporter",
4550
})
4651

4752
// Sign and get the complete encoded token as a string using the secret
@@ -50,5 +55,5 @@ func CreateArangodJwtAuthorizationHeader(jwtSecret string) (string, error) {
5055
return "", maskAny(err)
5156
}
5257

53-
return "bearer " + signedToken, nil
58+
return signedToken, nil
5459
}

main.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package main
2424

2525
import (
2626
"fmt"
27+
"io/ioutil"
2728
"net/http"
2829
_ "net/http/pprof"
2930
"time"
@@ -49,6 +50,7 @@ var (
4950
arangodbOptions struct {
5051
endpoint string
5152
jwtSecret string
53+
jwtFile string
5254
timeout time.Duration
5355
}
5456
)
@@ -61,7 +63,10 @@ func init() {
6163

6264
f.StringVar(&arangodbOptions.endpoint, "arangodb.endpoint", "http://127.0.0.1:8529", "Endpoint used to reach the ArangoDB server")
6365
f.StringVar(&arangodbOptions.jwtSecret, "arangodb.jwtsecret", "", "JWT Secret used for authentication with ArangoDB server")
66+
f.StringVar(&arangodbOptions.jwtFile, "arangodb.jwt-file", "", "File containing the JWT for authentication with ArangoDB server")
6467
f.DurationVar(&arangodbOptions.timeout, "arangodb.timeout", time.Second*15, "Timeout of statistics requests for ArangoDB")
68+
69+
f.MarkDeprecated("arangodb.jwtsecret", "please use --arangodb.jwt-file instead")
6570
}
6671

6772
func main() {
@@ -71,7 +76,22 @@ func main() {
7176
func cmdMainRun(cmd *cobra.Command, args []string) {
7277
log.Infoln(fmt.Sprintf("Starting arangodb-exporter %s, build %s", projectVersion, projectBuild))
7378

74-
exporter, err := NewExporter(arangodbOptions.endpoint, arangodbOptions.jwtSecret, false, arangodbOptions.timeout)
79+
var token string
80+
if arangodbOptions.jwtFile != "" {
81+
data, err := ioutil.ReadFile(arangodbOptions.jwtFile)
82+
if err != nil {
83+
log.Fatal(err)
84+
}
85+
token = string(data)
86+
} else if arangodbOptions.jwtSecret != "" {
87+
var err error
88+
token, err = CreateArangodJWT(arangodbOptions.jwtSecret)
89+
if err != nil {
90+
log.Fatal(err)
91+
}
92+
}
93+
94+
exporter, err := NewExporter(arangodbOptions.endpoint, token, false, arangodbOptions.timeout)
7595
if err != nil {
7696
log.Fatal(err)
7797
}

0 commit comments

Comments
 (0)