|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + "io/ioutil" |
| 28 | + "strings" |
| 29 | + |
| 30 | + "github.com/spf13/cobra" |
| 31 | + |
| 32 | + service "github.com/arangodb-helper/arangodb/service" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + cmdAuth = &cobra.Command{ |
| 37 | + Use: "auth", |
| 38 | + Short: "ArangoDB authentication helper commands", |
| 39 | + Run: cmdShowUsage, |
| 40 | + } |
| 41 | + cmdAuthHeader = &cobra.Command{ |
| 42 | + Use: "header", |
| 43 | + Short: "Create a full HTTP Authorization header for accessing an ArangoDB server", |
| 44 | + Run: cmdAuthHeaderRun, |
| 45 | + } |
| 46 | + cmdAuthToken = &cobra.Command{ |
| 47 | + Use: "token", |
| 48 | + Short: "Create a JWT authentication token for accessing an ArangoDB server", |
| 49 | + Run: cmdAuthTokenRun, |
| 50 | + } |
| 51 | + authOptions struct { |
| 52 | + jwtSecretFile string |
| 53 | + user string |
| 54 | + } |
| 55 | +) |
| 56 | + |
| 57 | +func init() { |
| 58 | + cmdMain.AddCommand(cmdAuth) |
| 59 | + cmdAuth.AddCommand(cmdAuthHeader) |
| 60 | + cmdAuth.AddCommand(cmdAuthToken) |
| 61 | + |
| 62 | + pf := cmdAuth.PersistentFlags() |
| 63 | + pf.StringVar(&authOptions.jwtSecretFile, "auth.jwt-secret", "", "name of a plain text file containing a JWT secret used for server authentication") |
| 64 | + pf.StringVar(&authOptions.user, "auth.user", "", "name of a user to authenticate as. If empty, 'super-user' authentication is used") |
| 65 | +} |
| 66 | + |
| 67 | +// mustAuthCreateJWTToken creates a the JWT token based on authentication options. |
| 68 | +// On error the process is exited with a non-zero exit code. |
| 69 | +func mustAuthCreateJWTToken() string { |
| 70 | + authOptions.jwtSecretFile = mustExpand(authOptions.jwtSecretFile) |
| 71 | + |
| 72 | + if authOptions.jwtSecretFile == "" { |
| 73 | + log.Fatal().Msg("A JWT secret file is required. Set --auth.jwt-secret option.") |
| 74 | + } |
| 75 | + content, err := ioutil.ReadFile(authOptions.jwtSecretFile) |
| 76 | + if err != nil { |
| 77 | + log.Fatal().Err(err).Msgf("Failed to read JWT secret file '%s'", authOptions.jwtSecretFile) |
| 78 | + } |
| 79 | + jwtSecret := strings.TrimSpace(string(content)) |
| 80 | + token, err := service.CreateJwtToken(jwtSecret, authOptions.user) |
| 81 | + if err != nil { |
| 82 | + log.Fatal().Err(err).Msg("Failed to create JWT token") |
| 83 | + } |
| 84 | + return token |
| 85 | +} |
| 86 | + |
| 87 | +// cmdAuthHeaderRun prints a JWT authorization header on stdout and exits. |
| 88 | +func cmdAuthHeaderRun(cmd *cobra.Command, args []string) { |
| 89 | + token := mustAuthCreateJWTToken() |
| 90 | + fmt.Printf("%s: %s%s\n", service.AuthorizationHeader, service.BearerPrefix, token) |
| 91 | +} |
| 92 | + |
| 93 | +// cmdAuthTokenRun prints a JWT authorization token on stdout and exits. |
| 94 | +func cmdAuthTokenRun(cmd *cobra.Command, args []string) { |
| 95 | + token := mustAuthCreateJWTToken() |
| 96 | + fmt.Println(token) |
| 97 | +} |
0 commit comments