Reorganized some of the commands

This commit is contained in:
Gregory Ballantine 2018-07-20 14:21:51 -04:00
parent 73ce95d30b
commit 4960f91665
10 changed files with 228 additions and 210 deletions

View File

@ -18,15 +18,11 @@ var (
func init() { func init() {
// register group subcommands // register group subcommands
groupCmd.AddCommand(groupAddCmd, groupDeleteCmd) GroupCmd.AddCommand(groupAddCmd, groupDeleteCmd)
}
func GetGroupCmd() *cobra.Command {
return groupCmd
} }
// define group command // define group command
var groupCmd = &cobra.Command{ var GroupCmd = &cobra.Command{
Use: "group", Use: "group",
Short: "Manage LDAP group resources", Short: "Manage LDAP group resources",
Long: `Perform various LDAP operations on group resources.`, Long: `Perform various LDAP operations on group resources.`,

View File

@ -8,6 +8,7 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
"git.metaunix.net/metaunix.net/muldap/cmd/group" "git.metaunix.net/metaunix.net/muldap/cmd/group"
"git.metaunix.net/metaunix.net/muldap/cmd/user"
) )
func init() { func init() {
@ -19,7 +20,7 @@ func init() {
viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port")) viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
// register commands // register commands
rootCmd.AddCommand(group.GetGroupCmd()) rootCmd.AddCommand(group.GroupCmd, user.UserCmd, searchCmd, setupCmd, versionCmd)
} }
// define root command // define root command
@ -28,7 +29,7 @@ var rootCmd = &cobra.Command{
Short: "muldap is Metaunix.net's LDAP management tool", Short: "muldap is Metaunix.net's LDAP management tool",
Long: "LDAP management tool for Metaunix.net user and group resources.", Long: "LDAP management tool for Metaunix.net user and group resources.",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Metaunix.net LDAP tool - available commands are: group, user, search, version") fmt.Println("Metaunix.net LDAP tool - available commands are: group, user, search, setup, version")
}, },
} }

View File

@ -24,9 +24,6 @@ func init() {
// bind config file values to flags // bind config file values to flags
viper.BindPFlag("search_base", searchCmd.Flags().Lookup("base")) viper.BindPFlag("search_base", searchCmd.Flags().Lookup("base"))
viper.BindPFlag("print_indent", searchCmd.Flags().Lookup("indent")) viper.BindPFlag("print_indent", searchCmd.Flags().Lookup("indent"))
// register search command
rootCmd.AddCommand(searchCmd)
} }
// define search command // define search command

View File

