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

Add LDAP commands to the CLI #116

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 6 additions & 5 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/goharbor/harbor-cli/cmd/harbor/root/artifact"
"github.com/goharbor/harbor-cli/cmd/harbor/root/ldap"
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
repositry "github.com/goharbor/harbor-cli/cmd/harbor/root/repository"
Expand Down Expand Up @@ -49,13 +50,11 @@ func initConfig() {
}
}
err = utils.CreateConfigFile()

if err != nil {
log.Fatal(err)
}

err = utils.AddCredentialsToConfigFile(utils.Credential{}, cfgFile)

if err != nil {
log.Fatal(err)
}
Expand All @@ -67,7 +66,6 @@ func initConfig() {
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file: %s", err)
}

}

func RootCmd() *cobra.Command {
Expand All @@ -92,8 +90,10 @@ harbor help

cobra.OnInitialize(initConfig)

root.PersistentFlags().StringVarP(&output, "output-format", "o", "", "Output format. One of: json|yaml")
root.PersistentFlags().StringVar(&cfgFile, "config", utils.DefaultConfigPath, "config file (default is $HOME/.harbor/config.yaml)")
root.PersistentFlags().
StringVarP(&output, "output-format", "o", "", "Output format. One of: json|yaml")
root.PersistentFlags().
StringVar(&cfgFile, "config", utils.DefaultConfigPath, "config file (default is $HOME/.harbor/config.yaml)")
root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")

viper.BindPFlag("output-format", root.PersistentFlags().Lookup("output-format"))
Expand All @@ -106,6 +106,7 @@ harbor help
repositry.Repository(),
user.User(),
artifact.Artifact(),
ldap.Ldap(),
)

return root
Expand Down
19 changes: 19 additions & 0 deletions cmd/harbor/root/ldap/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ldap

import (
"github.com/spf13/cobra"
)

func Ldap() *cobra.Command {
cmd := &cobra.Command{
Use: "ldap",
Short: "Manage ldap users and groups",
Example: ` harbor ldap ping`,
}
cmd.AddCommand(
LdapSearchUserCmd(),
LdapPingCmd(),
)

return cmd
}
41 changes: 41 additions & 0 deletions cmd/harbor/root/ldap/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ldap

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// Ping ldap server command
func LdapPingCmd() *cobra.Command {
opts := &models.LdapConf{}
cmd := &cobra.Command{
Use: "ping",
Short: "ping ldap server",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
response, err := api.LdapPingServer(opts)
if err != nil {
log.Fatalf("failed to ping ldap server: %v", err)
}
if response.Payload.Success {
log.Info("Connection to LDAP Server Success")
} else {
log.Fatalf("connection to ldap server failed: %v", response.Payload.Message)
}
},
}

flags := cmd.Flags()
flags.StringVarP(&opts.LdapURL, "ldap-url", "", "", "URL of the ldap service")
flags.StringVarP(&opts.LdapSearchPassword, "ldap-password", "", "", "search password of the ldap service")
flags.StringVarP(&opts.LdapSearchDn, "ldap-search-dn", "", "", "User's dn who has the permission to search the ldap server")
flags.StringVarP(&opts.LdapBaseDn, "ldap-base-dn", "", "", "The base dn from which to lookup the user")
flags.StringVarP(&opts.LdapUID, "ldap-uid", "", "", "attribute used in search to match the user. It could be cn, uid based on your LDAP/AD.")
flags.Int64VarP(&opts.LdapScope, "ldap-scope", "", 0, "search scope of ldap service default 0 base, 1 OneLevel, 2 Subtree.")
flags.StringVarP(&opts.LdapFilter, "ldap-filter", "", "", "Search Filter of ldap service")
flags.BoolVarP(&opts.LdapVerifyCert, "ldap-verify", "", false, "Verify Ldap server certificate")

return cmd
}
27 changes: 27 additions & 0 deletions cmd/harbor/root/ldap/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ldap

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// Search ldap users command
func LdapSearchUserCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "search [userID]",
Short: "search ldap user by registered userid",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
response, err := api.LdapSearchUser(args[0])
if err != nil {
log.Fatalf("failed to search ldap user: %v", err)
}

utils.PrintPayloadInJSONFormat(response.Payload)
},
}

return cmd
}
39 changes: 39 additions & 0 deletions pkg/api/ldap_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package api

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/ldap"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
)

func LdapSearchUser(username string) (*ldap.SearchLdapUserOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil, err
}

res, err := client.Ldap.SearchLdapUser(ctx, &ldap.SearchLdapUserParams{
Username: &username,
})
if err != nil {
return nil, err
}

return res, nil
}

func LdapPingServer(ldapConf *models.LdapConf) (*ldap.PingLdapOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil, err
}

res, err := client.Ldap.PingLdap(ctx, &ldap.PingLdapParams{
Ldapconf: ldapConf,
})
if err != nil {
return nil, err
}

return res, nil
}