| 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 |
"fmt" |
| 18 |
"os" |
| 19 |
"path/filepath" |
| 20 |
"strings" |
| 21 |
|
| 22 |
"github.com/notaryproject/notation-go/internal/file" |
| 23 |
"github.com/notaryproject/notation-plugin-framework-go/plugin" |
| 24 |
) |
| 25 |
|
| 26 |
func binName(name string) string { |
| 27 |
return plugin.BinaryPrefix + name + ".exe" |
| 28 |
} |
| 29 |
|
| 30 |
// isExecutableFile checks if a file at filePath is executable |
| 31 |
func isExecutableFile(filePath string) (bool, error) { |
| 32 |
fi, err := os.Stat(filePath) |
| 33 |
if err != nil { |
| 34 |
return false, err |
| 35 |
} |
| 36 |
if !fi.Mode().IsRegular() { |
| 37 |
return false, ErrNotRegularFile |
| 38 |
} |
| 39 |
return strings.EqualFold(filepath.Ext(filepath.Base(filePath)), ".exe"), nil |
| 40 |
} |
| 41 |
|
| 42 |
// parsePluginName checks if fileName is a valid plugin file name |
| 43 |
// and gets plugin name from it based on spec: https://github.com/notaryproject/specifications/blob/main/specs/plugin-extensibility.md#installation |
| 44 |
func parsePluginName(fileName string) (string, error) { |
| 45 |
if !strings.EqualFold(filepath.Ext(fileName), ".exe") { |
| 46 |
return "", fmt.Errorf("invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got %s", fileName) |
| 47 |
} |
| 48 |
fname := file.TrimFileExtension(fileName) |
| 49 |
pluginName, found := strings.CutPrefix(fname, plugin.BinaryPrefix) |
| 50 |
if !found || pluginName == "" { |
| 51 |
return "", fmt.Errorf("invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got %s", fileName) |
| 52 |
} |
| 53 |
return pluginName, nil |
| 54 |
} |
| 55 |
|
| 56 |
// setExecutable returns error on Windows. User needs to install the correct |
| 57 |
// plugin file. |
| 58 |
func setExecutable(filePath string) error { |
| 59 |
return fmt.Errorf(`plugin executable file must have file extension ".exe", but got %q`, filepath.Base(filePath)) |
| 60 |
} |