blob: e91a56621ae5b646cf9edee561ceee1a22269a7c [file] [log] [blame]
Asim Shankar59b8b692015-03-30 01:23:36 -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
Todd Wang8c4e5cc2015-04-09 11:30:52 -07005// Package envvar defines the environment variables used by the reference v23
6// implementation.
Asim Shankar59b8b692015-03-30 01:23:36 -07007package envvar
8
9import (
10 "os"
11 "strings"
12)
13
14const (
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 Wangb3511492015-04-07 23:32:34 -070022 // See v.io/x/ref/lib/security.CreatePersistentPrincipal.
Asim Shankar59b8b692015-03-30 01:23:36 -070023 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.
39func 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.
63func ClearCredentials() error {
64 for _, v := range []string{
65 Credentials,
Asim Shankara3b4be02015-04-06 15:07:34 -070066 // Remove when https://github.com/veyron/release-issues/issues/1597 is closed.
Asim Shankar59b8b692015-03-30 01:23:36 -070067 "VEYRON_AGENT_FD",
68 } {
69 if err := os.Unsetenv(v); err != nil {
70 return err
71 }
72 }
73 return nil
74}