misc

package
v0.0.0-...-8fe0c81 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2022 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Copyright (C) 2020 Serge ALEXANDRE

This file is part of koobind project

koobind is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

koobind is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with koobind. If not, see <http://www.gnu.org/licenses/>.

Copyright (C) 2020 Serge ALEXANDRE

This file is part of koobind project

koobind is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

koobind is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with koobind.  If not, see <http://www.gnu.org/licenses/>.

This file is part of koobind project

koobind is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

koobind is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with koobind. If not, see <http://www.gnu.org/licenses/>.

This file is part of koobind project

koobind is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

koobind is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with koobind. If not, see <http://www.gnu.org/licenses/>.

This file is part of koobind project

koobind is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

koobind is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with koobind. If not, see <http://www.gnu.org/licenses/>.

Copyright (C) 2020 Serge ALEXANDRE

This file is part of koobind project

koobind is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

koobind is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with koobind.  If not, see <http://www.gnu.org/licenses/>.

This file is part of koobind project

koobind is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

koobind is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with koobind. If not, see <http://www.gnu.org/licenses/>.

Index

Constants

This section is empty.

Variables

View Source
var AuthCmd = &cobra.Command{
	Use:    "auth",
	Short:  "To be used as client-go exec plugin",
	Hidden: true,
	Run: func(cmd *cobra.Command, args []string) {
		common.InitHttpConnection()
		tokenBag := common.RetrieveTokenBag()
		if tokenBag == nil {
			tokenBag = common.DoLoginSilently("", "")
		}
		ec := ExecCredential{
			ApiVersion: "client.authentication.k8s.io/v1beta1",
			Kind:       "ExecCredential",
		}
		if tokenBag == nil {

		} else {
			ec.Status.Token = tokenBag.Token
		}
		err := json.NewEncoder(os.Stdout).Encode(ec)
		if err != nil {
			panic(err)
		}
	},
}
View Source
var HashCmd = &cobra.Command{
	Use:   "hash",
	Short: "Provided password hash, for use in config file",
	Run: func(cmd *cobra.Command, args []string) {
		if hash_password == "" {
			hash_password = inputPassword("Password:")
			password2 := inputPassword("Confirm password:")
			if hash_password != password2 {
				fmt.Printf("Passwords did not match!\n")
				return
			}
		}
		hash, err := bcrypt.GenerateFromPassword([]byte(hash_password), bcrypt.DefaultCost)
		if err != nil {
			panic(err)
		}
		fmt.Printf("%s\n", string(hash))
	},
}
View Source
var LoginCmd = &cobra.Command{
	Use:   "login",
	Short: "Logout and get a new token",
	Run: func(cmd *cobra.Command, args []string) {
		common.InitHttpConnection()
		internal.DeleteTokenBag(common.Context)
		tokenBag := common.DoLogin(login, login_password)
		if tokenBag == nil {
			os.Exit(3)
		}
	},
}
View Source
var LogoutCmd = &cobra.Command{
	Use:   "logout",
	Short: "Clear local token",
	Run: func(cmd *cobra.Command, args []string) {
		internal.DeleteTokenBag(common.Context)
		fmt.Printf("Bye!\n")
	},
}
View Source
var PasswordCmd = &cobra.Command{
	Use:   "password",
	Short: "Change current user password",
	Run: func(cmd *cobra.Command, args []string) {
		common.InitHttpConnection()
		tokenBag := common.RetrieveTokenBag()
		if tokenBag == nil {
			_, _ = fmt.Fprintf(os.Stderr, "Not logged in currently\n")
			return
		}
		if oldPassword == "" {
			oldPassword = inputPassword("Old password:")
		}
		if newPassword == "" {
			newPassword = inputPasswordWithConfirm()
			if newPassword == "" {
				_, _ = fmt.Fprintf(os.Stderr, "Too many failure !!!\n")
				return
			}
		}
		changePasswordRequest := proto.ChangePasswordRequest{
			OldPassword: oldPassword,
			NewPassword: newPassword,
			Client:      common.Config.Client,
		}
		body, err := json.Marshal(changePasswordRequest)
		response, err := common.HttpConnection.Do("POST", proto.ChangePasswordUrlPath, &internal.HttpAuth{Token: tokenBag.Token}, bytes.NewBuffer(body))
		if err != nil {
			panic(err)
		}
		if response.StatusCode == http.StatusOK {
			fmt.Printf("Password changed successfully.\n")
			return
		} else {
			common.PrintHttpResponseMessage(response)
			os.Exit(internal.ReturnCodeFromStatusCode(response.StatusCode))
		}
	},
}
View Source
var VersionCmd = &cobra.Command{
	Use:   "version",
	Short: "Display current version",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Printf("Version %s\n", version)
	},
}
View Source
var WhoamiCmd = &cobra.Command{
	Use:   "whoami",
	Short: "Display current logged user, if any",
	Run: func(cmd *cobra.Command, args []string) {
		common.InitHttpConnection()
		tokenBag := common.RetrieveTokenBag()
		if tokenBag != nil {
			tw := new(tabwriter.Writer)
			tw.Init(os.Stdout, 2, 4, 3, ' ', 0)
			if displayToken {
				_, _ = fmt.Fprintf(tw, "USER\tID\tGROUPS\tTOKEN")
				_, _ = fmt.Fprintf(tw, "\n%s\t%s\t%s\t%s", tokenBag.Username, tokenBag.Uid, strings.Join(tokenBag.Groups, ","), tokenBag.Token)
			} else {
				_, _ = fmt.Fprintf(tw, "USER\tID\tGROUPS")
				_, _ = fmt.Fprintf(tw, "\n%s\t%s\t%s", tokenBag.Username, tokenBag.Uid, strings.Join(tokenBag.Groups, ","))
			}
			_, _ = fmt.Fprintf(tw, "\n")
			_ = tw.Flush()
		} else {
			fmt.Printf("Nobody! (Not logged)\n")
			os.Exit(3)
		}
	},
}

Functions

This section is empty.

Types

type ExecCredential

type ExecCredential struct {
	ApiVersion string `json:"apiVersion"`
	Kind       string `json:"kind"`
	Status     struct {
		Token string `json:"token"`
	} `json:"status"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL