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