Reorganized some commands to make more sense
This commit is contained in:
parent
4e8d5bb4e1
commit
0a534c537b
100
cmd/add.go
100
cmd/add.go
@ -1,100 +0,0 @@
|
|||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
ldap "gopkg.in/ldap.v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// user subcommand arguments
|
|
||||||
flagUserUsername string
|
|
||||||
flagUserEmail string
|
|
||||||
flagUserFirstName string
|
|
||||||
flagUserLastName string
|
|
||||||
flagUserIdNumber int
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
// define add user subcommand flags
|
|
||||||
addUserCmd.Flags().StringP("base_ou", "o", "", "LDAP OU to create the new user entry under")
|
|
||||||
addUserCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
|
|
||||||
addUserCmd.Flags().StringP("home_directory", "d", "/home/%s", "User's home directory")
|
|
||||||
addUserCmd.Flags().StringVarP(&flagUserUsername, "username", "u", "", "Username for a new user")
|
|
||||||
addUserCmd.Flags().StringVarP(&flagUserEmail, "email", "e", "", "Email address for a new user")
|
|
||||||
addUserCmd.Flags().StringVarP(&flagUserFirstName, "first_name", "f", "", "First name of a new user")
|
|
||||||
addUserCmd.Flags().StringVarP(&flagUserLastName, "last_name", "l", "", "Last name of a new user")
|
|
||||||
addUserCmd.Flags().IntVarP(&flagUserIdNumber, "id_number", "i", -1, "ID Number for a new user")
|
|
||||||
// bind config file values to user flags
|
|
||||||
viper.BindPFlag("user.base_ou", addUserCmd.Flags().Lookup("base_ou"))
|
|
||||||
viper.BindPFlag("user.uid_attr", addUserCmd.Flags().Lookup("uid_attribute"))
|
|
||||||
viper.BindPFlag("user.home_directory", addUserCmd.Flags().Lookup("home_directory"))
|
|
||||||
|
|
||||||
// register add command and subcommands
|
|
||||||
addCmd.AddCommand(addUserCmd)
|
|
||||||
rootCmd.AddCommand(addCmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
// define add command
|
|
||||||
var addCmd = &cobra.Command{
|
|
||||||
Use: "add",
|
|
||||||
Short: "Add an LDAP resource to the directory",
|
|
||||||
Long: `Create and add an LDAP resource to your directory.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
fmt.Println("Metaunix.net LDAP tool, add command. Available subcommands are: user")
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// define add user subcommand
|
|
||||||
var addUserCmd = &cobra.Command{
|
|
||||||
Use: "user",
|
|
||||||
Short: "Add an LDAP user resource 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)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
@ -1,72 +0,0 @@
|
|||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
ldap "gopkg.in/ldap.v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// user subcommand arguments
|
|
||||||
flagUsername string
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
// define delete user subcommand flags
|
|
||||||
deleteUserCmd.Flags().StringP("base_ou", "o", "", "LDAP OU where your user entries are stored")
|
|
||||||
deleteUserCmd.Flags().StringP("uid_attribute", "a", "uid", "LDAP DN attribute for users")
|
|
||||||
deleteUserCmd.Flags().StringVarP(&flagUsername, "username", "u", "", "Username of user to delete")
|
|
||||||
// bind config file values to user flags
|
|
||||||
viper.BindPFlag("user.base_ou", deleteUserCmd.Flags().Lookup("base_ou"))
|
|
||||||
viper.BindPFlag("user.uid_attr", deleteUserCmd.Flags().Lookup("uid_attribute"))
|
|
||||||
|
|
||||||
// register delete command and subcommands
|
|
||||||
deleteCmd.AddCommand(deleteUserCmd)
|
|
||||||
rootCmd.AddCommand(deleteCmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
// define delete command
|
|
||||||
var deleteCmd = &cobra.Command{
|
|
||||||
Use: "delete",
|
|
||||||
Short: "delete an LDAP resource from the directory",
|
|
||||||
Long: `Delete an LDAP resource from the directory.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
fmt.Println("Metaunix.net LDAP tool, delete command. Available subcommands are: user")
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// define delete user subcommand
|
|
||||||
var deleteUserCmd = &cobra.Command{
|
|
||||||
Use: "user",
|
|
||||||
Short: "delete an LDAP user resource 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"), flagUsername, 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)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
141
cmd/user.go
Normal file
141
cmd/user.go
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
ldap "gopkg.in/ldap.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"))
|
||||||
|
|
||||||
|
// register add command and subcommands
|
||||||
|
userCmd.AddCommand(userAddCmd, userDeleteCmd)
|
||||||
|
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")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user