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 | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 12 | "sync" |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 13 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 14 | "v.io/v23/security" |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 15 | "v.io/v23/verror" |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 16 | "v.io/x/ref/internal/logger" |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 17 | "v.io/x/ref/services/agent" |
Todd Wang | b351149 | 2015-04-07 23:32:34 -0700 | [diff] [blame] | 18 | "v.io/x/ref/services/agent/internal/cache" |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 19 | "v.io/x/ref/services/agent/internal/ipc" |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
Ryan Brown | 7f950a8 | 2015-04-20 18:08:39 -0700 | [diff] [blame] | 22 | const pkgPath = "v.io/x/ref/services/agent/agentlib" |
| 23 | |
| 24 | // Errors |
| 25 | var ( |
| 26 | errInvalidProtocol = verror.Register(pkgPath+".errInvalidProtocol", |
| 27 | verror.NoRetry, "{1:}{2:} invalid agent protocol {3}") |
| 28 | ) |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 29 | |
| 30 | type client struct { |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 31 | caller caller |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 32 | key security.PublicKey |
| 33 | } |
| 34 | |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 35 | type caller interface { |
| 36 | call(name string, results []interface{}, args ...interface{}) error |
| 37 | io.Closer |
| 38 | } |
| 39 | |
| 40 | type ipcCaller struct { |
| 41 | conn *ipc.IPCConn |
| 42 | flush func() |
| 43 | mu sync.Mutex |
| 44 | } |
| 45 | |
| 46 | func (i *ipcCaller) call(name string, results []interface{}, args ...interface{}) error { |
| 47 | return i.conn.Call(name, args, results...) |
| 48 | } |
| 49 | |
| 50 | func (i *ipcCaller) Close() error { |
| 51 | i.conn.Close() |
| 52 | return nil |
| 53 | } |
| 54 | |
| 55 | func (i *ipcCaller) FlushAllCaches() error { |
| 56 | var flush func() |
| 57 | i.mu.Lock() |
| 58 | flush = i.flush |
| 59 | i.mu.Unlock() |
| 60 | if flush != nil { |
| 61 | flush() |
| 62 | } |
| 63 | return nil |
| 64 | } |
| 65 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 66 | func results(inputs ...interface{}) []interface{} { |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 67 | return inputs |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 70 | func newUncachedPrincipalX(path string) (*client, error) { |
| 71 | caller := new(ipcCaller) |
| 72 | i := ipc.NewIPC() |
| 73 | i.Serve(caller) |
| 74 | conn, err := i.Connect(path) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | caller.conn = conn |
| 79 | agent := &client{caller: caller} |
| 80 | if err := agent.fetchPublicKey(); err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | return agent, nil |
| 84 | } |
| 85 | |
| 86 | // NewAgentPrincipal returns a security.Pricipal using the PrivateKey held in a remote agent process. |
| 87 | // 'path' is the path to the agent socket, typically obtained from |
| 88 | // os.GetEnv(envvar.AgentAddress). |
| 89 | func NewAgentPrincipalX(path string) (agent.Principal, error) { |
| 90 | p, err := newUncachedPrincipalX(path) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | cached, flush, err := cache.NewCachedPrincipalX(p) |
| 95 | if err != nil { |
| 96 | return nil, err |
| 97 | } |
| 98 | caller := p.caller.(*ipcCaller) |
| 99 | caller.mu.Lock() |
| 100 | caller.flush = flush |
| 101 | caller.mu.Unlock() |
| 102 | return cached, nil |
| 103 | } |
| 104 | |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 105 | func (c *client) Close() error { |
| 106 | return c.caller.Close() |
| 107 | } |
| 108 | |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 109 | func (c *client) fetchPublicKey() (err error) { |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 110 | var b []byte |
| 111 | if err = c.caller.call("PublicKey", results(&b)); err != nil { |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 112 | return |
| 113 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 114 | c.key, err = security.UnmarshalPublicKey(b) |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 115 | return |
| 116 | } |
| 117 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 118 | 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] | 119 | var blessings security.Blessings |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 120 | marshalledKey, err := key.MarshalBinary() |
| 121 | if err != nil { |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 122 | return security.Blessings{}, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 123 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 124 | err = c.caller.call("Bless", results(&blessings), marshalledKey, with, extension, caveat, additionalCaveats) |
| 125 | return blessings, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | func (c *client) BlessSelf(name string, caveats ...security.Caveat) (security.Blessings, error) { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 129 | var blessings security.Blessings |
| 130 | err := c.caller.call("BlessSelf", results(&blessings), name, caveats) |
| 131 | return blessings, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | func (c *client) Sign(message []byte) (sig security.Signature, err error) { |
| 135 | err = c.caller.call("Sign", results(&sig), message) |
| 136 | return |
| 137 | } |
| 138 | |
Asim Shankar | 19da818 | 2015-02-06 01:41:16 -0800 | [diff] [blame] | 139 | 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] | 140 | var discharge security.Discharge |
Asim Shankar | 19da818 | 2015-02-06 01:41:16 -0800 | [diff] [blame] | 141 | 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] | 142 | return security.Discharge{}, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 143 | } |
Asim Shankar | 0864282 | 2015-03-02 21:21:09 -0800 | [diff] [blame] | 144 | return discharge, nil |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 147 | func (c *client) PublicKey() security.PublicKey { |
| 148 | return c.key |
| 149 | } |
| 150 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 151 | func (c *client) BlessingStore() security.BlessingStore { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 152 | return &blessingStore{caller: c.caller, key: c.key} |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | func (c *client) Roots() security.BlessingRoots { |
| 156 | return &blessingRoots{c.caller} |
| 157 | } |
| 158 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 159 | type blessingStore struct { |
| 160 | caller caller |
| 161 | key security.PublicKey |
| 162 | } |
| 163 | |
| 164 | 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] | 165 | var previous security.Blessings |
| 166 | err := b.caller.call("BlessingStoreSet", results(&previous), blessings, forPeers) |
| 167 | return previous, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | func (b *blessingStore) ForPeer(peerBlessings ...string) security.Blessings { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 171 | var blessings security.Blessings |
| 172 | if err := b.caller.call("BlessingStoreForPeer", results(&blessings), peerBlessings); err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 173 | logger.Global().Infof("error calling BlessingStorePeerBlessings: %v", err) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 174 | } |
| 175 | return blessings |
| 176 | } |
| 177 | |
| 178 | func (b *blessingStore) SetDefault(blessings security.Blessings) error { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 179 | return b.caller.call("BlessingStoreSetDefault", results(), blessings) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | func (b *blessingStore) Default() security.Blessings { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 183 | var blessings security.Blessings |
| 184 | err := b.caller.call("BlessingStoreDefault", results(&blessings)) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 185 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 186 | logger.Global().Infof("error calling BlessingStoreDefault: %v", err) |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 187 | return security.Blessings{} |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 188 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 189 | return blessings |
| 190 | } |
| 191 | |
| 192 | func (b *blessingStore) PublicKey() security.PublicKey { |
| 193 | return b.key |
| 194 | } |
| 195 | |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 196 | func (b *blessingStore) PeerBlessings() map[security.BlessingPattern]security.Blessings { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 197 | var bmap map[security.BlessingPattern]security.Blessings |
| 198 | err := b.caller.call("BlessingStorePeerBlessings", results(&bmap)) |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 199 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 200 | logger.Global().Infof("error calling BlessingStorePeerBlessings: %v", err) |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 201 | return nil |
| 202 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 203 | return bmap |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 206 | func (b *blessingStore) DebugString() (s string) { |
| 207 | err := b.caller.call("BlessingStoreDebugString", results(&s)) |
| 208 | if err != nil { |
| 209 | s = fmt.Sprintf("error calling BlessingStoreDebugString: %v", err) |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 210 | logger.Global().Infof(s) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 211 | } |
| 212 | return |
| 213 | } |
| 214 | |
Suharsh Sivakumar | d7d4e22 | 2015-06-22 11:10:44 -0700 | [diff] [blame] | 215 | func (b *blessingStore) CacheDischarge(d security.Discharge, c security.Caveat, i security.DischargeImpetus) { |
| 216 | err := b.caller.call("BlessingStoreCacheDischarge", results(), d, c, i) |
| 217 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 218 | logger.Global().Infof("error calling BlessingStoreCacheDischarge: %v", err) |
Suharsh Sivakumar | d7d4e22 | 2015-06-22 11:10:44 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | func (b *blessingStore) ClearDischarges(discharges ...security.Discharge) { |
| 223 | err := b.caller.call("BlessingStoreClearDischarges", results(), discharges) |
| 224 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 225 | logger.Global().Infof("error calling BlessingStoreClearDischarges: %v", err) |
Suharsh Sivakumar | d7d4e22 | 2015-06-22 11:10:44 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | func (b *blessingStore) Discharge(caveat security.Caveat, impetus security.DischargeImpetus) (out security.Discharge) { |
| 230 | err := b.caller.call("BlessingStoreDischarge", results(&out), caveat, impetus) |
| 231 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 232 | logger.Global().Infof("error calling BlessingStoreDischarge: %v", err) |
Suharsh Sivakumar | d7d4e22 | 2015-06-22 11:10:44 -0700 | [diff] [blame] | 233 | } |
| 234 | return |
| 235 | } |
| 236 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 237 | type blessingRoots struct { |
| 238 | caller caller |
| 239 | } |
| 240 | |
Asim Shankar | 6eba986 | 2015-09-03 01:07:14 -0400 | [diff] [blame] | 241 | func (b *blessingRoots) Add(root []byte, pattern security.BlessingPattern) error { |
| 242 | return b.caller.call("BlessingRootsAdd", results(), root, pattern) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Asim Shankar | 6eba986 | 2015-09-03 01:07:14 -0400 | [diff] [blame] | 245 | func (b *blessingRoots) Recognized(root []byte, blessing string) error { |
| 246 | return b.caller.call("BlessingRootsRecognized", results(), root, blessing) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Ankur | 9e5b772 | 2015-04-28 15:00:25 -0700 | [diff] [blame] | 249 | func (b *blessingRoots) Dump() map[security.BlessingPattern][]security.PublicKey { |
| 250 | var marshaledRoots map[security.BlessingPattern][][]byte |
| 251 | if err := b.caller.call("BlessingRootsDump", results(&marshaledRoots)); err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 252 | logger.Global().Infof("error calling BlessingRootsDump: %v", err) |
Ankur | 9e5b772 | 2015-04-28 15:00:25 -0700 | [diff] [blame] | 253 | return nil |
| 254 | } |
| 255 | ret := make(map[security.BlessingPattern][]security.PublicKey) |
| 256 | for p, marshaledKeys := range marshaledRoots { |
| 257 | for _, marshaledKey := range marshaledKeys { |
| 258 | key, err := security.UnmarshalPublicKey(marshaledKey) |
| 259 | if err != nil { |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 260 | logger.Global().Infof("security.UnmarshalPublicKey(%v) returned error: %v", marshaledKey, err) |
Ankur | 9e5b772 | 2015-04-28 15:00:25 -0700 | [diff] [blame] | 261 | continue |
| 262 | } |
| 263 | ret[p] = append(ret[p], key) |
| 264 | } |
| 265 | } |
| 266 | return ret |
| 267 | } |
| 268 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 269 | func (b *blessingRoots) DebugString() (s string) { |
| 270 | err := b.caller.call("BlessingRootsDebugString", results(&s)) |
| 271 | if err != nil { |
| 272 | s = fmt.Sprintf("error calling BlessingRootsDebugString: %v", err) |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 273 | logger.Global().Infof(s) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 274 | } |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 275 | return |
| 276 | } |