Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package impl |
| 2 | |
| 3 | import ( |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 4 | "bytes" |
| 5 | "crypto/md5" |
| 6 | "encoding/hex" |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 7 | "fmt" |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 8 | "io/ioutil" |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 9 | "os" |
| 10 | "path/filepath" |
Bogdan Caprita | 4d67c04 | 2014-08-19 10:41:19 -0700 | [diff] [blame] | 11 | "strings" |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 12 | "sync" |
Jiri Simsa | 24e87aa | 2014-06-09 09:27:34 -0700 | [diff] [blame] | 13 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 14 | vsecurity "veyron.io/veyron/veyron/security" |
| 15 | vflag "veyron.io/veyron/veyron/security/flag" |
| 16 | "veyron.io/veyron/veyron/security/serialization" |
| 17 | inode "veyron.io/veyron/veyron/services/mgmt/node" |
| 18 | "veyron.io/veyron/veyron/services/mgmt/node/config" |
Jiri Simsa | 24e87aa | 2014-06-09 09:27:34 -0700 | [diff] [blame] | 19 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 20 | "veyron.io/veyron/veyron2/ipc" |
| 21 | "veyron.io/veyron/veyron2/rt" |
| 22 | "veyron.io/veyron/veyron2/security" |
| 23 | "veyron.io/veyron/veyron2/services/mgmt/node" |
| 24 | "veyron.io/veyron/veyron2/services/security/access" |
| 25 | "veyron.io/veyron/veyron2/verror" |
| 26 | "veyron.io/veyron/veyron2/vlog" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
Bogdan Caprita | 4d67c04 | 2014-08-19 10:41:19 -0700 | [diff] [blame] | 29 | // internalState wraps state shared between different node manager |
| 30 | // invocations. |
| 31 | type internalState struct { |
| 32 | callback *callbackState |
| 33 | updating *updatingState |
| 34 | } |
| 35 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 36 | // dispatcher holds the state of the node manager dispatcher. |
| 37 | type dispatcher struct { |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 38 | // acl/auth hold the acl and authorizer used to authorize access to the |
| 39 | // node manager methods. |
| 40 | acl security.ACL |
Bogdan Caprita | 4d67c04 | 2014-08-19 10:41:19 -0700 | [diff] [blame] | 41 | auth security.Authorizer |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 42 | // etag holds the version string for the ACL. We use this for optimistic |
| 43 | // concurrency control when clients update the ACLs for the node manager. |
| 44 | etag string |
Bogdan Caprita | 4d67c04 | 2014-08-19 10:41:19 -0700 | [diff] [blame] | 45 | // internal holds the state that persists across RPC method invocations. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 46 | internal *internalState |
Bogdan Caprita | 4d67c04 | 2014-08-19 10:41:19 -0700 | [diff] [blame] | 47 | // config holds the node manager's (immutable) configuration state. |
| 48 | config *config.State |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 49 | // dispatcherMutex is a lock for coordinating concurrent access to some |
| 50 | // dispatcher methods. |
| 51 | mu sync.RWMutex |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Bogdan Caprita | 4d67c04 | 2014-08-19 10:41:19 -0700 | [diff] [blame] | 54 | const ( |
| 55 | appsSuffix = "apps" |
| 56 | nodeSuffix = "nm" |
| 57 | configSuffix = "cfg" |
| 58 | ) |
| 59 | |
| 60 | var ( |
| 61 | errInvalidSuffix = verror.BadArgf("invalid suffix") |
| 62 | errOperationFailed = verror.Internalf("operation failed") |
| 63 | errInProgress = verror.Existsf("operation in progress") |
| 64 | errIncompatibleUpdate = verror.BadArgf("update failed: mismatching app title") |
Tilak Sharma | 492e8e9 | 2014-09-18 10:58:14 -0700 | [diff] [blame] | 65 | errUpdateNoOp = verror.NoExistf("no different version available") |
| 66 | errNotExist = verror.NoExistf("object does not exist") |
Bogdan Caprita | 4d67c04 | 2014-08-19 10:41:19 -0700 | [diff] [blame] | 67 | errInvalidOperation = verror.BadArgf("invalid operation") |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 68 | errInvalidBlessing = verror.BadArgf("invalid claim blessing") |
Bogdan Caprita | 4d67c04 | 2014-08-19 10:41:19 -0700 | [diff] [blame] | 69 | ) |
| 70 | |
Jiri Simsa | 24e87aa | 2014-06-09 09:27:34 -0700 | [diff] [blame] | 71 | // NewDispatcher is the node manager dispatcher factory. |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 72 | func NewDispatcher(config *config.State) (*dispatcher, error) { |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 73 | if err := config.Validate(); err != nil { |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 74 | return nil, fmt.Errorf("invalid config %v: %v", config, err) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 75 | } |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 76 | d := &dispatcher{ |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 77 | etag: "default", |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 78 | internal: &internalState{ |
Bogdan Caprita | 78b6216 | 2014-08-21 15:35:08 -0700 | [diff] [blame] | 79 | callback: newCallbackState(config.Name), |
Bogdan Caprita | 4d67c04 | 2014-08-19 10:41:19 -0700 | [diff] [blame] | 80 | updating: newUpdatingState(), |
Jiri Simsa | 70c3205 | 2014-06-18 11:38:21 -0700 | [diff] [blame] | 81 | }, |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 82 | config: config, |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 83 | } |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 84 | // If there exists a signed ACL from a previous instance we prefer that. |
| 85 | aclFile, sigFile, _ := d.getACLFilePaths() |
| 86 | if _, err := os.Stat(aclFile); err == nil { |
| 87 | perm := os.FileMode(0700) |
| 88 | data, err := os.OpenFile(aclFile, os.O_RDONLY, perm) |
| 89 | if err != nil { |
| 90 | return nil, fmt.Errorf("failed to open acl file:%v", err) |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 91 | } |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 92 | defer data.Close() |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 93 | sig, err := os.OpenFile(sigFile, os.O_RDONLY, perm) |
| 94 | if err != nil { |
| 95 | return nil, fmt.Errorf("failed to open signature file:%v", err) |
| 96 | } |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 97 | defer sig.Close() |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 98 | // read and verify the signature of the acl file |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 99 | reader, err := serialization.NewVerifyingReader(data, sig, rt.R().Identity().PublicKey()) |
| 100 | if err != nil { |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 101 | return nil, fmt.Errorf("failed to read nodemanager ACL file:%v", err) |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 102 | } |
| 103 | acl, err := vsecurity.LoadACL(reader) |
| 104 | if err != nil { |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 105 | return nil, fmt.Errorf("failed to load nodemanager ACL:%v", err) |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 106 | } |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 107 | if err := d.setACL(acl, d.etag, false /* just update etag */); err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | } else { |
| 111 | if d.auth = vflag.NewAuthorizerOrDie(); d.auth == nil { |
| 112 | // If there are no specified ACLs we grant nodemanager access to all |
| 113 | // principals until it is claimed. |
| 114 | d.auth = vsecurity.NewACLAuthorizer(vsecurity.OpenACL()) |
| 115 | } |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 116 | } |
| 117 | return d, nil |
| 118 | } |
| 119 | |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 120 | func (d *dispatcher) getACLFilePaths() (acl, signature, nodedata string) { |
| 121 | nodedata = filepath.Join(d.config.Root, "node-manager", "node-data") |
| 122 | acl, signature = filepath.Join(nodedata, "acl.nodemanager"), filepath.Join(nodedata, "acl.signature") |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 123 | return |
| 124 | } |
| 125 | |
| 126 | func (d *dispatcher) claimNodeManager(id security.PublicID) error { |
| 127 | // TODO(gauthamt): Should we start trusting these identity providers? |
| 128 | if id.Names() == nil { |
| 129 | vlog.Errorf("Identity provider for device claimer is not trusted") |
| 130 | return errOperationFailed |
| 131 | } |
| 132 | rt.R().PublicIDStore().Add(id, security.AllPrincipals) |
| 133 | // Create ACLs to transfer nodemanager permissions to the provided identity. |
| 134 | acl := security.ACL{In: make(map[security.BlessingPattern]security.LabelSet)} |
| 135 | for _, name := range id.Names() { |
| 136 | acl.In[security.BlessingPattern(name)] = security.AllLabels |
| 137 | } |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 138 | _, etag, err := d.getACL() |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 139 | if err != nil { |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 140 | vlog.Errorf("Failed to getACL:%v", err) |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 141 | return errOperationFailed |
| 142 | } |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 143 | return d.setACL(acl, etag, true /* store ACL on disk */) |
| 144 | } |
| 145 | |
| 146 | func (d *dispatcher) setACL(acl security.ACL, etag string, writeToFile bool) error { |
| 147 | d.mu.Lock() |
| 148 | defer d.mu.Unlock() |
| 149 | if len(etag) > 0 && etag != d.etag { |
| 150 | return verror.Make(access.ErrBadEtag, fmt.Sprintf("etag mismatch in:%s vers:%s", etag, d.etag)) |
| 151 | } |
| 152 | if writeToFile { |
| 153 | // Create nodedata directory if it does not exist |
| 154 | aclFile, sigFile, nodedata := d.getACLFilePaths() |
| 155 | os.MkdirAll(nodedata, os.FileMode(0700)) |
| 156 | // Save the object to temporary data and signature files, and then move |
| 157 | // those files to the actual data and signature file. |
| 158 | data, err := ioutil.TempFile(nodedata, "data") |
| 159 | if err != nil { |
| 160 | vlog.Errorf("Failed to open tmpfile data:%v", err) |
| 161 | return errOperationFailed |
| 162 | } |
| 163 | defer os.Remove(data.Name()) |
| 164 | sig, err := ioutil.TempFile(nodedata, "sig") |
| 165 | if err != nil { |
| 166 | vlog.Errorf("Failed to open tmpfile sig:%v", err) |
| 167 | return errOperationFailed |
| 168 | } |
| 169 | defer os.Remove(sig.Name()) |
| 170 | writer, err := serialization.NewSigningWriteCloser(data, sig, rt.R().Identity(), nil) |
| 171 | if err != nil { |
| 172 | vlog.Errorf("Failed to create NewSigningWriteCloser:%v", err) |
| 173 | return errOperationFailed |
| 174 | } |
| 175 | if err = vsecurity.SaveACL(writer, acl); err != nil { |
| 176 | vlog.Errorf("Failed to SaveACL:%v", err) |
| 177 | return errOperationFailed |
| 178 | } |
| 179 | if err = writer.Close(); err != nil { |
| 180 | vlog.Errorf("Failed to Close() SigningWriteCloser:%v", err) |
| 181 | return errOperationFailed |
| 182 | } |
| 183 | if err := os.Rename(data.Name(), aclFile); err != nil { |
| 184 | return err |
| 185 | } |
| 186 | if err := os.Rename(sig.Name(), sigFile); err != nil { |
| 187 | return err |
| 188 | } |
| 189 | } |
| 190 | // update the etag for the ACL |
| 191 | var b bytes.Buffer |
| 192 | if err := vsecurity.SaveACL(&b, acl); err != nil { |
| 193 | vlog.Errorf("Failed to save ACL:%v", err) |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 194 | return errOperationFailed |
| 195 | } |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 196 | // Update the acl/etag/authorizer for this dispatcher |
| 197 | md5hash := md5.Sum(b.Bytes()) |
| 198 | d.acl, d.etag, d.auth = acl, hex.EncodeToString(md5hash[:]), vsecurity.NewACLAuthorizer(acl) |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 199 | return nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 202 | func (d *dispatcher) getACL() (acl security.ACL, etag string, err error) { |
| 203 | d.mu.RLock() |
| 204 | defer d.mu.RUnlock() |
| 205 | return d.acl, d.etag, nil |
| 206 | } |
| 207 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 208 | // DISPATCHER INTERFACE IMPLEMENTATION |
| 209 | |
Cosmos Nicolaou | 8bfacf2 | 2014-08-19 11:19:36 -0700 | [diff] [blame] | 210 | func (d *dispatcher) Lookup(suffix, method string) (ipc.Invoker, security.Authorizer, error) { |
Bogdan Caprita | 4d67c04 | 2014-08-19 10:41:19 -0700 | [diff] [blame] | 211 | components := strings.Split(suffix, "/") |
| 212 | for i := 0; i < len(components); i++ { |
| 213 | if len(components[i]) == 0 { |
| 214 | components = append(components[:i], components[i+1:]...) |
| 215 | i-- |
| 216 | } |
| 217 | } |
| 218 | if len(components) == 0 { |
| 219 | return nil, nil, errInvalidSuffix |
| 220 | } |
| 221 | // The implementation of the node manager is split up into several |
| 222 | // invokers, which are instantiated depending on the receiver name |
| 223 | // prefix. |
| 224 | var receiver interface{} |
| 225 | switch components[0] { |
| 226 | case nodeSuffix: |
| 227 | receiver = node.NewServerNode(&nodeInvoker{ |
| 228 | callback: d.internal.callback, |
| 229 | updating: d.internal.updating, |
| 230 | config: d.config, |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 231 | disp: d, |
Bogdan Caprita | 4d67c04 | 2014-08-19 10:41:19 -0700 | [diff] [blame] | 232 | }) |
| 233 | case appsSuffix: |
| 234 | receiver = node.NewServerApplication(&appInvoker{ |
| 235 | callback: d.internal.callback, |
| 236 | config: d.config, |
| 237 | suffix: components[1:], |
| 238 | }) |
| 239 | case configSuffix: |
| 240 | if len(components) != 2 { |
| 241 | return nil, nil, errInvalidSuffix |
| 242 | } |
| 243 | receiver = inode.NewServerConfig(&configInvoker{ |
| 244 | callback: d.internal.callback, |
| 245 | suffix: components[1], |
| 246 | }) |
| 247 | default: |
| 248 | return nil, nil, errInvalidSuffix |
| 249 | } |
| 250 | return ipc.ReflectInvoker(receiver), d.auth, nil |
Jiri Simsa | 24e87aa | 2014-06-09 09:27:34 -0700 | [diff] [blame] | 251 | } |