| 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 plugin |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"io/fs" |
| 21 |
"os" |
| 22 |
"path" |
| 23 |
"path/filepath" |
| 24 |
|
| 25 |
"github.com/notaryproject/notation-go/dir" |
| 26 |
"github.com/notaryproject/notation-go/internal/file" |
| 27 |
"github.com/notaryproject/notation-go/internal/semver" |
| 28 |
"github.com/notaryproject/notation-go/log" |
| 29 |
"github.com/notaryproject/notation-plugin-framework-go/plugin" |
| 30 |
) |
| 31 |
|
| 32 |
// Manager manages plugins installed on the system. |
| 33 |
type Manager interface { |
| 34 |
Get(ctx context.Context, name string) (plugin.Plugin, error) |
| 35 |
List(ctx context.Context) ([]string, error) |
| 36 |
} |
| 37 |
|
| 38 |
// CLIManager implements Manager |
| 39 |
type CLIManager struct { |
| 40 |
pluginFS dir.SysFS |
| 41 |
} |
| 42 |
|
| 43 |
// NewCLIManager returns CLIManager for named pluginFS. |
| 44 |
func NewCLIManager(pluginFS dir.SysFS) *CLIManager { |
| 45 |
return &CLIManager{pluginFS: pluginFS} |
| 46 |
} |
| 47 |
|
| 48 |
// Get returns a plugin on the system by its name. |
| 49 |
// |
| 50 |
// If the plugin is not found, the error is of type os.ErrNotExist. |
| 51 |
func (m *CLIManager) Get(ctx context.Context, name string) (plugin.Plugin, error) { |
| 52 |
pluginPath := path.Join(name, binName(name)) |
| 53 |
path, err := m.pluginFS.SysPath(pluginPath) |
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
|
| 58 |
// validate and create plugin |
| 59 |
return NewCLIPlugin(ctx, name, path) |
| 60 |
} |
| 61 |
|
| 62 |
// List produces a list of the plugin names on the system. |
| 63 |
func (m *CLIManager) List(ctx context.Context) ([]string, error) { |
| 64 |
var plugins []string |
| 65 |
if err := fs.WalkDir(m.pluginFS, ".", func(dir string, d fs.DirEntry, err error) error { |
| 66 |
if err != nil { |
| 67 |
if errors.Is(err, os.ErrNotExist) { |
| 68 |
return nil |
| 69 |
} |
| 70 |
return err |
| 71 |
} |
| 72 |
if dir == "." { |
| 73 |
// Ignore root dir. |
| 74 |
return nil |
| 75 |
} |
| 76 |
typ := d.Type() |
| 77 |
if !typ.IsDir() || typ&fs.ModeSymlink != 0 { |
| 78 |
// Ignore non-directories and symlinked directories. |
| 79 |
return nil |
| 80 |
} |
| 81 |
|
| 82 |
// add plugin name |
| 83 |
plugins = append(plugins, d.Name()) |
| 84 |
return fs.SkipDir |
| 85 |
}); err != nil { |
| 86 |
return nil, PluginDirectoryWalkError(fmt.Errorf("failed to list plugin: %w", err)) |
| 87 |
} |
| 88 |
return plugins, nil |
| 89 |
} |
| 90 |
|
| 91 |
// CLIInstallOptions provides user customized options for plugin installation |
| 92 |
type CLIInstallOptions struct { |
| 93 |
// PluginPath can be path of: |
| 94 |
// |
| 95 |
// 1. A directory which contains plugin related files. Sub-directories are |
| 96 |
// ignored. It MUST contain one and only one valid plugin executable file |
| 97 |
// following spec: https://github.com/notaryproject/specifications/blob/v1.0.0/specs/plugin-extensibility.md#installation |
| 98 |
// It may contain extra lib files and LICENSE files. |
| 99 |
// On success, these files will be installed as well. |
| 100 |
// |
| 101 |
// 2. A single plugin executable file following the spec. |
| 102 |
PluginPath string |
| 103 |
|
| 104 |
// Overwrite is a boolean flag. When set, always install the new plugin. |
| 105 |
Overwrite bool |
| 106 |
} |
| 107 |
|
| 108 |
// Install installs a plugin to the system. It returns existing |
| 109 |
// plugin metadata, new plugin metadata, and error. It returns nil error |
| 110 |
// if and only if the installation succeeded. |
| 111 |
// |
| 112 |
// If plugin does not exist, directly install the new plugin. |
| 113 |
// |
| 114 |
// If plugin already exists: |
| 115 |
// |
| 116 |
// If overwrite is not set, then the new plugin |
| 117 |
// version MUST be higher than the existing plugin version. |
| 118 |
// |
| 119 |
// If overwrite is set, version check is skipped. If existing |
| 120 |
// plugin is malfunctioning, it will be overwritten. |
| 121 |
func (m *CLIManager) Install(ctx context.Context, installOpts CLIInstallOptions) (*plugin.GetMetadataResponse, *plugin.GetMetadataResponse, error) { |
| 122 |
// initialization |
| 123 |
logger := log.GetLogger(ctx) |
| 124 |
overwrite := installOpts.Overwrite |
| 125 |
if installOpts.PluginPath == "" { |
| 126 |
return nil, nil, errors.New("plugin source path cannot be empty") |
| 127 |
} |
| 128 |
logger.Debugf("Installing plugin from path %s", installOpts.PluginPath) |
| 129 |
var installFromNonDir bool |
| 130 |
pluginExecutableFile, pluginName, err := parsePluginFromDir(ctx, installOpts.PluginPath) |
| 131 |
if err != nil { |
| 132 |
if !errors.Is(err, file.ErrNotDirectory) { |
| 133 |
return nil, nil, fmt.Errorf("failed to read plugin from input directory: %w", err) |
| 134 |
} |
| 135 |
// input is not a dir, check if it's a single plugin executable file |
| 136 |
installFromNonDir = true |
| 137 |
pluginExecutableFile = installOpts.PluginPath |
| 138 |
pluginExecutableFileName := filepath.Base(pluginExecutableFile) |
| 139 |
pluginName, err = parsePluginName(pluginExecutableFileName) |
| 140 |
if err != nil { |
| 141 |
return nil, nil, fmt.Errorf("failed to read plugin name from input file %s: %w", pluginExecutableFileName, err) |
| 142 |
} |
| 143 |
isExec, err := isExecutableFile(pluginExecutableFile) |
| 144 |
if err != nil { |
| 145 |
return nil, nil, fmt.Errorf("failed to check if input file %s is executable: %w", pluginExecutableFileName, err) |
| 146 |
} |
| 147 |
if !isExec { |
| 148 |
return nil, nil, fmt.Errorf("input file %s is not executable", pluginExecutableFileName) |
| 149 |
} |
| 150 |
} |
| 151 |
// validate and get new plugin metadata |
| 152 |
newPlugin, err := NewCLIPlugin(ctx, pluginName, pluginExecutableFile) |
| 153 |
if err != nil { |
| 154 |
return nil, nil, err |
| 155 |
} |
| 156 |
newPluginMetadata, err := newPlugin.GetMetadata(ctx, &plugin.GetMetadataRequest{}) |
| 157 |
if err != nil { |
| 158 |
return nil, nil, fmt.Errorf("failed to get metadata of new plugin: %w", err) |
| 159 |
} |
| 160 |
// check plugin existence and get existing plugin metadata |
| 161 |
var existingPluginMetadata *plugin.GetMetadataResponse |
| 162 |
existingPlugin, err := m.Get(ctx, pluginName) |
| 163 |
if err != nil { |
| 164 |
// fail only if overwrite is not set |
| 165 |
if !errors.Is(err, os.ErrNotExist) && !overwrite { |
| 166 |
return nil, nil, fmt.Errorf("failed to check plugin existence: %w", err) |
| 167 |
} |
| 168 |
} else { // plugin already exists |
| 169 |
existingPluginMetadata, err = existingPlugin.GetMetadata(ctx, &plugin.GetMetadataRequest{}) |
| 170 |
if err != nil && !overwrite { // fail only if overwrite is not set |
| 171 |
return nil, nil, fmt.Errorf("failed to get metadata of existing plugin: %w", err) |
| 172 |
} |
| 173 |
// existing plugin is valid, and overwrite is not set, check version |
| 174 |
if !overwrite { |
| 175 |
comp, err := semver.ComparePluginVersion(newPluginMetadata.Version, existingPluginMetadata.Version) |
| 176 |
if err != nil { |
| 177 |
return nil, nil, fmt.Errorf("failed to compare plugin versions: %w", err) |
| 178 |
} |
| 179 |
switch { |
| 180 |
case comp < 0: |
| 181 |
return nil, nil, PluginDowngradeError{Msg: fmt.Sprintf("failed to install plugin %s. The installing plugin version %s is lower than the existing plugin version %s", pluginName, newPluginMetadata.Version, existingPluginMetadata.Version)} |
| 182 |
case comp == 0: |
| 183 |
return nil, nil, InstallEqualVersionError{Msg: fmt.Sprintf("plugin %s with version %s already exists", pluginName, existingPluginMetadata.Version)} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
// clean up before installation, this guarantees idempotent for install |
| 188 |
if err := m.Uninstall(ctx, pluginName); err != nil { |
| 189 |
if !errors.Is(err, os.ErrNotExist) { |
| 190 |
return nil, nil, fmt.Errorf("failed to clean up plugin %s before installation: %w", pluginName, err) |
| 191 |
} |
| 192 |
} |
| 193 |
// core process |
| 194 |
pluginDirPath, err := m.pluginFS.SysPath(pluginName) |
| 195 |
if err != nil { |
| 196 |
return nil, nil, fmt.Errorf("failed to get the system path of plugin %s: %w", pluginName, err) |
| 197 |
} |
| 198 |
if installFromNonDir { |
| 199 |
if err := file.CopyToDir(pluginExecutableFile, pluginDirPath); err != nil { |
| 200 |
return nil, nil, fmt.Errorf("failed to copy plugin executable file from %s to %s: %w", pluginExecutableFile, pluginDirPath, err) |
| 201 |
} |
| 202 |
} else { |
| 203 |
if err := file.CopyDirToDir(installOpts.PluginPath, pluginDirPath); err != nil { |
| 204 |
return nil, nil, fmt.Errorf("failed to copy plugin files from %s to %s: %w", installOpts.PluginPath, pluginDirPath, err) |
| 205 |
} |
| 206 |
} |
| 207 |
return existingPluginMetadata, newPluginMetadata, nil |
| 208 |
} |
| 209 |
|
| 210 |
// Uninstall uninstalls a plugin on the system by its name. |
| 211 |
// If the plugin dir does not exist, os.ErrNotExist is returned. |
| 212 |
func (m *CLIManager) Uninstall(ctx context.Context, name string) error { |
| 213 |
pluginDirPath, err := m.pluginFS.SysPath(name) |
| 214 |
if err != nil { |
| 215 |
return err |
| 216 |
} |
| 217 |
if _, err := os.Stat(pluginDirPath); err != nil { |
| 218 |
return err |
| 219 |
} |
| 220 |
return os.RemoveAll(pluginDirPath) |
| 221 |
} |
| 222 |
|
| 223 |
// parsePluginFromDir checks if a dir is a valid plugin dir which contains |
| 224 |
// one and only one plugin executable file candidate. |
| 225 |
// The dir may contain extra lib files and LICENSE files. |
| 226 |
// Sub-directories are ignored. |
| 227 |
// |
| 228 |
// On success, the plugin executable file path, plugin name and |
| 229 |
// nil error are returned. |
| 230 |
func parsePluginFromDir(ctx context.Context, path string) (string, string, error) { |
| 231 |
// sanity check |
| 232 |
fi, err := os.Stat(path) |
| 233 |
if err != nil { |
| 234 |
return "", "", err |
| 235 |
} |
| 236 |
if !fi.Mode().IsDir() { |
| 237 |
return "", "", file.ErrNotDirectory |
| 238 |
} |
| 239 |
logger := log.GetLogger(ctx) |
| 240 |
// walk the path |
| 241 |
var pluginExecutableFile, pluginName, candidatePluginName string |
| 242 |
var foundPluginExecutableFile bool |
| 243 |
var filesWithValidNameFormat []string |
| 244 |
if err := filepath.WalkDir(path, func(p string, d fs.DirEntry, err error) error { |
| 245 |
if err != nil { |
| 246 |
return err |
| 247 |
} |
| 248 |
// skip sub-directories |
| 249 |
if d.IsDir() && d.Name() != filepath.Base(path) { |
| 250 |
return fs.SkipDir |
| 251 |
} |
| 252 |
info, err := d.Info() |
| 253 |
if err != nil { |
| 254 |
return err |
| 255 |
} |
| 256 |
// only take regular files |
| 257 |
if info.Mode().IsRegular() { |
| 258 |
if candidatePluginName, err = parsePluginName(d.Name()); err != nil { |
| 259 |
// file name does not follow the notation-{plugin-name} format, |
| 260 |
// continue |
| 261 |
return nil |
| 262 |
} |
| 263 |
filesWithValidNameFormat = append(filesWithValidNameFormat, p) |
| 264 |
isExec, err := isExecutableFile(p) |
| 265 |
if err != nil { |
| 266 |
return err |
| 267 |
} |
| 268 |
if !isExec { |
| 269 |
return nil |
| 270 |
} |
| 271 |
if foundPluginExecutableFile { |
| 272 |
return errors.New("found more than one plugin executable files") |
| 273 |
} |
| 274 |
foundPluginExecutableFile = true |
| 275 |
pluginExecutableFile = p |
| 276 |
pluginName = candidatePluginName |
| 277 |
} |
| 278 |
return nil |
| 279 |
}); err != nil { |
| 280 |
return "", "", err |
| 281 |
} |
| 282 |
if !foundPluginExecutableFile { |
| 283 |
// if no executable file was found, but there's one and only one |
| 284 |
// potential candidate, try install the candidate |
| 285 |
if len(filesWithValidNameFormat) == 1 { |
| 286 |
candidate := filesWithValidNameFormat[0] |
| 287 |
if err := setExecutable(candidate); err != nil { |
| 288 |
return "", "", fmt.Errorf("no plugin executable file was found: %w", err) |
| 289 |
} |
| 290 |
logger.Warnf("Found candidate plugin executable file %q without executable permission. Setting user executable bit and trying to install.", filepath.Base(candidate)) |
| 291 |
return candidate, candidatePluginName, nil |
| 292 |
} |
| 293 |
return "", "", errors.New("no plugin executable file was found") |
| 294 |
} |
| 295 |
return pluginExecutableFile, pluginName, nil |
| 296 |
} |