Skip to content

Commit

Permalink
add --local auth
Browse files Browse the repository at this point in the history
This introduces a new Authorizer, which is hookable per KlothoMain.
The default one just calls auth.Authorize(), but KlothoMains can provide
an alternative, including one that adds flags.

resolves #164
  • Loading branch information
Yuval Shavit authored and Yuval Shavit committed Feb 16, 2023
1 parent 627ea64 commit 7d56f89
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
18 changes: 18 additions & 0 deletions cmd/klotho/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
package main

import (
"github.com/klothoplatform/klotho/pkg/auth"
"github.com/klothoplatform/klotho/pkg/cli"
"github.com/spf13/pflag"
)

func main() {
authRequirement := LocalAuth(false)
km := cli.KlothoMain{
DefaultUpdateStream: "open:latest",
Version: Version,
PluginSetup: func(psb *cli.PluginSetBuilder) error {
return psb.AddAll()
},
Authorizer: &authRequirement,
}

km.Main()
}

// LocalAuth is an auth.Authorizer that requires login unless its value is true.
type LocalAuth bool

func (local *LocalAuth) SetUpCliFlags(flags *pflag.FlagSet) {
flags.BoolVar((*bool)(local), "local", bool(*local), "If provided, runs Klotho with a local login (that is, not requiring an authenticated login)")
}

func (local *LocalAuth) Authorize() error {
if !*local {
return auth.Authorize()
}
return nil
}
27 changes: 24 additions & 3 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ type LoginResponse struct {
State string
}

type Authorizer interface {
Authorize() error
}

func DefaultIfNil(auth Authorizer) Authorizer {
if auth == nil {
return standardAuthorizer{}
}
return auth
}

type standardAuthorizer struct{}

func (s standardAuthorizer) Authorize() error {
return Authorize()
}

func Login() error {
state, err := CallLoginEndpoint()
if err != nil {
Expand Down Expand Up @@ -126,7 +143,11 @@ type MyCustomClaims struct {
jwt.StandardClaims
}

func Authorize(tokenRefreshed bool) error {
func Authorize() error {
return authorize(false)
}

func authorize(tokenRefreshed bool) error {
creds, err := GetIDToken()
if err != nil {
return errors.New("failed to get credentials for user, please login")
Expand All @@ -147,7 +168,7 @@ func Authorize(tokenRefreshed bool) error {
if err != nil {
return err
}
err = Authorize(true)
err = authorize(true)
if err != nil {
return err
}
Expand All @@ -161,7 +182,7 @@ func Authorize(tokenRefreshed bool) error {
if err != nil {
return err
}
err = Authorize(true)
err = authorize(true)
if err != nil {
return err
}
Expand Down
26 changes: 20 additions & 6 deletions pkg/cli/klothomain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"github.com/spf13/pflag"
"os"
"regexp"

Expand Down Expand Up @@ -29,6 +30,12 @@ type KlothoMain struct {
Version string
VersionQualifier string
PluginSetup func(*PluginSetBuilder) error
// Authorizer is an optional authorizer override. If this also conforms to FlagsProvider, those flags will be added.
Authorizer auth.Authorizer
}

type FlagsProvider interface {
SetUpCliFlags(flags *pflag.FlagSet)
}

var cfg struct {
Expand Down Expand Up @@ -71,6 +78,7 @@ const (
)

func (km KlothoMain) Main() {
km.Authorizer = auth.DefaultIfNil(km.Authorizer)

var root = &cobra.Command{
Use: "klotho [path to source]",
Expand All @@ -94,8 +102,13 @@ func (km KlothoMain) Main() {
flags.BoolVar(&cfg.version, "version", false, "Print the version")
flags.BoolVar(&cfg.update, "update", false, "update the cli to the latest version")
flags.StringToStringVar(&cfg.setOption, "set-option", nil, "Sets a CLI option")
flags.BoolVar(&cfg.login, "login", false, "Login to Klotho with email. For anonymous login, use 'local'")
flags.BoolVar(&cfg.login, "login", false, "Login to Klotho with email.")
flags.BoolVar(&cfg.logout, "logout", false, "Logout of current klotho account.")

if authFlags, hasFlags := km.Authorizer.(FlagsProvider); hasFlags {
authFlags.SetUpCliFlags(flags)
}

_ = flags.MarkHidden("internalDebug")

err := root.Execute()
Expand Down Expand Up @@ -205,11 +218,6 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
return nil
}

err = auth.Authorize(false)
if err != nil {
return err

}
// Set up analytics
analyticsClient, err := analytics.NewClient(map[string]interface{}{
"version": km.Version,
Expand Down Expand Up @@ -253,6 +261,12 @@ func (km KlothoMain) run(cmd *cobra.Command, args []string) (err error) {
analyticsClient.Properties[km.VersionQualifier] = true
}

// Needs to go after the --version and --update checks
err = km.Authorizer.Authorize()
if err != nil {
return err
}

// if update is specified do the update in place
var klothoUpdater = updater.Updater{
ServerURL: updater.DefaultServer,
Expand Down

0 comments on commit 7d56f89

Please sign in to comment.