Debian Codegraph @

dir/path.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // Package dir implements Notation directory structure.
15 // [directory spec]: https://notaryproject.dev/docs/user-guides/how-to/directory-structure/
16 //
17 // Example:
18 //
19 // - Read config.json:
20 // file, err := dir.ConfigFS().Open(dir.PathConfigFile)
21 //
22 // - Get the path of config.json:
23 // path, err := dir.ConfigFS().SysPath(dir.PathConfigFile)
24 //
25 // - Read trustpolicy.json:
26 // file, err := dir.ConfigFS().Open(dir.PathTrustPolicy)
27 //
28 // - Get the path of trustpolicy.json:
29 // path, err := dir.ConfigFS().SysPath(dir.PathTrustPolicy)
30 //
31 // - Set custom configurations directory:
32 // dir.UserConfigDir = '/path/to/configurations/'
33 //
34 // Only user level directory is supported, and system level directory
35 // may be added later.
36 package dir
37
38 import (
39 "os"
40 "path"
41 "path/filepath"
42 )
43
44 var (
45 UserConfigDir string // Absolute path of user level {NOTATION_CONFIG}
46 UserLibexecDir string // Absolute path of user level {NOTATION_LIBEXEC}
47 UserCacheDir string // Absolute path of user level {NOTATION_CACHE}
48 )
49
50 const (
51 // notation is the directory name for notation configurations.
52 notation = "notation"
53 )
54
55 // The relative path to {NOTATION_CONFIG}
56 const (
57 // PathConfigFile is the config.json file relative path.
58 PathConfigFile = "config.json"
59 // PathSigningKeys is the signingkeys file relative path.
60 PathSigningKeys = "signingkeys.json"
61 // PathTrustPolicy is the trust policy file relative path.
62 PathTrustPolicy = "trustpolicy.json"
63 // LocalKeysDir is the directory name for local key relative path.
64 LocalKeysDir = "localkeys"
65 // LocalCertificateExtension defines the extension of the certificate files.
66 LocalCertificateExtension = ".crt"
67 // LocalKeyExtension defines the extension of the key files.
68 LocalKeyExtension = ".key"
69 // TrustStoreDir is the directory name of trust store.
70 TrustStoreDir = "truststore"
71 )
72
73 // The relative path to {NOTATION_LIBEXEC}
74 const (
75 // PathPlugins is the plugins directory relative path.
76 PathPlugins = "plugins"
77 )
78
79 // The relative path to {NOTATION_CACHE}
80 const (
81 // PathCRLCache is the crl file cache directory relative path.
82 PathCRLCache = "crl"
83 )
84
85 // for unit tests
86 var (
87 userConfigDir = os.UserConfigDir
88
89 userCacheDir = os.UserCacheDir
90 )
91
92 // userConfigDirPath returns the user level {NOTATION_CONFIG} path.
93 func userConfigDirPath() string {
94 if UserConfigDir == "" {
95 userDir, err := userConfigDir()
96 if err != nil {
97 // fallback to current directory
98 UserConfigDir = "." + notation
99 return UserConfigDir
100 }
101 // set user config
102 UserConfigDir = filepath.Join(userDir, notation)
103 }
104 return UserConfigDir
105 }
106
107 // userLibexecDirPath returns the user level {NOTATION_LIBEXEC} path.
108 func userLibexecDirPath() string {
109 if UserLibexecDir == "" {
110 // set user libexec
111 UserLibexecDir = userConfigDirPath()
112 }
113 return UserLibexecDir
114 }
115
116 // userCacheDirPath returns the user level {NOTATION_CACHE} path.
117 func userCacheDirPath() string {
118 if UserCacheDir == "" {
119 userDir, err := userCacheDir()
120 if err != nil {
121 // fallback to current directory
122 UserCacheDir = filepath.Join("."+notation, "cache")
123 return UserCacheDir
124 }
125 // set user cache
126 UserCacheDir = filepath.Join(userDir, notation)
127 }
128 return UserCacheDir
129 }
130
131 // LocalKeyPath returns the local key and local cert relative paths.
132 func LocalKeyPath(name string) (keyPath, certPath string) {
133 basePath := path.Join(LocalKeysDir, name)
134 return basePath + LocalKeyExtension, basePath + LocalCertificateExtension
135 }
136
137 // X509TrustStoreDir returns the trust store relative path.
138 //
139 // items includes named-store and cert-file names.
140 // the directory follows the pattern of
141 // {NOTATION_CONFIG}/truststore/x509/{store-type}/{named-store}/{cert-file}
142 func X509TrustStoreDir(items ...string) string {
143 pathItems := []string{TrustStoreDir, "x509"}
144 pathItems = append(pathItems, items...)
145 return path.Join(pathItems...)
146 }