Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Todd Wang | 8850968 | 2015-04-10 10:28:24 -0700 | [diff] [blame] | 5 | // Package agentlib implements a client for communicating with an agentd process |
| 6 | // holding the private key for an identity. |
| 7 | package agentlib |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 8 | |
| 9 | import ( |
| 10 | "fmt" |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 11 | "io" |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 12 | "net" |
| 13 | "os" |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 14 | "strconv" |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 15 | "sync" |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 16 | "syscall" |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 17 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 18 | "v.io/v23/context" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 19 | "v.io/v23/naming" |
| 20 | "v.io/v23/options" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 21 | "v.io/v23/rpc" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 22 | "v.io/v23/security" |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 23 | "v.io/v23/verror" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 24 | "v.io/v23/vtrace" |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 25 | "v.io/x/ref/internal/logger" |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 26 | "v.io/x/ref/services/agent" |
Todd Wang | b351149 | 2015-04-07 23:32:34 -0700 | [diff] [blame] | 27 | "v.io/x/ref/services/agent/internal/cache" |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 28 | "v.io/x/ref/services/agent/internal/ipc" |
Todd Wang | fb93903 | 2015-04-08 16:42:44 -0700 | [diff] [blame] | 29 | "v.io/x/ref/services/agent/internal/unixfd" |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 32 | const pkgPath = "v.io/x/ref/services/agent/agentlib" |
| 33 | |
| 34 | // Errors |
| 35 | var ( |
| 36 | errInvalidProtocol = verror.Register(pkgPath+".errInvalidProtocol", |
| 37 | verror.NoRetry, "{1:}{2:} invalid agent protocol {3}") |
| 38 | ) |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 39 | |
| 40 | type client struct { |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 41 | caller caller |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 42 | key security.PublicKey |
| 43 | } |
| 44 | |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 45 | type caller interface { |
| 46 | call(name string, results []interface{}, args ...interface{}) error |
| 47 | io.Closer |
| 48 | } |
| 49 | |
| 50 | type ipcCaller struct { |
| 51 | conn *ipc.IPCConn |
| 52 | flush func() |
| 53 | mu sync.Mutex |
| 54 | } |
| 55 | |
| 56 | func (i *ipcCaller) call(name string, results []interface{}, args ...interface{}) error { |
| 57 | return i.conn.Call(name, args, results...) |
| 58 | } |
| 59 | |
| 60 | func (i *ipcCaller) Close() error { |
| 61 | i.conn.Close() |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | func (i *ipcCaller) FlushAllCaches() error { |
| 66 | var flush func() |
| 67 | i.mu.Lock() |
| 68 | flush = i.flush |
| 69 | i.mu.Unlock() |
| 70 | if flush != nil { |
| 71 | flush() |
| 72 | } |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | type vrpcCaller struct { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 77 | ctx *context.T |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 78 | client rpc.Client |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 79 | name string |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 80 | cancel func() |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 83 | func (c *vrpcCaller) Close() error { |
| 84 | c.cancel() |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | func (c *vrpcCaller) call(name string, results []interface{}, args ...interface{}) error { |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 89 | call, err := c.startCall(name, args...) |
| 90 | if err != nil { |
| 91 | return err |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 92 | } |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 93 | if err := call.Finish(results...); err != nil { |
| 94 | return err |
| 95 | } |
| 96 | return nil |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 99 | func (c *vrpcCaller) startCall(name string, args ...interface{}) (rpc.ClientCall, error) { |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 100 | ctx, _ := vtrace.WithNewTrace(c.ctx) |
Suharsh Sivakumar | 2c5d810 | 2015-03-23 08:49:12 -0700 | [diff] [blame] | 101 | // SecurityNone is safe here since we're using anonymous unix sockets. |
| 102 | return c.client.StartCall(ctx, c.name, name, args, options.SecurityNone, options.NoResolve{}) |
Ryan Brown | 81bcb3a | 2015-02-11 10:58:01 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 105 | func results(inputs ...interface{}) []interface{} { |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 106 | return inputs |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 109 | func newUncachedPrincipalX(path string) (*client, error) { |
| 110 | caller := new(ipcCaller) |
| 111 | i := ipc.NewIPC() |
| 112 | i.Serve(caller) |
| 113 | conn, err := i.Connect(path) |
| 114 | if err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | caller.conn = conn |
| 118 | agent := &client{caller: caller} |
| 119 | if err := agent.fetchPublicKey(); err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | return agent, nil |
| 123 | } |
| 124 | |
| 125 | // NewAgentPrincipal returns a security.Pricipal using the PrivateKey held in a remote agent process. |
| 126 | // 'path' is the path to the agent socket, typically obtained from |
| 127 | // os.GetEnv(envvar.AgentAddress). |
| 128 | func NewAgentPrincipalX(path string) (agent.Principal, error) { |
| 129 | p, err := newUncachedPrincipalX(path) |
| 130 | if err != nil { |
| 131 | return nil, err |
| 132 | } |
| 133 | cached, flush, err := cache.NewCachedPrincipalX(p) |
| 134 | if err != nil { |
| 135 | return nil, err |
| 136 | } |
| 137 | caller := p.caller.(*ipcCaller) |
| 138 | caller.mu.Lock() |
| 139 | caller.flush = flush |
| 140 | caller.mu.Unlock() |
| 141 | return cached, nil |
| 142 | } |
| 143 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 144 | // NewAgentPrincipal returns a security.Pricipal using the PrivateKey held in a remote agent process. |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 145 | // 'endpoint' is the endpoint for connecting to the agent, typically obtained from |
| 146 | // os.GetEnv(envvar.AgentEndpoint). |
Bogdan Caprita | 6613fc4 | 2015-01-28 11:54:23 -0800 | [diff] [blame] | 147 | // 'ctx' should not have a deadline, and should never be cancelled while the |
| 148 | // principal is in use. |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 149 | func NewAgentPrincipal(ctx *context.T, endpoint naming.Endpoint, insecureClient rpc.Client) (security.Principal, error) { |
| 150 | p, err := newUncachedPrincipal(ctx, endpoint, insecureClient) |
Ryan Brown | 81bcb3a | 2015-02-11 10:58:01 -0800 | [diff] [blame] | 151 | if err != nil { |
| 152 | return p, err |
| 153 | } |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 154 | caller := p.caller.(*vrpcCaller) |
| 155 | call, callErr := caller.startCall("NotifyWhenChanged") |
Ryan Brown | 81bcb3a | 2015-02-11 10:58:01 -0800 | [diff] [blame] | 156 | if callErr != nil { |
| 157 | return nil, callErr |
| 158 | } |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 159 | return cache.NewCachedPrincipal(caller.ctx, p, call) |
Ryan Brown | 81bcb3a | 2015-02-11 10:58:01 -0800 | [diff] [blame] | 160 | } |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 161 | func newUncachedPrincipal(ctx *context.T, ep naming.Endpoint, insecureClient rpc.Client) (*client, error) { |
| 162 | // This isn't a real vanadium endpoint. It contains the vanadium version |
| 163 | // info, but the address is serving the agent protocol. |
| 164 | if ep.Addr().Network() != "" { |
| 165 | return nil, verror.New(errInvalidProtocol, ctx, ep.Addr().Network()) |
| 166 | } |
| 167 | fd, err := strconv.Atoi(ep.Addr().String()) |
| 168 | if err != nil { |
| 169 | return nil, err |
| 170 | } |
| 171 | syscall.ForkLock.Lock() |
| 172 | fd, err = syscall.Dup(fd) |
| 173 | if err == nil { |
| 174 | syscall.CloseOnExec(fd) |
| 175 | } |
| 176 | syscall.ForkLock.Unlock() |
| 177 | if err != nil { |
| 178 | return nil, err |
| 179 | } |
Ryan Brown | 8178944 | 2014-10-30 13:23:53 -0700 | [diff] [blame] | 180 | f := os.NewFile(uintptr(fd), "agent_client") |
| 181 | defer f.Close() |
| 182 | conn, err := net.FileConn(f) |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 183 | if err != nil { |
| 184 | return nil, err |
| 185 | } |
| 186 | // This is just an arbitrary 1 byte string. The value is ignored. |
| 187 | data := make([]byte, 1) |
Bogdan Caprita | bb37c54 | 2015-01-22 10:21:57 -0800 | [diff] [blame] | 188 | addr, err := unixfd.SendConnection(conn.(*net.UnixConn), data) |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 189 | if err != nil { |
| 190 | return nil, err |
| 191 | } |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 192 | ctx, cancel := context.WithCancel(ctx) |
| 193 | caller := &vrpcCaller{ |
Matt Rosencrantz | 99cc06e | 2015-01-16 10:25:11 -0800 | [diff] [blame] | 194 | client: insecureClient, |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 195 | name: naming.JoinAddressName(agentEndpoint("unixfd", addr.String()), ""), |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 196 | ctx: ctx, |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 197 | cancel: cancel, |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 198 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 199 | agent := &client{caller: caller} |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 200 | if err := agent.fetchPublicKey(); err != nil { |
| 201 | return nil, err |
| 202 | } |
| 203 | return agent, nil |
| 204 | } |
| 205 | |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 206 | func (c *client) Close() error { |
| 207 | return c.caller.Close() |
| 208 | } |
| 209 | |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 210 | func (c *client) fetchPublicKey() (err error) { |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 211 | var b []byte |
| 212 | if err = c.caller.call("PublicKey", results(&b)); err != nil { |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 213 | return |
| 214 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 215 | c.key, err = security.UnmarshalPublicKey(b) |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 216 | return |
| 217 | } |
| 218 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 219 | func (c *client) Bless(key security.PublicKey, with security.Blessings, extension string, caveat security.Caveat, additionalCaveats ...security.Caveat) (security.Blessings, error) { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 220 | var blessings security.Blessings |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 221 | marshalledKey, err := key.MarshalBinary() |
| 222 | if err != nil { |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 223 | return security.Blessings{}, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 224 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 225 | err = c.caller.call("Bless", results(&blessings), marshalledKey, with, extension, caveat, additionalCaveats) |
| 226 | return blessings, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | func (c *client) BlessSelf(name string, caveats ...security.Caveat) (security.Blessings, error) { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 230 | var blessings security.Blessings |
| 231 | err := c.caller.call("BlessSelf", results(&blessings), name, caveats) |
| 232 | return blessings, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | func (c *client) Sign(message []byte) (sig security.Signature, err error) { |
| 236 | err = c.caller.call("Sign", results(&sig), message) |
| 237 | return |
| 238 | } |
| 239 | |
Asim Shankar | 19da818 | 2015-02-06 01:41:16 -0800 | [diff] [blame] | 240 | func (c *client) MintDischarge(forCaveat, caveatOnDischarge security.Caveat, additionalCaveatsOnDischarge ...security.Caveat) (security.Discharge, error) { |
Asim Shankar | 0864282 | 2015-03-02 21:21:09 -0800 | [diff] [blame] | 241 | var discharge security.Discharge |
Asim Shankar | 19da818 | 2015-02-06 01:41:16 -0800 | [diff] [blame] | 242 | if err := c.caller.call("MintDischarge", results(&discharge), forCaveat, caveatOnDischarge, additionalCaveatsOnDischarge); err != nil { |
Asim Shankar | 3ad0b8a | 2015-02-25 00:37:21 -0800 | [diff] [blame] | 243 | return security.Discharge{}, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 244 | } |
Asim Shankar | 0864282 | 2015-03-02 21:21:09 -0800 | [diff] [blame] | 245 | return discharge, nil |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 248 | func (c *client) PublicKey() security.PublicKey { |
| 249 | return c.key |
| 250 | } |
| 251 | |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 252 | func (c *client) BlessingsByName(pattern security.BlessingPattern) []security.Blessings { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 253 | var blessings []security.Blessings |
| 254 | if err := c.caller.call("BlessingsByName", results(&blessings), pattern); err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 255 | logger.Global().Infof("error calling BlessingsByName: %v", err) |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 256 | return nil |
| 257 | } |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 258 | return blessings |
| 259 | } |
| 260 | |
gauthamt | 8dc9a18 | 2015-01-08 18:03:18 -0800 | [diff] [blame] | 261 | func (c *client) BlessingsInfo(blessings security.Blessings) map[string][]security.Caveat { |
| 262 | var bInfo map[string][]security.Caveat |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 263 | err := c.caller.call("BlessingsInfo", results(&bInfo), blessings) |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 264 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 265 | logger.Global().Infof("error calling BlessingsInfo: %v", err) |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 266 | return nil |
| 267 | } |
gauthamt | 8dc9a18 | 2015-01-08 18:03:18 -0800 | [diff] [blame] | 268 | return bInfo |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 269 | } |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 270 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 271 | func (c *client) BlessingStore() security.BlessingStore { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 272 | return &blessingStore{caller: c.caller, key: c.key} |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | func (c *client) Roots() security.BlessingRoots { |
| 276 | return &blessingRoots{c.caller} |
| 277 | } |
| 278 | |
| 279 | func (c *client) AddToRoots(blessings security.Blessings) error { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 280 | return c.caller.call("AddToRoots", results(), blessings) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | type blessingStore struct { |
| 284 | caller caller |
| 285 | key security.PublicKey |
| 286 | } |
| 287 | |
| 288 | func (b *blessingStore) Set(blessings security.Blessings, forPeers security.BlessingPattern) (security.Blessings, error) { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 289 | var previous security.Blessings |
| 290 | err := b.caller.call("BlessingStoreSet", results(&previous), blessings, forPeers) |
| 291 | return previous, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | func (b *blessingStore) ForPeer(peerBlessings ...string) security.Blessings { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 295 | var blessings security.Blessings |
| 296 | if err := b.caller.call("BlessingStoreForPeer", results(&blessings), peerBlessings); err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 297 | logger.Global().Infof("error calling BlessingStorePeerBlessings: %v", err) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 298 | } |
| 299 | return blessings |
| 300 | } |
| 301 | |
| 302 | func (b *blessingStore) SetDefault(blessings security.Blessings) error { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 303 | return b.caller.call("BlessingStoreSetDefault", results(), blessings) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | func (b *blessingStore) Default() security.Blessings { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 307 | var blessings security.Blessings |
| 308 | err := b.caller.call("BlessingStoreDefault", results(&blessings)) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 309 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 310 | logger.Global().Infof("error calling BlessingStoreDefault: %v", err) |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 311 | return security.Blessings{} |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 312 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 313 | return blessings |
| 314 | } |
| 315 | |
| 316 | func (b *blessingStore) PublicKey() security.PublicKey { |
| 317 | return b.key |
| 318 | } |
| 319 | |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 320 | func (b *blessingStore) PeerBlessings() map[security.BlessingPattern]security.Blessings { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 321 | var bmap map[security.BlessingPattern]security.Blessings |
| 322 | err := b.caller.call("BlessingStorePeerBlessings", results(&bmap)) |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 323 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 324 | logger.Global().Infof("error calling BlessingStorePeerBlessings: %v", err) |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 325 | return nil |
| 326 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 327 | return bmap |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 330 | func (b *blessingStore) DebugString() (s string) { |
| 331 | err := b.caller.call("BlessingStoreDebugString", results(&s)) |
| 332 | if err != nil { |
| 333 | s = fmt.Sprintf("error calling BlessingStoreDebugString: %v", err) |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 334 | logger.Global().Infof(s) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 335 | } |
| 336 | return |
| 337 | } |
| 338 | |
Suharsh Sivakumar | d7d4e22 | 2015-06-22 11:10:44 -0700 | [diff] [blame] | 339 | func (b *blessingStore) CacheDischarge(d security.Discharge, c security.Caveat, i security.DischargeImpetus) { |
| 340 | err := b.caller.call("BlessingStoreCacheDischarge", results(), d, c, i) |
| 341 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 342 | logger.Global().Infof("error calling BlessingStoreCacheDischarge: %v", err) |
Suharsh Sivakumar | d7d4e22 | 2015-06-22 11:10:44 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | func (b *blessingStore) ClearDischarges(discharges ...security.Discharge) { |
| 347 | err := b.caller.call("BlessingStoreClearDischarges", results(), discharges) |
| 348 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 349 | logger.Global().Infof("error calling BlessingStoreClearDischarges: %v", err) |
Suharsh Sivakumar | d7d4e22 | 2015-06-22 11:10:44 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | func (b *blessingStore) Discharge(caveat security.Caveat, impetus security.DischargeImpetus) (out security.Discharge) { |
| 354 | err := b.caller.call("BlessingStoreDischarge", results(&out), caveat, impetus) |
| 355 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 356 | logger.Global().Infof("error calling BlessingStoreDischarge: %v", err) |
Suharsh Sivakumar | d7d4e22 | 2015-06-22 11:10:44 -0700 | [diff] [blame] | 357 | } |
| 358 | return |
| 359 | } |
| 360 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 361 | type blessingRoots struct { |
| 362 | caller caller |
| 363 | } |
| 364 | |
Asim Shankar | 6eba986 | 2015-09-03 01:07:14 -0400 | [diff] [blame^] | 365 | func (b *blessingRoots) Add(root []byte, pattern security.BlessingPattern) error { |
| 366 | return b.caller.call("BlessingRootsAdd", results(), root, pattern) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Asim Shankar | 6eba986 | 2015-09-03 01:07:14 -0400 | [diff] [blame^] | 369 | func (b *blessingRoots) Recognized(root []byte, blessing string) error { |
| 370 | return b.caller.call("BlessingRootsRecognized", results(), root, blessing) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Ankur | 9e5b772 | 2015-04-28 15:00:25 -0700 | [diff] [blame] | 373 | func (b *blessingRoots) Dump() map[security.BlessingPattern][]security.PublicKey { |
| 374 | var marshaledRoots map[security.BlessingPattern][][]byte |
| 375 | if err := b.caller.call("BlessingRootsDump", results(&marshaledRoots)); err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 376 | logger.Global().Infof("error calling BlessingRootsDump: %v", err) |
Ankur | 9e5b772 | 2015-04-28 15:00:25 -0700 | [diff] [blame] | 377 | return nil |
| 378 | } |
| 379 | ret := make(map[security.BlessingPattern][]security.PublicKey) |
| 380 | for p, marshaledKeys := range marshaledRoots { |
| 381 | for _, marshaledKey := range marshaledKeys { |
| 382 | key, err := security.UnmarshalPublicKey(marshaledKey) |
| 383 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 384 | logger.Global().Infof("security.UnmarshalPublicKey(%v) returned error: %v", marshaledKey, err) |
Ankur | 9e5b772 | 2015-04-28 15:00:25 -0700 | [diff] [blame] | 385 | continue |
| 386 | } |
| 387 | ret[p] = append(ret[p], key) |
| 388 | } |
| 389 | } |
| 390 | return ret |
| 391 | } |
| 392 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 393 | func (b *blessingRoots) DebugString() (s string) { |
| 394 | err := b.caller.call("BlessingRootsDebugString", results(&s)) |
| 395 | if err != nil { |
| 396 | s = fmt.Sprintf("error calling BlessingRootsDebugString: %v", err) |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 397 | logger.Global().Infof(s) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 398 | } |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 399 | return |
| 400 | } |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 401 | |
| 402 | func agentEndpoint(proto, addr string) string { |
Suharsh Sivakumar | fa2e50a | 2015-08-31 12:35:12 -0700 | [diff] [blame] | 403 | // TODO: use naming.FormatEndpoint when it supports version 6. |
| 404 | return fmt.Sprintf("@6@%s@%s@@@s@@@", proto, addr) |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | func AgentEndpoint(fd int) string { |
| 408 | // We use an empty protocol here because this isn't really speaking |
| 409 | // veyron rpc. |
| 410 | return agentEndpoint("", fmt.Sprintf("%d", fd)) |
| 411 | } |