Skip to content

Commit 2611066

Browse files
authored
Change get_config to show config (cea-hpc#7)
1 parent a959ed1 commit 2611066

File tree

1 file changed

+41
-56
lines changed

1 file changed

+41
-56
lines changed

cmd/sshproxyctl/sshproxyctl.go

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,33 @@ func showErrorBanner(configFile string) {
429429
}
430430
}
431431

432+
func showConfig(configFile, userString, groupsString string) {
433+
groupsMap := make(map[string]bool)
434+
userComment := ""
435+
// get system groups of given user, if it exists
436+
userObject, err := user.Lookup(userString)
437+
if err != nil {
438+
userComment = " (unknown on this system)"
439+
} else {
440+
groupsMap, _ = utils.GetGroupUser(userObject)
441+
}
442+
// add given groups to system groups
443+
for _, group := range strings.Split(groupsString, ",") {
444+
if group != "" {
445+
groupsMap[group] = true
446+
}
447+
}
448+
// get config for given user / groups
449+
config, err := utils.LoadConfig(configFile, userString, "", time.Now(), groupsMap)
450+
if err != nil {
451+
log.Fatalf("reading configuration file %s: %v", configFile, err)
452+
}
453+
fmt.Fprintf(os.Stdout, "user = %s%s\n", userString, userComment)
454+
for _, configLine := range utils.PrintConfig(config, groupsMap) {
455+
fmt.Fprintln(os.Stdout, configLine)
456+
}
457+
}
458+
432459
func showVersion() {
433460
fmt.Fprintf(flag.CommandLine.Output(), "%s version %s\n", os.Args[0], SshproxyVersion)
434461
}
@@ -444,7 +471,6 @@ The commands are:
444471
forget forget a host in etcd
445472
disable disable a host in etcd
446473
error_banner set the error banner in etcd
447-
get_config display the calculated configuration
448474
449475
The common options are:
450476
`, os.Args[0])
@@ -476,20 +502,23 @@ Show version and exit.
476502
return fs
477503
}
478504

479-
func newShowParser(csvFlag *bool, jsonFlag *bool, allFlag *bool) *flag.FlagSet {
505+
func newShowParser(csvFlag *bool, jsonFlag *bool, allFlag *bool, userString *string, groupsString *string) *flag.FlagSet {
480506
fs := flag.NewFlagSet("show", flag.ExitOnError)
481507
fs.BoolVar(csvFlag, "csv", false, "show results in CSV format")
482508
fs.BoolVar(jsonFlag, "json", false, "show results in JSON format")
483509
fs.BoolVar(allFlag, "all", false, "show all connections / users / groups")
510+
fs.StringVar(userString, "user", "", "show the config for this specific user and this user's groups (if any)")
511+
fs.StringVar(groupsString, "groups", "", "show the config for these specific groups (comma separated)")
484512
fs.Usage = func() {
485-
fmt.Fprintf(flag.CommandLine.Output(), `Usage: %s show [OPTIONS] COMMAND
513+
fmt.Fprintf(flag.CommandLine.Output(), `Usage: %s show COMMAND [OPTIONS]
486514
487515
The commands are:
488-
connections show connections stored in etcd
489-
hosts show hosts stored in etcd
490-
users show users stored in etcd
491-
groups show groups stored in etcd
492-
error_banner show error banners stored in etcd and in configuration
516+
connections [-all] [-csv|-json] show connections stored in etcd
517+
hosts [-csv|-json] show hosts stored in etcd
518+
users [-all] [-csv|-json] show users stored in etcd
519+
groups [-all] [-csv|-json] show groups stored in etcd
520+
error_banner show error banners stored in etcd and in configuration
521+
config [-user USER] [-groups GROUPS] show the calculated configuration
493522
494523
The options are:
495524
`, os.Args[0])
@@ -542,7 +571,7 @@ func newErrorBannerParser(expireFlag *string) *flag.FlagSet {
542571
fs := flag.NewFlagSet("error_banner", flag.ExitOnError)
543572
fs.StringVar(expireFlag, "expire", "", "set the expiration date of this error banner. Format: YYYY-MM-DD[ HH:MM[:SS]]")
544573
fs.Usage = func() {
545-
fmt.Fprintf(flag.CommandLine.Output(), `Usage: %s error_banner MESSAGE
574+
fmt.Fprintf(flag.CommandLine.Output(), `Usage: %s error_banner [-expire DATE] MESSAGE
546575
547576
Set the error banner in etcd.
548577
@@ -554,24 +583,6 @@ The options are:
554583
return fs
555584
}
556585

557-
func newGetConfigParser(userFlag *string, groupsFlag *string) *flag.FlagSet {
558-
fs := flag.NewFlagSet("get_config", flag.ExitOnError)
559-
fs.StringVar(userFlag, "user", "", "get the config for this specific user")
560-
fs.StringVar(groupsFlag, "groups", "", "get the config for these specific groups (comma separated)")
561-
fs.Usage = func() {
562-
fmt.Fprintf(flag.CommandLine.Output(), `Usage: %s get_config [-user USER] [-groups GROUPS]
563-
564-
Display the calculated configuration. If a user is given, its system groups (if
565-
any) are added to the given groups.
566-
567-
The options are:
568-
`, os.Args[0])
569-
fs.PrintDefaults()
570-
os.Exit(2)
571-
}
572-
return fs
573-
}
574-
575586
func getHostPortFromCommandLine(args []string) ([]string, []string, error) {
576587
hostsNodeset, portsNodeset := "", defaultHostPort
577588
switch len(args) {
@@ -671,12 +682,11 @@ func main() {
671682
parsers := map[string]*flag.FlagSet{
672683
"help": newHelpParser(),
673684
"version": newVersionParser(),
674-
"show": newShowParser(&csvFlag, &jsonFlag, &allFlag),
685+
"show": newShowParser(&csvFlag, &jsonFlag, &allFlag, &userString, &groupsString),
675686
"enable": newEnableParser(),
676687
"forget": newForgetParser(),
677688
"disable": newDisableParser(),
678689
"error_banner": newErrorBannerParser(&expire),
679-
"get_config": newGetConfigParser(&userString, &groupsString),
680690
}
681691

682692
cmd := flag.Arg(0)
@@ -721,6 +731,8 @@ func main() {
721731
showGroups(*configFile, csvFlag, jsonFlag, allFlag)
722732
case "error_banner":
723733
showErrorBanner(*configFile)
734+
case "config":
735+
showConfig(*configFile, userString, groupsString)
724736
default:
725737
fmt.Fprintf(os.Stderr, "ERROR: unknown subcommand: %s\n\n", subcmd)
726738
p.Usage()
@@ -782,33 +794,6 @@ func main() {
782794
p.Usage()
783795
}
784796
setErrorBanner(errorBanner, t, *configFile)
785-
case "get_config":
786-
p := parsers[cmd]
787-
p.Parse(args)
788-
groupsMap := make(map[string]bool)
789-
userComment := ""
790-
// get system groups of given user, if it exists
791-
userObject, err := user.Lookup(userString)
792-
if err != nil {
793-
userComment = " (unknown on this system)"
794-
} else {
795-
groupsMap, _ = utils.GetGroupUser(userObject)
796-
}
797-
// add given groups to system groups
798-
for _, group := range strings.Split(groupsString, ",") {
799-
if group != "" {
800-
groupsMap[group] = true
801-
}
802-
}
803-
// get config for given user / groups
804-
config, err := utils.LoadConfig(*configFile, userString, "", time.Now(), groupsMap)
805-
if err != nil {
806-
log.Fatalf("reading configuration file %s: %v", *configFile, err)
807-
}
808-
fmt.Fprintf(os.Stdout, "user = %s%s\n", userString, userComment)
809-
for _, configLine := range utils.PrintConfig(config, groupsMap) {
810-
fmt.Fprintln(os.Stdout, configLine)
811-
}
812797
default:
813798
fmt.Fprintf(os.Stderr, "ERROR: unknown command: %s\n\n", cmd)
814799
usage()

0 commit comments

Comments
 (0)