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