Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -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 | |
Todd Wang | 8c4e5cc | 2015-04-09 11:30:52 -0700 | [diff] [blame] | 5 | // Package envvar defines the environment variables used by the reference v23 |
| 6 | // implementation. |
Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -0700 | [diff] [blame] | 7 | package envvar |
| 8 | |
| 9 | import ( |
| 10 | "os" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
| 14 | const ( |
| 15 | // Credentials points to a directory containing all the credentials of |
| 16 | // a principal (the blessing store, the blessing roots, possibly the |
| 17 | // private key etc.). |
| 18 | // |
| 19 | // Typically only one of Credentials or AgentEndpoint will be set |
| 20 | // in a process. If both are set, then Credentials takes preference. |
| 21 | // |
Todd Wang | b351149 | 2015-04-07 23:32:34 -0700 | [diff] [blame] | 22 | // See v.io/x/ref/lib/security.CreatePersistentPrincipal. |
Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -0700 | [diff] [blame] | 23 | Credentials = "V23_CREDENTIALS" |
| 24 | |
| 25 | // NamespacePrefix is the prefix of all environment variables that define |
| 26 | // a namespace root. |
| 27 | NamespacePrefix = "V23_NAMESPACE" |
| 28 | |
| 29 | // I18nCatalogueFiles points to a comma-separated list of i18n |
| 30 | // catalogue files to be loaded at startup. |
| 31 | I18nCatalogueFiles = "V23_I18N_CATALOGUE" |
| 32 | ) |
| 33 | |
| 34 | // NamespaceRoots returns the set of namespace roots to be used by the process, |
| 35 | // as specified by environment variables. |
| 36 | // |
| 37 | // It returns both a map of environment variable name to value and the list of |
| 38 | // values. |
| 39 | func NamespaceRoots() (map[string]string, []string) { |
| 40 | m := make(map[string]string) |
| 41 | var l []string |
| 42 | for _, ev := range os.Environ() { |
| 43 | p := strings.SplitN(ev, "=", 2) |
| 44 | if len(p) != 2 { |
| 45 | continue |
| 46 | } |
| 47 | k, v := p[0], p[1] |
| 48 | if strings.HasPrefix(k, NamespacePrefix) && len(v) > 0 { |
| 49 | l = append(l, v) |
| 50 | m[k] = v |
| 51 | } else if strings.HasPrefix(k, "NAMESPACE_ROOT") && len(v) > 0 { |
| 52 | // TODO(ashankar): Remove this once the transition to |
| 53 | // the new enviornment variables is complete. |
| 54 | l = append(l, v) |
| 55 | m[k] = v |
| 56 | } |
| 57 | } |
| 58 | return m, l |
| 59 | } |
| 60 | |
| 61 | // ClearCredentials unsets all environment variables that are used by |
| 62 | // the Runtime to intialize the principal. |
| 63 | func ClearCredentials() error { |
| 64 | for _, v := range []string{ |
| 65 | Credentials, |
Asim Shankar | a3b4be0 | 2015-04-06 15:07:34 -0700 | [diff] [blame] | 66 | // Remove when https://github.com/veyron/release-issues/issues/1597 is closed. |
Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -0700 | [diff] [blame] | 67 | "VEYRON_AGENT_FD", |
| 68 | } { |
| 69 | if err := os.Unsetenv(v); err != nil { |
| 70 | return err |
| 71 | } |
| 72 | } |
| 73 | return nil |
| 74 | } |