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 | |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 5 | // Package agent provides a client for communicating with an "Agent" |
| 6 | // process holding the private key for an identity. |
| 7 | package agent |
| 8 | |
| 9 | import ( |
| 10 | "fmt" |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 11 | "net" |
| 12 | "os" |
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/context" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 15 | "v.io/v23/naming" |
| 16 | "v.io/v23/options" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 17 | "v.io/v23/rpc" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 18 | "v.io/v23/security" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 19 | "v.io/v23/vtrace" |
Jiri Simsa | 337af23 | 2015-02-27 14:36:46 -0800 | [diff] [blame] | 20 | "v.io/x/lib/vlog" |
Jiri Simsa | ffceefa | 2015-02-28 11:03:34 -0800 | [diff] [blame] | 21 | "v.io/x/ref/lib/unixfd" |
| 22 | "v.io/x/ref/security/agent/cache" |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 25 | // FdVarName is the name of the environment variable containing |
| 26 | // the file descriptor for talking to the agent. |
| 27 | const FdVarName = "VEYRON_AGENT_FD" |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 28 | |
| 29 | type client struct { |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 30 | caller caller |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 31 | key security.PublicKey |
| 32 | } |
| 33 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 34 | type caller struct { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 35 | ctx *context.T |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 36 | client rpc.Client |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 37 | name string |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 40 | func (c *caller) call(name string, results []interface{}, args ...interface{}) error { |
| 41 | call, err := c.startCall(name, args...) |
| 42 | if err != nil { |
| 43 | return err |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 44 | } |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 45 | if err := call.Finish(results...); err != nil { |
| 46 | return err |
| 47 | } |
| 48 | return nil |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 51 | func (c *caller) startCall(name string, args ...interface{}) (rpc.ClientCall, error) { |
Ryan Brown | 81bcb3a | 2015-02-11 10:58:01 -0800 | [diff] [blame] | 52 | ctx, _ := vtrace.SetNewTrace(c.ctx) |
Suharsh Sivakumar | 2c5d810 | 2015-03-23 08:49:12 -0700 | [diff] [blame] | 53 | // SecurityNone is safe here since we're using anonymous unix sockets. |
| 54 | 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] | 55 | } |
| 56 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 57 | func results(inputs ...interface{}) []interface{} { |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 58 | return inputs |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // NewAgentPrincipal returns a security.Pricipal using the PrivateKey held in a remote agent process. |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 62 | // 'fd' is the socket for connecting to the agent, typically obtained from |
| 63 | // os.GetEnv(agent.FdVarName). |
Bogdan Caprita | 6613fc4 | 2015-01-28 11:54:23 -0800 | [diff] [blame] | 64 | // 'ctx' should not have a deadline, and should never be cancelled while the |
| 65 | // principal is in use. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 66 | func NewAgentPrincipal(ctx *context.T, fd int, insecureClient rpc.Client) (security.Principal, error) { |
Ryan Brown | 81bcb3a | 2015-02-11 10:58:01 -0800 | [diff] [blame] | 67 | p, err := newUncachedPrincipal(ctx, fd, insecureClient) |
| 68 | if err != nil { |
| 69 | return p, err |
| 70 | } |
| 71 | call, callErr := p.caller.startCall("NotifyWhenChanged") |
| 72 | if callErr != nil { |
| 73 | return nil, callErr |
| 74 | } |
| 75 | return cache.NewCachedPrincipal(p.caller.ctx, p, call) |
| 76 | } |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 77 | func newUncachedPrincipal(ctx *context.T, fd int, insecureClient rpc.Client) (*client, error) { |
Ryan Brown | 8178944 | 2014-10-30 13:23:53 -0700 | [diff] [blame] | 78 | f := os.NewFile(uintptr(fd), "agent_client") |
| 79 | defer f.Close() |
| 80 | conn, err := net.FileConn(f) |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | // This is just an arbitrary 1 byte string. The value is ignored. |
| 85 | data := make([]byte, 1) |
Bogdan Caprita | bb37c54 | 2015-01-22 10:21:57 -0800 | [diff] [blame] | 86 | addr, err := unixfd.SendConnection(conn.(*net.UnixConn), data) |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 90 | caller := caller{ |
Matt Rosencrantz | 99cc06e | 2015-01-16 10:25:11 -0800 | [diff] [blame] | 91 | client: insecureClient, |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 92 | name: naming.JoinAddressName(naming.FormatEndpoint(addr.Network(), addr.String()), ""), |
| 93 | ctx: ctx, |
| 94 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 95 | agent := &client{caller: caller} |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 96 | if err := agent.fetchPublicKey(); err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | return agent, nil |
| 100 | } |
| 101 | |
| 102 | func (c *client) fetchPublicKey() (err error) { |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 103 | var b []byte |
| 104 | if err = c.caller.call("PublicKey", results(&b)); err != nil { |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 105 | return |
| 106 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 107 | c.key, err = security.UnmarshalPublicKey(b) |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 108 | return |
| 109 | } |
| 110 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 111 | 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] | 112 | var blessings security.Blessings |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 113 | marshalledKey, err := key.MarshalBinary() |
| 114 | if err != nil { |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 115 | return security.Blessings{}, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 116 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 117 | err = c.caller.call("Bless", results(&blessings), marshalledKey, with, extension, caveat, additionalCaveats) |
| 118 | return blessings, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | func (c *client) BlessSelf(name string, caveats ...security.Caveat) (security.Blessings, error) { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 122 | var blessings security.Blessings |
| 123 | err := c.caller.call("BlessSelf", results(&blessings), name, caveats) |
| 124 | return blessings, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | func (c *client) Sign(message []byte) (sig security.Signature, err error) { |
| 128 | err = c.caller.call("Sign", results(&sig), message) |
| 129 | return |
| 130 | } |
| 131 | |
Asim Shankar | 19da818 | 2015-02-06 01:41:16 -0800 | [diff] [blame] | 132 | 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] | 133 | var discharge security.Discharge |
Asim Shankar | 19da818 | 2015-02-06 01:41:16 -0800 | [diff] [blame] | 134 | 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] | 135 | return security.Discharge{}, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 136 | } |
Asim Shankar | 0864282 | 2015-03-02 21:21:09 -0800 | [diff] [blame] | 137 | return discharge, nil |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 140 | func (c *client) PublicKey() security.PublicKey { |
| 141 | return c.key |
| 142 | } |
| 143 | |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 144 | func (c *client) BlessingsByName(pattern security.BlessingPattern) []security.Blessings { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 145 | var blessings []security.Blessings |
| 146 | if err := c.caller.call("BlessingsByName", results(&blessings), pattern); err != nil { |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 147 | vlog.Errorf("error calling BlessingsByName: %v", err) |
| 148 | return nil |
| 149 | } |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 150 | return blessings |
| 151 | } |
| 152 | |
gauthamt | 8dc9a18 | 2015-01-08 18:03:18 -0800 | [diff] [blame] | 153 | func (c *client) BlessingsInfo(blessings security.Blessings) map[string][]security.Caveat { |
| 154 | var bInfo map[string][]security.Caveat |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 155 | err := c.caller.call("BlessingsInfo", results(&bInfo), blessings) |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 156 | if err != nil { |
| 157 | vlog.Errorf("error calling BlessingsInfo: %v", err) |
| 158 | return nil |
| 159 | } |
gauthamt | 8dc9a18 | 2015-01-08 18:03:18 -0800 | [diff] [blame] | 160 | return bInfo |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 161 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 162 | func (c *client) BlessingStore() security.BlessingStore { |
| 163 | return &blessingStore{c.caller, c.key} |
| 164 | } |
| 165 | |
| 166 | func (c *client) Roots() security.BlessingRoots { |
| 167 | return &blessingRoots{c.caller} |
| 168 | } |
| 169 | |
| 170 | func (c *client) AddToRoots(blessings security.Blessings) error { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 171 | return c.caller.call("AddToRoots", results(), blessings) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | type blessingStore struct { |
| 175 | caller caller |
| 176 | key security.PublicKey |
| 177 | } |
| 178 | |
| 179 | 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] | 180 | var previous security.Blessings |
| 181 | err := b.caller.call("BlessingStoreSet", results(&previous), blessings, forPeers) |
| 182 | return previous, err |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | func (b *blessingStore) ForPeer(peerBlessings ...string) security.Blessings { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 186 | var blessings security.Blessings |
| 187 | if err := b.caller.call("BlessingStoreForPeer", results(&blessings), peerBlessings); err != nil { |
| 188 | vlog.Errorf("error calling BlessingStorePeerBlessings: %v", err) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 189 | } |
| 190 | return blessings |
| 191 | } |
| 192 | |
| 193 | func (b *blessingStore) SetDefault(blessings security.Blessings) error { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 194 | return b.caller.call("BlessingStoreSetDefault", results(), blessings) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | func (b *blessingStore) Default() security.Blessings { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 198 | var blessings security.Blessings |
| 199 | err := b.caller.call("BlessingStoreDefault", results(&blessings)) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 200 | if err != nil { |
| 201 | vlog.Errorf("error calling BlessingStoreDefault: %v", err) |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 202 | return security.Blessings{} |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 203 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 204 | return blessings |
| 205 | } |
| 206 | |
| 207 | func (b *blessingStore) PublicKey() security.PublicKey { |
| 208 | return b.key |
| 209 | } |
| 210 | |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 211 | func (b *blessingStore) PeerBlessings() map[security.BlessingPattern]security.Blessings { |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 212 | var bmap map[security.BlessingPattern]security.Blessings |
| 213 | err := b.caller.call("BlessingStorePeerBlessings", results(&bmap)) |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 214 | if err != nil { |
| 215 | vlog.Errorf("error calling BlessingStorePeerBlessings: %v", err) |
| 216 | return nil |
| 217 | } |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 218 | return bmap |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 221 | func (b *blessingStore) DebugString() (s string) { |
| 222 | err := b.caller.call("BlessingStoreDebugString", results(&s)) |
| 223 | if err != nil { |
| 224 | s = fmt.Sprintf("error calling BlessingStoreDebugString: %v", err) |
| 225 | vlog.Errorf(s) |
| 226 | } |
| 227 | return |
| 228 | } |
| 229 | |
| 230 | type blessingRoots struct { |
| 231 | caller caller |
| 232 | } |
| 233 | |
| 234 | func (b *blessingRoots) Add(root security.PublicKey, pattern security.BlessingPattern) error { |
| 235 | marshalledKey, err := root.MarshalBinary() |
| 236 | if err != nil { |
| 237 | return err |
| 238 | } |
| 239 | return b.caller.call("BlessingRootsAdd", results(), marshalledKey, pattern) |
| 240 | } |
| 241 | |
| 242 | func (b *blessingRoots) Recognized(root security.PublicKey, blessing string) error { |
| 243 | marshalledKey, err := root.MarshalBinary() |
| 244 | if err != nil { |
| 245 | return err |
| 246 | } |
Asim Shankar | b378e66 | 2015-01-16 10:50:48 -0800 | [diff] [blame] | 247 | return b.caller.call("BlessingRootsRecognized", results(), marshalledKey, blessing) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | func (b *blessingRoots) DebugString() (s string) { |
| 251 | err := b.caller.call("BlessingRootsDebugString", results(&s)) |
| 252 | if err != nil { |
| 253 | s = fmt.Sprintf("error calling BlessingRootsDebugString: %v", err) |
| 254 | vlog.Errorf(s) |
| 255 | } |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 256 | return |
| 257 | } |