@@ -10,9 +10,11 @@ import (
1010 "net/url"
1111 "os"
1212 "path"
13+ "strconv"
1314 "strings"
1415 "text/tabwriter"
1516
17+ f_common "github.com/iron-io/functions/common"
1618 image_commands "github.com/iron-io/functions/fn/commands/images"
1719 "github.com/iron-io/functions/fn/common"
1820 fnclient "github.com/iron-io/functions_go/client"
@@ -56,6 +58,10 @@ var routeFlags = []cli.Flag{
5658 Name : "max-concurrency,mc" ,
5759 Usage : "maximum concurrency for hot container" ,
5860 },
61+ cli.StringFlag {
62+ Name : "jwt-key,j" ,
63+ Usage : "Signing key for JWT" ,
64+ },
5965 cli.DurationFlag {
6066 Name : "timeout" ,
6167 Usage : "route timeout (eg. 30s)" ,
@@ -134,6 +140,13 @@ func Routes() cli.Command {
134140 ArgsUsage : "<app> </path> [property.[key]]" ,
135141 Action : r .inspect ,
136142 },
143+ {
144+ Name : "token" ,
145+ Aliases : []string {"t" },
146+ Usage : "retrieve jwt for authentication" ,
147+ ArgsUsage : "<app> </path> [expiration(sec)]" ,
148+ Action : r .token ,
149+ },
137150 },
138151 }
139152}
@@ -203,10 +216,28 @@ func (a *routesCmd) call(c *cli.Context) error {
203216 u .Path = path .Join (u .Path , "r" , appName , route )
204217 content := image_commands .Stdin ()
205218
206- return callfn (u .String (), content , os .Stdout , c .String ("method" ), c .StringSlice ("e" ))
219+ resp , err := a .client .Routes .GetAppsAppRoutesRoute (& apiroutes.GetAppsAppRoutesRouteParams {
220+ Context : context .Background (),
221+ App : appName ,
222+ Route : route ,
223+ })
224+
225+ if err != nil {
226+ switch err .(type ) {
227+ case * apiroutes.GetAppsAppRoutesRouteNotFound :
228+ return fmt .Errorf ("error: %s" , err .(* apiroutes.GetAppsAppRoutesRouteNotFound ).Payload .Error .Message )
229+ case * apiroutes.GetAppsAppRoutesRouteDefault :
230+ return fmt .Errorf ("unexpected error: %s" , err .(* apiroutes.GetAppsAppRoutesRouteDefault ).Payload .Error .Message )
231+ }
232+ return fmt .Errorf ("unexpected error: %s" , err )
233+ }
234+
235+ rt := resp .Payload .Route
236+
237+ return callfn (u .String (), rt , content , os .Stdout , c .String ("method" ), c .StringSlice ("e" ))
207238}
208239
209- func callfn (u string , content io.Reader , output io.Writer , method string , env []string ) error {
240+ func callfn (u string , rt * models. Route , content io.Reader , output io.Writer , method string , env []string ) error {
210241 if method == "" {
211242 if content == nil {
212243 method = "GET"
@@ -226,6 +257,14 @@ func callfn(u string, content io.Reader, output io.Writer, method string, env []
226257 envAsHeader (req , env )
227258 }
228259
260+ if rt .JwtKey != "" {
261+ ss , err := f_common .GetJwt (rt .JwtKey , 60 * 60 )
262+ if err != nil {
263+ return fmt .Errorf ("unexpected error: %s" , err )
264+ }
265+ req .Header .Set ("Authorization" , fmt .Sprintf ("Bearer %v" , ss ))
266+ }
267+
229268 resp , err := http .DefaultClient .Do (req )
230269 if err != nil {
231270 return fmt .Errorf ("error running route: %s" , err )
@@ -275,6 +314,10 @@ func routeWithFlags(c *cli.Context, rt *models.Route) {
275314 rt .Timeout = & to
276315 }
277316
317+ if j := c .String ("jwt-key" ); j != "" {
318+ rt .JwtKey = j
319+ }
320+
278321 if len (c .StringSlice ("headers" )) > 0 {
279322 headers := map [string ][]string {}
280323 for _ , header := range c .StringSlice ("headers" ) {
@@ -305,6 +348,10 @@ func routeWithFuncFile(c *cli.Context, rt *models.Route) {
305348 to := int64 (ff .Timeout .Seconds ())
306349 rt .Timeout = & to
307350 }
351+ if ff .JwtKey != nil && * ff .JwtKey != "" {
352+ rt .JwtKey = * ff .JwtKey
353+ }
354+
308355 if rt .Path == "" && ff .Path != nil {
309356 rt .Path = * ff .Path
310357 }
@@ -419,6 +466,10 @@ func (a *routesCmd) patchRoute(appName, routePath string, r *fnmodels.Route) err
419466 if r .Timeout != nil {
420467 resp .Payload .Route .Timeout = r .Timeout
421468 }
469+ if r .JwtKey != "" {
470+ resp .Payload .Route .JwtKey = r .JwtKey
471+ }
472+
422473 }
423474
424475 _ , err = a .client .Routes .PatchAppsAppRoutesRoute (& apiroutes.PatchAppsAppRoutesRouteParams {
@@ -572,3 +623,52 @@ func (a *routesCmd) delete(c *cli.Context) error {
572623 fmt .Println (appName , route , "deleted" )
573624 return nil
574625}
626+
627+ func (a * routesCmd ) token (c * cli.Context ) error {
628+ appName := c .Args ().Get (0 )
629+ route := cleanRoutePath (c .Args ().Get (1 ))
630+ e := c .Args ().Get (2 )
631+ expiration := 60 * 60
632+ if e != "" {
633+ var err error
634+ expiration , err = strconv .Atoi (e )
635+ if err != nil {
636+ return fmt .Errorf ("invalid expiration: %s" , err )
637+ }
638+ }
639+
640+ resp , err := a .client .Routes .GetAppsAppRoutesRoute (& apiroutes.GetAppsAppRoutesRouteParams {
641+ Context : context .Background (),
642+ App : appName ,
643+ Route : route ,
644+ })
645+
646+ if err != nil {
647+ switch err .(type ) {
648+ case * apiroutes.GetAppsAppRoutesRouteNotFound :
649+ return fmt .Errorf ("error: %s" , err .(* apiroutes.GetAppsAppRoutesRouteNotFound ).Payload .Error .Message )
650+ case * apiroutes.GetAppsAppRoutesRouteDefault :
651+ return fmt .Errorf ("unexpected error: %s" , err .(* apiroutes.GetAppsAppRoutesRouteDefault ).Payload .Error .Message )
652+ }
653+ return fmt .Errorf ("unexpected error: %s" , err )
654+ }
655+
656+ enc := json .NewEncoder (os .Stdout )
657+ enc .SetIndent ("" , "\t " )
658+ jwtKey := resp .Payload .Route .JwtKey
659+ if jwtKey == "" {
660+ return errors .New ("Empty JWT Key" )
661+ }
662+
663+ // Create the Claims
664+ ss , err := f_common .GetJwt (jwtKey , expiration )
665+ if err != nil {
666+ return fmt .Errorf ("unexpected error: %s" , err )
667+ }
668+ t := struct {
669+ Token string `json:"token"`
670+ }{Token : ss }
671+ enc .Encode (t )
672+
673+ return nil
674+ }
0 commit comments