Skip to content

Commit

Permalink
add support for auth providers
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatjindal committed Nov 20, 2019
1 parent c426c12 commit 2bb12a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/authproviders/authprovider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package authproviders

import (
"fmt"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

//GetToken gets the token from auth provider config
func GetToken(ap *clientcmdapi.AuthProviderConfig) (string, error) {
switch ap.Name {
case "gcp":
return ap.Config["access-token"], nil
case "azure":
return ap.Config["access-token"], nil
case "oidc":
return ap.Config["id-token"], nil
}

return "", fmt.Errorf("unsupported auth provider %s", ap.Name)
}
20 changes: 20 additions & 0 deletions pkg/cmd/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"

"github.com/rajatjindal/kubectl-whoami/pkg/authproviders"
"github.com/rajatjindal/kubectl-whoami/pkg/k8s"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
Expand Down Expand Up @@ -100,6 +101,25 @@ func (o *WhoAmIOptions) Run() error {
return err
}

if config.AuthProvider != nil {
token, err := authproviders.GetToken(config.AuthProvider)
if err != nil {
return err
}

username, err := k8s.WhoAmI(o.kubeclient, token)
if err != nil {
return err
}

if username == "" {
return fmt.Errorf("failed to find subject of token. please report a ticket at https://github.com/rajatjindal/kubectl-whoami")
}

fmt.Println(username)
return nil
}

c, err := config.TransportConfig()
if err != nil {
return err
Expand Down

0 comments on commit 2bb12a2

Please sign in to comment.