Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 1 | // 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 Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 7 | package ref |
| 8 | |
| 9 | import ( |
| 10 | "os" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
| 14 | const ( |
| 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 Brown | 0438443 | 2015-08-27 16:08:32 -0700 | [diff] [blame] | 25 | // 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 Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 28 | // |
Ryan Brown | 0438443 | 2015-08-27 16:08:32 -0700 | [diff] [blame] | 29 | // Typically only one of EnvCredentials or EnvAgentPath will be set in a |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 30 | // process. If both are set, then EnvCredentials takes preference. |
| 31 | EnvAgentPath = "V23_AGENT_PATH" |
| 32 | |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 33 | // 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 Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 37 | // EnvAgentEndpoint is deprecated. New agentd processes should use EnvAgentPath. |
| 38 | // If both are set, EnvAgentPath takes preference. |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 39 | 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 Rosencrantz | af3c76f | 2015-09-19 17:17:32 -0700 | [diff] [blame] | 53 | |
| 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 Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 58 | ) |
| 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. |
| 65 | func 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. |
| 84 | func 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 Rosencrantz | af3c76f | 2015-09-19 17:17:32 -0700 | [diff] [blame] | 95 | |
| 96 | type TransitionState int |
| 97 | |
| 98 | const ( |
| 99 | None TransitionState = iota |
| 100 | XClients |
| 101 | XServers |
| 102 | ) |
| 103 | |
| 104 | func 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 | } |