github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/kbpagesconfig/cmd_per_path.go (about)

     1  // Copyright 2018 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"text/tabwriter"
    11  
    12  	"github.com/urfave/cli"
    13  )
    14  
    15  var perPathClearCmd = cli.Command{
    16  	Name:      "clear",
    17  	Usage:     "clear the PerPathConfig for the given path(s)",
    18  	UsageText: "clear <path> [path ...]",
    19  	Action: func(c *cli.Context) {
    20  		if len(c.Args()) < 1 {
    21  			fmt.Fprintln(os.Stderr, "need at least 1 arg")
    22  			os.Exit(1)
    23  		}
    24  		editor, err := newKBPConfigEditor(c.GlobalString("dir"))
    25  		if err != nil {
    26  			fmt.Fprintf(os.Stderr,
    27  				"creating config editor error: %v\n", err)
    28  			os.Exit(1)
    29  		}
    30  		for _, p := range c.Args() {
    31  			editor.clearPerPathConfig(p)
    32  		}
    33  		if err := editor.confirmAndWrite(); err != nil {
    34  			fmt.Fprintf(os.Stderr, "writing new config error: %v\n", err)
    35  			os.Exit(1)
    36  		}
    37  	},
    38  }
    39  
    40  var perPathUnsetUserPermissionsCmd = cli.Command{
    41  	Name: "unset-user",
    42  	Usage: "remove a user from the PerPathConfig(s) additional " +
    43  		"permissions of the given path(s). This essentially reverts the " +
    44  		"user's effective permission back to anonymous.",
    45  	UsageText: "unset-user <username> <path> [path ...]",
    46  	Action: func(c *cli.Context) {
    47  		if len(c.Args()) < 2 {
    48  			fmt.Fprintln(os.Stderr, "need at least 2 args")
    49  			os.Exit(1)
    50  		}
    51  		editor, err := newKBPConfigEditor(c.GlobalString("dir"))
    52  		if err != nil {
    53  			fmt.Fprintf(os.Stderr,
    54  				"creating config editor error: %v\n", err)
    55  			os.Exit(1)
    56  		}
    57  		for _, p := range c.Args()[1:] {
    58  			editor.removeUserPermissionsFromPerPathConfig(c.Args()[0], p)
    59  		}
    60  		if err := editor.confirmAndWrite(); err != nil {
    61  			fmt.Fprintf(os.Stderr, "writing new config error: %v\n", err)
    62  			os.Exit(1)
    63  		}
    64  	},
    65  }
    66  
    67  var perPathGetPermissionCmd = cli.Command{
    68  	Name:      "get-permission",
    69  	Usage:     "get permissions for a user on the given path(s)",
    70  	UsageText: "get-permission <username> <path> [path ...]",
    71  	Action: func(c *cli.Context) {
    72  		if len(c.Args()) < 2 {
    73  			fmt.Fprintln(os.Stderr, "need at least 2 args")
    74  			os.Exit(1)
    75  		}
    76  		editor, err := newKBPConfigEditor(c.GlobalString("dir"))
    77  		if err != nil {
    78  			fmt.Fprintf(os.Stderr,
    79  				"creating config editor error: %v\n", err)
    80  			os.Exit(1)
    81  		}
    82  		writer := tabwriter.NewWriter(os.Stdout, 0, 4, 1, '\t', 0)
    83  		fmt.Fprintln(writer, "read\tlist\tpath")
    84  		for _, p := range c.Args()[1:] {
    85  			read, list, err := editor.getUserPermissionsOnPath(c.Args()[0], p)
    86  			if err != nil {
    87  				fmt.Fprintf(os.Stderr, "getting permissions for "+
    88  					"%q on %q error: %v\n", c.Args()[0], p, err)
    89  				os.Exit(1)
    90  			}
    91  			fmt.Fprintf(writer, "%t\t%t\t%s\n", read, list, p)
    92  		}
    93  		if err := writer.Flush(); err != nil {
    94  			fmt.Fprintf(os.Stderr, "flushing tabwriter error: %v\n", err)
    95  			os.Exit(1)
    96  		}
    97  	},
    98  }
    99  
   100  var perPathSetPermissionDefaultCmd = cli.Command{
   101  	Name: "default",
   102  	Usage: "set default permission(s) that all users are granted, " +
   103  		"for the given path(s)",
   104  	UsageText: "default <read|list|read,list> <path> [path ...]",
   105  	Action: func(c *cli.Context) {
   106  		if len(c.Args()) < 2 {
   107  			fmt.Fprintln(os.Stderr, "need at least 2 args")
   108  			os.Exit(1)
   109  		}
   110  		editor, err := newKBPConfigEditor(c.GlobalString("dir"))
   111  		if err != nil {
   112  			fmt.Fprintf(os.Stderr,
   113  				"creating config editor error: %v\n", err)
   114  			os.Exit(1)
   115  		}
   116  		for _, p := range c.Args()[1:] {
   117  			err := editor.setAnonymousPermission(c.Args()[0], p)
   118  			if err != nil {
   119  				fmt.Fprintf(os.Stderr,
   120  					"setting anonymous permission %q on %q error: %v\n",
   121  					c.Args()[0], p, err)
   122  				os.Exit(1)
   123  			}
   124  		}
   125  		if err := editor.confirmAndWrite(); err != nil {
   126  			fmt.Fprintf(os.Stderr, "writing new config error: %v\n", err)
   127  			os.Exit(1)
   128  		}
   129  	},
   130  }
   131  
   132  var perPathSetPermissionAdditionalCmd = cli.Command{
   133  	Name: "additional",
   134  	Usage: "set additional permission(s) that <username> are granted on " +
   135  		"top of default ones on the given path(s) ",
   136  	UsageText: "additional <username> <read|list|read,list> <path> [path ...]",
   137  	Action: func(c *cli.Context) {
   138  		if len(c.Args()) < 3 {
   139  			fmt.Fprintln(os.Stderr, "need at least 3 args")
   140  			os.Exit(1)
   141  		}
   142  		editor, err := newKBPConfigEditor(c.GlobalString("dir"))
   143  		if err != nil {
   144  			fmt.Fprintf(os.Stderr,
   145  				"creating config editor error: %v\n", err)
   146  			os.Exit(1)
   147  		}
   148  		for _, p := range c.Args()[2:] {
   149  			err := editor.setAdditionalPermission(c.Args()[0], c.Args()[1], p)
   150  			if err != nil {
   151  				fmt.Fprintf(os.Stderr,
   152  					"setting additional permission(s) %q for username "+
   153  						"%q on %q error: %v\n",
   154  					c.Args()[1], c.Args()[0], p, err)
   155  				os.Exit(1)
   156  			}
   157  		}
   158  		if err := editor.confirmAndWrite(); err != nil {
   159  			fmt.Fprintf(os.Stderr, "writing new config error: %v\n", err)
   160  			os.Exit(1)
   161  		}
   162  	},
   163  }
   164  
   165  var perPathSetPermissionCmd = cli.Command{
   166  	Name:      "set-permission",
   167  	Usage:     "set default or additional permissions on path(s)",
   168  	UsageText: "set-permission <default|additional> [args]",
   169  	Subcommands: []cli.Command{
   170  		perPathSetPermissionDefaultCmd,
   171  		perPathSetPermissionAdditionalCmd,
   172  	},
   173  }
   174  
   175  // This isn't supported by kbpagesd at this time.
   176  const customPagesEnabled = false
   177  
   178  func getPerPathSetCmdUsageText() string {
   179  	customPage := ""
   180  	if customPagesEnabled {
   181  		customPage = "\n   set <403|404> <path_relative_to_site_root> <path> [path ...]"
   182  	}
   183  	return "set Access-Control-Allow-Origin <''|'*'> <path> [path ...]" + customPage
   184  }
   185  
   186  var perPathSetCmd = cli.Command{
   187  	Name:      "set",
   188  	Usage:     "configure a parameter on path(s)",
   189  	UsageText: getPerPathSetCmdUsageText(),
   190  	Action: func(c *cli.Context) {
   191  		if len(c.Args()) < 3 {
   192  			fmt.Fprintln(os.Stderr, "need at least 3 args")
   193  			os.Exit(1)
   194  		}
   195  		editor, err := newKBPConfigEditor(c.GlobalString("dir"))
   196  		if err != nil {
   197  			fmt.Fprintf(os.Stderr,
   198  				"creating config editor error: %v\n", err)
   199  			os.Exit(1)
   200  		}
   201  		switch {
   202  		case c.Args()[0] == "Access-Control-Allow-Origin":
   203  			for _, p := range c.Args()[2:] {
   204  				err := editor.setAccessControlAllowOrigin(p, c.Args()[1])
   205  				if err != nil {
   206  					fmt.Fprintf(os.Stderr,
   207  						"setting Access-Control-Allow-Origin %q on %q error: %v\n",
   208  						c.Args()[1], p, err)
   209  					os.Exit(1)
   210  				}
   211  			}
   212  		case customPagesEnabled && c.Args()[0] == "403":
   213  			for _, p := range c.Args()[2:] {
   214  				err := editor.set403(p, c.Args()[1])
   215  				if err != nil {
   216  					fmt.Fprintf(os.Stderr,
   217  						"setting custom 403 page %q on %q error: %v\n",
   218  						c.Args()[1], p, err)
   219  					os.Exit(1)
   220  				}
   221  			}
   222  		case customPagesEnabled && c.Args()[0] == "404":
   223  			for _, p := range c.Args()[2:] {
   224  				err := editor.set404(p, c.Args()[1])
   225  				if err != nil {
   226  					fmt.Fprintf(os.Stderr,
   227  						"setting custom 404 page %q on %q error: %v\n",
   228  						c.Args()[1], p, err)
   229  					os.Exit(1)
   230  				}
   231  			}
   232  		default:
   233  			fmt.Fprintf(os.Stderr, "unknown parameter: %s\n", c.Args()[0])
   234  			os.Exit(1)
   235  		}
   236  		if err := editor.confirmAndWrite(); err != nil {
   237  			fmt.Fprintf(os.Stderr, "writing new config error: %v\n", err)
   238  			os.Exit(1)
   239  		}
   240  	},
   241  }
   242  
   243  var perPathCmd = cli.Command{
   244  	Name:      "per-path",
   245  	Usage:     "make changes to the 'per_path_configs' section of the config",
   246  	UsageText: "per-path <set|clear|remove|get> [args]",
   247  	Subcommands: []cli.Command{
   248  		perPathSetPermissionCmd,
   249  		perPathClearCmd,
   250  		perPathUnsetUserPermissionsCmd,
   251  		perPathGetPermissionCmd,
   252  		perPathSetCmd,
   253  	},
   254  }