@@ -39,26 +39,71 @@ import (
3939 "github.com/sigstore/policy-controller/pkg/webhook"
4040)
4141
42- var (
43- ctx = logging .WithLogger (context .Background (), func () * zap.SugaredLogger {
44- x , _ := zap .NewDevelopmentConfig ().Build ()
45- return x .Sugar ()
46- }())
47- )
48-
4942type output struct {
5043 Errors []string `json:"errors,omitempty"`
5144 Warnings []string `json:"warnings,omitempty"`
5245}
5346
47+ type LogLevel string
48+
49+ const (
50+ LevelDebug LogLevel = "debug"
51+ LevelInfo LogLevel = "info"
52+ LevelWarn LogLevel = "warn"
53+ LevelError LogLevel = "error"
54+ )
55+
56+ func getSugaredLogger (value string ) (* zap.SugaredLogger , error ) {
57+ ll := LogLevel (value )
58+ switch ll {
59+ case LevelDebug , LevelInfo , LevelWarn , LevelError :
60+ return setSugaredLogger (ll )
61+ default :
62+ return nil , fmt .Errorf ("invalid log level" )
63+ }
64+ }
65+
66+ func setSugaredLogger (logLevel LogLevel ) (* zap.SugaredLogger , error ) {
67+ cfg := zap .NewDevelopmentConfig ()
68+ switch logLevel {
69+ case LevelDebug :
70+ cfg .Level .SetLevel (zap .DebugLevel )
71+ case LevelInfo :
72+ cfg .Level .SetLevel (zap .InfoLevel )
73+ case LevelWarn :
74+ cfg = zap .NewProductionConfig ()
75+ cfg .Level .SetLevel (zap .WarnLevel )
76+ case LevelError :
77+ cfg = zap .NewProductionConfig ()
78+ cfg .Level .SetLevel (zap .ErrorLevel )
79+ default :
80+ panic ("invalid log level" )
81+ }
82+
83+ logger , err := cfg .Build ()
84+ if err != nil {
85+ return nil , fmt .Errorf ("failed to build logger: %w" , err )
86+ }
87+ return logger .Sugar (), nil
88+ }
89+
5490func main () {
5591 cipFilePath := flag .String ("policy" , "" , "path to ClusterImagePolicy or URL to fetch from (http/https)" )
5692 versionFlag := flag .Bool ("version" , false , "return the policy-controller tester version" )
5793 image := flag .String ("image" , "" , "image to compare against policy" )
5894 resourceFilePath := flag .String ("resource" , "" , "path to a kubernetes resource to use with includeSpec, includeObjectMeta" )
5995 trustRootFilePath := flag .String ("trustroot" , "" , "path to a kubernetes TrustRoot resource to use with the ClusterImagePolicy" )
96+ logLevelStr := flag .String ("log-level" , "info" , "configure the tool's log level (debug, info, warn, error)" )
6097 flag .Parse ()
6198
99+ logger , err := getSugaredLogger (* logLevelStr )
100+ if err != nil {
101+ flag .Usage ()
102+ os .Exit (1 )
103+ }
104+
105+ ctx := logging .WithLogger (context .Background (), logger )
106+
62107 if * versionFlag {
63108 v := version .GetVersionInfo ()
64109 fmt .Println (v .String ())
@@ -82,6 +127,8 @@ func main() {
82127 })
83128 }
84129
130+ logging .FromContext (ctx ).Infof ("Validating policy\n " )
131+
85132 v := policy.Verification {
86133 NoMatchPolicy : "deny" ,
87134 Policies : & pols ,
@@ -97,6 +144,8 @@ func main() {
97144 }
98145 }
99146
147+ logging .FromContext (ctx ).Infof ("Policy was successfully validated\n " )
148+
100149 ref , err := name .ParseReference (* image )
101150 if err != nil {
102151 log .Fatal (err )
@@ -111,6 +160,8 @@ func main() {
111160 }
112161
113162 if * resourceFilePath != "" {
163+ logging .FromContext (ctx ).Infof ("Parsing the provided Kubernetes resource\n " )
164+
114165 raw , err := os .ReadFile (* resourceFilePath )
115166 if err != nil {
116167 log .Fatal (err )
@@ -141,9 +192,13 @@ func main() {
141192 typeMeta ["kind" ] = kind
142193 typeMeta ["apiVersion" ] = apiVersion
143194 ctx = webhook .IncludeTypeMeta (ctx , typeMeta )
195+
196+ logging .FromContext (ctx ).Infof ("The Kuberentes resource will be used with includeSpec\n " )
144197 }
145198
146199 if * trustRootFilePath != "" {
200+ logging .FromContext (ctx ).Infof ("Parsing the custom trust root\n " )
201+
147202 configCtx := config .FromContextOrDefaults (ctx )
148203 raw , err := os .ReadFile (* trustRootFilePath )
149204 if err != nil {
@@ -166,24 +221,31 @@ func main() {
166221 configCtx .SigstoreKeysConfig = & config.SigstoreKeysMap {SigstoreKeys : maps }
167222
168223 ctx = config .ToContext (ctx , configCtx )
224+
225+ logging .FromContext (ctx ).Infof ("The custom trust root has been successfully added\n " )
169226 }
170227
228+ logging .FromContext (ctx ).Infof ("Verifying the provided image against the policy\n " )
229+
171230 errStrings := []string {}
172231 if err := vfy .Verify (ctx , ref , authn .DefaultKeychain ); err != nil {
173232 errStrings = append (errStrings , strings .Trim (err .Error (), "\n " ))
174233 }
175234
176- var o []byte
177- o , err = json .Marshal (& output {
178- Errors : errStrings ,
179- Warnings : warningStrings ,
180- })
181- if err != nil {
182- log .Fatal (err )
183- }
235+ if len (errStrings ) != 0 {
236+ logging .FromContext (ctx ).Infof ("Errors encountered during verification\n " )
237+
238+ var o []byte
239+ o , err = json .Marshal (& output {
240+ Errors : errStrings ,
241+ Warnings : warningStrings ,
242+ })
243+ if err != nil {
244+ log .Fatal (err )
245+ }
184246
185- fmt .Println (string (o ))
186- if len (errStrings ) > 0 {
247+ fmt .Println (string (o ))
187248 os .Exit (1 )
188249 }
250+ logging .FromContext (ctx ).Infof ("Verification was successful!\n " )
189251}
0 commit comments