blob: d6c074aa1a03081dd97aba7715e73c33c051f91c [file] [log] [blame]
Todd Wang8123b5e2015-05-14 18:44:43 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package ref defines constants used through the Vanadium reference
6// implementation, which is implemented in its subdirectories.
Todd Wang8123b5e2015-05-14 18:44:43 -07007package ref
8
9import (
10 "os"
11 "strings"
12)
13
14const (
15 // EnvCredentials is the name of the environment variable pointing to a
16 // directory containing all the credentials of a principal (the blessing
17 // store, the blessing roots, possibly the private key etc.).
18 //
19 // Typically only one of EnvCredentials or EnvAgentEndpoint will be set in a
20 // process. If both are set, then EnvCredentials takes preference.
21 //
22 // See v.io/x/ref/lib/security.CreatePersistentPrincipal.
23 EnvCredentials = "V23_CREDENTIALS"
24
Ryan Brown04384432015-08-27 16:08:32 -070025 // EnvAgentPath is the name of the environment variable pointing to a socket
26 // of the agentd process containing all the credentials for a principal (the
27 // blessing store, the blessing roots, possibly the private key etc.).
Ryan Brown0a3e28a2015-08-12 14:59:14 -070028 //
Ryan Brown04384432015-08-27 16:08:32 -070029 // Typically only one of EnvCredentials or EnvAgentPath will be set in a
Ryan Brown0a3e28a2015-08-12 14:59:14 -070030 // process. If both are set, then EnvCredentials takes preference.
31 EnvAgentPath = "V23_AGENT_PATH"
32
Todd Wang8123b5e2015-05-14 18:44:43 -070033 // EnvAgentEndpoint is the name of the environment variable pointing to an
34 // agentd process containing all the credentials a principal (the blessing
35 // store, the blessing roots, possibly the private key etc.).
36 //
Ryan Brown0a3e28a2015-08-12 14:59:14 -070037 // EnvAgentEndpoint is deprecated. New agentd processes should use EnvAgentPath.
38 // If both are set, EnvAgentPath takes preference.
Todd Wang8123b5e2015-05-14 18:44:43 -070039 EnvAgentEndpoint = "V23_AGENT_ENDPOINT"
40
41 // EnvNamespacePrefix is the prefix of all environment variables that define a
42 // namespace root.
43 EnvNamespacePrefix = "V23_NAMESPACE"
44
45 // EnvI18nCatalogueFiles is the name of the environment variable pointing to a
46 // comma-separated list of i18n catalogue files to be loaded at startup.
47 EnvI18nCatalogueFiles = "V23_I18N_CATALOGUE"
48
49 // EnvOAuthIdentityProvider is the name of the environment variable pointing
50 // to the url of the OAuth identity provider used by the principal
51 // seekblessings command.
52 EnvOAuthIdentityProvider = "V23_OAUTH_IDENTITY_PROVIDER"
Matt Rosencrantzaf3c76f2015-09-19 17:17:32 -070053
54 // RPCTransitionStateVar is a temporary variable that determines how far along we
55 // are in the transition from old to new RPC systems. It should be removed
56 // when the transition is complete.
57 RPCTransitionStateVar = "V23_RPC_TRANSITION_STATE"
Todd Wang8123b5e2015-05-14 18:44:43 -070058)
59
60// EnvNamespaceRoots returns the set of namespace roots to be used by the
61// process, as specified by environment variables.
62//
63// It returns both a map of environment variable name to value and the list of
64// values.
65func EnvNamespaceRoots() (map[string]string, []string) {
66 m := make(map[string]string)
67 var l []string
68 for _, ev := range os.Environ() {
69 p := strings.SplitN(ev, "=", 2)
70 if len(p) != 2 {
71 continue
72 }
73 k, v := p[0], p[1]
74 if strings.HasPrefix(k, EnvNamespacePrefix) && len(v) > 0 {
75 l = append(l, v)
76 m[k] = v
77 }
78 }
79 return m, l
80}
81
82// EnvClearCredentials unsets all environment variables that are used by the
83// Runtime to intialize the principal.
84func EnvClearCredentials() error {
85 for _, v := range []string{
86 EnvCredentials,
87 EnvAgentEndpoint,
88 } {
89 if err := os.Unsetenv(v); err != nil {
90 return err
91 }
92 }
93 return nil
94}
Matt Rosencrantzaf3c76f2015-09-19 17:17:32 -070095
96type TransitionState int
97
98const (
99 None TransitionState = iota
100 XClients
101 XServers
102)
103
104func RPCTransitionState() TransitionState {
105 switch ts := os.Getenv(RPCTransitionStateVar); ts {
106 case "xclients":
107 return XClients
108 case "xservers":
109 return XServers
110 case "":
111 return None
112 default:
113 panic("Unknown transition state: " + ts)
114 }
115}