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). |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 60 | // 'ctx' should not have a deadline, and should never be cancelled. |
Matt Rosencrantz | 99cc06e | 2015-01-16 10:25:11 -0800 | [diff] [blame] | 61 | 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] | 62 | f := os.NewFile(uintptr(fd), "agent_client") |
| 63 | defer f.Close() |
| 64 | conn, err := net.FileConn(f) |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | // This is just an arbitrary 1 byte string. The value is ignored. |
| 69 | data := make([]byte, 1) |
Bogdan Caprita | bb37c54 | 2015-01-22 10:21:57 -0800 | [diff] [blame] | 70 | addr, err := unixfd.SendConnection(conn.(*net.UnixConn), data) |
Ryan Brown | 50b473a | 2014-09-23 14:23:00 -0700 | [diff] [blame] | 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 74 | caller := caller{ |
Matt Rosencrantz | 99cc06e | 2015-01-16 10:25:11 -0800 | [diff] [blame] | 75 | client: insecureClient, |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 76 | name: naming.JoinAddressName(naming.FormatEndpoint(addr.Network(), addr.String()), ""), |
| 77 | ctx: ctx, |
| 78 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 79 | agent := &client{caller: caller} |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 80 | if err := agent.fetchPublicKey(); err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | return agent, nil |
| 84 | } |
| 85 | |
| 86 | func (c *client) fetchPublicKey() (err error) { |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 87 | var b []byte |
| 88 | if err = c.caller.call("PublicKey", results(&b)); err != nil { |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 89 | return |
| 90 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 91 | c.key, err = security.UnmarshalPublicKey(b) |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 92 | return |
| 93 | } |
| 94 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 95 | func (c *client) Bless(key security.PublicKey, with security.Blessings, extension string, caveat security.Caveat, additionalCaveats ...security.Caveat) (security.Blessings, error) { |
| 96 | var blessings security.WireBlessings |
| 97 | marshalledKey, err := key.MarshalBinary() |
| 98 | if err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | err = c.caller.call("Bless", results(&blessings), marshalledKey, security.MarshalBlessings(with), extension, caveat, additionalCaveats) |
| 102 | if err != nil { |
| 103 | return nil, err |
| 104 | } |
| 105 | return security.NewBlessings(blessings) |
| 106 | } |
| 107 | |
| 108 | func (c *client) BlessSelf(name string, caveats ...security.Caveat) (security.Blessings, error) { |
| 109 | var blessings security.WireBlessings |
| 110 | err := c.caller.call("BlessSelf", results(&blessings), name, caveats) |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | return security.NewBlessings(blessings) |
| 115 | } |
| 116 | |
| 117 | func (c *client) Sign(message []byte) (sig security.Signature, err error) { |
| 118 | err = c.caller.call("Sign", results(&sig), message) |
| 119 | return |
| 120 | } |
| 121 | |
| 122 | func (c *client) MintDischarge(tp security.ThirdPartyCaveat, caveat security.Caveat, additionalCaveats ...security.Caveat) (security.Discharge, error) { |
| 123 | var discharge security.Discharge |
Todd Wang | b86b352 | 2015-01-22 13:34:20 -0800 | [diff] [blame] | 124 | err := c.caller.call("MintDischarge", results(&discharge), vdl.AnyRep(tp), caveat, additionalCaveats) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 125 | if err != nil { |
| 126 | return nil, err |
| 127 | } |
| 128 | return discharge, nil |
| 129 | } |
| 130 | |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 131 | func (c *client) PublicKey() security.PublicKey { |
| 132 | return c.key |
| 133 | } |
| 134 | |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 135 | func (c *client) BlessingsByName(pattern security.BlessingPattern) []security.Blessings { |
| 136 | var wbResults []security.WireBlessings |
| 137 | err := c.caller.call("BlessingsByName", results(&wbResults), pattern) |
| 138 | if err != nil { |
| 139 | vlog.Errorf("error calling BlessingsByName: %v", err) |
| 140 | return nil |
| 141 | } |
| 142 | blessings := make([]security.Blessings, len(wbResults)) |
| 143 | for i, wb := range wbResults { |
| 144 | var err error |
| 145 | blessings[i], err = security.NewBlessings(wb) |
| 146 | if err != nil { |
| 147 | vlog.Errorf("error creating Blessing from WireBlessings: %v", err) |
| 148 | } |
| 149 | } |
| 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 |
| 155 | err := c.caller.call("BlessingsInfo", results(&bInfo), security.MarshalBlessings(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 { |
| 171 | return c.caller.call("AddToRoots", results(), security.MarshalBlessings(blessings)) |
| 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) { |
| 180 | var resultBlessings security.WireBlessings |
| 181 | err := b.caller.call("BlessingStoreSet", results(&resultBlessings), security.MarshalBlessings(blessings), forPeers) |
| 182 | if err != nil { |
| 183 | return nil, err |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 184 | } |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 185 | return security.NewBlessings(resultBlessings) |
| 186 | } |
| 187 | |
| 188 | func (b *blessingStore) ForPeer(peerBlessings ...string) security.Blessings { |
| 189 | var resultBlessings security.WireBlessings |
| 190 | err := b.caller.call("BlessingStoreForPeer", results(&resultBlessings), peerBlessings) |
| 191 | if err != nil { |
| 192 | vlog.Errorf("error calling BlessingStoreForPeer: %v", err) |
| 193 | return nil |
| 194 | } |
| 195 | blessings, err := security.NewBlessings(resultBlessings) |
| 196 | if err != nil { |
| 197 | vlog.Errorf("error creating Blessings from WireBlessings: %v", err) |
| 198 | return nil |
| 199 | } |
| 200 | return blessings |
| 201 | } |
| 202 | |
| 203 | func (b *blessingStore) SetDefault(blessings security.Blessings) error { |
| 204 | return b.caller.call("BlessingStoreSetDefault", results(), security.MarshalBlessings(blessings)) |
| 205 | } |
| 206 | |
| 207 | func (b *blessingStore) Default() security.Blessings { |
| 208 | var resultBlessings security.WireBlessings |
| 209 | err := b.caller.call("BlessingStoreDefault", results(&resultBlessings)) |
| 210 | if err != nil { |
| 211 | vlog.Errorf("error calling BlessingStoreDefault: %v", err) |
| 212 | return nil |
| 213 | } |
| 214 | blessings, err := security.NewBlessings(resultBlessings) |
| 215 | if err != nil { |
| 216 | vlog.Errorf("error creating Blessing from WireBlessings: %v", err) |
| 217 | return nil |
| 218 | } |
| 219 | return blessings |
| 220 | } |
| 221 | |
| 222 | func (b *blessingStore) PublicKey() security.PublicKey { |
| 223 | return b.key |
| 224 | } |
| 225 | |
gauthamt | f826393 | 2014-12-16 10:59:09 -0800 | [diff] [blame] | 226 | func (b *blessingStore) PeerBlessings() map[security.BlessingPattern]security.Blessings { |
| 227 | var wbMap map[security.BlessingPattern]security.WireBlessings |
| 228 | err := b.caller.call("BlessingStorePeerBlessings", results(&wbMap)) |
| 229 | if err != nil { |
| 230 | vlog.Errorf("error calling BlessingStorePeerBlessings: %v", err) |
| 231 | return nil |
| 232 | } |
| 233 | bMap := make(map[security.BlessingPattern]security.Blessings) |
| 234 | for pattern, wb := range wbMap { |
| 235 | blessings, err := security.NewBlessings(wb) |
| 236 | if err != nil { |
| 237 | vlog.Errorf("error creating Blessing from WireBlessings: %v", err) |
| 238 | return nil |
| 239 | } |
| 240 | bMap[pattern] = blessings |
| 241 | } |
| 242 | return bMap |
| 243 | } |
| 244 | |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 245 | func (b *blessingStore) DebugString() (s string) { |
| 246 | err := b.caller.call("BlessingStoreDebugString", results(&s)) |
| 247 | if err != nil { |
| 248 | s = fmt.Sprintf("error calling BlessingStoreDebugString: %v", err) |
| 249 | vlog.Errorf(s) |
| 250 | } |
| 251 | return |
| 252 | } |
| 253 | |
| 254 | type blessingRoots struct { |
| 255 | caller caller |
| 256 | } |
| 257 | |
| 258 | func (b *blessingRoots) Add(root security.PublicKey, pattern security.BlessingPattern) error { |
| 259 | marshalledKey, err := root.MarshalBinary() |
| 260 | if err != nil { |
| 261 | return err |
| 262 | } |
| 263 | return b.caller.call("BlessingRootsAdd", results(), marshalledKey, pattern) |
| 264 | } |
| 265 | |
| 266 | func (b *blessingRoots) Recognized(root security.PublicKey, blessing string) error { |
| 267 | marshalledKey, err := root.MarshalBinary() |
| 268 | if err != nil { |
| 269 | return err |
| 270 | } |
Asim Shankar | b378e66 | 2015-01-16 10:50:48 -0800 | [diff] [blame] | 271 | return b.caller.call("BlessingRootsRecognized", results(), marshalledKey, blessing) |
Suharsh Sivakumar | 8a7fba4 | 2014-10-27 12:40:48 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | func (b *blessingRoots) DebugString() (s string) { |
| 275 | err := b.caller.call("BlessingRootsDebugString", results(&s)) |
| 276 | if err != nil { |
| 277 | s = fmt.Sprintf("error calling BlessingRootsDebugString: %v", err) |
| 278 | vlog.Errorf(s) |
| 279 | } |
Ryan Brown | fed691e | 2014-09-15 13:09:40 -0700 | [diff] [blame] | 280 | return |
| 281 | } |