-
Notifications
You must be signed in to change notification settings - Fork 56
/
skel.go
191 lines (159 loc) · 6.09 KB
/
skel.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
Copyright The Ratify 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 skel
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/deislabs/ratify/pkg/common"
"github.com/deislabs/ratify/pkg/common/plugin"
"github.com/deislabs/ratify/pkg/ocispecs"
"github.com/deislabs/ratify/pkg/referrerstore"
storeConfig "github.com/deislabs/ratify/pkg/referrerstore/config"
"github.com/deislabs/ratify/pkg/referrerstore/factory"
"github.com/deislabs/ratify/pkg/utils"
"github.com/deislabs/ratify/pkg/verifier"
"github.com/deislabs/ratify/pkg/verifier/config"
vp "github.com/deislabs/ratify/pkg/verifier/plugin"
"github.com/deislabs/ratify/pkg/verifier/types"
)
type pcontext struct {
GetEnviron func(string) string
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
type VerifyReference func(args *CmdArgs, subjectReference common.Reference, referenceDescriptor ocispecs.ReferenceDescriptor, referrerStore referrerstore.ReferrerStore) (*verifier.VerifierResult, error)
// CmdArgs describes arguments that are passed when the plugin is invoked
type CmdArgs struct {
Version string
Subject string
subjectRef common.Reference
StdinData []byte
}
// PluginMain is the core "main" for a plugin which includes error handling.
func PluginMain(name, version string, verifyReference VerifyReference, supportedVersions []string) {
if e := (&pcontext{
GetEnviron: os.Getenv,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}).pluginMainCore(name, version, verifyReference, supportedVersions); e != nil {
if err := e.Print(); err != nil {
log.Print("Error writing error response to stdout: ", err)
}
os.Exit(1)
}
}
func (pc *pcontext) pluginMainCore(_, version string, verifyReference VerifyReference, supportedVersions []string) *plugin.Error {
cmd, cmdArgs, err := pc.getCmdArgsFromEnv()
if err != nil {
return err
}
if err = validateVersion(cmdArgs.Version, supportedVersions); err != nil {
return err
}
input, err := validateAndGetConfig(cmdArgs.StdinData)
if err != nil {
return err
}
// The below is a workaround to be able to use built-in referrer store plugins from within verifier plugins
storeConfigs := storeConfig.StoresConfig{
Version: version,
PluginBinDirs: input.StoreConfig.PluginBinDirs,
Stores: []storeConfig.StorePluginConfig{input.StoreConfig.Store},
}
stores, storeErr := factory.CreateStoresFromConfig(storeConfigs, "")
if storeErr != nil || stores == nil || len(stores) == 0 {
return plugin.NewError(types.ErrArgsParsingFailure, fmt.Sprintf("create store from input config failed with error %v", storeErr), "")
}
store := stores[0]
// This is the original implementation for initialization of a referrer store which does not support built-ins
//store, serr := rp.NewStore(input.StoreConfig.Version, input.StoreConfig.Store, input.StoreConfig.PluginBinDirs)
//if serr != nil {
// return plugin.NewError(types.ErrArgsParsingFailure, fmt.Sprintf("create store from the input config failed with error %v", serr), "")
//}
switch cmd {
case vp.VerifyCommand:
result, err := verifyReference(cmdArgs, cmdArgs.subjectRef, input.ReferencDesc, store)
if err != nil {
return plugin.NewError(types.ErrPluginCmdFailure, fmt.Sprintf("plugin command %s failed", vp.VerifyCommand), err.Error())
}
err = types.WriteVerifyResultResult(result, pc.Stdout)
if err != nil {
return plugin.NewError(types.ErrIOFailure, "failed to write plugin output", err.Error())
}
return nil
default:
return plugin.NewError(types.ErrUnknownCommand, fmt.Sprintf("unknown %s: %v", vp.CommandEnvKey, cmd), "")
}
}
func (pc *pcontext) getCmdArgsFromEnv() (string, *CmdArgs, *plugin.Error) {
argsMissing := make([]string, 0)
// #1 Command
var cmd = pc.GetEnviron(vp.CommandEnvKey)
if cmd == "" {
argsMissing = append(argsMissing, vp.CommandEnvKey)
}
// #2 Version
var version = pc.GetEnviron(vp.VersionEnvKey)
if version == "" {
argsMissing = append(argsMissing, vp.VersionEnvKey)
}
// #3 Subject
var subject = pc.GetEnviron(vp.SubjectEnvKey)
if subject == "" {
argsMissing = append(argsMissing, vp.SubjectEnvKey)
}
if len(argsMissing) > 0 {
joined := strings.Join(argsMissing, ",")
return "", nil, plugin.NewError(types.ErrMissingEnvironmentVariables, fmt.Sprintf("missing env variables [%s]", joined), "")
}
stdinData, err := io.ReadAll(pc.Stdin)
if err != nil {
return "", nil, plugin.NewError(types.ErrIOFailure, fmt.Sprintf("error reading from stdin: %v", err), "")
}
subRef, err := utils.ParseSubjectReference(subject)
if err != nil {
return "", nil, plugin.NewError(types.ErrArgsParsingFailure, fmt.Sprintf("cannot parse subject reference %s", subject), err.Error())
}
cmdArgs := &CmdArgs{
Version: version,
Subject: subject,
StdinData: stdinData,
subjectRef: subRef,
}
return cmd, cmdArgs, nil
}
func validateVersion(version string, supportedVersions []string) *plugin.Error {
for _, v := range supportedVersions {
// TODO check for compatibility using semversion
if v == version {
return nil
}
}
return plugin.NewError(types.ErrVersionNotSupported, fmt.Sprintf("plugin doesn't support version %s", version), "")
}
func validateAndGetConfig(jsonBytes []byte) (*config.PluginInputConfig, *plugin.Error) {
var input config.PluginInputConfig
if err := json.Unmarshal(jsonBytes, &input); err != nil {
return nil, plugin.NewError(types.ErrConfigParsingFailure, fmt.Sprintf("error unmarshall verifier config: %v", err), "")
}
if input.Config[types.Name] == "" || input.Config[types.Name] == nil {
return nil, plugin.NewError(types.ErrInvalidVerifierConfig, "missing verifier name", "")
}
return &input, nil
}