Added LDIF subcommand to add resources using LDIF files

This commit is contained in:
Gregory Ballantine 2018-08-27 16:29:36 -04:00
parent 9b7763156d
commit b98d681bb0
4 changed files with 99 additions and 2 deletions

8
Gopkg.lock generated
View File

@ -87,6 +87,12 @@
revision = "b5e8006cbee93ec955a89ab31e0e3ce3204f3736"
version = "v1.0.2"
[[projects]]
branch = "master"
name = "github.com/vetinari/ldif"
packages = ["."]
revision = "2a83f6d395431fb91033fc94648b51d61eb8a51d"
[[projects]]
branch = "master"
name = "golang.org/x/sys"
@ -127,6 +133,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "f8e69db90fe3209c5c8054779e7a7719beee8090ee5d5c690d8f1bb96ff940c3"
inputs-digest = "1ec167dd8928fc2b65331156073a8bf55130cb0524f594653a9a10f633458e46"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -40,3 +40,7 @@
[[constraint]]
name = "github.com/spf13/viper"
version = "1.0.2"
[[constraint]]
branch = "master"
name = "github.com/vetinari/ldif"

86
cmd/ldif/init.go Normal file
View File

@ -0,0 +1,86 @@
package ldif
import (
"fmt"
"io/ioutil"
"log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
ldif "github.com/vetinari/ldif"
ldap "gopkg.in/ldap.v2"
)
var (
// ldif subcommand arguments
flagLdifFilePath string
)
func init() {
// define ldif command flags
LdifCmd.Flags().StringVarP(&flagLdifFilePath, "file_path", "f", "", "LDIF file to import")
}
// define ldif subcommand
var LdifCmd = &cobra.Command{
Use: "ldif",
Short: "Import an LDIF file",
Long: `Import an LDIF file 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)
}
// read LDIF file to bytes
ldifBytes, err := ioutil.ReadFile(flagLdifFilePath)
if err != nil {
log.Fatal(err)
}
// convert bytes to string
ldifText := string(ldifBytes)
// parse ldif text
ldifParse, err := ldif.Parse(ldifText)
if err != nil {
log.Fatal(err)
}
// let the user know how many entries we found in the LDIF
fmt.Printf("I found %d entries in the LDIF file.\n", len(ldifParse.Entries))
// loop through each entry
for _, entryWrap := range ldifParse.Entries {
// grab the entry
entry := entryWrap.Entry
// grab the entry DN
entryDn := entry.DN
// create a new LDAP add request
addRequest := ldap.NewAddRequest(entryDn)
// loop through and add the entry attributes
for _, entryAttr := range entry.Attributes {
addRequest.Attribute(entryAttr.Name, entryAttr.Values)
}
// let the user know what is currently being added
fmt.Printf("I am adding %s right now.\n", entryDn)
// perform the add operation
err = l.Add(addRequest)
if err != nil {
log.Fatal(err)
}
}
},
}

View File

@ -8,6 +8,7 @@ import (
"github.com/spf13/viper"
"git.metaunix.net/metaunix.net/muldap/cmd/group"
"git.metaunix.net/metaunix.net/muldap/cmd/ldif"
"git.metaunix.net/metaunix.net/muldap/cmd/user"
)
@ -20,7 +21,7 @@ func init() {
viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
// register commands
rootCmd.AddCommand(group.GroupCmd, user.UserCmd, searchCmd, setupCmd, versionCmd)
rootCmd.AddCommand(group.GroupCmd, ldif.LdifCmd, user.UserCmd, searchCmd, setupCmd, versionCmd)
}
// define root command