Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 5 | // The following enables go generate to generate the doc.go file. |
Jiri Simsa | 24a7155 | 2015-02-27 11:31:36 -0800 | [diff] [blame] | 6 | //go:generate go run $VANADIUM_ROOT/release/go/src/v.io/x/lib/cmdline/testdata/gendoc.go . |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 7 | |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 8 | package main |
| 9 | |
| 10 | import ( |
| 11 | "bytes" |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 12 | "crypto/rand" |
| 13 | "crypto/subtle" |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 14 | "encoding/base64" |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 15 | "encoding/json" |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 16 | "fmt" |
| 17 | "io" |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 18 | "io/ioutil" |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 19 | "os" |
| 20 | "os/user" |
| 21 | "time" |
| 22 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 23 | "v.io/v23" |
| 24 | "v.io/v23/context" |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 25 | "v.io/v23/options" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 26 | "v.io/v23/rpc" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 27 | "v.io/v23/security" |
| 28 | "v.io/v23/vom" |
Jiri Simsa | 24a7155 | 2015-02-27 11:31:36 -0800 | [diff] [blame] | 29 | "v.io/x/lib/cmdline" |
Jiri Simsa | ffceefa | 2015-02-28 11:03:34 -0800 | [diff] [blame] | 30 | _ "v.io/x/ref/profiles/static" |
| 31 | vsecurity "v.io/x/ref/security" |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | var ( |
| 35 | // Flags for the "blessself" command |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 36 | flagBlessSelfCaveats caveatsFlag |
| 37 | flagBlessSelfFor time.Duration |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 38 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 39 | // Flags for the "bless" command |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 40 | flagBlessCaveats caveatsFlag |
| 41 | flagBlessFor time.Duration |
| 42 | flagBlessRequireCaveats bool |
| 43 | flagBlessWith string |
| 44 | flagBlessRemoteKey string |
| 45 | flagBlessRemoteToken string |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 46 | |
Ankur | 77c32ac | 2014-12-18 14:18:19 -0800 | [diff] [blame] | 47 | // Flags for the "fork" command |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 48 | flagForkCaveats caveatsFlag |
| 49 | flagForkFor time.Duration |
| 50 | flagForkRequireCaveats bool |
| 51 | flagForkWith string |
Ankur | 77c32ac | 2014-12-18 14:18:19 -0800 | [diff] [blame] | 52 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 53 | // Flags for the "seekblessings" command |
| 54 | flagSeekBlessingsFrom string |
| 55 | flagSeekBlessingsSetDefault bool |
| 56 | flagSeekBlessingsForPeer string |
Suharsh Sivakumar | a76dba6 | 2014-12-22 16:00:34 -0800 | [diff] [blame] | 57 | flagSeekBlessingsBrowser bool |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 58 | |
| 59 | // Flags common to many commands |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 60 | flagAddToRoots bool |
| 61 | flagCreateOverwrite bool |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 62 | flagRemoteArgFile string |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 63 | |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 64 | // Flags for the "recvblessings" command |
| 65 | flagRecvBlessingsSetDefault bool |
| 66 | flagRecvBlessingsForPeer string |
| 67 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 68 | errNoCaveats = fmt.Errorf("no caveats provided: it is generally dangerous to bless another principal without any caveats as that gives them almost unrestricted access to the blesser's credentials. If you really want to do this, set --require-caveats=false") |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 69 | cmdDump = &cmdline.Command{ |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 70 | Name: "dump", |
| 71 | Short: "Dump out information about the principal", |
| 72 | Long: ` |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 73 | Prints out information about the principal specified by the environment |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 74 | that this tool is running in. |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 75 | `, |
| 76 | Run: func(cmd *cmdline.Command, args []string) error { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 77 | ctx, shutdown := v23.Init() |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 78 | defer shutdown() |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 79 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 80 | p := v23.GetPrincipal(ctx) |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 81 | fmt.Printf("Public key : %v\n", p.PublicKey()) |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 82 | fmt.Println("---------------- BlessingStore ----------------") |
| 83 | fmt.Printf("%v", p.BlessingStore().DebugString()) |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 84 | fmt.Println("---------------- BlessingRoots ----------------") |
| 85 | fmt.Printf("%v", p.Roots().DebugString()) |
| 86 | return nil |
| 87 | }, |
| 88 | } |
| 89 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 90 | cmdDumpBlessings = &cmdline.Command{ |
| 91 | Name: "dumpblessings", |
| 92 | Short: "Dump out information about the provided blessings", |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 93 | Long: ` |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 94 | Prints out information about the blessings (typically obtained from this tool) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 95 | encoded in the provided file. |
| 96 | `, |
| 97 | ArgsName: "<file>", |
| 98 | ArgsLong: ` |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 99 | <file> is the path to a file containing blessings typically obtained from |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 100 | this tool. - is used for STDIN. |
| 101 | `, |
| 102 | Run: func(cmd *cmdline.Command, args []string) error { |
| 103 | if len(args) != 1 { |
| 104 | return fmt.Errorf("requires exactly one argument, <file>, provided %d", len(args)) |
| 105 | } |
| 106 | blessings, err := decodeBlessings(args[0]) |
| 107 | if err != nil { |
| 108 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 109 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 110 | wire, err := blessings2wire(blessings) |
| 111 | if err != nil { |
| 112 | return fmt.Errorf("failed to decode certificate chains: %v", err) |
| 113 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 114 | fmt.Printf("Blessings : %v\n", blessings) |
| 115 | fmt.Printf("PublicKey : %v\n", blessings.PublicKey()) |
Asim Shankar | df88a2e | 2014-10-21 17:20:28 -0700 | [diff] [blame] | 116 | fmt.Printf("Certificate chains : %d\n", len(wire.CertificateChains)) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 117 | for idx, chain := range wire.CertificateChains { |
Asim Shankar | df88a2e | 2014-10-21 17:20:28 -0700 | [diff] [blame] | 118 | fmt.Printf("Chain #%d (%d certificates). Root certificate public key: %v\n", idx, len(chain), rootkey(chain)) |
| 119 | for certidx, cert := range chain { |
| 120 | fmt.Printf(" Certificate #%d: %v with ", certidx, cert.Extension) |
| 121 | switch n := len(cert.Caveats); n { |
| 122 | case 1: |
| 123 | fmt.Printf("1 caveat") |
| 124 | default: |
| 125 | fmt.Printf("%d caveats", n) |
| 126 | } |
| 127 | fmt.Println("") |
| 128 | for cavidx, cav := range cert.Caveats { |
| 129 | fmt.Printf(" (%d) %v\n", cavidx, &cav) |
| 130 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 131 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 132 | } |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 133 | return nil |
| 134 | }, |
| 135 | } |
| 136 | |
| 137 | cmdBlessSelf = &cmdline.Command{ |
| 138 | Name: "blessself", |
| 139 | Short: "Generate a self-signed blessing", |
| 140 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 141 | Returns a blessing with name <name> and self-signed by the principal specified |
| 142 | by the environment that this tool is running in. Optionally, the blessing can |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 143 | be restricted with an expiry caveat specified using the --for flag. Additional |
| 144 | caveats can be added with the --caveat flag. |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 145 | `, |
| 146 | ArgsName: "[<name>]", |
| 147 | ArgsLong: ` |
| 148 | <name> is the name used to create the self-signed blessing. If not |
| 149 | specified, a name will be generated based on the hostname of the |
| 150 | machine and the name of the user running this command. |
| 151 | `, |
| 152 | Run: func(cmd *cmdline.Command, args []string) error { |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 153 | var name string |
| 154 | switch len(args) { |
| 155 | case 0: |
| 156 | name = defaultBlessingName() |
| 157 | case 1: |
| 158 | name = args[0] |
| 159 | default: |
| 160 | return fmt.Errorf("requires at most one argument, provided %d", len(args)) |
| 161 | } |
| 162 | |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 163 | caveats, err := caveatsFromFlags(flagBlessSelfFor, &flagBlessSelfCaveats) |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 164 | if err != nil { |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 165 | return err |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 166 | } |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 167 | ctx, shutdown := v23.Init() |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 168 | defer shutdown() |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 169 | principal := v23.GetPrincipal(ctx) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 170 | blessing, err := principal.BlessSelf(name, caveats...) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 171 | if err != nil { |
| 172 | return fmt.Errorf("failed to create self-signed blessing for name %q: %v", name, err) |
| 173 | } |
| 174 | |
| 175 | return dumpBlessings(blessing) |
| 176 | }, |
| 177 | } |
| 178 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 179 | cmdBless = &cmdline.Command{ |
| 180 | Name: "bless", |
| 181 | Short: "Bless another principal", |
| 182 | Long: ` |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 183 | Bless another principal. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 184 | |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 185 | The blesser is obtained from the runtime this tool is using. The blessing that |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 186 | will be extended is the default one from the blesser's store, or specified by |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 187 | the --with flag. Expiration on the blessing are controlled via the --for flag. |
| 188 | Additional caveats are controlled with the --caveat flag. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 189 | |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 190 | For example, let's say a principal "alice" wants to bless another principal "bob" |
| 191 | as "alice/friend", the invocation would be: |
Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -0700 | [diff] [blame] | 192 | V23_CREDENTIALS=<path to alice> principal bless <path to bob> friend |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 193 | and this will dump the blessing to STDOUT. |
| 194 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 195 | With the --remote-key and --remote-token flags, this command can be used to |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 196 | bless a principal on a remote machine as well. In this case, the blessing is |
| 197 | not dumped to STDOUT but sent to the remote end. Use 'principal help |
| 198 | recvblessings' for more details on that. |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 199 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 200 | When --remote-arg-file is specified, only the blessing extension is required, as all other |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 201 | arguments will be extracted from the specified file. |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 202 | `, |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 203 | ArgsName: "[<principal to bless>] <extension>", |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 204 | ArgsLong: ` |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 205 | <principal to bless> represents the principal to be blessed (i.e., whose public |
| 206 | key will be provided with a name). This can be either: |
| 207 | (a) The directory containing credentials for that principal, |
| 208 | OR |
| 209 | (b) The filename (- for STDIN) containing any other blessings of that |
| 210 | principal, |
| 211 | OR |
| 212 | (c) The object name produced by the 'recvblessings' command of this tool |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 213 | running on behalf of another principal (if the --remote-key and |
| 214 | --remote-token flags are specified). |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 215 | OR |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 216 | (d) None (if the --remote-arg-file flag is specified, only <extension> should be provided |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 217 | to bless). |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 218 | |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 219 | <extension> is the string extension that will be applied to create the |
| 220 | blessing. |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 221 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 222 | `, |
| 223 | Run: func(cmd *cmdline.Command, args []string) error { |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 224 | if len(flagRemoteArgFile) > 0 && len(args) != 1 { |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 225 | return fmt.Errorf("when --remote-arg-file is provided, only <extension> is expected, provided %d", len(args)) |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 226 | } else if len(flagRemoteArgFile) == 0 && len(args) != 2 { |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 227 | return fmt.Errorf("require exactly two arguments when --remote-arg-file is not provided, provided %d", len(args)) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 228 | } |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 229 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 230 | ctx, shutdown := v23.Init() |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 231 | defer shutdown() |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 232 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 233 | p := v23.GetPrincipal(ctx) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 234 | |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 235 | var ( |
| 236 | err error |
| 237 | with security.Blessings |
| 238 | ) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 239 | if len(flagBlessWith) > 0 { |
| 240 | if with, err = decodeBlessings(flagBlessWith); err != nil { |
| 241 | return fmt.Errorf("failed to read blessings from --with=%q: %v", flagBlessWith, err) |
| 242 | } |
| 243 | } else { |
| 244 | with = p.BlessingStore().Default() |
| 245 | } |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 246 | caveats, err := caveatsFromFlags(flagBlessFor, &flagBlessCaveats) |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 247 | if err != nil { |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 248 | return err |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 249 | } |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 250 | if !flagBlessRequireCaveats && len(caveats) == 0 { |
| 251 | caveats = []security.Caveat{security.UnconstrainedUse()} |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 252 | } |
Bogdan Caprita | 553e582 | 2015-02-20 14:50:41 -0800 | [diff] [blame] | 253 | if len(caveats) == 0 { |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 254 | return errNoCaveats |
Bogdan Caprita | 553e582 | 2015-02-20 14:50:41 -0800 | [diff] [blame] | 255 | } |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 256 | |
| 257 | tobless, extension, remoteKey, remoteToken, err := blessArgs(args) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 258 | if err != nil { |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 259 | return err |
| 260 | } |
| 261 | |
| 262 | // Send blessings to a "server" started by a "recvblessings" command, either |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 263 | // with the --remote-arg-file flag, or with --remote-key and --remote-token flags. |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 264 | if len(remoteKey) > 0 { |
| 265 | granter := &granter{p, with, extension, caveats, remoteKey} |
| 266 | return blessOverNetwork(ctx, tobless, granter, remoteToken) |
| 267 | } |
| 268 | |
| 269 | // Blessing a principal whose key is available locally. |
| 270 | blessings, err := blessOverFileSystem(p, tobless, with, extension, caveats) |
| 271 | if err != nil { |
| 272 | return err |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 273 | } |
| 274 | return dumpBlessings(blessings) |
| 275 | }, |
| 276 | } |
| 277 | |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 278 | cmdGetForPeer = &cmdline.Command{ |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 279 | Name: "forpeer", |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 280 | Short: "Return blessings marked for the provided peer", |
| 281 | Long: ` |
| 282 | Returns blessings that are marked for the provided peer in the |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 283 | BlessingStore specified by the environment that this tool is |
| 284 | running in. |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 285 | `, |
| 286 | ArgsName: "[<peer_1> ... <peer_k>]", |
| 287 | ArgsLong: ` |
| 288 | <peer_1> ... <peer_k> are the (human-readable string) blessings bound |
| 289 | to the peer. The returned blessings are marked with a pattern that is |
| 290 | matched by at least one of these. If no arguments are specified, |
| 291 | store.forpeer returns the blessings that are marked for all peers (i.e., |
| 292 | blessings set on the store with the "..." pattern). |
| 293 | `, |
| 294 | Run: func(cmd *cmdline.Command, args []string) error { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 295 | ctx, shutdown := v23.Init() |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 296 | defer shutdown() |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 297 | principal := v23.GetPrincipal(ctx) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 298 | return dumpBlessings(principal.BlessingStore().ForPeer(args...)) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 299 | }, |
| 300 | } |
| 301 | |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 302 | cmdGetDefault = &cmdline.Command{ |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 303 | Name: "default", |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 304 | Short: "Return blessings marked as default", |
| 305 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 306 | Returns blessings that are marked as default in the BlessingStore specified by |
| 307 | the environment that this tool is running in. |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 308 | `, |
| 309 | Run: func(cmd *cmdline.Command, args []string) error { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 310 | ctx, shutdown := v23.Init() |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 311 | defer shutdown() |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 312 | principal := v23.GetPrincipal(ctx) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 313 | return dumpBlessings(principal.BlessingStore().Default()) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 314 | }, |
| 315 | } |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 316 | |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 317 | cmdSetForPeer = &cmdline.Command{ |
| 318 | Name: "forpeer", |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 319 | Short: "Set provided blessings for peer", |
| 320 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 321 | Marks the provided blessings to be shared with the provided peers on the |
| 322 | BlessingStore specified by the environment that this tool is running in. |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 323 | |
| 324 | 'set b pattern' marks the intention to reveal b to peers who |
| 325 | present blessings of their own matching 'pattern'. |
| 326 | |
| 327 | 'set nil pattern' can be used to remove the blessings previously |
| 328 | associated with the pattern (by a prior 'set' command). |
| 329 | |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 330 | It is an error to call 'set forpeer' with blessings whose public |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 331 | key does not match the public key of this principal specified |
| 332 | by the environment. |
| 333 | `, |
| 334 | ArgsName: "<file> <pattern>", |
| 335 | ArgsLong: ` |
| 336 | <file> is the path to a file containing a blessing typically obtained |
| 337 | from this tool. - is used for STDIN. |
| 338 | |
| 339 | <pattern> is the BlessingPattern used to identify peers with whom this |
| 340 | blessing can be shared with. |
| 341 | `, |
| 342 | Run: func(cmd *cmdline.Command, args []string) error { |
| 343 | if len(args) != 2 { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 344 | return fmt.Errorf("requires exactly two arguments <file>, <pattern>, provided %d", len(args)) |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 345 | } |
| 346 | blessings, err := decodeBlessings(args[0]) |
| 347 | if err != nil { |
| 348 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 349 | } |
| 350 | pattern := security.BlessingPattern(args[1]) |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 351 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 352 | ctx, shutdown := v23.Init() |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 353 | defer shutdown() |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 354 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 355 | p := v23.GetPrincipal(ctx) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 356 | if _, err := p.BlessingStore().Set(blessings, pattern); err != nil { |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 357 | return fmt.Errorf("failed to set blessings %v for peers %v: %v", blessings, pattern, err) |
| 358 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 359 | if flagAddToRoots { |
| 360 | if err := p.AddToRoots(blessings); err != nil { |
| 361 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 362 | } |
| 363 | } |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 364 | return nil |
| 365 | }, |
| 366 | } |
| 367 | |
Asim Shankar | 5fbe326 | 2015-03-11 23:03:51 -0700 | [diff] [blame] | 368 | cmdAddToRoots = &cmdline.Command{ |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 369 | Name: "addtoroots", |
Asim Shankar | 80277d0 | 2015-03-31 12:57:06 -0700 | [diff] [blame] | 370 | Short: "Add to the set of identity providers recognized by this principal", |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 371 | Long: ` |
Asim Shankar | 80277d0 | 2015-03-31 12:57:06 -0700 | [diff] [blame] | 372 | Adds an identity provider to the set of recognized roots public keys for this principal. |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 373 | |
Asim Shankar | 80277d0 | 2015-03-31 12:57:06 -0700 | [diff] [blame] | 374 | It accepts either a single argument (which points to a file containing a blessing) |
| 375 | or two arguments (a name and a base64-encoded DER-encoded public key). |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 376 | |
Asim Shankar | 80277d0 | 2015-03-31 12:57:06 -0700 | [diff] [blame] | 377 | For example, to make the principal in credentials directory A recognize the |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 378 | root of the default blessing in credentials directory B: |
Asim Shankar | f32d24d | 2015-04-01 16:34:26 -0700 | [diff] [blame] | 379 | principal -v23.credentials=B bless A some_extension | |
| 380 | principal -v23.credentials=A addtoroots - |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 381 | The extension 'some_extension' has no effect in the command above. |
Asim Shankar | 80277d0 | 2015-03-31 12:57:06 -0700 | [diff] [blame] | 382 | |
| 383 | Or to make the principal in credentials director A recognize the base64-encoded |
| 384 | public key KEY for blessing patterns P: |
Asim Shankar | f32d24d | 2015-04-01 16:34:26 -0700 | [diff] [blame] | 385 | principal -v23.credentials=A addtoroots KEY P |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 386 | `, |
Asim Shankar | 80277d0 | 2015-03-31 12:57:06 -0700 | [diff] [blame] | 387 | ArgsName: "<key|blessing> [<blessing pattern>]", |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 388 | ArgsLong: ` |
Asim Shankar | 80277d0 | 2015-03-31 12:57:06 -0700 | [diff] [blame] | 389 | <blessing> is the path to a file containing a blessing typically obtained from |
| 390 | this tool. - is used for STDIN. |
| 391 | |
| 392 | <key> is a base64-encoded, DER-encoded public key. |
| 393 | |
| 394 | <blessing pattern> is the blessing pattern for which <key> should be recognized. |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 395 | `, |
| 396 | Run: func(cmd *cmdline.Command, args []string) error { |
Asim Shankar | 80277d0 | 2015-03-31 12:57:06 -0700 | [diff] [blame] | 397 | if len(args) != 1 && len(args) != 2 { |
| 398 | return fmt.Errorf("requires either one argument <file>, or two arguments <key> <blessing pattern>, provided %d", len(args)) |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 399 | } |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 400 | ctx, shutdown := v23.Init() |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 401 | defer shutdown() |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 402 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 403 | p := v23.GetPrincipal(ctx) |
Asim Shankar | 80277d0 | 2015-03-31 12:57:06 -0700 | [diff] [blame] | 404 | if len(args) == 1 { |
| 405 | blessings, err := decodeBlessings(args[0]) |
| 406 | if err != nil { |
| 407 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 408 | } |
| 409 | if err := p.AddToRoots(blessings); err != nil { |
| 410 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 411 | } |
| 412 | return nil |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 413 | } |
Asim Shankar | 80277d0 | 2015-03-31 12:57:06 -0700 | [diff] [blame] | 414 | // len(args) == 2 |
| 415 | der, err := base64.URLEncoding.DecodeString(args[0]) |
| 416 | if err != nil { |
| 417 | return fmt.Errorf("invalid base64 encoding of public key: %v", err) |
| 418 | } |
| 419 | key, err := security.UnmarshalPublicKey(der) |
| 420 | if err != nil { |
| 421 | return fmt.Errorf("invalid DER encoding of public key: %v", err) |
| 422 | } |
| 423 | return p.Roots().Add(key, security.BlessingPattern(args[1])) |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 424 | }, |
| 425 | } |
| 426 | |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 427 | cmdSetDefault = &cmdline.Command{ |
| 428 | Name: "default", |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 429 | Short: "Set provided blessings as default", |
| 430 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 431 | Sets the provided blessings as default in the BlessingStore specified by the |
| 432 | environment that this tool is running in. |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 433 | |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 434 | It is an error to call 'set default' with blessings whose public key does |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 435 | not match the public key of the principal specified by the environment. |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 436 | `, |
| 437 | ArgsName: "<file>", |
| 438 | ArgsLong: ` |
| 439 | <file> is the path to a file containing a blessing typically obtained from |
| 440 | this tool. - is used for STDIN. |
| 441 | `, |
| 442 | Run: func(cmd *cmdline.Command, args []string) error { |
| 443 | if len(args) != 1 { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 444 | return fmt.Errorf("requires exactly one argument, <file>, provided %d", len(args)) |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 445 | } |
| 446 | blessings, err := decodeBlessings(args[0]) |
| 447 | if err != nil { |
| 448 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 449 | } |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 450 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 451 | ctx, shutdown := v23.Init() |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 452 | defer shutdown() |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 453 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 454 | p := v23.GetPrincipal(ctx) |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 455 | if err := p.BlessingStore().SetDefault(blessings); err != nil { |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 456 | return fmt.Errorf("failed to set blessings %v as default: %v", blessings, err) |
| 457 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 458 | if flagAddToRoots { |
| 459 | if err := p.AddToRoots(blessings); err != nil { |
| 460 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 461 | } |
| 462 | } |
| 463 | return nil |
| 464 | }, |
| 465 | } |
| 466 | |
| 467 | cmdCreate = &cmdline.Command{ |
| 468 | Name: "create", |
| 469 | Short: "Create a new principal and persist it into a directory", |
| 470 | Long: ` |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 471 | Creates a new principal with a single self-blessed blessing and writes it out |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 472 | to the provided directory. The same directory can then be used to set the |
Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -0700 | [diff] [blame] | 473 | V23_CREDENTIALS environment variable for other vanadium applications. |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 474 | |
| 475 | The operation fails if the directory already contains a principal. In this case |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 476 | the --overwrite flag can be provided to clear the directory and write out the |
gauthamt | b7bb39b | 2014-11-10 11:40:41 -0800 | [diff] [blame] | 477 | new principal. |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 478 | `, |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 479 | ArgsName: "<directory> <blessing>", |
| 480 | ArgsLong: ` |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 481 | <directory> is the directory to which the new principal will be persisted. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 482 | <blessing> is the self-blessed blessing that the principal will be setup to use by default. |
| 483 | `, |
| 484 | Run: func(cmd *cmdline.Command, args []string) error { |
| 485 | if len(args) != 2 { |
| 486 | return fmt.Errorf("requires exactly two arguments: <directory> and <blessing>, provided %d", len(args)) |
| 487 | } |
| 488 | dir, name := args[0], args[1] |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 489 | if flagCreateOverwrite { |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 490 | if err := os.RemoveAll(dir); err != nil { |
gauthamt | b7bb39b | 2014-11-10 11:40:41 -0800 | [diff] [blame] | 491 | return err |
| 492 | } |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 493 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 494 | p, err := vsecurity.CreatePersistentPrincipal(dir, nil) |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 495 | if err != nil { |
| 496 | return err |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 497 | } |
| 498 | blessings, err := p.BlessSelf(name) |
| 499 | if err != nil { |
| 500 | return fmt.Errorf("BlessSelf(%q) failed: %v", name, err) |
| 501 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 502 | if err := vsecurity.SetDefaultBlessings(p, blessings); err != nil { |
| 503 | return fmt.Errorf("could not set blessings %v as default: %v", blessings, err) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 504 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 505 | return nil |
| 506 | }, |
| 507 | } |
| 508 | |
| 509 | cmdFork = &cmdline.Command{ |
| 510 | Name: "fork", |
| 511 | Short: "Fork a new principal from the principal that this tool is running as and persist it into a directory", |
| 512 | Long: ` |
| 513 | Creates a new principal with a blessing from the principal specified by the |
| 514 | environment that this tool is running in, and writes it out to the provided |
| 515 | directory. The blessing that will be extended is the default one from the |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 516 | blesser's store, or specified by the --with flag. Expiration on the blessing |
| 517 | are controlled via the --for flag. Additional caveats on the blessing are |
| 518 | controlled with the --caveat flag. The blessing is marked as default and |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 519 | shareable with all peers on the new principal's blessing store. |
| 520 | |
| 521 | The operation fails if the directory already contains a principal. In this case |
| 522 | the --overwrite flag can be provided to clear the directory and write out the |
| 523 | forked principal. |
| 524 | `, |
| 525 | ArgsName: "<directory> <extension>", |
| 526 | ArgsLong: ` |
| 527 | <directory> is the directory to which the forked principal will be persisted. |
| 528 | <extension> is the extension under which the forked principal is blessed. |
| 529 | `, |
| 530 | Run: func(cmd *cmdline.Command, args []string) error { |
| 531 | if len(args) != 2 { |
| 532 | return fmt.Errorf("requires exactly two arguments: <directory> and <extension>, provided %d", len(args)) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 533 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 534 | dir, extension := args[0], args[1] |
| 535 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 536 | ctx, shutdown := v23.Init() |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 537 | defer shutdown() |
| 538 | |
| 539 | caveats, err := caveatsFromFlags(flagForkFor, &flagForkCaveats) |
| 540 | if err != nil { |
| 541 | return err |
| 542 | } |
| 543 | if !flagForkRequireCaveats && len(caveats) == 0 { |
| 544 | caveats = []security.Caveat{security.UnconstrainedUse()} |
| 545 | } |
| 546 | if len(caveats) == 0 { |
| 547 | return errNoCaveats |
| 548 | } |
| 549 | var with security.Blessings |
| 550 | if len(flagForkWith) > 0 { |
| 551 | if with, err = decodeBlessings(flagForkWith); err != nil { |
| 552 | return fmt.Errorf("failed to read blessings from --with=%q: %v", flagForkWith, err) |
| 553 | } |
| 554 | } else { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 555 | with = v23.GetPrincipal(ctx).BlessingStore().Default() |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 558 | if flagCreateOverwrite { |
| 559 | if err := os.RemoveAll(dir); err != nil { |
| 560 | return err |
| 561 | } |
| 562 | } |
| 563 | p, err := vsecurity.CreatePersistentPrincipal(dir, nil) |
| 564 | if err != nil { |
| 565 | return err |
| 566 | } |
| 567 | |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 568 | key := p.PublicKey() |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 569 | rp := v23.GetPrincipal(ctx) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 570 | blessings, err := rp.Bless(key, with, extension, caveats[0], caveats[1:]...) |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 571 | if err != nil { |
| 572 | return fmt.Errorf("Bless(%v, %v, %q, ...) failed: %v", key, with, extension, err) |
| 573 | } |
| 574 | if err := vsecurity.SetDefaultBlessings(p, blessings); err != nil { |
| 575 | return fmt.Errorf("could not set blessings %v as default: %v", blessings, err) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 576 | } |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 577 | return nil |
| 578 | }, |
| 579 | } |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 580 | |
| 581 | cmdSeekBlessings = &cmdline.Command{ |
| 582 | Name: "seekblessings", |
Suharsh Sivakumar | d1cc6e0 | 2015-03-16 13:58:49 -0700 | [diff] [blame] | 583 | Short: "Seek blessings from a web-based Vanadium blessing service", |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 584 | Long: ` |
Suharsh Sivakumar | d1cc6e0 | 2015-03-16 13:58:49 -0700 | [diff] [blame] | 585 | Seeks blessings from a web-based Vanadium blesser which |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 586 | requires the caller to first authenticate with Google using OAuth. Simply |
| 587 | run the command to see what happens. |
| 588 | |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 589 | The blessings are sought for the principal specified by the environment that |
| 590 | this tool is running in. |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 591 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 592 | The blessings obtained are set as default, unless the --set-default flag is |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 593 | set to true, and are also set for sharing with all peers, unless a more |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 594 | specific peer pattern is provided using the --for-peer flag. |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 595 | `, |
| 596 | Run: func(cmd *cmdline.Command, args []string) error { |
Asim Shankar | 08ab389 | 2014-10-16 12:04:07 -0700 | [diff] [blame] | 597 | // Initialize the runtime first so that any local errors are reported |
| 598 | // before the HTTP roundtrips for obtaining the macaroon begin. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 599 | ctx, shutdown := v23.Init() |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 600 | defer shutdown() |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 601 | |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 602 | blessedChan := make(chan string) |
| 603 | defer close(blessedChan) |
Suharsh Sivakumar | a76dba6 | 2014-12-22 16:00:34 -0800 | [diff] [blame] | 604 | macaroonChan, err := getMacaroonForBlessRPC(flagSeekBlessingsFrom, blessedChan, flagSeekBlessingsBrowser) |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 605 | if err != nil { |
Suharsh Sivakumar | d1cc6e0 | 2015-03-16 13:58:49 -0700 | [diff] [blame] | 606 | return fmt.Errorf("failed to get macaroon from Vanadium blesser: %v", err) |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 607 | } |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 608 | |
Suharsh Sivakumar | 67ef84a | 2015-02-13 13:04:44 -0800 | [diff] [blame] | 609 | blessings, err := exchangeMacaroonForBlessing(ctx, macaroonChan) |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 610 | if err != nil { |
Suharsh Sivakumar | 67ef84a | 2015-02-13 13:04:44 -0800 | [diff] [blame] | 611 | return err |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 612 | } |
| 613 | blessedChan <- fmt.Sprint(blessings) |
| 614 | // Wait for getTokenForBlessRPC to clean up: |
| 615 | <-macaroonChan |
| 616 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 617 | p := v23.GetPrincipal(ctx) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 618 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 619 | if flagSeekBlessingsSetDefault { |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 620 | if err := p.BlessingStore().SetDefault(blessings); err != nil { |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 621 | return fmt.Errorf("failed to set blessings %v as default: %v", blessings, err) |
| 622 | } |
| 623 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 624 | if pattern := security.BlessingPattern(flagSeekBlessingsForPeer); len(pattern) > 0 { |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 625 | if _, err := p.BlessingStore().Set(blessings, pattern); err != nil { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 626 | return fmt.Errorf("failed to set blessings %v for peers %v: %v", blessings, pattern, err) |
| 627 | } |
| 628 | } |
| 629 | if flagAddToRoots { |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 630 | if err := p.AddToRoots(blessings); err != nil { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 631 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 632 | } |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 633 | } |
Asim Shankar | 3f9bbab | 2015-02-15 13:51:37 -0800 | [diff] [blame] | 634 | fmt.Fprintf(cmd.Stdout(), "Received blessings: %v\n", blessings) |
Jiri Simsa | 37b0a09 | 2015-02-13 09:32:39 -0800 | [diff] [blame] | 635 | return nil |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 636 | }, |
| 637 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 638 | |
| 639 | cmdRecvBlessings = &cmdline.Command{ |
| 640 | Name: "recvblessings", |
| 641 | Short: "Receive blessings sent by another principal and use them as the default", |
| 642 | Long: ` |
| 643 | Allow another principal (likely a remote process) to bless this one. |
| 644 | |
| 645 | This command sets up the invoker (this process) to wait for a blessing |
| 646 | from another invocation of this tool (remote process) and prints out the |
| 647 | command to be run as the remote principal. |
| 648 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 649 | The received blessings are set as default, unless the --set-default flag is |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 650 | set to true, and are also set for sharing with all peers, unless a more |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 651 | specific peer pattern is provided using the --for-peer flag. |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 652 | |
| 653 | TODO(ashankar,cnicolaou): Make this next paragraph possible! Requires |
| 654 | the ability to obtain the proxied endpoint. |
| 655 | |
| 656 | Typically, this command should require no arguments. |
| 657 | However, if the sender and receiver are on different network domains, it may |
Asim Shankar | f32d24d | 2015-04-01 16:34:26 -0700 | [diff] [blame] | 658 | make sense to use the --v23.proxy flag: |
| 659 | principal --v23.proxy=proxy recvblessings |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 660 | |
| 661 | The command to be run at the sender is of the form: |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 662 | principal bless --remote-key=KEY --remote-token=TOKEN ADDRESS EXTENSION |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 663 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 664 | The --remote-key flag is used to by the sender to "authenticate" the receiver, |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 665 | ensuring it blesses the intended recipient and not any attacker that may have |
| 666 | taken over the address. |
| 667 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 668 | The --remote-token flag is used by the sender to authenticate itself to the |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 669 | receiver. This helps ensure that the receiver rejects blessings from senders |
| 670 | who just happened to guess the network address of the 'recvblessings' |
| 671 | invocation. |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 672 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 673 | If the --remote-arg-file flag is provided to recvblessings, the remote key, remote token |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 674 | and object address of this principal will be written to the specified location. |
| 675 | This file can be supplied to bless: |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 676 | principal bless --remote-arg-file FILE EXTENSION |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 677 | |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 678 | `, |
| 679 | Run: func(cmd *cmdline.Command, args []string) error { |
| 680 | if len(args) != 0 { |
| 681 | return fmt.Errorf("command accepts no arguments") |
| 682 | } |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 683 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 684 | ctx, shutdown := v23.Init() |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 685 | defer shutdown() |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 686 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 687 | server, err := v23.NewServer(ctx) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 688 | if err != nil { |
| 689 | return fmt.Errorf("failed to create server to listen for blessings: %v", err) |
| 690 | } |
| 691 | defer server.Stop() |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 692 | eps, err := server.Listen(v23.GetListenSpec(ctx)) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 693 | if err != nil { |
| 694 | return fmt.Errorf("failed to setup listening: %v", err) |
| 695 | } |
| 696 | var token [24]byte |
| 697 | if _, err := rand.Read(token[:]); err != nil { |
| 698 | return fmt.Errorf("unable to generate token: %v", err) |
| 699 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 700 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 701 | p := v23.GetPrincipal(ctx) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 702 | service := &recvBlessingsService{ |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 703 | principal: p, |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 704 | token: base64.URLEncoding.EncodeToString(token[:]), |
| 705 | notify: make(chan error), |
| 706 | } |
| 707 | if err := server.Serve("", service, allowAnyone{}); err != nil { |
| 708 | return fmt.Errorf("failed to setup service: %v", err) |
| 709 | } |
| 710 | // Proposed name: |
| 711 | extension := fmt.Sprintf("extension%d", int(token[0])<<16|int(token[1])<<8|int(token[2])) |
| 712 | fmt.Println("Run the following command on behalf of the principal that will send blessings:") |
| 713 | fmt.Println("You may want to adjust flags affecting the caveats on this blessing, for example using") |
| 714 | fmt.Println("the --for flag, or change the extension to something more meaningful") |
| 715 | fmt.Println() |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 716 | if len(flagRemoteArgFile) > 0 { |
| 717 | if err := writeRecvBlessingsInfo(flagRemoteArgFile, p.PublicKey().String(), service.token, eps[0].Name()); err != nil { |
| 718 | return fmt.Errorf("failed to write recvblessings info to %v: %v", flagRemoteArgFile, err) |
| 719 | } |
| 720 | fmt.Printf("make %q accessible to the blesser, possibly by copying the file over and then run:\n", flagRemoteArgFile) |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 721 | fmt.Printf("principal bless --remote-arg-file=%v %v", flagRemoteArgFile, extension) |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 722 | } else { |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 723 | fmt.Printf("principal bless --remote-key=%v --remote-token=%v %v %v\n", p.PublicKey(), service.token, eps[0].Name(), extension) |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 724 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 725 | fmt.Println() |
| 726 | fmt.Println("...waiting for sender..") |
| 727 | return <-service.notify |
| 728 | }, |
| 729 | } |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 730 | ) |
| 731 | |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 732 | func blessArgs(args []string) (tobless, extension, remoteKey, remoteToken string, err error) { |
| 733 | if len(flagRemoteArgFile) > 0 && (len(flagBlessRemoteKey)+len(flagBlessRemoteToken) > 0) { |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 734 | return "", "", "", "", fmt.Errorf("--remote-key and --remote-token cannot be provided with --remote-arg-file") |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 735 | } |
| 736 | if (len(flagBlessRemoteKey) == 0) != (len(flagBlessRemoteToken) == 0) { |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 737 | return "", "", "", "", fmt.Errorf("either both --remote-key and --remote-token should be set, or neither should") |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | if len(flagRemoteArgFile) == 0 { |
| 741 | tobless, extension = args[0], args[1] |
| 742 | remoteKey = flagBlessRemoteKey |
| 743 | remoteToken = flagBlessRemoteToken |
| 744 | } else if len(flagRemoteArgFile) > 0 { |
| 745 | extension = args[0] |
| 746 | remoteKey, remoteToken, tobless, err = blessArgsFromFile(flagRemoteArgFile) |
| 747 | } |
| 748 | return |
| 749 | } |
| 750 | |
| 751 | func blessOverFileSystem(p security.Principal, tobless string, with security.Blessings, extension string, caveats []security.Caveat) (security.Blessings, error) { |
| 752 | var key security.PublicKey |
| 753 | if finfo, err := os.Stat(tobless); err == nil && finfo.IsDir() { |
| 754 | other, err := vsecurity.LoadPersistentPrincipal(tobless, nil) |
| 755 | if err != nil { |
| 756 | if other, err = vsecurity.CreatePersistentPrincipal(tobless, nil); err != nil { |
| 757 | return security.Blessings{}, fmt.Errorf("failed to read principal in directory %q: %v", tobless, err) |
| 758 | } |
| 759 | } |
| 760 | key = other.PublicKey() |
| 761 | } else if other, err := decodeBlessings(tobless); err != nil { |
| 762 | return security.Blessings{}, fmt.Errorf("failed to decode blessings in %q: %v", tobless, err) |
| 763 | } else { |
| 764 | key = other.PublicKey() |
| 765 | } |
| 766 | return p.Bless(key, with, extension, caveats[0], caveats[1:]...) |
| 767 | } |
| 768 | |
| 769 | type recvBlessingsInfo struct { |
| 770 | RemoteKey string `json:remote_key` |
| 771 | RemoteToken string `json:remote_token` |
| 772 | Name string `json:name` |
| 773 | } |
| 774 | |
| 775 | func writeRecvBlessingsInfo(fname string, remoteKey, remoteToken, name string) error { |
| 776 | f, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE, 0666) |
| 777 | if err != nil { |
| 778 | return err |
| 779 | } |
| 780 | b, err := json.Marshal(recvBlessingsInfo{remoteKey, remoteToken, name}) |
| 781 | if err != nil { |
| 782 | return err |
| 783 | } |
| 784 | if _, err := f.Write(b); err != nil { |
| 785 | return err |
| 786 | } |
| 787 | return nil |
| 788 | } |
| 789 | |
| 790 | func blessArgsFromFile(fname string) (remoteKey, remoteToken, tobless string, err error) { |
| 791 | blessJSON, err := ioutil.ReadFile(fname) |
| 792 | if err != nil { |
| 793 | return "", "", "", err |
| 794 | } |
| 795 | var binfo recvBlessingsInfo |
| 796 | if err := json.Unmarshal(blessJSON, &binfo); err != nil { |
| 797 | return "", "", "", err |
| 798 | } |
| 799 | return binfo.RemoteKey, binfo.RemoteToken, binfo.Name, err |
| 800 | } |
| 801 | |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 802 | func main() { |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 803 | cmdBlessSelf.Flags.Var(&flagBlessSelfCaveats, "caveat", flagBlessSelfCaveats.usage()) |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 804 | cmdBlessSelf.Flags.DurationVar(&flagBlessSelfFor, "for", 0, "Duration of blessing validity (zero implies no expiration)") |
| 805 | |
Ankur | 77c32ac | 2014-12-18 14:18:19 -0800 | [diff] [blame] | 806 | cmdFork.Flags.BoolVar(&flagCreateOverwrite, "overwrite", false, "If true, any existing principal data in the directory will be overwritten") |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 807 | cmdFork.Flags.Var(&flagForkCaveats, "caveat", flagForkCaveats.usage()) |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 808 | cmdFork.Flags.DurationVar(&flagForkFor, "for", 0, "Duration of blessing validity (zero implies no expiration caveat)") |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 809 | cmdFork.Flags.BoolVar(&flagForkRequireCaveats, "require-caveats", true, "If false, allow blessing without any caveats. This is typically not advised as the principal wielding the blessing will be almost as powerful as its blesser") |
Ankur | 77c32ac | 2014-12-18 14:18:19 -0800 | [diff] [blame] | 810 | cmdFork.Flags.StringVar(&flagForkWith, "with", "", "Path to file containing blessing to extend") |
| 811 | |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 812 | cmdBless.Flags.Var(&flagBlessCaveats, "caveat", flagBlessCaveats.usage()) |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 813 | cmdBless.Flags.DurationVar(&flagBlessFor, "for", 0, "Duration of blessing validity (zero implies no expiration caveat)") |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 814 | cmdBless.Flags.BoolVar(&flagBlessRequireCaveats, "require-caveats", true, "If false, allow blessing without any caveats. This is typically not advised as the principal wielding the blessing will be almost as powerful as its blesser") |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 815 | cmdBless.Flags.StringVar(&flagBlessWith, "with", "", "Path to file containing blessing to extend") |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 816 | cmdBless.Flags.StringVar(&flagBlessRemoteKey, "remote-key", "", "Public key of the remote principal to bless (obtained from the 'recvblessings' command run by the remote principal") |
| 817 | cmdBless.Flags.StringVar(&flagBlessRemoteToken, "remote-token", "", "Token provided by principal running the 'recvblessings' command") |
| 818 | cmdBless.Flags.StringVar(&flagRemoteArgFile, "remote-arg-file", "", "File containing bless arguments written by 'principal recvblessings -remote-arg-file FILE EXTENSION' command. This can be provided to bless in place of --remote-key, --remote-token, and <principal>.") |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 819 | |
Suharsh Sivakumar | 3388401 | 2015-03-25 13:27:55 -0700 | [diff] [blame] | 820 | cmdSeekBlessings.Flags.StringVar(&flagSeekBlessingsFrom, "from", "https://dev.v.io/auth/google", "URL to use to begin the seek blessings process") |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 821 | cmdSeekBlessings.Flags.BoolVar(&flagSeekBlessingsSetDefault, "set-default", true, "If true, the blessings obtained will be set as the default blessing in the store") |
| 822 | cmdSeekBlessings.Flags.StringVar(&flagSeekBlessingsForPeer, "for-peer", string(security.AllPrincipals), "If non-empty, the blessings obtained will be marked for peers matching this pattern in the store") |
Suharsh Sivakumar | a76dba6 | 2014-12-22 16:00:34 -0800 | [diff] [blame] | 823 | cmdSeekBlessings.Flags.BoolVar(&flagSeekBlessingsBrowser, "browser", true, "If false, the seekblessings command will not open the browser and only print the url to visit.") |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 824 | cmdSeekBlessings.Flags.BoolVar(&flagAddToRoots, "add-to-roots", true, "If true, the root certificate of the blessing will be added to the principal's set of recognized root certificates") |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 825 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 826 | cmdSetForPeer.Flags.BoolVar(&flagAddToRoots, "add-to-roots", true, "If true, the root certificate of the blessing will be added to the principal's set of recognized root certificates") |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 827 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 828 | cmdSetDefault.Flags.BoolVar(&flagAddToRoots, "add-to-roots", true, "If true, the root certificate of the blessing will be added to the principal's set of recognized root certificates") |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 829 | |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 830 | cmdCreate.Flags.BoolVar(&flagCreateOverwrite, "overwrite", false, "If true, any existing principal data in the directory will be overwritten") |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 831 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 832 | cmdRecvBlessings.Flags.BoolVar(&flagRecvBlessingsSetDefault, "set-default", true, "If true, the blessings received will be set as the default blessing in the store") |
| 833 | cmdRecvBlessings.Flags.StringVar(&flagRecvBlessingsForPeer, "for-peer", string(security.AllPrincipals), "If non-empty, the blessings received will be marked for peers matching this pattern in the store") |
| 834 | cmdRecvBlessings.Flags.StringVar(&flagRemoteArgFile, "remote-arg-file", "", "If non-empty, the remote key, remote token, and principal will be written to the specified file in a JSON object. This can be provided to 'principal bless --remote-arg-file FILE EXTENSION'.") |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 835 | |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 836 | cmdSet := &cmdline.Command{ |
| 837 | Name: "set", |
| 838 | Short: "Mutate the principal's blessings.", |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 839 | Long: ` |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 840 | Commands to mutate the blessings of the principal. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 841 | |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 842 | All input blessings are expected to be serialized using base64-VOM-encoding. |
| 843 | See 'principal get'. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 844 | `, |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 845 | Children: []*cmdline.Command{cmdSetDefault, cmdSetForPeer}, |
| 846 | } |
| 847 | |
| 848 | cmdGet := &cmdline.Command{ |
| 849 | Name: "get", |
| 850 | Short: "Read the principal's blessings.", |
| 851 | Long: ` |
| 852 | Commands to inspect the blessings of the principal. |
| 853 | |
| 854 | All blessings are printed to stdout using base64-VOM-encoding. |
| 855 | `, |
| 856 | Children: []*cmdline.Command{cmdGetDefault, cmdGetForPeer}, |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 857 | } |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 858 | |
Matt Rosencrantz | 92303e1 | 2015-01-21 09:02:42 -0800 | [diff] [blame] | 859 | root := &cmdline.Command{ |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 860 | Name: "principal", |
Suharsh Sivakumar | d1cc6e0 | 2015-03-16 13:58:49 -0700 | [diff] [blame] | 861 | Short: "Create and manage vanadium principals", |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 862 | Long: ` |
| 863 | The principal tool helps create and manage blessings and the set of trusted |
| 864 | roots bound to a principal. |
| 865 | |
| 866 | All objects are printed using base64-VOM-encoding. |
| 867 | `, |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 868 | Children: []*cmdline.Command{cmdCreate, cmdFork, cmdSeekBlessings, cmdRecvBlessings, cmdDump, cmdDumpBlessings, cmdBlessSelf, cmdBless, cmdSet, cmdGet, cmdAddToRoots}, |
Matt Rosencrantz | 92303e1 | 2015-01-21 09:02:42 -0800 | [diff] [blame] | 869 | } |
| 870 | os.Exit(root.Main()) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 873 | func decodeBlessings(fname string) (security.Blessings, error) { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 874 | var b security.Blessings |
| 875 | err := decode(fname, &b) |
| 876 | return b, err |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | func dumpBlessings(blessings security.Blessings) error { |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 880 | if blessings.IsZero() { |
| 881 | return fmt.Errorf("no blessings found") |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 882 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 883 | str, err := base64VomEncode(blessings) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 884 | if err != nil { |
| 885 | return fmt.Errorf("base64-VOM encoding failed: %v", err) |
| 886 | } |
| 887 | fmt.Println(str) |
| 888 | return nil |
| 889 | } |
| 890 | |
| 891 | func read(fname string) (string, error) { |
| 892 | if len(fname) == 0 { |
| 893 | return "", nil |
| 894 | } |
| 895 | f := os.Stdin |
| 896 | if fname != "-" { |
| 897 | var err error |
| 898 | if f, err = os.Open(fname); err != nil { |
| 899 | return "", fmt.Errorf("failed to open %q: %v", fname, err) |
| 900 | } |
| 901 | } |
| 902 | defer f.Close() |
| 903 | var buf bytes.Buffer |
| 904 | if _, err := io.Copy(&buf, f); err != nil { |
| 905 | return "", fmt.Errorf("failed to read %q: %v", fname, err) |
| 906 | } |
| 907 | return buf.String(), nil |
| 908 | } |
| 909 | |
| 910 | func decode(fname string, val interface{}) error { |
| 911 | str, err := read(fname) |
| 912 | if err != nil { |
| 913 | return err |
| 914 | } |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 915 | if err := base64VomDecode(str, val); err != nil || val == nil { |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 916 | return fmt.Errorf("failed to decode %q: %v", fname, err) |
| 917 | } |
| 918 | return nil |
| 919 | } |
| 920 | |
| 921 | func defaultBlessingName() string { |
| 922 | var name string |
| 923 | if user, _ := user.Current(); user != nil && len(user.Username) > 0 { |
| 924 | name = user.Username |
| 925 | } else { |
| 926 | name = "anonymous" |
| 927 | } |
| 928 | if host, _ := os.Hostname(); len(host) > 0 { |
| 929 | name = name + "@" + host |
| 930 | } |
| 931 | return name |
| 932 | } |
Asim Shankar | df88a2e | 2014-10-21 17:20:28 -0700 | [diff] [blame] | 933 | |
| 934 | func rootkey(chain []security.Certificate) string { |
| 935 | if len(chain) == 0 { |
| 936 | return "<empty certificate chain>" |
| 937 | } |
| 938 | key, err := security.UnmarshalPublicKey(chain[0].PublicKey) |
| 939 | if err != nil { |
| 940 | return fmt.Sprintf("<invalid PublicKey: %v>", err) |
| 941 | } |
| 942 | return fmt.Sprintf("%v", key) |
| 943 | } |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 944 | |
| 945 | func base64VomEncode(i interface{}) (string, error) { |
| 946 | buf := &bytes.Buffer{} |
| 947 | closer := base64.NewEncoder(base64.URLEncoding, buf) |
Todd Wang | 8e17bff | 2015-02-18 11:18:56 -0800 | [diff] [blame] | 948 | enc, err := vom.NewEncoder(closer) |
Suharsh Sivakumar | 12b089d | 2015-01-02 14:17:12 -0800 | [diff] [blame] | 949 | if err != nil { |
| 950 | return "", err |
| 951 | } |
| 952 | if err := enc.Encode(i); err != nil { |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 953 | return "", err |
| 954 | } |
| 955 | // Must close the base64 encoder to flush out any partially written |
| 956 | // blocks. |
| 957 | if err := closer.Close(); err != nil { |
| 958 | return "", err |
| 959 | } |
| 960 | return buf.String(), nil |
| 961 | } |
| 962 | |
| 963 | func base64VomDecode(s string, i interface{}) error { |
| 964 | b, err := base64.URLEncoding.DecodeString(s) |
| 965 | if err != nil { |
| 966 | return err |
| 967 | } |
Todd Wang | 3425a90 | 2015-01-21 18:43:59 -0800 | [diff] [blame] | 968 | dec, err := vom.NewDecoder(bytes.NewBuffer(b)) |
Suharsh Sivakumar | 12b089d | 2015-01-02 14:17:12 -0800 | [diff] [blame] | 969 | if err != nil { |
| 970 | return err |
| 971 | } |
| 972 | return dec.Decode(i) |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 973 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 974 | |
| 975 | type recvBlessingsService struct { |
| 976 | principal security.Principal |
| 977 | notify chan error |
| 978 | token string |
| 979 | } |
| 980 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 981 | func (r *recvBlessingsService) Grant(call rpc.StreamServerCall, token string) error { |
Suharsh Sivakumar | 380bf34 | 2015-02-27 15:38:27 -0800 | [diff] [blame] | 982 | b := call.GrantedBlessings() |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 983 | if b.IsZero() { |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 984 | return fmt.Errorf("no blessings granted by sender") |
| 985 | } |
| 986 | if len(token) != len(r.token) { |
| 987 | // A timing attack can be used to figure out the length |
| 988 | // of the token, but then again, so can looking at the |
| 989 | // source code. So, it's okay. |
| 990 | return fmt.Errorf("blessings received from unexpected sender") |
| 991 | } |
| 992 | if subtle.ConstantTimeCompare([]byte(token), []byte(r.token)) != 1 { |
| 993 | return fmt.Errorf("blessings received from unexpected sender") |
| 994 | } |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 995 | if flagRecvBlessingsSetDefault { |
| 996 | if err := r.principal.BlessingStore().SetDefault(b); err != nil { |
| 997 | return fmt.Errorf("failed to set blessings %v as default: %v", b, err) |
| 998 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 999 | } |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 1000 | if pattern := security.BlessingPattern(flagRecvBlessingsForPeer); len(pattern) > 0 { |
| 1001 | if _, err := r.principal.BlessingStore().Set(b, pattern); err != nil { |
| 1002 | return fmt.Errorf("failed to set blessings %v for peers %v: %v", b, pattern, err) |
| 1003 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1004 | } |
| 1005 | if flagAddToRoots { |
| 1006 | if err := r.principal.AddToRoots(b); err != nil { |
| 1007 | return fmt.Errorf("failed to add blessings to recognized roots: %v", err) |
| 1008 | } |
| 1009 | } |
| 1010 | fmt.Println("Received blessings:", b) |
| 1011 | r.notify <- nil |
| 1012 | return nil |
| 1013 | } |
| 1014 | |
| 1015 | type allowAnyone struct{} |
| 1016 | |
Matt Rosencrantz | 250558f | 2015-03-17 11:37:31 -0700 | [diff] [blame] | 1017 | func (allowAnyone) Authorize(*context.T) error { return nil } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1018 | |
| 1019 | type granter struct { |
| 1020 | p security.Principal |
| 1021 | with security.Blessings |
| 1022 | extension string |
| 1023 | caveats []security.Caveat |
| 1024 | serverKey string |
| 1025 | } |
| 1026 | |
| 1027 | func (g *granter) Grant(server security.Blessings) (security.Blessings, error) { |
| 1028 | if got := fmt.Sprintf("%v", server.PublicKey()); got != g.serverKey { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 1029 | // If the granter returns an error, the RPC framework should |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1030 | // abort the RPC before sending the request to the server. |
| 1031 | // Thus, there is no concern about leaking the token to an |
| 1032 | // imposter server. |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 1033 | return security.Blessings{}, fmt.Errorf("key mismatch: Remote end has public key %v, want %v", got, g.serverKey) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1034 | } |
| 1035 | return g.p.Bless(server.PublicKey(), g.with, g.extension, g.caveats[0], g.caveats[1:]...) |
| 1036 | } |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 1037 | func (*granter) RPCCallOpt() {} |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1038 | |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 1039 | func blessOverNetwork(ctx *context.T, object string, granter *granter, remoteToken string) error { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 1040 | client := v23.GetClient(ctx) |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 1041 | // The receiver is being authorized based on the hash of its public key |
| 1042 | // (see Grant), so it should be fine to ignore the blessing names in the endpoint |
| 1043 | // (which are likely to not be recognized by the sender anyway). |
| 1044 | // |
| 1045 | // At worst, there is a privacy leak of the senders intent to send some |
| 1046 | // blessings. That could be addressed by making the full public key of |
| 1047 | // the recipeint available to the sender and using |
| 1048 | // options.ServerPublicKey instead of providing a "hash" of the |
| 1049 | // recipients public key and verifying in the Granter implementation. |
| 1050 | call, err := client.StartCall(ctx, object, "Grant", []interface{}{remoteToken}, granter, options.SkipServerEndpointAuthorization{}) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1051 | if err != nil { |
| 1052 | return fmt.Errorf("failed to start RPC to %q: %v", object, err) |
| 1053 | } |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 1054 | if err := call.Finish(); err != nil { |
| 1055 | return fmt.Errorf("failed to finish RPC to %q: %v", object, err) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1056 | } |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 1057 | return nil |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1058 | } |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 1059 | |
| 1060 | func caveatsFromFlags(expiry time.Duration, caveatsFlag *caveatsFlag) ([]security.Caveat, error) { |
| 1061 | caveats, err := caveatsFlag.Compile() |
| 1062 | if err != nil { |
| 1063 | return nil, fmt.Errorf("failed to parse caveats: %v", err) |
| 1064 | } |
| 1065 | if expiry > 0 { |
| 1066 | ecav, err := security.ExpiryCaveat(time.Now().Add(expiry)) |
| 1067 | if err != nil { |
| 1068 | return nil, fmt.Errorf("failed to create expiration caveat: %v", err) |
| 1069 | } |
| 1070 | caveats = append(caveats, ecav) |
| 1071 | } |
| 1072 | return caveats, nil |
| 1073 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 1074 | |
| 1075 | // Circuitous route to get to the certificate chains. |
| 1076 | // See comments on why security.MarshalBlessings is discouraged. |
| 1077 | // Though, a better alternative is worth looking into. |
| 1078 | func blessings2wire(b security.Blessings) (security.WireBlessings, error) { |
| 1079 | var wire security.WireBlessings |
| 1080 | data, err := vom.Encode(b) |
| 1081 | if err != nil { |
| 1082 | return wire, err |
| 1083 | } |
| 1084 | err = vom.Decode(data, &wire) |
| 1085 | return wire, err |
| 1086 | } |