"veyron2": RuntimePrincipal option
This CL adds an option (ROpt) to specify a Principal that should be
used to initialize a runtime. This option would be useful in a few places,
e.g., in WSPR while initializing runtimes for per-app Controllers based on
the principal created for the app, in the identity service to initialize the
runtime with a specific auditing principal, etc.
Change-Id: I1537b021b5107efdbe1cc5ebf7baeaa2bdc4eaf0
diff --git a/runtimes/google/rt/rt.go b/runtimes/google/rt/rt.go
index f3f9822..d33e95e 100644
--- a/runtimes/google/rt/rt.go
+++ b/runtimes/google/rt/rt.go
@@ -61,6 +61,8 @@
nsRoots := []string{}
for _, o := range opts {
switch v := o.(type) {
+ case veyron2.RuntimePrincipal:
+ rt.principal = v.Principal
case veyron2.RuntimeIDOpt:
rt.id = v.PrivateID
case veyron2.RuntimePublicIDStoreOpt:
diff --git a/runtimes/google/rt/rt_test.go b/runtimes/google/rt/rt_test.go
index 51f2031..05164f0 100644
--- a/runtimes/google/rt/rt_test.go
+++ b/runtimes/google/rt/rt_test.go
@@ -20,6 +20,7 @@
"veyron.io/veyron/veyron/lib/modules"
_ "veyron.io/veyron/veyron/lib/testutil"
irt "veyron.io/veyron/veyron/runtimes/google/rt"
+ vsecurity "veyron.io/veyron/veyron/security"
)
type context struct {
@@ -159,3 +160,19 @@
t.Fatalf("Initialized Principal: %v, expected: %v", got.PublicKey(), p.PublicKey())
}
}
+
+func TestInitPrincipalFromOption(t *testing.T) {
+ p, err := vsecurity.NewPrincipal()
+ if err != nil {
+ t.Fatalf("NewPrincipal() failed: %v", err)
+ }
+
+ r, err := rt.New(veyron2.RuntimePrincipal{p})
+ if err != nil {
+ t.Fatalf("rt.New failed: %v", err)
+ }
+
+ if got := r.Principal(); !reflect.DeepEqual(got, p) {
+ t.Fatalf("r.Principal(): got %v, want %v", got, p)
+ }
+}
diff --git a/runtimes/google/rt/security.go b/runtimes/google/rt/security.go
index f034ce7..d141d3e 100644
--- a/runtimes/google/rt/security.go
+++ b/runtimes/google/rt/security.go
@@ -49,6 +49,9 @@
}
func (rt *vrt) initPrincipal() error {
+ if rt.principal != nil {
+ return nil
+ }
// TODO(ataly, ashankar): Check if agent environment variables are
// specified and if so initialize principal from agent.
var err error