File: path.go

package info (click to toggle)
golang-github-notaryproject-notation-go 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,736 kB
  • sloc: makefile: 23
file content (146 lines) | stat: -rw-r--r-- 4,455 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright The Notary Project Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package dir implements Notation directory structure.
// [directory spec]: https://notaryproject.dev/docs/user-guides/how-to/directory-structure/
//
// Example:
//
//   - Read config.json:
//     file, err := dir.ConfigFS().Open(dir.PathConfigFile)
//
//   - Get the path of config.json:
//     path, err := dir.ConfigFS().SysPath(dir.PathConfigFile)
//
//   - Read trustpolicy.json:
//     file, err := dir.ConfigFS().Open(dir.PathTrustPolicy)
//
//   - Get the path of trustpolicy.json:
//     path, err := dir.ConfigFS().SysPath(dir.PathTrustPolicy)
//
//   - Set custom configurations directory:
//     dir.UserConfigDir = '/path/to/configurations/'
//
// Only user level directory is supported, and system level directory
// may be added later.
package dir

import (
	"os"
	"path"
	"path/filepath"
)

var (
	UserConfigDir  string // Absolute path of user level {NOTATION_CONFIG}
	UserLibexecDir string // Absolute path of user level {NOTATION_LIBEXEC}
	UserCacheDir   string // Absolute path of user level {NOTATION_CACHE}
)

const (
	// notation is the directory name for notation configurations.
	notation = "notation"
)

// The relative path to {NOTATION_CONFIG}
const (
	// PathConfigFile is the config.json file relative path.
	PathConfigFile = "config.json"
	// PathSigningKeys is the signingkeys file relative path.
	PathSigningKeys = "signingkeys.json"
	// PathTrustPolicy is the trust policy file relative path.
	PathTrustPolicy = "trustpolicy.json"
	// LocalKeysDir is the directory name for local key relative path.
	LocalKeysDir = "localkeys"
	// LocalCertificateExtension defines the extension of the certificate files.
	LocalCertificateExtension = ".crt"
	// LocalKeyExtension defines the extension of the key files.
	LocalKeyExtension = ".key"
	// TrustStoreDir is the directory name of trust store.
	TrustStoreDir = "truststore"
)

// The relative path to {NOTATION_LIBEXEC}
const (
	// PathPlugins is the plugins directory relative path.
	PathPlugins = "plugins"
)

// The relative path to {NOTATION_CACHE}
const (
	// PathCRLCache is the crl file cache directory relative path.
	PathCRLCache = "crl"
)

// for unit tests
var (
	userConfigDir = os.UserConfigDir

	userCacheDir = os.UserCacheDir
)

// userConfigDirPath returns the user level {NOTATION_CONFIG} path.
func userConfigDirPath() string {
	if UserConfigDir == "" {
		userDir, err := userConfigDir()
		if err != nil {
			// fallback to current directory
			UserConfigDir = "." + notation
			return UserConfigDir
		}
		// set user config
		UserConfigDir = filepath.Join(userDir, notation)
	}
	return UserConfigDir
}

// userLibexecDirPath returns the user level {NOTATION_LIBEXEC} path.
func userLibexecDirPath() string {
	if UserLibexecDir == "" {
		// set user libexec
		UserLibexecDir = userConfigDirPath()
	}
	return UserLibexecDir
}

// userCacheDirPath returns the user level {NOTATION_CACHE} path.
func userCacheDirPath() string {
	if UserCacheDir == "" {
		userDir, err := userCacheDir()
		if err != nil {
			// fallback to current directory
			UserCacheDir = filepath.Join("."+notation, "cache")
			return UserCacheDir
		}
		// set user cache
		UserCacheDir = filepath.Join(userDir, notation)
	}
	return UserCacheDir
}

// LocalKeyPath returns the local key and local cert relative paths.
func LocalKeyPath(name string) (keyPath, certPath string) {
	basePath := path.Join(LocalKeysDir, name)
	return basePath + LocalKeyExtension, basePath + LocalCertificateExtension
}

// X509TrustStoreDir returns the trust store relative path.
//
// items includes named-store and cert-file names.
// the directory follows the pattern of
// {NOTATION_CONFIG}/truststore/x509/{store-type}/{named-store}/{cert-file}
func X509TrustStoreDir(items ...string) string {
	pathItems := []string{TrustStoreDir, "x509"}
	pathItems = append(pathItems, items...)
	return path.Join(pathItems...)
}