11package main
22
33import (
4+ "crypto/rand"
45"flag"
56"fmt"
67"os"
78"path/filepath"
89
10+ "io/ioutil"
11+
12+ "github.com/dgrijalva/jwt-go"
913"github.com/gaia-pipeline/gaia"
1014"github.com/gaia-pipeline/gaia/handlers"
1115"github.com/gaia-pipeline/gaia/pipeline"
@@ -16,7 +20,8 @@ import (
1620)
1721
1822var (
19- echoInstance * echo.Echo
23+ echoInstance * echo.Echo
24+ jwtPrivateKeyPath string
2025)
2126
2227const (
@@ -35,6 +40,7 @@ func init() {
3540flag .StringVar (& gaia .Cfg .ListenPort ,"port" ,"8080" ,"Listen port for gaia" )
3641flag .StringVar (& gaia .Cfg .HomePath ,"homepath" ,"" ,"Path to the gaia home folder" )
3742flag .StringVar (& gaia .Cfg .Worker ,"worker" ,"2" ,"Number of worker gaia will use to execute pipelines in parallel" )
43+ flag .StringVar (& jwtPrivateKeyPath ,"jwtPrivateKeyPath" ,"" ,"A RSA private key used to sign JWT tokens" )
3844flag .BoolVar (& gaia .Cfg .DevMode ,"dev" ,false ,"If true, gaia will be started in development mode. Don't use this in production!" )
3945flag .BoolVar (& gaia .Cfg .VersionSwitch ,"version" ,false ,"If true, will print the version and immediately exit" )
4046
@@ -59,6 +65,30 @@ func main() {
5965Name :"Gaia" ,
6066})
6167
68+ var jwtKey interface {}
69+ // Check JWT key is set
70+ if jwtPrivateKeyPath == "" {
71+ gaia .Cfg .Logger .Warn ("using auto-generated key to sign jwt tokens, do not use in production" )
72+ jwtKey = make ([]byte ,64 )
73+ _ ,err := rand .Read (jwtKey .([]byte ))
74+ if err != nil {
75+ gaia .Cfg .Logger .Error ("error auto-generating jwt key" ,"error" ,err .Error ())
76+ os .Exit (1 )
77+ }
78+ }else {
79+ keyData ,err := ioutil .ReadFile (jwtPrivateKeyPath )
80+ if err != nil {
81+ gaia .Cfg .Logger .Error ("could not read jwt key file" ,"error" ,err .Error ())
82+ os .Exit (1 )
83+ }
84+ jwtKey ,err = jwt .ParseRSAPrivateKeyFromPEM (keyData )
85+ if err != nil {
86+ gaia .Cfg .Logger .Error ("could not parse jwt key file" ,"error" ,err .Error ())
87+ os .Exit (1 )
88+ }
89+ }
90+ gaia .Cfg .JWTKey = jwtKey
91+
6292// Find path for gaia home folder if not given by parameter
6393if gaia .Cfg .HomePath == "" {
6494// Find executeable path