Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 5 | "crypto/rand" |
| 6 | "crypto/subtle" |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 7 | "encoding/base64" |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 8 | "errors" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "os" |
| 12 | "os/user" |
| 13 | "time" |
| 14 | |
Jiri Simsa | bc26d69 | 2014-11-19 18:30:55 -0800 | [diff] [blame] | 15 | "veyron.io/lib/cmdline" |
| 16 | profile "veyron.io/veyron/veyron/profiles/static" |
| 17 | vsecurity "veyron.io/veyron/veyron/security" |
| 18 | "veyron.io/veyron/veyron/services/identity" |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 19 | "veyron.io/veyron/veyron2" |
| 20 | "veyron.io/veyron/veyron2/ipc" |
| 21 | "veyron.io/veyron/veyron2/naming" |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 22 | "veyron.io/veyron/veyron2/rt" |
| 23 | "veyron.io/veyron/veyron2/security" |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 24 | "veyron.io/veyron/veyron2/vom" |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | // Flags for the "blessself" command |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 29 | flagBlessSelfFor time.Duration |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 30 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 31 | // Flags for the "bless" command |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 32 | flagBlessFor time.Duration |
| 33 | flagBlessWith string |
| 34 | flagBlessRemoteKey string |
| 35 | flagBlessRemoteToken string |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 36 | |
Ankur | 77c32ac | 2014-12-18 14:18:19 -0800 | [diff] [blame] | 37 | // Flags for the "fork" command |
| 38 | flagForkFor time.Duration |
| 39 | flagForkWith string |
| 40 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 41 | // Flags for the "seekblessings" command |
| 42 | flagSeekBlessingsFrom string |
| 43 | flagSeekBlessingsSetDefault bool |
| 44 | flagSeekBlessingsForPeer string |
Suharsh Sivakumar | a76dba6 | 2014-12-22 16:00:34 -0800 | [diff] [blame^] | 45 | flagSeekBlessingsBrowser bool |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 46 | |
| 47 | // Flags common to many commands |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 48 | flagAddToRoots bool |
| 49 | flagCreateOverwrite bool |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 50 | |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 51 | // Flags for the "recvblessings" command |
| 52 | flagRecvBlessingsSetDefault bool |
| 53 | flagRecvBlessingsForPeer string |
| 54 | |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 55 | cmdDump = &cmdline.Command{ |
| 56 | Name: "dump", |
| 57 | Short: "Dump out information about the principal", |
| 58 | Long: ` |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 59 | Prints out information about the principal specified by the environment |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 60 | that this tool is running in. |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 61 | `, |
| 62 | Run: func(cmd *cmdline.Command, args []string) error { |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 63 | runtime, err := rt.New() |
| 64 | if err != nil { |
| 65 | panic(err) |
| 66 | } |
| 67 | defer runtime.Cleanup() |
| 68 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 69 | p := runtime.Principal() |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 70 | fmt.Printf("Public key : %v\n", p.PublicKey()) |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 71 | fmt.Println("---------------- BlessingStore ----------------") |
| 72 | fmt.Printf("%v", p.BlessingStore().DebugString()) |
Ankur | 1615a7d | 2014-10-09 11:58:02 -0700 | [diff] [blame] | 73 | fmt.Println("---------------- BlessingRoots ----------------") |
| 74 | fmt.Printf("%v", p.Roots().DebugString()) |
| 75 | return nil |
| 76 | }, |
| 77 | } |
| 78 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 79 | cmdDumpBlessings = &cmdline.Command{ |
| 80 | Name: "dumpblessings", |
| 81 | Short: "Dump out information about the provided blessings", |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 82 | Long: ` |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 83 | Prints out information about the blessings (typically obtained from this tool) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 84 | encoded in the provided file. |
| 85 | `, |
| 86 | ArgsName: "<file>", |
| 87 | ArgsLong: ` |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 88 | <file> is the path to a file containing blessings typically obtained from |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 89 | this tool. - is used for STDIN. |
| 90 | `, |
| 91 | Run: func(cmd *cmdline.Command, args []string) error { |
| 92 | if len(args) != 1 { |
| 93 | return fmt.Errorf("requires exactly one argument, <file>, provided %d", len(args)) |
| 94 | } |
| 95 | blessings, err := decodeBlessings(args[0]) |
| 96 | if err != nil { |
| 97 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 98 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 99 | wire := security.MarshalBlessings(blessings) |
| 100 | fmt.Printf("Blessings : %v\n", blessings) |
| 101 | fmt.Printf("PublicKey : %v\n", blessings.PublicKey()) |
Asim Shankar | df88a2e | 2014-10-21 17:20:28 -0700 | [diff] [blame] | 102 | fmt.Printf("Certificate chains : %d\n", len(wire.CertificateChains)) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 103 | for idx, chain := range wire.CertificateChains { |
Asim Shankar | df88a2e | 2014-10-21 17:20:28 -0700 | [diff] [blame] | 104 | fmt.Printf("Chain #%d (%d certificates). Root certificate public key: %v\n", idx, len(chain), rootkey(chain)) |
| 105 | for certidx, cert := range chain { |
| 106 | fmt.Printf(" Certificate #%d: %v with ", certidx, cert.Extension) |
| 107 | switch n := len(cert.Caveats); n { |
| 108 | case 1: |
| 109 | fmt.Printf("1 caveat") |
| 110 | default: |
| 111 | fmt.Printf("%d caveats", n) |
| 112 | } |
| 113 | fmt.Println("") |
| 114 | for cavidx, cav := range cert.Caveats { |
| 115 | fmt.Printf(" (%d) %v\n", cavidx, &cav) |
| 116 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 117 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 118 | } |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 119 | return nil |
| 120 | }, |
| 121 | } |
| 122 | |
| 123 | cmdBlessSelf = &cmdline.Command{ |
| 124 | Name: "blessself", |
| 125 | Short: "Generate a self-signed blessing", |
| 126 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 127 | Returns a blessing with name <name> and self-signed by the principal specified |
| 128 | by the environment that this tool is running in. Optionally, the blessing can |
| 129 | be restricted with an expiry caveat specified using the --for flag. |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 130 | `, |
| 131 | ArgsName: "[<name>]", |
| 132 | ArgsLong: ` |
| 133 | <name> is the name used to create the self-signed blessing. If not |
| 134 | specified, a name will be generated based on the hostname of the |
| 135 | machine and the name of the user running this command. |
| 136 | `, |
| 137 | Run: func(cmd *cmdline.Command, args []string) error { |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 138 | var name string |
| 139 | switch len(args) { |
| 140 | case 0: |
| 141 | name = defaultBlessingName() |
| 142 | case 1: |
| 143 | name = args[0] |
| 144 | default: |
| 145 | return fmt.Errorf("requires at most one argument, provided %d", len(args)) |
| 146 | } |
| 147 | |
| 148 | var caveats []security.Caveat |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 149 | if flagBlessSelfFor != 0 { |
| 150 | cav, err := security.ExpiryCaveat(time.Now().Add(flagBlessSelfFor)) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 151 | if err != nil { |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 152 | return fmt.Errorf("failed to create expiration Caveat: %v", err) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 153 | } |
| 154 | caveats = append(caveats, cav) |
| 155 | } |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 156 | |
| 157 | runtime, err := rt.New() |
| 158 | if err != nil { |
| 159 | panic(err) |
| 160 | } |
| 161 | defer runtime.Cleanup() |
| 162 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 163 | blessing, err := runtime.Principal().BlessSelf(name, caveats...) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 164 | if err != nil { |
| 165 | return fmt.Errorf("failed to create self-signed blessing for name %q: %v", name, err) |
| 166 | } |
| 167 | |
| 168 | return dumpBlessings(blessing) |
| 169 | }, |
| 170 | } |
| 171 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 172 | cmdBless = &cmdline.Command{ |
| 173 | Name: "bless", |
| 174 | Short: "Bless another principal", |
| 175 | Long: ` |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 176 | Bless another principal. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 177 | |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 178 | 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] | 179 | will be extended is the default one from the blesser's store, or specified by |
| 180 | the --with flag. Caveats on the blessing are controlled via the --for flag. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 181 | |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 182 | For example, let's say a principal "alice" wants to bless another principal "bob" |
| 183 | as "alice/friend", the invocation would be: |
| 184 | VEYRON_CREDENTIALS=<path to alice> principal bless <path to bob> friend |
| 185 | and this will dump the blessing to STDOUT. |
| 186 | |
| 187 | With the --remote_key and --remote_token flags, this command can be used to |
| 188 | bless a principal on a remote machine as well. In this case, the blessing is |
| 189 | not dumped to STDOUT but sent to the remote end. Use 'principal help |
| 190 | recvblessings' for more details on that. |
| 191 | `, |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 192 | ArgsName: "<principal to bless> <extension>", |
| 193 | ArgsLong: ` |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 194 | <principal to bless> represents the principal to be blessed (i.e., whose public |
| 195 | key will be provided with a name). This can be either: |
| 196 | (a) The directory containing credentials for that principal, |
| 197 | OR |
| 198 | (b) The filename (- for STDIN) containing any other blessings of that |
| 199 | principal, |
| 200 | OR |
| 201 | (c) The object name produced by the 'recvblessings' command of this tool |
| 202 | running on behalf of another principal (if the --remote_key and |
| 203 | --remote_token flags are specified). |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 204 | |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 205 | <extension> is the string extension that will be applied to create the |
| 206 | blessing. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 207 | `, |
| 208 | Run: func(cmd *cmdline.Command, args []string) error { |
| 209 | if len(args) != 2 { |
| 210 | return fmt.Errorf("require exactly two arguments, provided %d", len(args)) |
| 211 | } |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 212 | |
| 213 | runtime, err := rt.New() |
| 214 | if err != nil { |
| 215 | panic(err) |
| 216 | } |
| 217 | defer runtime.Cleanup() |
| 218 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 219 | p := runtime.Principal() |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 220 | |
| 221 | var with security.Blessings |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 222 | var caveats []security.Caveat |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 223 | if len(flagBlessWith) > 0 { |
| 224 | if with, err = decodeBlessings(flagBlessWith); err != nil { |
| 225 | return fmt.Errorf("failed to read blessings from --with=%q: %v", flagBlessWith, err) |
| 226 | } |
| 227 | } else { |
| 228 | with = p.BlessingStore().Default() |
| 229 | } |
| 230 | |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 231 | if c, err := security.ExpiryCaveat(time.Now().Add(flagBlessFor)); err != nil { |
| 232 | return fmt.Errorf("failed to create ExpiryCaveat: %v", err) |
| 233 | } else { |
| 234 | caveats = append(caveats, c) |
| 235 | } |
| 236 | // TODO(ashankar,ataly,suharshs): Work out how to add additional caveats, like maybe |
| 237 | // revocation, method etc. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 238 | tobless, extension := args[0], args[1] |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 239 | if (len(flagBlessRemoteKey) == 0) != (len(flagBlessRemoteToken) == 0) { |
| 240 | return fmt.Errorf("either both --remote_key and --remote_token should be set, or neither should") |
| 241 | } |
| 242 | if len(flagBlessRemoteKey) > 0 { |
| 243 | // Send blessings to a "server" started by a "recvblessings" command |
| 244 | granter := &granter{p, with, extension, caveats, flagBlessRemoteKey} |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 245 | return sendBlessings(runtime, tobless, granter, flagBlessRemoteToken) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 246 | } |
| 247 | // Blessing a principal whose key is available locally. |
| 248 | var key security.PublicKey |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 249 | if finfo, err := os.Stat(tobless); err == nil && finfo.IsDir() { |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 250 | other, err := vsecurity.LoadPersistentPrincipal(tobless, nil) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 251 | if err != nil { |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 252 | if other, err = vsecurity.CreatePersistentPrincipal(tobless, nil); err != nil { |
| 253 | return fmt.Errorf("failed to read principal in directory %q: %v", tobless, err) |
| 254 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 255 | } |
| 256 | key = other.PublicKey() |
| 257 | } else if other, err := decodeBlessings(tobless); err != nil { |
| 258 | return fmt.Errorf("failed to decode blessings in %q: %v", tobless, err) |
| 259 | } else { |
| 260 | key = other.PublicKey() |
| 261 | } |
| 262 | |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 263 | blessings, err := p.Bless(key, with, extension, caveats[0], caveats[1:]...) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 264 | if err != nil { |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 265 | return fmt.Errorf("Bless(%v, %v, %q, ...) failed: %v", key, with, extension, err) |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 266 | } |
| 267 | return dumpBlessings(blessings) |
| 268 | }, |
| 269 | } |
| 270 | |
| 271 | cmdStoreForPeer = &cmdline.Command{ |
| 272 | Name: "forpeer", |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 273 | Short: "Return blessings marked for the provided peer", |
| 274 | Long: ` |
| 275 | Returns blessings that are marked for the provided peer in the |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 276 | BlessingStore specified by the environment that this tool is |
| 277 | running in. |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 278 | `, |
| 279 | ArgsName: "[<peer_1> ... <peer_k>]", |
| 280 | ArgsLong: ` |
| 281 | <peer_1> ... <peer_k> are the (human-readable string) blessings bound |
| 282 | to the peer. The returned blessings are marked with a pattern that is |
| 283 | matched by at least one of these. If no arguments are specified, |
| 284 | store.forpeer returns the blessings that are marked for all peers (i.e., |
| 285 | blessings set on the store with the "..." pattern). |
| 286 | `, |
| 287 | Run: func(cmd *cmdline.Command, args []string) error { |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 288 | runtime, err := rt.New() |
| 289 | if err != nil { |
| 290 | panic(err) |
| 291 | } |
| 292 | defer runtime.Cleanup() |
| 293 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 294 | return dumpBlessings(runtime.Principal().BlessingStore().ForPeer(args...)) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 295 | }, |
| 296 | } |
| 297 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 298 | cmdStoreDefault = &cmdline.Command{ |
| 299 | Name: "default", |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 300 | Short: "Return blessings marked as default", |
| 301 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 302 | Returns blessings that are marked as default in the BlessingStore specified by |
| 303 | the environment that this tool is running in. |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 304 | `, |
| 305 | Run: func(cmd *cmdline.Command, args []string) error { |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 306 | runtime, err := rt.New() |
| 307 | if err != nil { |
| 308 | panic(err) |
| 309 | } |
| 310 | defer runtime.Cleanup() |
| 311 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 312 | return dumpBlessings(runtime.Principal().BlessingStore().Default()) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 313 | }, |
| 314 | } |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 315 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 316 | cmdStoreSet = &cmdline.Command{ |
| 317 | Name: "set", |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 318 | Short: "Set provided blessings for peer", |
| 319 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 320 | Marks the provided blessings to be shared with the provided peers on the |
| 321 | BlessingStore specified by the environment that this tool is running in. |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 322 | |
| 323 | 'set b pattern' marks the intention to reveal b to peers who |
| 324 | present blessings of their own matching 'pattern'. |
| 325 | |
| 326 | 'set nil pattern' can be used to remove the blessings previously |
| 327 | associated with the pattern (by a prior 'set' command). |
| 328 | |
| 329 | It is an error to call 'store.set' with blessings whose public |
| 330 | key does not match the public key of this principal specified |
| 331 | by the environment. |
| 332 | `, |
| 333 | ArgsName: "<file> <pattern>", |
| 334 | ArgsLong: ` |
| 335 | <file> is the path to a file containing a blessing typically obtained |
| 336 | from this tool. - is used for STDIN. |
| 337 | |
| 338 | <pattern> is the BlessingPattern used to identify peers with whom this |
| 339 | blessing can be shared with. |
| 340 | `, |
| 341 | Run: func(cmd *cmdline.Command, args []string) error { |
| 342 | if len(args) != 2 { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 343 | return fmt.Errorf("requires exactly two arguments <file>, <pattern>, provided %d", len(args)) |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 344 | } |
| 345 | blessings, err := decodeBlessings(args[0]) |
| 346 | if err != nil { |
| 347 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 348 | } |
| 349 | pattern := security.BlessingPattern(args[1]) |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 350 | |
| 351 | runtime, err := rt.New() |
| 352 | if err != nil { |
| 353 | panic(err) |
| 354 | } |
| 355 | defer runtime.Cleanup() |
| 356 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 357 | p := runtime.Principal() |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 358 | if _, err := p.BlessingStore().Set(blessings, pattern); err != nil { |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 359 | return fmt.Errorf("failed to set blessings %v for peers %v: %v", blessings, pattern, err) |
| 360 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 361 | if flagAddToRoots { |
| 362 | if err := p.AddToRoots(blessings); err != nil { |
| 363 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 364 | } |
| 365 | } |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 366 | return nil |
| 367 | }, |
| 368 | } |
| 369 | |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 370 | cmdStoreAddToRoots = &cmdline.Command{ |
| 371 | Name: "addtoroots", |
| 372 | Short: "Add provided blessings to root set", |
| 373 | Long: ` |
| 374 | Adds the provided blessings to the set of trusted roots for this principal. |
| 375 | |
| 376 | 'addtoroots b' adds blessings b to the trusted root set. |
| 377 | |
| 378 | For example, to make the principal in credentials directory A trust the |
| 379 | root of the default blessing in credentials directory B: |
| 380 | principal -veyron.credentials=B bless A some_extension | |
| 381 | principal -veyron.credentials=A store addtoroots - |
| 382 | |
| 383 | The extension 'some_extension' has no effect in the command above. |
| 384 | `, |
| 385 | ArgsName: "<file>", |
| 386 | ArgsLong: ` |
| 387 | <file> is the path to a file containing a blessing typically obtained |
| 388 | from this tool. - is used for STDIN. |
| 389 | `, |
| 390 | Run: func(cmd *cmdline.Command, args []string) error { |
| 391 | if len(args) != 1 { |
| 392 | return fmt.Errorf("requires exactly one argument <file>, provided %d", len(args)) |
| 393 | } |
| 394 | blessings, err := decodeBlessings(args[0]) |
| 395 | if err != nil { |
| 396 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 397 | } |
| 398 | |
| 399 | runtime, err := rt.New() |
| 400 | if err != nil { |
| 401 | panic(err) |
| 402 | } |
| 403 | defer runtime.Cleanup() |
| 404 | |
| 405 | p := runtime.Principal() |
| 406 | if err := p.AddToRoots(blessings); err != nil { |
| 407 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 408 | } |
| 409 | return nil |
| 410 | }, |
| 411 | } |
| 412 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 413 | cmdStoreSetDefault = &cmdline.Command{ |
| 414 | Name: "setdefault", |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 415 | Short: "Set provided blessings as default", |
| 416 | Long: ` |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 417 | Sets the provided blessings as default in the BlessingStore specified by the |
| 418 | environment that this tool is running in. |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 419 | |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 420 | It is an error to call 'store.setdefault' with blessings whose public key does |
| 421 | not match the public key of the principal specified by the environment. |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 422 | `, |
| 423 | ArgsName: "<file>", |
| 424 | ArgsLong: ` |
| 425 | <file> is the path to a file containing a blessing typically obtained from |
| 426 | this tool. - is used for STDIN. |
| 427 | `, |
| 428 | Run: func(cmd *cmdline.Command, args []string) error { |
| 429 | if len(args) != 1 { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 430 | return fmt.Errorf("requires exactly one argument, <file>, provided %d", len(args)) |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 431 | } |
| 432 | blessings, err := decodeBlessings(args[0]) |
| 433 | if err != nil { |
| 434 | return fmt.Errorf("failed to decode provided blessings: %v", err) |
| 435 | } |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 436 | |
| 437 | runtime, err := rt.New() |
| 438 | if err != nil { |
| 439 | panic(err) |
| 440 | } |
| 441 | defer runtime.Cleanup() |
| 442 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 443 | p := runtime.Principal() |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 444 | if err := p.BlessingStore().SetDefault(blessings); err != nil { |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 445 | return fmt.Errorf("failed to set blessings %v as default: %v", blessings, err) |
| 446 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 447 | if flagAddToRoots { |
| 448 | if err := p.AddToRoots(blessings); err != nil { |
| 449 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 450 | } |
| 451 | } |
| 452 | return nil |
| 453 | }, |
| 454 | } |
| 455 | |
| 456 | cmdCreate = &cmdline.Command{ |
| 457 | Name: "create", |
| 458 | Short: "Create a new principal and persist it into a directory", |
| 459 | Long: ` |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 460 | 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] | 461 | to the provided directory. The same directory can then be used to set the |
| 462 | VEYRON_CREDENTIALS environment variable for other veyron applications. |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 463 | |
| 464 | The operation fails if the directory already contains a principal. In this case |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 465 | 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] | 466 | new principal. |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 467 | `, |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 468 | ArgsName: "<directory> <blessing>", |
| 469 | ArgsLong: ` |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 470 | <directory> is the directory to which the new principal will be persisted. |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 471 | <blessing> is the self-blessed blessing that the principal will be setup to use by default. |
| 472 | `, |
| 473 | Run: func(cmd *cmdline.Command, args []string) error { |
| 474 | if len(args) != 2 { |
| 475 | return fmt.Errorf("requires exactly two arguments: <directory> and <blessing>, provided %d", len(args)) |
| 476 | } |
| 477 | dir, name := args[0], args[1] |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 478 | if flagCreateOverwrite { |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 479 | if err := os.RemoveAll(dir); err != nil { |
gauthamt | b7bb39b | 2014-11-10 11:40:41 -0800 | [diff] [blame] | 480 | return err |
| 481 | } |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 482 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 483 | p, err := vsecurity.CreatePersistentPrincipal(dir, nil) |
Suharsh Sivakumar | aca1c32 | 2014-10-21 11:27:32 -0700 | [diff] [blame] | 484 | if err != nil { |
| 485 | return err |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 486 | } |
| 487 | blessings, err := p.BlessSelf(name) |
| 488 | if err != nil { |
| 489 | return fmt.Errorf("BlessSelf(%q) failed: %v", name, err) |
| 490 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 491 | if err := vsecurity.SetDefaultBlessings(p, blessings); err != nil { |
| 492 | 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] | 493 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 494 | return nil |
| 495 | }, |
| 496 | } |
| 497 | |
| 498 | cmdFork = &cmdline.Command{ |
| 499 | Name: "fork", |
| 500 | Short: "Fork a new principal from the principal that this tool is running as and persist it into a directory", |
| 501 | Long: ` |
| 502 | Creates a new principal with a blessing from the principal specified by the |
| 503 | environment that this tool is running in, and writes it out to the provided |
| 504 | directory. The blessing that will be extended is the default one from the |
| 505 | blesser's store, or specified by the --with flag. Caveats on the blessing |
| 506 | are controlled via the --for flag. The blessing is marked as default and |
| 507 | shareable with all peers on the new principal's blessing store. |
| 508 | |
| 509 | The operation fails if the directory already contains a principal. In this case |
| 510 | the --overwrite flag can be provided to clear the directory and write out the |
| 511 | forked principal. |
| 512 | `, |
| 513 | ArgsName: "<directory> <extension>", |
| 514 | ArgsLong: ` |
| 515 | <directory> is the directory to which the forked principal will be persisted. |
| 516 | <extension> is the extension under which the forked principal is blessed. |
| 517 | `, |
| 518 | Run: func(cmd *cmdline.Command, args []string) error { |
| 519 | if len(args) != 2 { |
| 520 | 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] | 521 | } |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 522 | dir, extension := args[0], args[1] |
| 523 | |
| 524 | if flagCreateOverwrite { |
| 525 | if err := os.RemoveAll(dir); err != nil { |
| 526 | return err |
| 527 | } |
| 528 | } |
| 529 | p, err := vsecurity.CreatePersistentPrincipal(dir, nil) |
| 530 | if err != nil { |
| 531 | return err |
| 532 | } |
| 533 | |
| 534 | runtime, err := rt.New() |
| 535 | if err != nil { |
| 536 | return err |
| 537 | } |
| 538 | defer runtime.Cleanup() |
| 539 | |
| 540 | var ( |
| 541 | with security.Blessings |
| 542 | caveats []security.Caveat |
| 543 | ) |
Ankur | 77c32ac | 2014-12-18 14:18:19 -0800 | [diff] [blame] | 544 | if len(flagForkWith) > 0 { |
| 545 | if with, err = decodeBlessings(flagForkWith); err != nil { |
| 546 | return fmt.Errorf("failed to read blessings from --with=%q: %v", flagForkWith, err) |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 547 | } |
| 548 | } else { |
| 549 | with = runtime.Principal().BlessingStore().Default() |
| 550 | } |
Ankur | 77c32ac | 2014-12-18 14:18:19 -0800 | [diff] [blame] | 551 | if c, err := security.ExpiryCaveat(time.Now().Add(flagForkFor)); err != nil { |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 552 | return fmt.Errorf("failed to create ExpiryCaveat: %v", err) |
| 553 | } else { |
| 554 | caveats = append(caveats, c) |
| 555 | } |
| 556 | // TODO(ashankar,ataly,suharshs): Work out how to add additional caveats, like maybe |
| 557 | // revocation, method etc. |
| 558 | |
| 559 | key := p.PublicKey() |
| 560 | blessings, err := runtime.Principal().Bless(key, with, extension, caveats[0], caveats[1:]...) |
| 561 | if err != nil { |
| 562 | return fmt.Errorf("Bless(%v, %v, %q, ...) failed: %v", key, with, extension, err) |
| 563 | } |
| 564 | if err := vsecurity.SetDefaultBlessings(p, blessings); err != nil { |
| 565 | 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] | 566 | } |
Ankur | 8d30412 | 2014-10-07 10:43:43 -0700 | [diff] [blame] | 567 | return nil |
| 568 | }, |
| 569 | } |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 570 | |
| 571 | cmdSeekBlessings = &cmdline.Command{ |
| 572 | Name: "seekblessings", |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 573 | Short: "Seek blessings from a web-based Veyron blessing service", |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 574 | Long: ` |
| 575 | Seeks blessings from a web-based Veyron blesser which |
| 576 | requires the caller to first authenticate with Google using OAuth. Simply |
| 577 | run the command to see what happens. |
| 578 | |
Asim Shankar | 1789b8a | 2014-10-31 17:31:41 -0700 | [diff] [blame] | 579 | The blessings are sought for the principal specified by the environment that |
| 580 | this tool is running in. |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 581 | |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 582 | The blessings obtained are set as default, unless the --set_default flag is |
| 583 | set to true, and are also set for sharing with all peers, unless a more |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 584 | specific peer pattern is provided using the --for_peer flag. |
| 585 | `, |
| 586 | Run: func(cmd *cmdline.Command, args []string) error { |
Asim Shankar | 08ab389 | 2014-10-16 12:04:07 -0700 | [diff] [blame] | 587 | // Initialize the runtime first so that any local errors are reported |
| 588 | // before the HTTP roundtrips for obtaining the macaroon begin. |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 589 | runtime, err := rt.New() |
| 590 | if err != nil { |
| 591 | panic(err) |
| 592 | } |
| 593 | defer runtime.Cleanup() |
| 594 | |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 595 | blessedChan := make(chan string) |
| 596 | defer close(blessedChan) |
Suharsh Sivakumar | a76dba6 | 2014-12-22 16:00:34 -0800 | [diff] [blame^] | 597 | macaroonChan, err := getMacaroonForBlessRPC(flagSeekBlessingsFrom, blessedChan, flagSeekBlessingsBrowser) |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 598 | if err != nil { |
| 599 | return fmt.Errorf("failed to get macaroon from Veyron blesser: %v", err) |
| 600 | } |
| 601 | macaroon := <-macaroonChan |
| 602 | service := <-macaroonChan |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 603 | ctx, cancel := runtime.NewContext().WithTimeout(time.Minute) |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 604 | defer cancel() |
| 605 | |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 606 | var reply security.WireBlessings |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 607 | reply, err = identity.MacaroonBlesserClient(service).Bless(ctx, macaroon) |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 608 | if err != nil { |
| 609 | return fmt.Errorf("failed to get blessing from %q: %v", service, err) |
| 610 | } |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 611 | blessings, err := security.NewBlessings(reply) |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 612 | if err != nil { |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 613 | return fmt.Errorf("failed to construct Blessings object from response: %v", err) |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 614 | } |
| 615 | blessedChan <- fmt.Sprint(blessings) |
| 616 | // Wait for getTokenForBlessRPC to clean up: |
| 617 | <-macaroonChan |
| 618 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 619 | if flagSeekBlessingsSetDefault { |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 620 | if err := runtime.Principal().BlessingStore().SetDefault(blessings); err != nil { |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 621 | return fmt.Errorf("failed to set blessings %v as default: %v", blessings, err) |
| 622 | } |
| 623 | } |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 624 | if pattern := security.BlessingPattern(flagSeekBlessingsForPeer); len(pattern) > 0 { |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 625 | if _, err := runtime.Principal().BlessingStore().Set(blessings, pattern); err != nil { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 626 | return fmt.Errorf("failed to set blessings %v for peers %v: %v", blessings, pattern, err) |
| 627 | } |
| 628 | } |
| 629 | if flagAddToRoots { |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 630 | if err := runtime.Principal().AddToRoots(blessings); err != nil { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 631 | return fmt.Errorf("AddToRoots failed: %v", err) |
| 632 | } |
Ankur | 1d46f55 | 2014-10-09 12:13:31 -0700 | [diff] [blame] | 633 | } |
| 634 | return dumpBlessings(blessings) |
| 635 | }, |
| 636 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 637 | |
| 638 | cmdRecvBlessings = &cmdline.Command{ |
| 639 | Name: "recvblessings", |
| 640 | Short: "Receive blessings sent by another principal and use them as the default", |
| 641 | Long: ` |
| 642 | Allow another principal (likely a remote process) to bless this one. |
| 643 | |
| 644 | This command sets up the invoker (this process) to wait for a blessing |
| 645 | from another invocation of this tool (remote process) and prints out the |
| 646 | command to be run as the remote principal. |
| 647 | |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 648 | The received blessings are set as default, unless the --set_default flag is |
| 649 | set to true, and are also set for sharing with all peers, unless a more |
| 650 | specific peer pattern is provided using the --for_peer flag. |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 651 | |
| 652 | TODO(ashankar,cnicolaou): Make this next paragraph possible! Requires |
| 653 | the ability to obtain the proxied endpoint. |
| 654 | |
| 655 | Typically, this command should require no arguments. |
| 656 | However, if the sender and receiver are on different network domains, it may |
| 657 | make sense to use the --veyron.proxy flag: |
Robin Thellend | 8fea01c | 2014-12-11 13:48:10 -0800 | [diff] [blame] | 658 | principal --veyron.proxy=proxy recvblessings |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 659 | |
| 660 | The command to be run at the sender is of the form: |
| 661 | principal bless --remote_key=KEY --remote_token=TOKEN ADDRESS |
| 662 | |
| 663 | The --remote_key flag is used to by the sender to "authenticate" the receiver, |
| 664 | ensuring it blesses the intended recipient and not any attacker that may have |
| 665 | taken over the address. |
| 666 | |
| 667 | The --remote_token flag is used by the sender to authenticate itself to the |
| 668 | receiver. This helps ensure that the receiver rejects blessings from senders |
| 669 | who just happened to guess the network address of the 'recvblessings' |
| 670 | invocation. |
| 671 | `, |
| 672 | Run: func(cmd *cmdline.Command, args []string) error { |
| 673 | if len(args) != 0 { |
| 674 | return fmt.Errorf("command accepts no arguments") |
| 675 | } |
Matt Rosencrantz | 574d5e1 | 2014-11-26 10:01:37 -0800 | [diff] [blame] | 676 | |
| 677 | runtime, err := rt.New() |
| 678 | if err != nil { |
| 679 | panic(err) |
| 680 | } |
| 681 | defer runtime.Cleanup() |
| 682 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 683 | server, err := runtime.NewServer() |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 684 | if err != nil { |
| 685 | return fmt.Errorf("failed to create server to listen for blessings: %v", err) |
| 686 | } |
| 687 | defer server.Stop() |
Cosmos Nicolaou | 28dabfc | 2014-12-15 22:51:07 -0800 | [diff] [blame] | 688 | eps, err := server.Listen(profile.ListenSpec) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 689 | if err != nil { |
| 690 | return fmt.Errorf("failed to setup listening: %v", err) |
| 691 | } |
| 692 | var token [24]byte |
| 693 | if _, err := rand.Read(token[:]); err != nil { |
| 694 | return fmt.Errorf("unable to generate token: %v", err) |
| 695 | } |
| 696 | service := &recvBlessingsService{ |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 697 | principal: runtime.Principal(), |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 698 | token: base64.URLEncoding.EncodeToString(token[:]), |
| 699 | notify: make(chan error), |
| 700 | } |
| 701 | if err := server.Serve("", service, allowAnyone{}); err != nil { |
| 702 | return fmt.Errorf("failed to setup service: %v", err) |
| 703 | } |
| 704 | // Proposed name: |
| 705 | extension := fmt.Sprintf("extension%d", int(token[0])<<16|int(token[1])<<8|int(token[2])) |
| 706 | fmt.Println("Run the following command on behalf of the principal that will send blessings:") |
| 707 | fmt.Println("You may want to adjust flags affecting the caveats on this blessing, for example using") |
| 708 | fmt.Println("the --for flag, or change the extension to something more meaningful") |
| 709 | fmt.Println() |
Cosmos Nicolaou | 28dabfc | 2014-12-15 22:51:07 -0800 | [diff] [blame] | 710 | fmt.Printf("principal bless --remote_key=%v --remote_token=%v %v %v\n", runtime.Principal().PublicKey(), service.token, naming.JoinAddressName(eps[0].String(), ""), extension) |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 711 | fmt.Println() |
| 712 | fmt.Println("...waiting for sender..") |
| 713 | return <-service.notify |
| 714 | }, |
| 715 | } |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 716 | ) |
| 717 | |
| 718 | func main() { |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 719 | cmdBlessSelf.Flags.DurationVar(&flagBlessSelfFor, "for", 0, "Duration of blessing validity (zero means no that the blessing is always valid)") |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 720 | |
Ankur | 77c32ac | 2014-12-18 14:18:19 -0800 | [diff] [blame] | 721 | cmdFork.Flags.BoolVar(&flagCreateOverwrite, "overwrite", false, "If true, any existing principal data in the directory will be overwritten") |
| 722 | cmdFork.Flags.DurationVar(&flagForkFor, "for", 0, "Duration for which the forked blessing is valid (zero means no that the blessing is always valid)") |
| 723 | cmdFork.Flags.StringVar(&flagForkWith, "with", "", "Path to file containing blessing to extend") |
| 724 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 725 | cmdBless.Flags.DurationVar(&flagBlessFor, "for", time.Minute, "Duration of blessing validity") |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 726 | cmdBless.Flags.StringVar(&flagBlessWith, "with", "", "Path to file containing blessing to extend") |
| 727 | cmdBless.Flags.StringVar(&flagBlessRemoteKey, "remote_key", "", "Public key of the remote principal to bless (obtained from the 'recvblessings' command run by the remote principal") |
| 728 | cmdBless.Flags.StringVar(&flagBlessRemoteToken, "remote_token", "", "Token provided by principal running the 'recvblessings' command") |
| 729 | |
Robin Thellend | 8fea01c | 2014-12-11 13:48:10 -0800 | [diff] [blame] | 730 | cmdSeekBlessings.Flags.StringVar(&flagSeekBlessingsFrom, "from", "https://auth.dev.v.io:8125/google", "URL to use to begin the seek blessings process") |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 731 | cmdSeekBlessings.Flags.BoolVar(&flagSeekBlessingsSetDefault, "set_default", true, "If true, the blessings obtained will be set as the default blessing in the store") |
| 732 | 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^] | 733 | cmdSeekBlessings.Flags.BoolVar(&flagSeekBlessingsBrowser, "browser", true, "If false, the seekblessings command will not open the browser and only print the url to visit.") |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 734 | 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] | 735 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 736 | cmdStoreSet.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] | 737 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 738 | cmdStoreSetDefault.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] | 739 | |
Ankur | 4704f5f | 2014-10-23 12:40:54 -0700 | [diff] [blame] | 740 | 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] | 741 | |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 742 | cmdRecvBlessings.Flags.BoolVar(&flagRecvBlessingsSetDefault, "set_default", true, "If true, the blessings received will be set as the default blessing in the store") |
| 743 | 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") |
| 744 | |
Asim Shankar | 66c52f9 | 2014-10-15 23:39:10 -0700 | [diff] [blame] | 745 | cmdStore := &cmdline.Command{ |
| 746 | Name: "store", |
| 747 | Short: "Manipulate and inspect the principal's blessing store", |
| 748 | Long: ` |
| 749 | Commands to manipulate and inspect the blessing store of the principal. |
| 750 | |
| 751 | All blessings are printed to stdout using base64-VOM-encoding |
| 752 | `, |
Mike Burrows | 367515e | 2014-12-02 11:30:18 -0800 | [diff] [blame] | 753 | Children: []*cmdline.Command{cmdStoreDefault, cmdStoreSetDefault, cmdStoreForPeer, cmdStoreSet, cmdStoreAddToRoots}, |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 754 | } |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 755 | |
| 756 | (&cmdline.Command{ |
| 757 | Name: "principal", |
| 758 | Short: "Create and manage veyron principals", |
| 759 | Long: ` |
| 760 | The principal tool helps create and manage blessings and the set of trusted |
| 761 | roots bound to a principal. |
| 762 | |
| 763 | All objects are printed using base64-VOM-encoding. |
| 764 | `, |
Ankur | c24ff42 | 2014-12-16 17:59:26 -0800 | [diff] [blame] | 765 | Children: []*cmdline.Command{cmdCreate, cmdFork, cmdSeekBlessings, cmdRecvBlessings, cmdDump, cmdDumpBlessings, cmdBlessSelf, cmdBless, cmdStore}, |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 766 | }).Main() |
| 767 | } |
| 768 | |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 769 | func decodeBlessings(fname string) (security.Blessings, error) { |
| 770 | var wire security.WireBlessings |
| 771 | if err := decode(fname, &wire); err != nil { |
| 772 | return nil, err |
| 773 | } |
| 774 | return security.NewBlessings(wire) |
| 775 | } |
| 776 | |
| 777 | func dumpBlessings(blessings security.Blessings) error { |
| 778 | if blessings == nil { |
| 779 | return errors.New("no blessings found") |
| 780 | } |
Ankur | ee0aa81 | 2014-11-14 10:56:52 -0800 | [diff] [blame] | 781 | str, err := base64VomEncode(security.MarshalBlessings(blessings)) |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 782 | if err != nil { |
| 783 | return fmt.Errorf("base64-VOM encoding failed: %v", err) |
| 784 | } |
| 785 | fmt.Println(str) |
| 786 | return nil |
| 787 | } |
| 788 | |
| 789 | func read(fname string) (string, error) { |
| 790 | if len(fname) == 0 { |
| 791 | return "", nil |
| 792 | } |
| 793 | f := os.Stdin |
| 794 | if fname != "-" { |
| 795 | var err error |
| 796 | if f, err = os.Open(fname); err != nil { |
| 797 | return "", fmt.Errorf("failed to open %q: %v", fname, err) |
| 798 | } |
| 799 | } |
| 800 | defer f.Close() |
| 801 | var buf bytes.Buffer |
| 802 | if _, err := io.Copy(&buf, f); err != nil { |
| 803 | return "", fmt.Errorf("failed to read %q: %v", fname, err) |
| 804 | } |
| 805 | return buf.String(), nil |
| 806 | } |
| 807 | |
| 808 | func decode(fname string, val interface{}) error { |
| 809 | str, err := read(fname) |
| 810 | if err != nil { |
| 811 | return err |
| 812 | } |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 813 | if err := base64VomDecode(str, val); err != nil || val == nil { |
Ankur | cf6a89f | 2014-10-06 18:33:03 -0700 | [diff] [blame] | 814 | return fmt.Errorf("failed to decode %q: %v", fname, err) |
| 815 | } |
| 816 | return nil |
| 817 | } |
| 818 | |
| 819 | func defaultBlessingName() string { |
| 820 | var name string |
| 821 | if user, _ := user.Current(); user != nil && len(user.Username) > 0 { |
| 822 | name = user.Username |
| 823 | } else { |
| 824 | name = "anonymous" |
| 825 | } |
| 826 | if host, _ := os.Hostname(); len(host) > 0 { |
| 827 | name = name + "@" + host |
| 828 | } |
| 829 | return name |
| 830 | } |
Asim Shankar | df88a2e | 2014-10-21 17:20:28 -0700 | [diff] [blame] | 831 | |
| 832 | func rootkey(chain []security.Certificate) string { |
| 833 | if len(chain) == 0 { |
| 834 | return "<empty certificate chain>" |
| 835 | } |
| 836 | key, err := security.UnmarshalPublicKey(chain[0].PublicKey) |
| 837 | if err != nil { |
| 838 | return fmt.Sprintf("<invalid PublicKey: %v>", err) |
| 839 | } |
| 840 | return fmt.Sprintf("%v", key) |
| 841 | } |
Asim Shankar | b3a82ba | 2014-10-29 11:41:27 -0700 | [diff] [blame] | 842 | |
| 843 | func base64VomEncode(i interface{}) (string, error) { |
| 844 | buf := &bytes.Buffer{} |
| 845 | closer := base64.NewEncoder(base64.URLEncoding, buf) |
| 846 | if err := vom.NewEncoder(closer).Encode(i); err != nil { |
| 847 | return "", err |
| 848 | } |
| 849 | // Must close the base64 encoder to flush out any partially written |
| 850 | // blocks. |
| 851 | if err := closer.Close(); err != nil { |
| 852 | return "", err |
| 853 | } |
| 854 | return buf.String(), nil |
| 855 | } |
| 856 | |
| 857 | func base64VomDecode(s string, i interface{}) error { |
| 858 | b, err := base64.URLEncoding.DecodeString(s) |
| 859 | if err != nil { |
| 860 | return err |
| 861 | } |
| 862 | return vom.NewDecoder(bytes.NewBuffer(b)).Decode(i) |
| 863 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 864 | |
| 865 | type recvBlessingsService struct { |
| 866 | principal security.Principal |
| 867 | notify chan error |
| 868 | token string |
| 869 | } |
| 870 | |
| 871 | func (r *recvBlessingsService) Grant(call ipc.ServerCall, token string) error { |
| 872 | b := call.Blessings() |
| 873 | if b == nil { |
| 874 | return fmt.Errorf("no blessings granted by sender") |
| 875 | } |
| 876 | if len(token) != len(r.token) { |
| 877 | // A timing attack can be used to figure out the length |
| 878 | // of the token, but then again, so can looking at the |
| 879 | // source code. So, it's okay. |
| 880 | return fmt.Errorf("blessings received from unexpected sender") |
| 881 | } |
| 882 | if subtle.ConstantTimeCompare([]byte(token), []byte(r.token)) != 1 { |
| 883 | return fmt.Errorf("blessings received from unexpected sender") |
| 884 | } |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 885 | if flagRecvBlessingsSetDefault { |
| 886 | if err := r.principal.BlessingStore().SetDefault(b); err != nil { |
| 887 | return fmt.Errorf("failed to set blessings %v as default: %v", b, err) |
| 888 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 889 | } |
Ankur | e548f39 | 2014-12-08 18:42:41 -0800 | [diff] [blame] | 890 | if pattern := security.BlessingPattern(flagRecvBlessingsForPeer); len(pattern) > 0 { |
| 891 | if _, err := r.principal.BlessingStore().Set(b, pattern); err != nil { |
| 892 | return fmt.Errorf("failed to set blessings %v for peers %v: %v", b, pattern, err) |
| 893 | } |
Asim Shankar | f11b1bc | 2014-11-12 17:18:45 -0800 | [diff] [blame] | 894 | } |
| 895 | if flagAddToRoots { |
| 896 | if err := r.principal.AddToRoots(b); err != nil { |
| 897 | return fmt.Errorf("failed to add blessings to recognized roots: %v", err) |
| 898 | } |
| 899 | } |
| 900 | fmt.Println("Received blessings:", b) |
| 901 | r.notify <- nil |
| 902 | return nil |
| 903 | } |
| 904 | |
| 905 | type allowAnyone struct{} |
| 906 | |
| 907 | func (allowAnyone) Authorize(security.Context) error { return nil } |
| 908 | |
| 909 | type granter struct { |
| 910 | p security.Principal |
| 911 | with security.Blessings |
| 912 | extension string |
| 913 | caveats []security.Caveat |
| 914 | serverKey string |
| 915 | } |
| 916 | |
| 917 | func (g *granter) Grant(server security.Blessings) (security.Blessings, error) { |
| 918 | if got := fmt.Sprintf("%v", server.PublicKey()); got != g.serverKey { |
| 919 | // If the granter returns an error, the IPC framework should |
| 920 | // abort the RPC before sending the request to the server. |
| 921 | // Thus, there is no concern about leaking the token to an |
| 922 | // imposter server. |
| 923 | return nil, fmt.Errorf("key mismatch: Remote end has public key %v, want %v", got, g.serverKey) |
| 924 | } |
| 925 | return g.p.Bless(server.PublicKey(), g.with, g.extension, g.caveats[0], g.caveats[1:]...) |
| 926 | } |
| 927 | func (*granter) IPCCallOpt() {} |
| 928 | |
| 929 | func sendBlessings(r veyron2.Runtime, object string, granter *granter, remoteToken string) error { |
| 930 | call, err := r.Client().StartCall(r.NewContext(), object, "Grant", []interface{}{remoteToken}, granter) |
| 931 | if err != nil { |
| 932 | return fmt.Errorf("failed to start RPC to %q: %v", object, err) |
| 933 | } |
| 934 | if ierr := call.Finish(&err); ierr != nil { |
| 935 | return fmt.Errorf("failed to finish RPC to %q: %v", object, ierr) |
| 936 | } |
| 937 | return err |
| 938 | } |