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