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 | 32f76fb | 2015-04-07 15:39:23 -0700 | [diff] [blame] | 6 | //go:generate go run $V23_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" |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 21 | "strings" |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 22 | "time" |
| 23 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 24 | "v.io/v23" |
| 25 | "v.io/v23/context" |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 26 | "v.io/v23/options" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 27 | "v.io/v23/rpc" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 28 | "v.io/v23/security" |
| 29 | "v.io/v23/vom" |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 30 | "v.io/x/lib/cmdline" |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 31 | "v.io/x/ref" |
Todd Wang | b351149 | 2015-04-07 23:32:34 -0700 | [diff] [blame] | 32 | vsecurity "v.io/x/ref/lib/security" |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 33 | "v.io/x/ref/lib/v23cmd" |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 34 | "v.io/x/ref/lib/xrpc" |
Suharsh Sivakumar | dcc11d7 | 2015-05-11 12:19:20 -0700 | [diff] [blame] | 35 | _ "v.io/x/ref/runtime/factories/static" |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 36 | ) |
| 37 | |
| 38 | var ( |
| 39 | // Flags for the "blessself" command |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 40 | flagBlessSelfCaveats caveatsFlag |
| 41 | flagBlessSelfFor time.Duration |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 42 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 43 | // Flags for the "bless" command |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 44 | flagBlessCaveats caveatsFlag |
| 45 | flagBlessFor time.Duration |
| 46 | flagBlessRequireCaveats bool |
| 47 | flagBlessWith string |
| 48 | flagBlessRemoteKey string |
| 49 | flagBlessRemoteToken string |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 50 | |
Robin Thellend | 5396eae | 2015-04-13 09:57:06 -0700 | [diff] [blame] | 51 | // Flags for the "dump" command |
| 52 | flagDumpShort bool |
| 53 | |
Ankur | 77c32ac | 2014-12-18 14:18:19 -0800 | [diff] [blame] | 54 | // Flags for the "fork" command |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 55 | flagForkCaveats caveatsFlag |
| 56 | flagForkFor time.Duration |
| 57 | flagForkRequireCaveats bool |
| 58 | flagForkWith string |
Ankur | 77c32ac | 2014-12-18 14:18:19 -0800 | [diff] [blame] | 59 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 60 | // Flags for the "seekblessings" command |
| 61 | flagSeekBlessingsFrom string |
| 62 | flagSeekBlessingsSetDefault bool |
| 63 | flagSeekBlessingsForPeer string |
Suharsh Sivakumar | a76dba6 | 2014-12-22 16:00:34 -0800 | [diff] [blame] | 64 | flagSeekBlessingsBrowser bool |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 65 | |
| 66 | // Flags common to many commands |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 67 | flagAddToRoots bool |
| 68 | flagCreateOverwrite bool |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 69 | flagRemoteArgFile string |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 70 | |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 71 | // Flags for the "recvblessings" command |
| 72 | flagRecvBlessingsSetDefault bool |
| 73 | flagRecvBlessingsForPeer string |
| 74 | |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 75 | // Flags for the commands that get blessings |
| 76 | flagBlessingsNames bool |
| 77 | flagBlessingsRootKey string |
| 78 | flagBlessingsCaveats string |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 79 | |
Asim Shankar | de6fda5 | 2015-04-22 21:20:24 -0700 | [diff] [blame] | 80 | // Flags for the get publickey command. |
| 81 | flagGetPublicKeyPretty bool |
| 82 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 83 | 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") |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 84 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 85 | cmdDump = &cmdline.Command{ |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 86 | Name: "dump", |
| 87 | Short: "Dump out information about the principal", |
| 88 | Long: ` |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 89 | Prints out information about the principal specified by the environment |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 90 | that this tool is running in. |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 91 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 92 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 93 | p := v23.GetPrincipal(ctx) |
Robin Thellend | 5396eae | 2015-04-13 09:57:06 -0700 | [diff] [blame] | 94 | if flagDumpShort { |
Bogdan Caprita | 4ab9541 | 2015-05-13 13:37:46 -0700 | [diff] [blame] | 95 | fmt.Printf("%s\n", printAnnotatedBlessingsNames(p.BlessingStore().Default())) |
Robin Thellend | 5396eae | 2015-04-13 09:57:06 -0700 | [diff] [blame] | 96 | return nil |
| 97 | } |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 98 | fmt.Printf("Public key : %v\n", p.PublicKey()) |
Bogdan Caprita | 4ab9541 | 2015-05-13 13:37:46 -0700 | [diff] [blame] | 99 | // NOTE(caprita): We print the default blessings name |
| 100 | // twice (it's also printed as part of the blessing |
| 101 | // store below) -- the reason we print it here is to |
| 102 | // expose whether the blessings are expired. Ideally, |
| 103 | // the blessings store would print the expiry |
| 104 | // information about each blessing in the store, but |
| 105 | // that would require deeper changes beyond the |
| 106 | // principal tool. |
| 107 | fmt.Printf("Default Blessings : %s\n", printAnnotatedBlessingsNames(p.BlessingStore().Default())) |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 108 | fmt.Println("---------------- BlessingStore ----------------") |
| 109 | fmt.Printf("%v", p.BlessingStore().DebugString()) |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 110 | fmt.Println("---------------- BlessingRoots ----------------") |
| 111 | fmt.Printf("%v", p.Roots().DebugString()) |
| 112 | return nil |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 113 | }), |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 116 | cmdDumpBlessings = &cmdline.Command{ |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 117 | Name: "dumpblessings", |
| 118 | Short: "Dump out information about the provided blessings", |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 119 | Long: ` |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 120 | Prints out information about the blessings (typically obtained from this tool) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 121 | encoded in the provided file. |
| 122 | `, |
| 123 | ArgsName: "<file>", |
| 124 | ArgsLong: ` |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 125 | <file> is the path to a file containing blessings typically obtained from |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 126 | this tool. - is used for STDIN. |
| 127 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 128 | Runner: cmdline.RunnerFunc(func(env *cmdline.Env, args []string) error { |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 129 | if len(args) != 1 { |
| 130 | return fmt.Errorf("requires exactly one argument, <file>, provided %d", len(args)) |
| 131 | } |
| 132 | blessings, err := decodeBlessings(args[0]) |
| 133 | if err != nil { |
| 134 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 135 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 136 | wire, err := blessings2wire(blessings) |
| 137 | if err != nil { |
| 138 | return fmt.Errorf("failed to decode certificate chains: %v", err) |
| 139 | } |
Bogdan Caprita | 4ab9541 | 2015-05-13 13:37:46 -0700 | [diff] [blame] | 140 | fmt.Printf("Blessings : %s\n", printAnnotatedBlessingsNames(blessings)) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 141 | fmt.Printf("PublicKey : %v\n", blessings.PublicKey()) |
Asim Shankar | df88a2e | 2014-10-21 17:20:28 -0700 | [diff] [blame] | 142 | fmt.Printf("Certificate chains : %d\n", len(wire.CertificateChains)) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 143 | for idx, chain := range wire.CertificateChains { |
Asim Shankar | df88a2e | 2014-10-21 17:20:28 -0700 | [diff] [blame] | 144 | fmt.Printf("Chain #%d (%d certificates). Root certificate public key: %v\n", idx, len(chain), rootkey(chain)) |
| 145 | for certidx, cert := range chain { |
| 146 | fmt.Printf(" Certificate #%d: %v with ", certidx, cert.Extension) |
| 147 | switch n := len(cert.Caveats); n { |
| 148 | case 1: |
| 149 | fmt.Printf("1 caveat") |
| 150 | default: |
| 151 | fmt.Printf("%d caveats", n) |
| 152 | } |
| 153 | fmt.Println("") |
| 154 | for cavidx, cav := range cert.Caveats { |
| 155 | fmt.Printf(" (%d) %v\n", cavidx, &cav) |
| 156 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 157 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 158 | } |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 159 | return nil |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 160 | }), |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 163 | cmdBlessSelf = &cmdline.Command{ |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 164 | Name: "blessself", |
| 165 | Short: "Generate a self-signed blessing", |
| 166 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 167 | Returns a blessing with name <name> and self-signed by the principal specified |
| 168 | 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] | 169 | be restricted with an expiry caveat specified using the --for flag. Additional |
| 170 | caveats can be added with the --caveat flag. |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 171 | `, |
| 172 | ArgsName: "[<name>]", |
| 173 | ArgsLong: ` |
| 174 | <name> is the name used to create the self-signed blessing. If not |
| 175 | specified, a name will be generated based on the hostname of the |
| 176 | machine and the name of the user running this command. |
| 177 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 178 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 179 | var name string |
| 180 | switch len(args) { |
| 181 | case 0: |
| 182 | name = defaultBlessingName() |
| 183 | case 1: |
| 184 | name = args[0] |
| 185 | default: |
| 186 | return fmt.Errorf("requires at most one argument, provided %d", len(args)) |
| 187 | } |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 188 | caveats, err := caveatsFromFlags(flagBlessSelfFor, &flagBlessSelfCaveats) |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 189 | if err != nil { |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 190 | return err |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 191 | } |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 192 | principal := v23.GetPrincipal(ctx) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 193 | blessing, err := principal.BlessSelf(name, caveats...) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 194 | if err != nil { |
| 195 | return fmt.Errorf("failed to create self-signed blessing for name %q: %v", name, err) |
| 196 | } |
| 197 | |
| 198 | return dumpBlessings(blessing) |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 199 | }), |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 202 | cmdBless = &cmdline.Command{ |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 203 | Name: "bless", |
| 204 | Short: "Bless another principal", |
| 205 | Long: ` |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 206 | Bless another principal. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 207 | |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 208 | 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] | 209 | 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] | 210 | the --with flag. Expiration on the blessing are controlled via the --for flag. |
| 211 | Additional caveats are controlled with the --caveat flag. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 212 | |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 213 | For example, let's say a principal "alice" wants to bless another principal "bob" |
| 214 | as "alice/friend", the invocation would be: |
Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -0700 | [diff] [blame] | 215 | V23_CREDENTIALS=<path to alice> principal bless <path to bob> friend |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 216 | and this will dump the blessing to STDOUT. |
| 217 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 218 | 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] | 219 | bless a principal on a remote machine as well. In this case, the blessing is |
| 220 | not dumped to STDOUT but sent to the remote end. Use 'principal help |
| 221 | recvblessings' for more details on that. |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 222 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 223 | 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] | 224 | arguments will be extracted from the specified file. |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 225 | `, |
Asim Shankar | 2a32dd2 | 2015-05-29 15:45:22 -0700 | [diff] [blame] | 226 | ArgsName: "[<principal to bless>] [<extension>]", |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 227 | ArgsLong: ` |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 228 | <principal to bless> represents the principal to be blessed (i.e., whose public |
| 229 | key will be provided with a name). This can be either: |
| 230 | (a) The directory containing credentials for that principal, |
| 231 | OR |
| 232 | (b) The filename (- for STDIN) containing any other blessings of that |
| 233 | principal, |
| 234 | OR |
| 235 | (c) The object name produced by the 'recvblessings' command of this tool |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 236 | running on behalf of another principal (if the --remote-key and |
| 237 | --remote-token flags are specified). |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 238 | OR |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 239 | (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] | 240 | to bless). |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 241 | |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 242 | <extension> is the string extension that will be applied to create the |
| 243 | blessing. |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 244 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 245 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 246 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Asim Shankar | 2a32dd2 | 2015-05-29 15:45:22 -0700 | [diff] [blame] | 247 | if len(flagRemoteArgFile) > 0 { |
| 248 | if len(args) > 1 { |
| 249 | return fmt.Errorf("when --remote-arg-file is provided, only <extension> is expected, provided %d", len(args)) |
| 250 | } |
| 251 | if (len(flagBlessRemoteKey) + len(flagBlessRemoteToken)) > 0 { |
| 252 | return fmt.Errorf("--remote-key and --remote-token should not be specified when --remote-arg-file is") |
| 253 | } |
| 254 | } else if len(args) > 2 { |
| 255 | return fmt.Errorf("got %d arguments, require at most 2", len(args)) |
| 256 | } else if (len(flagBlessRemoteKey) == 0) != (len(flagBlessRemoteToken) == 0) { |
| 257 | return fmt.Errorf("either both --remote-key and --remote-token should be set, or neither should") |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 258 | } |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 259 | p := v23.GetPrincipal(ctx) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 260 | |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 261 | var ( |
| 262 | err error |
| 263 | with security.Blessings |
| 264 | ) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 265 | if len(flagBlessWith) > 0 { |
| 266 | if with, err = decodeBlessings(flagBlessWith); err != nil { |
| 267 | return fmt.Errorf("failed to read blessings from --with=%q: %v", flagBlessWith, err) |
| 268 | } |
| 269 | } else { |
| 270 | with = p.BlessingStore().Default() |
| 271 | } |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 272 | caveats, err := caveatsFromFlags(flagBlessFor, &flagBlessCaveats) |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 273 | if err != nil { |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 274 | return err |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 275 | } |
Asim Shankar | 2a32dd2 | 2015-05-29 15:45:22 -0700 | [diff] [blame] | 276 | if len(caveats) == 0 { |
| 277 | if flagBlessRequireCaveats { |
| 278 | if err := confirmNoCaveats(env); err != nil { |
| 279 | return err |
| 280 | } |
| 281 | } |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 282 | caveats = []security.Caveat{security.UnconstrainedUse()} |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 283 | } |
Bogdan Caprita | 553e582 | 2015-02-20 14:50:41 -0800 | [diff] [blame] | 284 | if len(caveats) == 0 { |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 285 | return errNoCaveats |
Bogdan Caprita | 553e582 | 2015-02-20 14:50:41 -0800 | [diff] [blame] | 286 | } |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 287 | |
Asim Shankar | 2a32dd2 | 2015-05-29 15:45:22 -0700 | [diff] [blame] | 288 | tobless, extension, remoteKey, remoteToken, err := blessArgs(env, args) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 289 | if err != nil { |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 290 | return err |
| 291 | } |
| 292 | |
| 293 | // Send blessings to a "server" started by a "recvblessings" command, either |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 294 | // 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] | 295 | if len(remoteKey) > 0 { |
Ankur | dda1649 | 2015-04-07 12:35:42 -0700 | [diff] [blame] | 296 | granter := &granter{with, extension, caveats, remoteKey} |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 297 | return blessOverNetwork(ctx, tobless, granter, remoteToken) |
| 298 | } |
| 299 | |
| 300 | // Blessing a principal whose key is available locally. |
| 301 | blessings, err := blessOverFileSystem(p, tobless, with, extension, caveats) |
| 302 | if err != nil { |
| 303 | return err |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 304 | } |
| 305 | return dumpBlessings(blessings) |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 306 | }), |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 309 | cmdGetPublicKey = &cmdline.Command{ |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 310 | Name: "publickey", |
| 311 | Short: "Prints the public key of the principal.", |
| 312 | Long: ` |
| 313 | Prints out the public key of the principal specified by the environment |
| 314 | that this tool is running in. |
Asim Shankar | de6fda5 | 2015-04-22 21:20:24 -0700 | [diff] [blame] | 315 | |
| 316 | The key is printed as a base64 encoded bytes of the DER-format representation |
Asim Shankar | 562b230 | 2015-04-27 13:52:43 -0700 | [diff] [blame] | 317 | of the key (suitable to be provided as an argument to the 'recognize' command |
Asim Shankar | 1213ab2 | 2015-04-23 10:11:07 -0700 | [diff] [blame] | 318 | for example). |
Asim Shankar | de6fda5 | 2015-04-22 21:20:24 -0700 | [diff] [blame] | 319 | |
Asim Shankar | 1213ab2 | 2015-04-23 10:11:07 -0700 | [diff] [blame] | 320 | With --pretty, a 16-byte fingerprint of the key instead. This format is easier |
| 321 | for humans to read and is used in output of other commands in this program, but |
Asim Shankar | 562b230 | 2015-04-27 13:52:43 -0700 | [diff] [blame] | 322 | is not suitable as an argument to the 'recognize' command. |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 323 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 324 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Asim Shankar | de6fda5 | 2015-04-22 21:20:24 -0700 | [diff] [blame] | 325 | key := v23.GetPrincipal(ctx).PublicKey() |
| 326 | if flagGetPublicKeyPretty { |
| 327 | fmt.Println(key) |
| 328 | return nil |
| 329 | } |
| 330 | der, err := key.MarshalBinary() |
| 331 | if err != nil { |
| 332 | return fmt.Errorf("corrupted key: %v", err) |
| 333 | } |
| 334 | fmt.Println(base64.URLEncoding.EncodeToString(der)) |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 335 | return nil |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 336 | }), |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 339 | cmdGetTrustedRoots = &cmdline.Command{ |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 340 | Name: "recognizedroots", |
| 341 | Short: "Return recognized blessings, and their associated public key.", |
| 342 | Long: ` |
| 343 | Shows list of blessing names that the principal recognizes, and their associated |
| 344 | public key. If the principal is operating as a client, contacted servers must |
| 345 | appear on this list. If the principal is operating as a server, clients must |
| 346 | present blessings derived from this list. |
| 347 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 348 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 349 | fmt.Printf(v23.GetPrincipal(ctx).Roots().DebugString()) |
| 350 | return nil |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 351 | }), |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 354 | cmdGetPeerMap = &cmdline.Command{ |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 355 | Name: "peermap", |
| 356 | Short: "Shows the map from peer pattern to which blessing name to present.", |
| 357 | Long: ` |
| 358 | Shows the map from peer pattern to which blessing name to present. |
| 359 | If the principal operates as a server, it presents its default blessing to all peers. |
| 360 | If the principal operates as a client, it presents the map value associated with |
| 361 | the peer it contacts. |
| 362 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 363 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 364 | fmt.Printf(v23.GetPrincipal(ctx).BlessingStore().DebugString()) |
| 365 | return nil |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 366 | }), |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 369 | cmdGetForPeer = &cmdline.Command{ |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 370 | Name: "forpeer", |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 371 | Short: "Return blessings marked for the provided peer", |
| 372 | Long: ` |
| 373 | Returns blessings that are marked for the provided peer in the |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 374 | BlessingStore specified by the environment that this tool is |
| 375 | running in. |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 376 | Providing --names will print the blessings' chain names. |
| 377 | Providing --rootkey <chain_name> will print the root key of the certificate chain |
| 378 | with chain_name. |
| 379 | Providing --caveats <chain_name> will print the caveats on the certificate chain |
| 380 | with chain_name. |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 381 | `, |
| 382 | ArgsName: "[<peer_1> ... <peer_k>]", |
| 383 | ArgsLong: ` |
| 384 | <peer_1> ... <peer_k> are the (human-readable string) blessings bound |
| 385 | to the peer. The returned blessings are marked with a pattern that is |
| 386 | matched by at least one of these. If no arguments are specified, |
| 387 | store.forpeer returns the blessings that are marked for all peers (i.e., |
| 388 | blessings set on the store with the "..." pattern). |
| 389 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 390 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 391 | return printBlessingsInfo(v23.GetPrincipal(ctx).BlessingStore().ForPeer(args...)) |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 392 | }), |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 395 | cmdGetDefault = &cmdline.Command{ |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 396 | Name: "default", |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 397 | Short: "Return blessings marked as default", |
| 398 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 399 | Returns blessings that are marked as default in the BlessingStore specified by |
| 400 | the environment that this tool is running in. |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 401 | Providing --names will print the default blessings' chain names. |
| 402 | Providing --rootkey <chain_name> will print the root key of the certificate chain |
| 403 | with chain_name. |
| 404 | Providing --caveats <chain_name> will print the caveats on the certificate chain |
| 405 | with chain_name. |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 406 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 407 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 408 | return printBlessingsInfo(v23.GetPrincipal(ctx).BlessingStore().Default()) |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 409 | }), |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 410 | } |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 411 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 412 | cmdSetForPeer = &cmdline.Command{ |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 413 | Name: "forpeer", |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 414 | Short: "Set provided blessings for peer", |
| 415 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 416 | Marks the provided blessings to be shared with the provided peers on the |
| 417 | BlessingStore specified by the environment that this tool is running in. |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 418 | |
| 419 | 'set b pattern' marks the intention to reveal b to peers who |
| 420 | present blessings of their own matching 'pattern'. |
| 421 | |
| 422 | 'set nil pattern' can be used to remove the blessings previously |
| 423 | associated with the pattern (by a prior 'set' command). |
| 424 | |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 425 | It is an error to call 'set forpeer' with blessings whose public |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 426 | key does not match the public key of this principal specified |
| 427 | by the environment. |
| 428 | `, |
| 429 | ArgsName: "<file> <pattern>", |
| 430 | ArgsLong: ` |
| 431 | <file> is the path to a file containing a blessing typically obtained |
| 432 | from this tool. - is used for STDIN. |
| 433 | |
| 434 | <pattern> is the BlessingPattern used to identify peers with whom this |
| 435 | blessing can be shared with. |
| 436 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 437 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 438 | if len(args) != 2 { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 439 | return fmt.Errorf("requires exactly two arguments <file>, <pattern>, provided %d", len(args)) |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 440 | } |
| 441 | blessings, err := decodeBlessings(args[0]) |
| 442 | if err != nil { |
| 443 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 444 | } |
| 445 | pattern := security.BlessingPattern(args[1]) |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 446 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 447 | p := v23.GetPrincipal(ctx) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 448 | if _, err := p.BlessingStore().Set(blessings, pattern); err != nil { |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 449 | return fmt.Errorf("failed to set blessings %v for peers %v: %v", blessings, pattern, err) |
| 450 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 451 | if flagAddToRoots { |
| 452 | if err := p.AddToRoots(blessings); err != nil { |
| 453 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 454 | } |
| 455 | } |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 456 | return nil |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 457 | }), |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 460 | cmdRecognize = &cmdline.Command{ |
Asim Shankar | 562b230 | 2015-04-27 13:52:43 -0700 | [diff] [blame] | 461 | Name: "recognize", |
| 462 | Short: "Add to the set of identity providers recognized by this principal", |
| 463 | Long: ` |
| 464 | Adds an identity provider to the set of recognized roots public keys for this principal. |
| 465 | |
| 466 | It accepts either a single argument (which points to a file containing a blessing) |
| 467 | or two arguments (a name and a base64-encoded DER-encoded public key). |
| 468 | |
| 469 | For example, to make the principal in credentials directory A recognize the |
| 470 | root of the default blessing in credentials directory B: |
| 471 | principal -v23.credentials=B bless A some_extension | |
| 472 | principal -v23.credentials=A recognize - |
| 473 | The extension 'some_extension' has no effect in the command above. |
| 474 | |
| 475 | Or to make the principal in credentials directory A recognize the base64-encoded |
| 476 | public key KEY for blessing pattern P: |
| 477 | principal -v23.credentials=A recognize P KEY |
| 478 | `, |
| 479 | ArgsName: "<key|blessing> [<blessing pattern>]", |
| 480 | ArgsLong: ` |
| 481 | <blessing> is the path to a file containing a blessing typically obtained from |
| 482 | this tool. - is used for STDIN. |
| 483 | |
| 484 | <key> is a base64-encoded, DER-encoded public key. |
| 485 | |
| 486 | <blessing pattern> is the blessing pattern for which <key> should be recognized. |
| 487 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 488 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Asim Shankar | 562b230 | 2015-04-27 13:52:43 -0700 | [diff] [blame] | 489 | if len(args) != 1 && len(args) != 2 { |
| 490 | return fmt.Errorf("requires either one argument <file>, or two arguments <key> <blessing pattern>, provided %d", len(args)) |
| 491 | } |
Asim Shankar | 562b230 | 2015-04-27 13:52:43 -0700 | [diff] [blame] | 492 | p := v23.GetPrincipal(ctx) |
| 493 | if len(args) == 1 { |
| 494 | blessings, err := decodeBlessings(args[0]) |
| 495 | if err != nil { |
| 496 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 497 | } |
| 498 | if err := p.AddToRoots(blessings); err != nil { |
| 499 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 500 | } |
| 501 | return nil |
| 502 | } |
| 503 | // len(args) == 2 |
| 504 | der, err := base64.URLEncoding.DecodeString(args[1]) |
| 505 | if err != nil { |
| 506 | return fmt.Errorf("invalid base64 encoding of public key: %v", err) |
| 507 | } |
| 508 | key, err := security.UnmarshalPublicKey(der) |
| 509 | if err != nil { |
| 510 | return fmt.Errorf("invalid DER encoding of public key: %v", err) |
| 511 | } |
| 512 | return p.Roots().Add(key, security.BlessingPattern(args[0])) |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 513 | }), |
Asim Shankar | 562b230 | 2015-04-27 13:52:43 -0700 | [diff] [blame] | 514 | } |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 515 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 516 | cmdSetDefault = &cmdline.Command{ |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 517 | Name: "default", |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 518 | Short: "Set provided blessings as default", |
| 519 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 520 | Sets the provided blessings as default in the BlessingStore specified by the |
| 521 | environment that this tool is running in. |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 522 | |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 523 | 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] | 524 | not match the public key of the principal specified by the environment. |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 525 | `, |
| 526 | ArgsName: "<file>", |
| 527 | ArgsLong: ` |
| 528 | <file> is the path to a file containing a blessing typically obtained from |
| 529 | this tool. - is used for STDIN. |
| 530 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 531 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 532 | if len(args) != 1 { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 533 | return fmt.Errorf("requires exactly one argument, <file>, provided %d", len(args)) |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 534 | } |
| 535 | blessings, err := decodeBlessings(args[0]) |
| 536 | if err != nil { |
| 537 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 538 | } |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 539 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 540 | p := v23.GetPrincipal(ctx) |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 541 | if err := p.BlessingStore().SetDefault(blessings); err != nil { |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 542 | return fmt.Errorf("failed to set blessings %v as default: %v", blessings, err) |
| 543 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 544 | if flagAddToRoots { |
| 545 | if err := p.AddToRoots(blessings); err != nil { |
| 546 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 547 | } |
| 548 | } |
| 549 | return nil |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 550 | }), |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 553 | cmdCreate = &cmdline.Command{ |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 554 | Name: "create", |
| 555 | Short: "Create a new principal and persist it into a directory", |
| 556 | Long: ` |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 557 | 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] | 558 | 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] | 559 | V23_CREDENTIALS environment variable for other vanadium applications. |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 560 | |
| 561 | The operation fails if the directory already contains a principal. In this case |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 562 | 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] | 563 | new principal. |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 564 | `, |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 565 | ArgsName: "<directory> <blessing>", |
| 566 | ArgsLong: ` |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 567 | <directory> is the directory to which the new principal will be persisted. |
| 568 | |
| 569 | <blessing> is the self-blessed blessing that the principal will be setup to use by default. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 570 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 571 | Runner: cmdline.RunnerFunc(func(env *cmdline.Env, args []string) error { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 572 | if len(args) != 2 { |
| 573 | return fmt.Errorf("requires exactly two arguments: <directory> and <blessing>, provided %d", len(args)) |
| 574 | } |
| 575 | dir, name := args[0], args[1] |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 576 | if flagCreateOverwrite { |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 577 | if err := os.RemoveAll(dir); err != nil { |
gauthamt | b7bb39b | 2014-11-10 11:40:41 -0800 | [diff] [blame] | 578 | return err |
| 579 | } |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 580 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 581 | p, err := vsecurity.CreatePersistentPrincipal(dir, nil) |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 582 | if err != nil { |
| 583 | return err |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 584 | } |
| 585 | blessings, err := p.BlessSelf(name) |
| 586 | if err != nil { |
| 587 | return fmt.Errorf("BlessSelf(%q) failed: %v", name, err) |
| 588 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 589 | if err := vsecurity.SetDefaultBlessings(p, blessings); err != nil { |
| 590 | 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] | 591 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 592 | return nil |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 593 | }), |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 596 | cmdFork = &cmdline.Command{ |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 597 | Name: "fork", |
| 598 | Short: "Fork a new principal from the principal that this tool is running as and persist it into a directory", |
| 599 | Long: ` |
| 600 | Creates a new principal with a blessing from the principal specified by the |
| 601 | environment that this tool is running in, and writes it out to the provided |
| 602 | 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] | 603 | blesser's store, or specified by the --with flag. Expiration on the blessing |
| 604 | are controlled via the --for flag. Additional caveats on the blessing are |
| 605 | controlled with the --caveat flag. The blessing is marked as default and |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 606 | shareable with all peers on the new principal's blessing store. |
| 607 | |
| 608 | The operation fails if the directory already contains a principal. In this case |
| 609 | the --overwrite flag can be provided to clear the directory and write out the |
| 610 | forked principal. |
| 611 | `, |
| 612 | ArgsName: "<directory> <extension>", |
| 613 | ArgsLong: ` |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 614 | <directory> is the directory to which the forked principal will be persisted. |
| 615 | |
| 616 | <extension> is the extension under which the forked principal is blessed. |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 617 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 618 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 619 | if len(args) != 2 { |
| 620 | 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] | 621 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 622 | dir, extension := args[0], args[1] |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 623 | caveats, err := caveatsFromFlags(flagForkFor, &flagForkCaveats) |
| 624 | if err != nil { |
| 625 | return err |
| 626 | } |
| 627 | if !flagForkRequireCaveats && len(caveats) == 0 { |
| 628 | caveats = []security.Caveat{security.UnconstrainedUse()} |
| 629 | } |
| 630 | if len(caveats) == 0 { |
| 631 | return errNoCaveats |
| 632 | } |
| 633 | var with security.Blessings |
| 634 | if len(flagForkWith) > 0 { |
| 635 | if with, err = decodeBlessings(flagForkWith); err != nil { |
| 636 | return fmt.Errorf("failed to read blessings from --with=%q: %v", flagForkWith, err) |
| 637 | } |
| 638 | } else { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 639 | with = v23.GetPrincipal(ctx).BlessingStore().Default() |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 642 | if flagCreateOverwrite { |
| 643 | if err := os.RemoveAll(dir); err != nil { |
| 644 | return err |
| 645 | } |
| 646 | } |
| 647 | p, err := vsecurity.CreatePersistentPrincipal(dir, nil) |
| 648 | if err != nil { |
| 649 | return err |
| 650 | } |
| 651 | |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 652 | key := p.PublicKey() |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 653 | rp := v23.GetPrincipal(ctx) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 654 | blessings, err := rp.Bless(key, with, extension, caveats[0], caveats[1:]...) |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 655 | if err != nil { |
| 656 | return fmt.Errorf("Bless(%v, %v, %q, ...) failed: %v", key, with, extension, err) |
| 657 | } |
| 658 | if err := vsecurity.SetDefaultBlessings(p, blessings); err != nil { |
| 659 | 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] | 660 | } |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 661 | return nil |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 662 | }), |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 663 | } |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 664 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 665 | cmdSeekBlessings = &cmdline.Command{ |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 666 | Name: "seekblessings", |
Suharsh Sivakumar | d1cc6e0 | 2015-03-16 13:58:49 -0700 | [diff] [blame] | 667 | Short: "Seek blessings from a web-based Vanadium blessing service", |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 668 | Long: ` |
Suharsh Sivakumar | d1cc6e0 | 2015-03-16 13:58:49 -0700 | [diff] [blame] | 669 | Seeks blessings from a web-based Vanadium blesser which |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 670 | requires the caller to first authenticate with Google using OAuth. Simply |
| 671 | run the command to see what happens. |
| 672 | |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 673 | The blessings are sought for the principal specified by the environment that |
| 674 | this tool is running in. |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 675 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 676 | The blessings obtained are set as default, unless the --set-default flag is |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 677 | 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] | 678 | specific peer pattern is provided using the --for-peer flag. |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 679 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 680 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Ankur | af98e57 | 2015-05-05 14:18:50 -0700 | [diff] [blame] | 681 | p := v23.GetPrincipal(ctx) |
| 682 | |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 683 | blessedChan := make(chan string) |
| 684 | defer close(blessedChan) |
Ankur | af98e57 | 2015-05-05 14:18:50 -0700 | [diff] [blame] | 685 | macaroonChan, err := getMacaroonForBlessRPC(p.PublicKey(), flagSeekBlessingsFrom, blessedChan, flagSeekBlessingsBrowser) |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 686 | if err != nil { |
Suharsh Sivakumar | d1cc6e0 | 2015-03-16 13:58:49 -0700 | [diff] [blame] | 687 | return fmt.Errorf("failed to get macaroon from Vanadium blesser: %v", err) |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 688 | } |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 689 | |
Suharsh Sivakumar | 67ef84a | 2015-02-13 13:04:44 -0800 | [diff] [blame] | 690 | blessings, err := exchangeMacaroonForBlessing(ctx, macaroonChan) |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 691 | if err != nil { |
Suharsh Sivakumar | 67ef84a | 2015-02-13 13:04:44 -0800 | [diff] [blame] | 692 | return err |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 693 | } |
| 694 | blessedChan <- fmt.Sprint(blessings) |
| 695 | // Wait for getTokenForBlessRPC to clean up: |
| 696 | <-macaroonChan |
| 697 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 698 | if flagSeekBlessingsSetDefault { |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 699 | if err := p.BlessingStore().SetDefault(blessings); err != nil { |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 700 | return fmt.Errorf("failed to set blessings %v as default: %v", blessings, err) |
| 701 | } |
| 702 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 703 | if pattern := security.BlessingPattern(flagSeekBlessingsForPeer); len(pattern) > 0 { |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 704 | if _, err := p.BlessingStore().Set(blessings, pattern); err != nil { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 705 | return fmt.Errorf("failed to set blessings %v for peers %v: %v", blessings, pattern, err) |
| 706 | } |
| 707 | } |
| 708 | if flagAddToRoots { |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 709 | if err := p.AddToRoots(blessings); err != nil { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 710 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 711 | } |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 712 | } |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 713 | fmt.Fprintf(env.Stdout, "Received blessings: %v\n", blessings) |
Jiri Simsa | 37b0a09 | 2015-02-13 09:32:39 -0800 | [diff] [blame] | 714 | return nil |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 715 | }), |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 716 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 717 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 718 | cmdRecvBlessings = &cmdline.Command{ |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 719 | Name: "recvblessings", |
| 720 | Short: "Receive blessings sent by another principal and use them as the default", |
| 721 | Long: ` |
| 722 | Allow another principal (likely a remote process) to bless this one. |
| 723 | |
| 724 | This command sets up the invoker (this process) to wait for a blessing |
| 725 | from another invocation of this tool (remote process) and prints out the |
| 726 | command to be run as the remote principal. |
| 727 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 728 | The received blessings are set as default, unless the --set-default flag is |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 729 | 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] | 730 | specific peer pattern is provided using the --for-peer flag. |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 731 | |
| 732 | TODO(ashankar,cnicolaou): Make this next paragraph possible! Requires |
| 733 | the ability to obtain the proxied endpoint. |
| 734 | |
| 735 | Typically, this command should require no arguments. |
| 736 | 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] | 737 | make sense to use the --v23.proxy flag: |
| 738 | principal --v23.proxy=proxy recvblessings |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 739 | |
| 740 | The command to be run at the sender is of the form: |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 741 | principal bless --remote-key=KEY --remote-token=TOKEN ADDRESS EXTENSION |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 742 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 743 | 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] | 744 | ensuring it blesses the intended recipient and not any attacker that may have |
| 745 | taken over the address. |
| 746 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 747 | 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] | 748 | receiver. This helps ensure that the receiver rejects blessings from senders |
| 749 | who just happened to guess the network address of the 'recvblessings' |
| 750 | invocation. |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 751 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 752 | 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] | 753 | and object address of this principal will be written to the specified location. |
| 754 | This file can be supplied to bless: |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 755 | principal bless --remote-arg-file FILE EXTENSION |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 756 | |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 757 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 758 | Runner: v23cmd.RunnerFunc(func(ctx *context.T, env *cmdline.Env, args []string) error { |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 759 | if len(args) != 0 { |
| 760 | return fmt.Errorf("command accepts no arguments") |
| 761 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 762 | var token [24]byte |
| 763 | if _, err := rand.Read(token[:]); err != nil { |
| 764 | return fmt.Errorf("unable to generate token: %v", err) |
| 765 | } |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 766 | p := v23.GetPrincipal(ctx) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 767 | service := &recvBlessingsService{ |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 768 | principal: p, |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 769 | token: base64.URLEncoding.EncodeToString(token[:]), |
| 770 | notify: make(chan error), |
| 771 | } |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 772 | server, err := xrpc.NewServer(ctx, "", service, security.AllowEveryone()) |
| 773 | if err != nil { |
| 774 | return fmt.Errorf("failed to create server to listen for blessings: %v", err) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 775 | } |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 776 | name := server.Status().Endpoints[0].Name() |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 777 | fmt.Println("Run the following command on behalf of the principal that will send blessings:") |
| 778 | fmt.Println("You may want to adjust flags affecting the caveats on this blessing, for example using") |
Asim Shankar | 2a32dd2 | 2015-05-29 15:45:22 -0700 | [diff] [blame] | 779 | fmt.Println("the --for flag") |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 780 | fmt.Println() |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 781 | if len(flagRemoteArgFile) > 0 { |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 782 | if err := writeRecvBlessingsInfo(flagRemoteArgFile, p.PublicKey().String(), service.token, name); err != nil { |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 783 | return fmt.Errorf("failed to write recvblessings info to %v: %v", flagRemoteArgFile, err) |
| 784 | } |
| 785 | fmt.Printf("make %q accessible to the blesser, possibly by copying the file over and then run:\n", flagRemoteArgFile) |
Asim Shankar | 2a32dd2 | 2015-05-29 15:45:22 -0700 | [diff] [blame] | 786 | fmt.Printf("principal bless --remote-arg-file=%v", flagRemoteArgFile) |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 787 | } else { |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 788 | fmt.Printf("principal bless --remote-key=%v --remote-token=%v %v\n", p.PublicKey(), service.token, name) |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 789 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 790 | fmt.Println() |
| 791 | fmt.Println("...waiting for sender..") |
| 792 | return <-service.notify |
Todd Wang | f1550cf | 2015-05-11 10:58:41 -0700 | [diff] [blame] | 793 | }), |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 794 | } |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 795 | ) |
| 796 | |
Bogdan Caprita | 4ab9541 | 2015-05-13 13:37:46 -0700 | [diff] [blame] | 797 | func printAnnotatedBlessingsNames(b security.Blessings) string { |
| 798 | // If the Blessings are expired, print a message saying so. |
| 799 | expiredMessage := "" |
| 800 | if exp := b.Expiry(); !exp.IsZero() && exp.Before(time.Now()) { |
| 801 | expiredMessage = " [EXPIRED]" |
| 802 | } |
| 803 | return fmt.Sprintf("%v%s", b, expiredMessage) |
| 804 | } |
| 805 | |
Asim Shankar | 2a32dd2 | 2015-05-29 15:45:22 -0700 | [diff] [blame] | 806 | func blessArgs(env *cmdline.Env, args []string) (tobless, extension, remoteKey, remoteToken string, err error) { |
| 807 | extensionInArgs := false |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 808 | if len(flagRemoteArgFile) == 0 { |
Asim Shankar | 2a32dd2 | 2015-05-29 15:45:22 -0700 | [diff] [blame] | 809 | tobless = args[0] |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 810 | remoteKey = flagBlessRemoteKey |
| 811 | remoteToken = flagBlessRemoteToken |
Asim Shankar | 2a32dd2 | 2015-05-29 15:45:22 -0700 | [diff] [blame] | 812 | extensionInArgs = len(args) > 1 |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 813 | } else if len(flagRemoteArgFile) > 0 { |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 814 | remoteKey, remoteToken, tobless, err = blessArgsFromFile(flagRemoteArgFile) |
Asim Shankar | 2a32dd2 | 2015-05-29 15:45:22 -0700 | [diff] [blame] | 815 | extensionInArgs = len(args) > 0 |
| 816 | } |
| 817 | if extensionInArgs { |
| 818 | extension = args[len(args)-1] |
| 819 | } else { |
| 820 | extension, err = readFromStdin(env, "Extension to use for blessing:") |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 821 | } |
| 822 | return |
| 823 | } |
| 824 | |
Asim Shankar | 2a32dd2 | 2015-05-29 15:45:22 -0700 | [diff] [blame] | 825 | func confirmNoCaveats(env *cmdline.Env) error { |
| 826 | text, err := readFromStdin(env, `WARNING: No caveats provided |
| 827 | It is generally dangerous to bless another principal without any caveats as |
| 828 | that gives them unrestricted access to the blesser's credentials. |
| 829 | |
| 830 | Caveats can be specified with the --for or --caveat flags. |
| 831 | |
| 832 | Do you really wish to bless without caveats? (YES to confirm)`) |
| 833 | if err != nil || strings.ToUpper(text) != "YES" { |
| 834 | return errNoCaveats |
| 835 | } |
| 836 | return nil |
| 837 | } |
| 838 | |
| 839 | func readFromStdin(env *cmdline.Env, prompt string) (string, error) { |
| 840 | fmt.Fprintf(env.Stdout, "%v ", prompt) |
| 841 | os.Stdout.Sync() |
| 842 | // Cannot use bufio because that may "lose" data beyond the line (the |
| 843 | // remainder in the buffer). |
| 844 | // Do the inefficient byte-by-byte scan for now - shouldn't be a problem |
| 845 | // given the common use case. If that becomes a problem, switch to bufio |
| 846 | // and share the bufio.Reader between multiple calls to readFromStdin. |
| 847 | buf := make([]byte, 0, 100) |
| 848 | r := make([]byte, 1) |
| 849 | for { |
| 850 | n, err := env.Stdin.Read(r) |
| 851 | if n == 1 && r[0] == '\n' { |
| 852 | break |
| 853 | } |
| 854 | if n == 1 { |
| 855 | buf = append(buf, r[0]) |
| 856 | continue |
| 857 | } |
| 858 | if err != nil { |
| 859 | return "", err |
| 860 | } |
| 861 | } |
| 862 | return strings.TrimSpace(string(buf)), nil |
| 863 | } |
| 864 | |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 865 | func blessOverFileSystem(p security.Principal, tobless string, with security.Blessings, extension string, caveats []security.Caveat) (security.Blessings, error) { |
| 866 | var key security.PublicKey |
| 867 | if finfo, err := os.Stat(tobless); err == nil && finfo.IsDir() { |
| 868 | other, err := vsecurity.LoadPersistentPrincipal(tobless, nil) |
| 869 | if err != nil { |
| 870 | if other, err = vsecurity.CreatePersistentPrincipal(tobless, nil); err != nil { |
| 871 | return security.Blessings{}, fmt.Errorf("failed to read principal in directory %q: %v", tobless, err) |
| 872 | } |
| 873 | } |
| 874 | key = other.PublicKey() |
| 875 | } else if other, err := decodeBlessings(tobless); err != nil { |
| 876 | return security.Blessings{}, fmt.Errorf("failed to decode blessings in %q: %v", tobless, err) |
| 877 | } else { |
| 878 | key = other.PublicKey() |
| 879 | } |
| 880 | return p.Bless(key, with, extension, caveats[0], caveats[1:]...) |
| 881 | } |
| 882 | |
| 883 | type recvBlessingsInfo struct { |
Asim Shankar | fe4899f | 2015-05-08 13:23:28 -0700 | [diff] [blame] | 884 | RemoteKey string `json:"remote_key"` |
| 885 | RemoteToken string `json:"remote_token"` |
| 886 | Name string `json:"name"` |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | func writeRecvBlessingsInfo(fname string, remoteKey, remoteToken, name string) error { |
| 890 | f, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE, 0666) |
| 891 | if err != nil { |
| 892 | return err |
| 893 | } |
| 894 | b, err := json.Marshal(recvBlessingsInfo{remoteKey, remoteToken, name}) |
| 895 | if err != nil { |
| 896 | return err |
| 897 | } |
| 898 | if _, err := f.Write(b); err != nil { |
| 899 | return err |
| 900 | } |
| 901 | return nil |
| 902 | } |
| 903 | |
| 904 | func blessArgsFromFile(fname string) (remoteKey, remoteToken, tobless string, err error) { |
| 905 | blessJSON, err := ioutil.ReadFile(fname) |
| 906 | if err != nil { |
| 907 | return "", "", "", err |
| 908 | } |
| 909 | var binfo recvBlessingsInfo |
| 910 | if err := json.Unmarshal(blessJSON, &binfo); err != nil { |
| 911 | return "", "", "", err |
| 912 | } |
| 913 | return binfo.RemoteKey, binfo.RemoteToken, binfo.Name, err |
| 914 | } |
| 915 | |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 916 | func main() { |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 917 | cmdline.HideGlobalFlagsExcept() |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 918 | cmdBlessSelf.Flags.Var(&flagBlessSelfCaveats, "caveat", flagBlessSelfCaveats.usage()) |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 919 | cmdBlessSelf.Flags.DurationVar(&flagBlessSelfFor, "for", 0, "Duration of blessing validity (zero implies no expiration)") |
| 920 | |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 921 | cmdDump.Flags.BoolVar(&flagDumpShort, "s", false, "If true, show only the default blessing names") |
Robin Thellend | 5396eae | 2015-04-13 09:57:06 -0700 | [diff] [blame] | 922 | |
Ankur | 77c32ac | 2014-12-18 14:18:19 -0800 | [diff] [blame] | 923 | 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] | 924 | cmdFork.Flags.Var(&flagForkCaveats, "caveat", flagForkCaveats.usage()) |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 925 | 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] | 926 | 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] | 927 | cmdFork.Flags.StringVar(&flagForkWith, "with", "", "Path to file containing blessing to extend") |
| 928 | |
Suharsh Sivakumar | 1d61f64 | 2015-02-17 20:56:14 -0800 | [diff] [blame] | 929 | cmdBless.Flags.Var(&flagBlessCaveats, "caveat", flagBlessCaveats.usage()) |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 930 | 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] | 931 | 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] | 932 | cmdBless.Flags.StringVar(&flagBlessWith, "with", "", "Path to file containing blessing to extend") |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 933 | cmdBless.Flags.StringVar(&flagBlessRemoteKey, "remote-key", "", "Public key of the remote principal to bless (obtained from the 'recvblessings' command run by the remote principal") |
| 934 | cmdBless.Flags.StringVar(&flagBlessRemoteToken, "remote-token", "", "Token provided by principal running the 'recvblessings' command") |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 935 | 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] | 936 | |
Robin Thellend | 157e2aa | 2015-04-17 14:04:40 -0700 | [diff] [blame] | 937 | defaultFrom := "https://dev.v.io/auth/google" |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 938 | if e := os.Getenv(ref.EnvOAuthIdentityProvider); e != "" { |
Robin Thellend | 157e2aa | 2015-04-17 14:04:40 -0700 | [diff] [blame] | 939 | defaultFrom = e |
| 940 | } |
| 941 | cmdSeekBlessings.Flags.StringVar(&flagSeekBlessingsFrom, "from", defaultFrom, "URL to use to begin the seek blessings process") |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 942 | cmdSeekBlessings.Flags.BoolVar(&flagSeekBlessingsSetDefault, "set-default", true, "If true, the blessings obtained will be set as the default blessing in the store") |
| 943 | 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] | 944 | 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] | 945 | 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] | 946 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 947 | 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] | 948 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 949 | 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] | 950 | |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 951 | 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] | 952 | |
Suharsh Sivakumar | ed5be1d | 2015-04-01 17:45:35 -0700 | [diff] [blame] | 953 | cmdRecvBlessings.Flags.BoolVar(&flagRecvBlessingsSetDefault, "set-default", true, "If true, the blessings received will be set as the default blessing in the store") |
| 954 | 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") |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 955 | 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'") |
| 956 | |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 957 | cmdGetForPeer.Flags.BoolVar(&flagBlessingsNames, "names", false, "If true, shows the value of the blessing name to be presented to the peer") |
| 958 | cmdGetForPeer.Flags.StringVar(&flagBlessingsRootKey, "rootkey", "", "Shows the value of the root key of the provided certificate chain name.") |
| 959 | cmdGetForPeer.Flags.StringVar(&flagBlessingsCaveats, "caveats", "", "Shows the caveats on the provided certificate chain name.") |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 960 | |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 961 | cmdGetDefault.Flags.BoolVar(&flagBlessingsNames, "names", false, "If true, shows the value of the blessing name to be presented to the peer") |
| 962 | cmdGetDefault.Flags.StringVar(&flagBlessingsRootKey, "rootkey", "", "Shows the value of the root key of the provided certificate chain name.") |
| 963 | cmdGetDefault.Flags.StringVar(&flagBlessingsCaveats, "caveats", "", "Shows the caveats on the provided certificate chain name.") |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 964 | |
Asim Shankar | de6fda5 | 2015-04-22 21:20:24 -0700 | [diff] [blame] | 965 | cmdGetPublicKey.Flags.BoolVar(&flagGetPublicKeyPretty, "pretty", false, "If true, print the key out in a more human-readable but lossy representation.") |
| 966 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 967 | cmdSet := &cmdline.Command{ |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 968 | Name: "set", |
| 969 | Short: "Mutate the principal's blessings.", |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 970 | Long: ` |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 971 | Commands to mutate the blessings of the principal. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 972 | |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 973 | All input blessings are expected to be serialized using base64-VOM-encoding. |
| 974 | See 'principal get'. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 975 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 976 | Children: []*cmdline.Command{cmdSetDefault, cmdSetForPeer}, |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 979 | cmdGet := &cmdline.Command{ |
Suharsh Sivakumar | 32c8e75 | 2015-03-31 19:26:28 -0700 | [diff] [blame] | 980 | Name: "get", |
| 981 | Short: "Read the principal's blessings.", |
| 982 | Long: ` |
| 983 | Commands to inspect the blessings of the principal. |
| 984 | |
| 985 | All blessings are printed to stdout using base64-VOM-encoding. |
| 986 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 987 | Children: []*cmdline.Command{cmdGetDefault, cmdGetForPeer, cmdGetPublicKey, cmdGetTrustedRoots, cmdGetPeerMap}, |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 988 | } |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 989 | |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 990 | root := &cmdline.Command{ |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 991 | Name: "principal", |
Todd Wang | 6ed3b6c | 2015-04-08 14:37:04 -0700 | [diff] [blame] | 992 | Short: "creates and manages Vanadium principals and blessings", |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 993 | Long: ` |
Todd Wang | 6ed3b6c | 2015-04-08 14:37:04 -0700 | [diff] [blame] | 994 | Command principal creates and manages Vanadium principals and blessings. |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 995 | |
| 996 | All objects are printed using base64-VOM-encoding. |
| 997 | `, |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 998 | Children: []*cmdline.Command{cmdCreate, cmdFork, cmdSeekBlessings, cmdRecvBlessings, cmdDump, cmdDumpBlessings, cmdBlessSelf, cmdBless, cmdSet, cmdGet, cmdRecognize}, |
Matt Rosencrantz | 92303e1 | 2015-01-21 09:02:42 -0800 | [diff] [blame] | 999 | } |
Todd Wang | 9560b9c | 2015-05-11 13:27:58 -0700 | [diff] [blame] | 1000 | cmdline.Main(root) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 1003 | func decodeBlessings(fname string) (security.Blessings, error) { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 1004 | var b security.Blessings |
| 1005 | err := decode(fname, &b) |
| 1006 | return b, err |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | func dumpBlessings(blessings security.Blessings) error { |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 1010 | if blessings.IsZero() { |
| 1011 | return fmt.Errorf("no blessings found") |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 1012 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 1013 | str, err := base64VomEncode(blessings) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 1014 | if err != nil { |
| 1015 | return fmt.Errorf("base64-VOM encoding failed: %v", err) |
| 1016 | } |
| 1017 | fmt.Println(str) |
| 1018 | return nil |
| 1019 | } |
| 1020 | |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 1021 | func printBlessingsInfo(blessings security.Blessings) error { |
| 1022 | if blessings.IsZero() { |
| 1023 | return fmt.Errorf("no blessings found") |
| 1024 | } |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 1025 | if flagBlessingsNames { |
| 1026 | fmt.Println(strings.Replace(fmt.Sprint(blessings), ",", "\n", -1)) |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 1027 | return nil |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 1028 | } else if len(flagBlessingsRootKey) > 0 { |
| 1029 | chain, err := getChainByName(blessings, flagBlessingsRootKey) |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 1030 | if err != nil { |
| 1031 | return err |
| 1032 | } |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 1033 | fmt.Println(rootkey(chain)) |
| 1034 | return nil |
| 1035 | } else if len(flagBlessingsCaveats) > 0 { |
| 1036 | chain, err := getChainByName(blessings, flagBlessingsCaveats) |
| 1037 | if err != nil { |
| 1038 | return err |
| 1039 | } |
| 1040 | cavs, err := prettyPrintCaveats(chain) |
| 1041 | if err != nil { |
| 1042 | return err |
| 1043 | } |
| 1044 | for _, c := range cavs { |
| 1045 | fmt.Println(c) |
| 1046 | } |
Suharsh Sivakumar | 4bbe8ed | 2015-04-09 14:21:44 -0700 | [diff] [blame] | 1047 | return nil |
| 1048 | } |
| 1049 | return dumpBlessings(blessings) |
| 1050 | } |
| 1051 | |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 1052 | func prettyPrintCaveats(chain []security.Certificate) ([]string, error) { |
| 1053 | var cavs []security.Caveat |
| 1054 | for _, cert := range chain { |
| 1055 | cavs = append(cavs, cert.Caveats...) |
| 1056 | } |
| 1057 | var s []string |
| 1058 | for _, cav := range cavs { |
Suharsh Sivakumar | 60b78e9 | 2015-04-23 21:36:49 -0700 | [diff] [blame] | 1059 | if cav.Id == security.PublicKeyThirdPartyCaveat.Id { |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 1060 | c := cav.ThirdPartyDetails() |
| 1061 | s = append(s, fmt.Sprintf("ThirdPartyCaveat: Requires discharge from %v (ID=%q)", c.Location(), c.ID())) |
| 1062 | continue |
| 1063 | } |
| 1064 | var param interface{} |
| 1065 | if err := vom.Decode(cav.ParamVom, ¶m); err != nil { |
| 1066 | return nil, err |
| 1067 | } |
| 1068 | switch cav.Id { |
| 1069 | case security.ConstCaveat.Id: |
| 1070 | // In the case a ConstCaveat is specified, we only want to print it |
| 1071 | // if it never validates. |
| 1072 | if !param.(bool) { |
| 1073 | s = append(s, fmt.Sprintf("Never validates")) |
| 1074 | } |
Suharsh Sivakumar | 60b78e9 | 2015-04-23 21:36:49 -0700 | [diff] [blame] | 1075 | case security.ExpiryCaveat.Id: |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 1076 | s = append(s, fmt.Sprintf("Expires at %v", param)) |
Suharsh Sivakumar | 60b78e9 | 2015-04-23 21:36:49 -0700 | [diff] [blame] | 1077 | case security.MethodCaveat.Id: |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 1078 | s = append(s, fmt.Sprintf("Restricted to methods %v", param)) |
| 1079 | case security.PeerBlessingsCaveat.Id: |
| 1080 | s = append(s, fmt.Sprintf("Restricted to peers with blessings %v", param)) |
| 1081 | default: |
| 1082 | s = append(s, cav.String()) |
| 1083 | } |
| 1084 | } |
| 1085 | return s, nil |
| 1086 | } |
| 1087 | |
| 1088 | func getChainByName(b security.Blessings, name string) ([]security.Certificate, error) { |
| 1089 | wire, err := blessings2wire(b) |
| 1090 | if err != nil { |
| 1091 | return nil, err |
| 1092 | } |
| 1093 | for _, chain := range wire.CertificateChains { |
| 1094 | if chainName(chain) == name { |
| 1095 | return chain, nil |
| 1096 | } |
| 1097 | } |
| 1098 | return nil, fmt.Errorf("no chains of name %v in %v", name, b) |
| 1099 | } |
| 1100 | |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 1101 | func read(fname string) (string, error) { |
| 1102 | if len(fname) == 0 { |
| 1103 | return "", nil |
| 1104 | } |
| 1105 | f := os.Stdin |
| 1106 | if fname != "-" { |
| 1107 | var err error |
| 1108 | if f, err = os.Open(fname); err != nil { |
| 1109 | return "", fmt.Errorf("failed to open %q: %v", fname, err) |
| 1110 | } |
| 1111 | } |
| 1112 | defer f.Close() |
| 1113 | var buf bytes.Buffer |
| 1114 | if _, err := io.Copy(&buf, f); err != nil { |
| 1115 | return "", fmt.Errorf("failed to read %q: %v", fname, err) |
| 1116 | } |
| 1117 | return buf.String(), nil |
| 1118 | } |
| 1119 | |
| 1120 | func decode(fname string, val interface{}) error { |
| 1121 | str, err := read(fname) |
| 1122 | if err != nil { |
| 1123 | return err |
| 1124 | } |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 1125 | if err := base64VomDecode(str, val); err != nil || val == nil { |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 1126 | return fmt.Errorf("failed to decode %q: %v", fname, err) |
| 1127 | } |
| 1128 | return nil |
| 1129 | } |
| 1130 | |
| 1131 | func defaultBlessingName() string { |
| 1132 | var name string |
| 1133 | if user, _ := user.Current(); user != nil && len(user.Username) > 0 { |
| 1134 | name = user.Username |
| 1135 | } else { |
| 1136 | name = "anonymous" |
| 1137 | } |
| 1138 | if host, _ := os.Hostname(); len(host) > 0 { |
| 1139 | name = name + "@" + host |
| 1140 | } |
| 1141 | return name |
| 1142 | } |
Asim Shankar | df88a2e | 2014-10-21 17:20:28 -0700 | [diff] [blame] | 1143 | |
| 1144 | func rootkey(chain []security.Certificate) string { |
| 1145 | if len(chain) == 0 { |
| 1146 | return "<empty certificate chain>" |
| 1147 | } |
| 1148 | key, err := security.UnmarshalPublicKey(chain[0].PublicKey) |
| 1149 | if err != nil { |
| 1150 | return fmt.Sprintf("<invalid PublicKey: %v>", err) |
| 1151 | } |
| 1152 | return fmt.Sprintf("%v", key) |
| 1153 | } |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 1154 | |
Suharsh Sivakumar | 2b22fc1 | 2015-04-15 19:38:04 -0700 | [diff] [blame] | 1155 | func chainName(chain []security.Certificate) string { |
| 1156 | exts := make([]string, len(chain)) |
| 1157 | for i, cert := range chain { |
| 1158 | exts[i] = cert.Extension |
| 1159 | } |
| 1160 | return strings.Join(exts, security.ChainSeparator) |
| 1161 | } |
| 1162 | |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 1163 | func base64VomEncode(i interface{}) (string, error) { |
| 1164 | buf := &bytes.Buffer{} |
| 1165 | closer := base64.NewEncoder(base64.URLEncoding, buf) |
Jungho Ahn | 5d1fe97 | 2015-04-27 17:51:32 -0700 | [diff] [blame] | 1166 | enc := vom.NewEncoder(closer) |
Suharsh Sivakumar | 12b089d | 2015-01-02 14:17:12 -0800 | [diff] [blame] | 1167 | if err := enc.Encode(i); err != nil { |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 1168 | return "", err |
| 1169 | } |
| 1170 | // Must close the base64 encoder to flush out any partially written |
| 1171 | // blocks. |
| 1172 | if err := closer.Close(); err != nil { |
| 1173 | return "", err |
| 1174 | } |
| 1175 | return buf.String(), nil |
| 1176 | } |
| 1177 | |
| 1178 | func base64VomDecode(s string, i interface{}) error { |
| 1179 | b, err := base64.URLEncoding.DecodeString(s) |
| 1180 | if err != nil { |
| 1181 | return err |
| 1182 | } |
Jungho Ahn | 5d1fe97 | 2015-04-27 17:51:32 -0700 | [diff] [blame] | 1183 | dec := vom.NewDecoder(bytes.NewBuffer(b)) |
Suharsh Sivakumar | 12b089d | 2015-01-02 14:17:12 -0800 | [diff] [blame] | 1184 | return dec.Decode(i) |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 1185 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1186 | |
| 1187 | type recvBlessingsService struct { |
| 1188 | principal security.Principal |
| 1189 | notify chan error |
| 1190 | token string |
| 1191 | } |
| 1192 | |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 1193 | func (r *recvBlessingsService) Grant(_ *context.T, call rpc.ServerCall, token string) error { |
Suharsh Sivakumar | 380bf34 | 2015-02-27 15:38:27 -0800 | [diff] [blame] | 1194 | b := call.GrantedBlessings() |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 1195 | if b.IsZero() { |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1196 | return fmt.Errorf("no blessings granted by sender") |
| 1197 | } |
| 1198 | if len(token) != len(r.token) { |
| 1199 | // A timing attack can be used to figure out the length |
| 1200 | // of the token, but then again, so can looking at the |
| 1201 | // source code. So, it's okay. |
| 1202 | return fmt.Errorf("blessings received from unexpected sender") |
| 1203 | } |
| 1204 | if subtle.ConstantTimeCompare([]byte(token), []byte(r.token)) != 1 { |
| 1205 | return fmt.Errorf("blessings received from unexpected sender") |
| 1206 | } |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 1207 | if flagRecvBlessingsSetDefault { |
| 1208 | if err := r.principal.BlessingStore().SetDefault(b); err != nil { |
| 1209 | return fmt.Errorf("failed to set blessings %v as default: %v", b, err) |
| 1210 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1211 | } |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 1212 | if pattern := security.BlessingPattern(flagRecvBlessingsForPeer); len(pattern) > 0 { |
| 1213 | if _, err := r.principal.BlessingStore().Set(b, pattern); err != nil { |
| 1214 | return fmt.Errorf("failed to set blessings %v for peers %v: %v", b, pattern, err) |
| 1215 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1216 | } |
| 1217 | if flagAddToRoots { |
| 1218 | if err := r.principal.AddToRoots(b); err != nil { |
| 1219 | return fmt.Errorf("failed to add blessings to recognized roots: %v", err) |
| 1220 | } |
| 1221 | } |
| 1222 | fmt.Println("Received blessings:", b) |
| 1223 | r.notify <- nil |
| 1224 | return nil |
| 1225 | } |
| 1226 | |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1227 | type granter struct { |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1228 | with security.Blessings |
| 1229 | extension string |
| 1230 | caveats []security.Caveat |
| 1231 | serverKey string |
| 1232 | } |
| 1233 | |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 1234 | func (g *granter) Grant(ctx *context.T, call security.Call) (security.Blessings, error) { |
Ankur | dda1649 | 2015-04-07 12:35:42 -0700 | [diff] [blame] | 1235 | server := call.RemoteBlessings() |
| 1236 | p := call.LocalPrincipal() |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1237 | if got := fmt.Sprintf("%v", server.PublicKey()); got != g.serverKey { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 1238 | // If the granter returns an error, the RPC framework should |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1239 | // abort the RPC before sending the request to the server. |
| 1240 | // Thus, there is no concern about leaking the token to an |
| 1241 | // imposter server. |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 1242 | 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] | 1243 | } |
Ankur | dda1649 | 2015-04-07 12:35:42 -0700 | [diff] [blame] | 1244 | return p.Bless(server.PublicKey(), g.with, g.extension, g.caveats[0], g.caveats[1:]...) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1245 | } |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 1246 | func (*granter) RPCCallOpt() {} |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1247 | |
Suharsh Sivakumar | ab21eb0 | 2015-04-01 12:58:20 -0700 | [diff] [blame] | 1248 | func blessOverNetwork(ctx *context.T, object string, granter *granter, remoteToken string) error { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 1249 | client := v23.GetClient(ctx) |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 1250 | // The receiver is being authorized based on the hash of its public key |
| 1251 | // (see Grant), so it should be fine to ignore the blessing names in the endpoint |
| 1252 | // (which are likely to not be recognized by the sender anyway). |
| 1253 | // |
| 1254 | // At worst, there is a privacy leak of the senders intent to send some |
| 1255 | // blessings. That could be addressed by making the full public key of |
| 1256 | // the recipeint available to the sender and using |
| 1257 | // options.ServerPublicKey instead of providing a "hash" of the |
| 1258 | // recipients public key and verifying in the Granter implementation. |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 1259 | if err := client.Call(ctx, object, "Grant", []interface{}{remoteToken}, nil, granter, options.SkipServerEndpointAuthorization{}); err != nil { |
| 1260 | return fmt.Errorf("failed to make RPC to %q: %v", object, err) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1261 | } |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 1262 | return nil |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 1263 | } |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 1264 | |
| 1265 | func caveatsFromFlags(expiry time.Duration, caveatsFlag *caveatsFlag) ([]security.Caveat, error) { |
| 1266 | caveats, err := caveatsFlag.Compile() |
| 1267 | if err != nil { |
| 1268 | return nil, fmt.Errorf("failed to parse caveats: %v", err) |
| 1269 | } |
Suharsh Sivakumar | 7e80c86 | 2015-05-11 15:33:55 -0700 | [diff] [blame] | 1270 | if expiry != 0 { |
Suharsh Sivakumar | 60b78e9 | 2015-04-23 21:36:49 -0700 | [diff] [blame] | 1271 | ecav, err := security.NewExpiryCaveat(time.Now().Add(expiry)) |
Asim Shankar | a0bba46 | 2015-02-20 22:50:51 -0800 | [diff] [blame] | 1272 | if err != nil { |
| 1273 | return nil, fmt.Errorf("failed to create expiration caveat: %v", err) |
| 1274 | } |
| 1275 | caveats = append(caveats, ecav) |
| 1276 | } |
| 1277 | return caveats, nil |
| 1278 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 1279 | |
| 1280 | // Circuitous route to get to the certificate chains. |
| 1281 | // See comments on why security.MarshalBlessings is discouraged. |
| 1282 | // Though, a better alternative is worth looking into. |
| 1283 | func blessings2wire(b security.Blessings) (security.WireBlessings, error) { |
| 1284 | var wire security.WireBlessings |
| 1285 | data, err := vom.Encode(b) |
| 1286 | if err != nil { |
| 1287 | return wire, err |
| 1288 | } |
| 1289 | err = vom.Decode(data, &wire) |
| 1290 | return wire, err |
| 1291 | } |