Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix set-org command #64

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion auth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,49 @@ func SetContextProject(kubeconfigPath string, contextName string, project string
}
context, exists := kubeconfig.Contexts[contextName]
if !exists {
return fmt.Errorf("could not find cluster %q in kubeconfig", contextName)
return fmt.Errorf("could not find context %q in kubeconfig", contextName)
}
context.Namespace = project
return clientcmd.WriteToFile(*kubeconfig, kubeconfigPath)
}

// SetContextOrganization sets the given organization in the given context of the kubeconfig
func SetContextOrganization(kubeconfigPath string, contextName string, organization string) error {
kubeconfig, err := clientcmd.LoadFromFile(kubeconfigPath)
if err != nil {
return fmt.Errorf("kubeconfig not found: %w", err)
}
context, exists := kubeconfig.Contexts[contextName]
if !exists {
return fmt.Errorf("could not find context %q in kubeconfig", contextName)
}
extension, exists := context.Extensions[util.NctlName]
if !exists {
return ErrConfigNotFound
}

cfg, err := parseConfig(extension)
if err != nil {
return err
}

if cfg.Organization == organization {
return nil
}

cfg.Organization = organization
pawelkuc marked this conversation as resolved.
Show resolved Hide resolved
cfgObject, err := cfg.ToObject()
if err != nil {
return err
}
context.Extensions[util.NctlName] = cfgObject

// change project to default for the the given organization:
context.Namespace = organization

return clientcmd.WriteToFile(*kubeconfig, kubeconfigPath)
}

func readConfig(kubeconfigContent []byte, contextName string) (*Config, error) {
kubeconfig, err := clientcmd.Load(kubeconfigContent)
if err != nil {
Expand Down
30 changes: 6 additions & 24 deletions auth/set_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,22 @@ package auth

import (
"context"
"net/url"

"github.com/ninech/nctl/api"
)

type SetOrgCmd struct {
Organization string `arg:"" help:"Name of the organization to login to."`
Organization string `arg:"" help:"Name of the organization to login to." default:""`
APIURL string `help:"The URL of the Nine API" default:"https://nineapis.ch" env:"NCTL_API_URL" name:"api-url"`
IssuerURL string `help:"Issuer URL is the OIDC issuer URL of the API." default:"https://auth.nine.ch/auth/realms/pub"`
ClientID string `help:"Client ID is the OIDC client ID of the API." default:"nineapis.ch-f178254"`
}

func (s *SetOrgCmd) Run(ctx context.Context, command string) error {
loadingRules, err := api.LoadingRules()
if err != nil {
return err
func (s *SetOrgCmd) Run(ctx context.Context, client *api.Client) error {
if s.Organization == "" {
whoamicmd := WhoAmICmd{APIURL: s.APIURL, IssuerURL: s.IssuerURL, ClientID: s.ClientID}
return whoamicmd.Run(ctx, client)
}

apiURL, err := url.Parse(s.APIURL)
if err != nil {
return err
}

issuerURL, err := url.Parse(s.IssuerURL)
if err != nil {
return err
}

cfg, err := newAPIConfig(apiURL, issuerURL, command, s.ClientID, withOrganization(s.Organization))
if err != nil {
return err
}

userInfo := &api.UserInfo{}

return login(ctx, cfg, loadingRules.GetDefaultFilename(), userInfo.User, s.Organization, project(s.Organization))
return SetContextOrganization(client.KubeconfigPath, client.KubeconfigContext, s.Organization)
}
11 changes: 5 additions & 6 deletions auth/whoami.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
)

type WhoAmICmd struct {
APIURL string `help:"The URL of the Nine API" default:"https://nineapis.ch" env:"NCTL_API_URL" name:"api-url"`
IssuerURL string `help:"Issuer URL is the OIDC issuer URL of the API." default:"https://auth.nine.ch/auth/realms/pub"`
ClientID string `help:"Client ID is the OIDC client ID of the API." default:"nineapis.ch-f178254"`
ExecPlugin bool `help:"Automatically run exec plugin after writing the kubeconfig." hidden:"" default:"true"`
APIURL string `help:"The URL of the Nine API" default:"https://nineapis.ch" env:"NCTL_API_URL" name:"api-url"`
IssuerURL string `help:"Issuer URL is the OIDC issuer URL of the API." default:"https://auth.nine.ch/auth/realms/pub"`
ClientID string `help:"Client ID is the OIDC client ID of the API." default:"nineapis.ch-f178254"`
}

func (s *WhoAmICmd) Run(ctx context.Context, client *api.Client) error {
Expand All @@ -38,13 +37,13 @@ func printUserInfo(userInfo *api.UserInfo, cfg *Config) {

fmt.Printf("Your current organization: %q\n", cfg.Organization)

if len(userInfo.Orgs) > 1 {
if len(userInfo.Orgs) > 0 {
printAvailableOrgsString(cfg.Organization, userInfo.Orgs)
}
}

func printAvailableOrgsString(currentorg string, orgs []string) {
fmt.Print("\nActive\tOrganization\n")
fmt.Println("\nAvailable Organizations:")

for _, org := range orgs {
activeMarker := ""
Expand Down
5 changes: 2 additions & 3 deletions auth/whoami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ func TestWhoAmICmd_Run(t *testing.T) {
defer os.Remove(kubeconfig)

s := &auth.WhoAmICmd{
IssuerURL: "https://auth.nine.ch/auth/realms/pub",
ClientID: "nineapis.ch-f178254",
ExecPlugin: true,
IssuerURL: "https://auth.nine.ch/auth/realms/pub",
ClientID: "nineapis.ch-f178254",
}

err = s.Run(context.Background(), apiClient)
Expand Down
5 changes: 0 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ func main() {
return
}

if strings.HasPrefix(kongCtx.Command(), format.SetOrgCommand) {
pawelkuc marked this conversation as resolved.
Show resolved Hide resolved
kongCtx.FatalIfErrorf(nctl.Auth.SetOrg.Run(ctx, command))
return
}

if strings.HasPrefix(kongCtx.Command(), auth.OIDCCmdName) {
kongCtx.FatalIfErrorf(nctl.Auth.OIDC.Run(ctx, os.Stdout))
return
Expand Down