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