func runClaim(ctx *context.T, env *cmdline.Env, args []string) error { if numargs := len(args); numargs != 2 { return fmt.Errorf("requires exactly two arguments <lock>, <name>, provided %d", numargs) } lockName, name := args[0], args[1] ctx, stop, err := withLocalNamespace(ctx, "", lockUserNhName(ctx)) if err != nil { return err } defer stop() ctx, cancel := context.WithTimeout(ctx, time.Minute) defer cancel() // TODO(ataly): We should not skip server endpoint authorization while // claiming locks but instead fetch the blessing root of the lock manufacturer // from an authoritative source and then appropriately authenticate the server. b, err := lock.UnclaimedLockClient(lockObjName(lockName)).Claim( ctx, name, options.ServerAuthorizer{security.AllowEveryone()}) if err != nil { return err } p := v23.GetPrincipal(ctx) if err := security.AddToRoots(p, b); err != nil { return fmt.Errorf("failed to add (key) blessing (%v) to roots: %v", b, err) } if _, err := p.BlessingStore().Set(b, security.BlessingPattern(name)); err != nil { return fmt.Errorf("failed to set (key) blessing (%v) for peer %v: %v", b, name, err) } fmt.Printf("Claimed lock: %v as %v and received key: %v\n", lockName, name, b) return nil }
func (ul *unclaimedLock) makeKey(principal security.Principal, name string, remoteKey security.PublicKey) (security.Blessings, error) { lockBlessing, err := principal.BlessSelf(name) if err != nil { return security.Blessings{}, err } if err := principal.BlessingStore().SetDefault(lockBlessing); err != nil { return security.Blessings{}, err } if err := security.AddToRoots(principal, lockBlessing); err != nil { return security.Blessings{}, err } // Add a caveat to the "key" blessing so that it can only be used to talking // to this lock object. // TODO(ataly): Add a client-only caveat as well so that someone who obtains // this blessing or an extension of it cannot maliciously (or accidentally) // start a server with this blessing (such a server could impersonate this // lock object). peerPattern := security.BlessingPattern(name) onlyThisLockCav, err := security.NewCaveat(security.PeerBlessingsCaveat, []security.BlessingPattern{peerPattern}) if err != nil { return security.Blessings{}, err } keyBlessing, err := principal.Bless(remoteKey, lockBlessing, keyBlessingExtension, onlyThisLockCav) if err != nil { return security.Blessings{}, err } return keyBlessing, nil }
func saveKeyForLock(ctx *context.T, key security.Blessings, lockName string) error { if isKeyValidForLock(ctx, key, lockName) { return fmt.Errorf("key %v is not valid for lock %v", key, lockName) } p := v23.GetPrincipal(ctx) if _, err := p.BlessingStore().Set(key, security.BlessingPattern(lockName)); err != nil { return fmt.Errorf("failed to save key %v for lock %v", key, lockName) } if err := security.AddToRoots(p, key); err != nil { return fmt.Errorf("failed to save key %v for lock %v", key, lockName) } return nil }
//export Java_io_v_v23_security_VSecurity_nativeAddToRoots func Java_io_v_v23_security_VSecurity_nativeAddToRoots(jenv *C.JNIEnv, jVSecurityClass C.jclass, jPrincipal C.jobject, jBlessings C.jobject) { env := jutil.Env(uintptr(unsafe.Pointer(jenv))) principal, err := GoPrincipal(env, jutil.Object(uintptr(unsafe.Pointer(jPrincipal)))) if err != nil { jutil.JThrowV(env, err) } blessings, err := GoBlessings(env, jutil.Object(uintptr(unsafe.Pointer(jBlessings)))) if err != nil { jutil.JThrowV(env, err) return } if err := security.AddToRoots(principal, blessings); err != nil { jutil.JThrowV(env, err) } }