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. |
| 7 | // |
| 8 | // For more details about the Vanadium project, please visit https://v.io. |
| 9 | package ref |
| 10 | |
| 11 | import ( |
| 12 | "os" |
| 13 | "strings" |
| 14 | ) |
| 15 | |
| 16 | const ( |
| 17 | // EnvCredentials is the name of the environment variable pointing to a |
| 18 | // directory containing all the credentials of a principal (the blessing |
| 19 | // store, the blessing roots, possibly the private key etc.). |
| 20 | // |
| 21 | // Typically only one of EnvCredentials or EnvAgentEndpoint will be set in a |
| 22 | // process. If both are set, then EnvCredentials takes preference. |
| 23 | // |
| 24 | // See v.io/x/ref/lib/security.CreatePersistentPrincipal. |
| 25 | EnvCredentials = "V23_CREDENTIALS" |
| 26 | |
Ryan Brown | 0438443 | 2015-08-27 16:08:32 -0700 | [diff] [blame] | 27 | // EnvAgentPath is the name of the environment variable pointing to a socket |
| 28 | // of the agentd process containing all the credentials for a principal (the |
| 29 | // blessing store, the blessing roots, possibly the private key etc.). |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 30 | // |
Ryan Brown | 0438443 | 2015-08-27 16:08:32 -0700 | [diff] [blame] | 31 | // Typically only one of EnvCredentials or EnvAgentPath will be set in a |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 32 | // process. If both are set, then EnvCredentials takes preference. |
| 33 | EnvAgentPath = "V23_AGENT_PATH" |
| 34 | |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 35 | // EnvAgentEndpoint is the name of the environment variable pointing to an |
| 36 | // agentd process containing all the credentials a principal (the blessing |
| 37 | // store, the blessing roots, possibly the private key etc.). |
| 38 | // |
Ryan Brown | 0a3e28a | 2015-08-12 14:59:14 -0700 | [diff] [blame] | 39 | // EnvAgentEndpoint is deprecated. New agentd processes should use EnvAgentPath. |
| 40 | // If both are set, EnvAgentPath takes preference. |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 41 | EnvAgentEndpoint = "V23_AGENT_ENDPOINT" |
| 42 | |
| 43 | // EnvNamespacePrefix is the prefix of all environment variables that define a |
| 44 | // namespace root. |
| 45 | EnvNamespacePrefix = "V23_NAMESPACE" |
| 46 | |
| 47 | // EnvI18nCatalogueFiles is the name of the environment variable pointing to a |
| 48 | // comma-separated list of i18n catalogue files to be loaded at startup. |
| 49 | EnvI18nCatalogueFiles = "V23_I18N_CATALOGUE" |
| 50 | |
| 51 | // EnvOAuthIdentityProvider is the name of the environment variable pointing |
| 52 | // to the url of the OAuth identity provider used by the principal |
| 53 | // seekblessings command. |
| 54 | EnvOAuthIdentityProvider = "V23_OAUTH_IDENTITY_PROVIDER" |
| 55 | ) |
| 56 | |
| 57 | // EnvNamespaceRoots returns the set of namespace roots to be used by the |
| 58 | // process, as specified by environment variables. |
| 59 | // |
| 60 | // It returns both a map of environment variable name to value and the list of |
| 61 | // values. |
| 62 | func EnvNamespaceRoots() (map[string]string, []string) { |
| 63 | m := make(map[string]string) |
| 64 | var l []string |
| 65 | for _, ev := range os.Environ() { |
| 66 | p := strings.SplitN(ev, "=", 2) |
| 67 | if len(p) != 2 { |
| 68 | continue |
| 69 | } |
| 70 | k, v := p[0], p[1] |
| 71 | if strings.HasPrefix(k, EnvNamespacePrefix) && len(v) > 0 { |
| 72 | l = append(l, v) |
| 73 | m[k] = v |
| 74 | } |
| 75 | } |
| 76 | return m, l |
| 77 | } |
| 78 | |
| 79 | // EnvClearCredentials unsets all environment variables that are used by the |
| 80 | // Runtime to intialize the principal. |
| 81 | func EnvClearCredentials() error { |
| 82 | for _, v := range []string{ |
| 83 | EnvCredentials, |
| 84 | EnvAgentEndpoint, |
| 85 | } { |
| 86 | if err := os.Unsetenv(v); err != nil { |
| 87 | return err |
| 88 | } |
| 89 | } |
| 90 | return nil |
| 91 | } |