@ -18,10 +18,6 @@ var (
defaultPerms os.FileMode = 0700 defaultPerms os.FileMode = 0700
) )
func init() {
rootCmd.AddCommand(setupCmd)
}
var setupCmd = &cobra.Command{ var setupCmd = &cobra.Command{
Use: "setup", Use: "setup",
Short: "Set up your user environment for muldap", Short: "Set up your user environment for muldap",

View File

@ -1,191 +0,0 @@
package cmd
import (
"fmt"
"log"
"strconv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ldap "gopkg.in/ldap.v2"
cli "git.metaunix.net/metaunix.net/muldap/lib/cli"
)
var (
// user subcommand arguments
flagUserUsername string
flagUserEmail string
flagUserFirstName string
flagUserLastName string
flagUserIdNumber int
)
func init() {
// define user add subcommand flags
userAddCmd.Flags().StringP("base_ou", "o", "", "LDAP OU to create the new user entry under")
userAddCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
userAddCmd.Flags().StringP("home_directory", "d", "/home/%s", "User's home directory")
userAddCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username for a new user")
userAddCmd.Flags().StringVarP(&flagUserEmail, "email", "e", "", "Email address for a new user")
userAddCmd.Flags().StringVarP(&flagUserFirstName, "first_name", "f", "", "First name of a new user")
userAddCmd.Flags().StringVarP(&flagUserLastName, "last_name", "l", "", "Last name of a new user")
userAddCmd.Flags().IntVarP(&flagUserIdNumber, "id_number", "i", -1, "ID Number for a new user")
// bind config file values to user add flags
viper.BindPFlag("user.base_ou", userAddCmd.Flags().Lookup("base_ou"))
viper.BindPFlag("user.uid_attr", userAddCmd.Flags().Lookup("uid_attribute"))
viper.BindPFlag("user.home_directory", userAddCmd.Flags().Lookup("home_directory"))
// define user delete subcommand flags
userDeleteCmd.Flags().StringP("base_ou", "o", "", "LDAP OU where your user entries are stored")
userDeleteCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
userDeleteCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username of user to delete")
// bind config file values to user delete flags
viper.BindPFlag("user.base_ou", userDeleteCmd.Flags().Lookup("base_ou"))
viper.BindPFlag("user.uid_attr", userDeleteCmd.Flags().Lookup("uid_attribute"))
// define user delete subcommand flags
userPwCmd.Flags().StringP("base_ou", "o", "", "LDAP OU where your user entries are stored")
userPwCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
userPwCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username of user to delete")
// bind config file values to user delete flags
viper.BindPFlag("user.base_ou", userPwCmd.Flags().Lookup("base_ou"))
viper.BindPFlag("user.uid_attr", userPwCmd.Flags().Lookup("uid_attribute"))
// register add command and subcommands
userCmd.AddCommand(userAddCmd, userDeleteCmd, userPwCmd)
rootCmd.AddCommand(userCmd)
}
// define user command
var userCmd = &cobra.Command{
Use: "user",
Short: "Manage LDAP user resources",
Long: `Perform various LDAP operations on user resources.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Metaunix.net LDAP tool, user command. Available subcommands are: add, delete, pw")
},
}
// define user subcommand
var userAddCmd = &cobra.Command{
Use: "add",
Short: "Add an LDAP user to the directory",
Long: `Create and add an LDAP user resource to your directory.`,
Run: func(cmd *cobra.Command, args []string) {
// create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port")))
if err != nil {
log.Fatal(err)
}
defer l.Close()
// bind as the admin user
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
if err != nil {
log.Fatal(err)
}
// set up user attributes
userDn := fmt.Sprintf("%s=%s,%s", viper.GetString("user.uid_attr"), flagUserUsername, viper.GetString("user.base_ou"))
userDisplayName := fmt.Sprintf("%s %s", flagUserFirstName, flagUserLastName)
userHome := fmt.Sprintf(viper.GetString("user.home_directory"), flagUserUsername)
// create a new add request object
addRequest := ldap.NewAddRequest(userDn)
// add user attributes to the request
addRequest.Attribute(viper.GetString("user.uid_attr"), []string{flagUserUsername})
addRequest.Attribute("objectClass", viper.GetStringSlice("user.object_class"))
addRequest.Attribute("mail", []string{flagUserEmail})
addRequest.Attribute("givenName", []string{flagUserFirstName})
addRequest.Attribute("sn", []string{flagUserLastName})
addRequest.Attribute("cn", []string{flagUserFirstName})
addRequest.Attribute("displayName", []string{userDisplayName})
addRequest.Attribute("uidNumber", []string{strconv.Itoa(flagUserIdNumber)})
addRequest.Attribute("gidNumber", []string{strconv.Itoa(flagUserIdNumber)})
addRequest.Attribute("homeDirectory", []string{userHome})
// loop through extra attributes
for key, value := range viper.GetStringMapString("user.extra_attributes") {
addRequest.Attribute(key, []string{value})
}
// perform the add operation
err = l.Add(addRequest)
if err != nil {
log.Fatal(err)
}
},
}
// define user delete subcommand
var userDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete an LDAP user from the directory",
Long: `Delete an LDAP user resource from the directory.`,
Run: func(cmd *cobra.Command, args []string) {
// create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port")))
if err != nil {
log.Fatal(err)
}
defer l.Close()
// bind as the admin user
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
if err != nil {
log.Fatal(err)
}
// set up user DN
userDn := fmt.Sprintf("%s=%s,%s", viper.GetString("user.uid_attr"), flagUserUsername, viper.GetString("user.base_ou"))
// create a new delete request object
deleteRequest := ldap.NewDelRequest(userDn, []ldap.Control{})
// perform the delete operation
err = l.Del(deleteRequest)
if err != nil {
log.Fatal(err)
}
},
}
// define user password subcommand
var userPwCmd = &cobra.Command{
Use: "pw",
Short: "Set an LDAP user's password",
Long: "Prompts you for a password to use to set an LDAP user's password",
Run: func(cmd *cobra.Command, args []string) {
// create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port")))
if err != nil {
log.Fatal(err)
}
defer l.Close()
// bind as the admin user
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
if err != nil {
log.Fatal(err)
}
// get password from user
userPw, pwErr := cli.GetUserInput("New user password", true)
if pwErr != nil {
log.Fatal(pwErr)
}
// set up user DN
userDn := fmt.Sprintf("%s=%s,%s", viper.GetString("user.uid_attr"), flagUserUsername, viper.GetString("user.base_ou"))
// create new password modify request
passwordModifyRequest := ldap.NewPasswordModifyRequest(userDn, "", userPw)
// perform password change operation
_, err = l.PasswordModify(passwordModifyRequest)
if err != nil {
log.Fatalf("Password could not be changed: %s", err.Error())
}
},
}

77
cmd/user/add.go Normal file
View File

@ -0,0 +1,77 @@
package user
import (
"fmt"
"log"
"strconv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ldap "gopkg.in/ldap.v2"
)
func init() {
// define user add subcommand flags
userAddCmd.Flags().StringP("base_ou", "o", "", "LDAP OU to create the new user entry under")
userAddCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
userAddCmd.Flags().StringP("home_directory", "d", "/home/%s", "User's home directory")
userAddCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username for a new user")
userAddCmd.Flags().StringVarP(&flagUserEmail, "email", "e", "", "Email address for a new user")
userAddCmd.Flags().StringVarP(&flagUserFirstName, "first_name", "f", "", "First name of a new user")
userAddCmd.Flags().StringVarP(&flagUserLastName, "last_name", "l", "", "Last name of a new user")
userAddCmd.Flags().IntVarP(&flagUserIdNumber, "id_number", "i", -1, "ID Number for a new user")
// bind config file values to user add flags
viper.BindPFlag("user.base_ou", userAddCmd.Flags().Lookup("base_ou"))
viper.BindPFlag("user.uid_attr", userAddCmd.Flags().Lookup("uid_attribute"))
viper.BindPFlag("user.home_directory", userAddCmd.Flags().Lookup("home_directory"))
}
// define user add subcommand
var userAddCmd = &cobra.Command{
Use: "add",
Short: "Add an LDAP user to the directory",
Long: `Create and add an LDAP user resource to your directory.`,
Run: func(cmd *cobra.Command, args []string) {
// create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port")))
if err != nil {
log.Fatal(err)
}
defer l.Close()
// bind as the admin user
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
if err != nil {
log.Fatal(err)
}
// set up user attributes
userDn := fmt.Sprintf("%s=%s,%s", viper.GetString("user.uid_attr"), flagUserUsername, viper.GetString("user.base_ou"))
userDisplayName := fmt.Sprintf("%s %s", flagUserFirstName, flagUserLastName)
userHome := fmt.Sprintf(viper.GetString("user.home_directory"), flagUserUsername)
// create a new add request object
addRequest := ldap.NewAddRequest(userDn)
// add user attributes to the request
addRequest.Attribute(viper.GetString("user.uid_attr"), []string{flagUserUsername})
addRequest.Attribute("objectClass", viper.GetStringSlice("user.object_class"))
addRequest.Attribute("mail", []string{flagUserEmail})
addRequest.Attribute("givenName", []string{flagUserFirstName})
addRequest.Attribute("sn", []string{flagUserLastName})
addRequest.Attribute("cn", []string{flagUserFirstName})
addRequest.Attribute("displayName", []string{userDisplayName})
addRequest.Attribute("uidNumber", []string{strconv.Itoa(flagUserIdNumber)})
addRequest.Attribute("gidNumber", []string{strconv.Itoa(flagUserIdNumber)})
addRequest.Attribute("homeDirectory", []string{userHome})
// loop through extra attributes
for key, value := range viper.GetStringMapString("user.extra_attributes") {
addRequest.Attribute(key, []string{value})
}
// perform the add operation
err = l.Add(addRequest)
if err != nil {
log.Fatal(err)
}
},
}

53
cmd/user/delete.go Normal file
View File

@ -0,0 +1,53 @@
package user
import (
"fmt"
"log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ldap "gopkg.in/ldap.v2"
)
func init() {
// define user delete subcommand flags
userDeleteCmd.Flags().StringP("base_ou", "o", "", "LDAP OU where your user entries are stored")
userDeleteCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
userDeleteCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username of user to delete")
// bind config file values to user delete flags
viper.BindPFlag("user.base_ou", userDeleteCmd.Flags().Lookup("base_ou"))
viper.BindPFlag("user.uid_attr", userDeleteCmd.Flags().Lookup("uid_attribute"))
}
// define user delete subcommand
var userDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete an LDAP user from the directory",
Long: `Delete an LDAP user resource from the directory.`,
Run: func(cmd *cobra.Command, args []string) {
// create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port")))
if err != nil {
log.Fatal(err)
}
defer l.Close()
// bind as the admin user
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
if err != nil {
log.Fatal(err)
}
// set up user DN
userDn := fmt.Sprintf("%s=%s,%s", viper.GetString("user.uid_attr"), flagUserUsername, viper.GetString("user.base_ou"))
// create a new delete request object
deleteRequest := ldap.NewDelRequest(userDn, []ldap.Control{})
// perform the delete operation
err = l.Del(deleteRequest)
if err != nil {
log.Fatal(err)
}
},
}

62
cmd/user/pw.go Normal file
View File

@ -0,0 +1,62 @@
package user
import (
"fmt"
"log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ldap "gopkg.in/ldap.v2"
cli "git.metaunix.net/metaunix.net/muldap/lib/cli"
)
func init() {
// define user delete subcommand flags
userPwCmd.Flags().StringP("base_ou", "o", "", "LDAP OU where your user entries are stored")
userPwCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
userPwCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username of user to delete")
// bind config file values to user delete flags
viper.BindPFlag("user.base_ou", userPwCmd.Flags().Lookup("base_ou"))
viper.BindPFlag("user.uid_attr", userPwCmd.Flags().Lookup("uid_attribute"))
}
// define user password subcommand
var userPwCmd = &cobra.Command{
Use: "pw",
Short: "Set an LDAP user's password",
Long: "Prompts you for a password to use to set an LDAP user's password",
Run: func(cmd *cobra.Command, args []string) {
// create new LDAP connection
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", viper.GetString("host"), viper.GetInt("port")))
if err != nil {
log.Fatal(err)
}
defer l.Close()
// bind as the admin user
err = l.Bind(viper.GetString("bind_dn"), viper.GetString("bind_pw"))
if err != nil {
log.Fatal(err)
}
// get password from user
userPw, pwErr := cli.GetUserInput("New user password", true)
if pwErr != nil {
log.Fatal(pwErr)
}
// set up user DN
userDn := fmt.Sprintf("%s=%s,%s", viper.GetString("user.uid_attr"), flagUserUsername, viper.GetString("user.base_ou"))
// create new password modify request
passwordModifyRequest := ldap.NewPasswordModifyRequest(userDn, "", userPw)
// perform password change operation
_, err = l.PasswordModify(passwordModifyRequest)
if err != nil {
log.Fatalf("Password could not be changed: %s", err.Error())
}
},
}

31
cmd/user/user.go Normal file
View File

@ -0,0 +1,31 @@
package user
import (
"fmt"
"github.com/spf13/cobra"
)
var (
// user subcommand arguments
flagUserUsername string
flagUserEmail string
flagUserFirstName string
flagUserLastName string
flagUserIdNumber int
)
func init() {
// register add command and subcommands
UserCmd.AddCommand(userAddCmd, userDeleteCmd, userPwCmd)
}
// define user command
var UserCmd = &cobra.Command{
Use: "user",
Short: "Manage LDAP user resources",
Long: `Perform various LDAP operations on user resources.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Metaunix.net LDAP tool, user command. Available subcommands are: add, delete, pw")
},
}

View File

@ -6,10 +6,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func init() {
rootCmd.AddCommand(versionCmd)
}
var versionCmd = &cobra.Command{ var versionCmd = &cobra.Command{
Use: "version", Use: "version",
Short: "Print the version number of muldap", Short: "Print the version number of muldap",