@@ -17,27 +17,90 @@ type HookHandler interface {
1717}
1818
1919type HookRequest interface {
20- Scan () * Scan
21- GetRawResults () (string , error )
22- GetFindings () ([]Finding , error )
23- UpdateRawResults (content string ) error
24- UpdateFindings (findings []Finding ) error
20+ Scan (ctx context. Context ) ( * Scan , error )
21+ GetRawResults (ctx context. Context ) (string , error )
22+ GetFindings (ctx context. Context ) ([]Finding , error )
23+ UpdateRawResults (ctx context. Context , content string ) error
24+ UpdateFindings (ctx context. Context , findings []Finding ) error
2525}
2626
2727type hookRequest struct {
28- scan * Scan
29- getRawResults func () (string , error )
30- getFindings func () ([]Finding , error )
31- updateRawResults func (string ) error
32- updateFindings func ([]Finding ) error
28+ k8sClient K8sClient
29+ fileClient FileClient
30+ scanName string
31+ namespace string
32+ urls []string
33+ }
34+
35+ func (r * hookRequest ) Scan (ctx context.Context ) (* Scan , error ) {
36+ scan , err := r .k8sClient .GetScan (ctx , r .scanName , r .namespace )
37+ if err != nil {
38+ return nil , fmt .Errorf ("get Scan from Kubernetes API: %w" , err )
39+ }
40+ return scan , nil
41+ }
42+
43+ func (r * hookRequest ) GetRawResults (ctx context.Context ) (string , error ) {
44+ url := r .urlAt (0 )
45+ if url == "" {
46+ return "" , fmt .Errorf ("raw results download URL not provided" )
47+ }
48+ return r .fileClient .DownloadText (ctx , url )
49+ }
50+
51+ func (r * hookRequest ) GetFindings (ctx context.Context ) ([]Finding , error ) {
52+ url := r .urlAt (1 )
53+ if url == "" {
54+ return nil , fmt .Errorf ("findings download URL not provided" )
55+ }
56+ var findings []Finding
57+ if err := r .fileClient .DownloadJSON (ctx , url , & findings ); err != nil {
58+ return nil , fmt .Errorf ("download findings: %w" , err )
59+ }
60+ for index , finding := range findings {
61+ if err := ValidateFinding (finding , index ); err != nil {
62+ return nil , err
63+ }
64+ }
65+ return findings , nil
3366}
3467
35- func (r * hookRequest ) Scan () * Scan { return r .scan }
36- func (r * hookRequest ) GetRawResults () (string , error ) { return r .getRawResults () }
37- func (r * hookRequest ) GetFindings () ([]Finding , error ) { return r .getFindings () }
38- func (r * hookRequest ) UpdateRawResults (content string ) error { return r .updateRawResults (content ) }
39- func (r * hookRequest ) UpdateFindings (findings []Finding ) error {
40- return r .updateFindings (findings )
68+ func (r * hookRequest ) UpdateRawResults (ctx context.Context , content string ) error {
69+ url := r .urlAt (2 )
70+ if url == "" {
71+ return fmt .Errorf ("cannot update raw results in a ReadOnly hook" )
72+ }
73+ return r .fileClient .Upload (ctx , url , "" , []byte (content ))
74+ }
75+
76+ func (r * hookRequest ) UpdateFindings (ctx context.Context , findings []Finding ) error {
77+ url := r .urlAt (3 )
78+ if url == "" {
79+ return fmt .Errorf ("cannot update findings in a ReadOnly hook" )
80+ }
81+ for index , finding := range findings {
82+ if err := ValidateFinding (finding , index ); err != nil {
83+ return err
84+ }
85+ }
86+ body , err := json .Marshal (findings )
87+ if err != nil {
88+ return fmt .Errorf ("marshal findings: %w" , err )
89+ }
90+ if err := r .fileClient .Upload (ctx , url , "" , body ); err != nil {
91+ return fmt .Errorf ("upload findings: %w" , err )
92+ }
93+ if err := r .k8sClient .PatchScanStatus (ctx , r .scanName , r .namespace , findings ); err != nil {
94+ return fmt .Errorf ("update scan status: %w" , err )
95+ }
96+ return nil
97+ }
98+
99+ func (r * hookRequest ) urlAt (index int ) string {
100+ if index < len (r .urls ) {
101+ return r .urls [index ]
102+ }
103+ return ""
41104}
42105
43106type Client struct {
@@ -85,70 +148,7 @@ func NewClient(opts ...Option) (*Client, error) {
85148}
86149
87150func (c * Client ) Run (ctx context.Context , handler HookHandler ) error {
88- scan , err := c .k8sClient .GetScan (ctx , c .scanName , c .namespace )
89- if err != nil {
90- return fmt .Errorf ("get Scan from Kubernetes API: %w" , err )
91- }
92- urls := c .args [1 :]
93- urlAt := func (index int ) string {
94- if index < len (urls ) {
95- return urls [index ]
96- }
97- return ""
98- }
99- getRawResults := func () (string , error ) {
100- url := urlAt (0 )
101- if url == "" {
102- return "" , fmt .Errorf ("raw results download URL not provided" )
103- }
104- return c .fileClient .DownloadText (ctx , url )
105- }
106- getFindings := func () ([]Finding , error ) {
107- url := urlAt (1 )
108- if url == "" {
109- return nil , fmt .Errorf ("findings download URL not provided" )
110- }
111- var findings []Finding
112- if err := c .fileClient .DownloadJSON (ctx , url , & findings ); err != nil {
113- return nil , fmt .Errorf ("download findings: %w" , err )
114- }
115- for index , finding := range findings {
116- if err := ValidateFinding (finding , index ); err != nil {
117- return nil , err
118- }
119- }
120- return findings , nil
121- }
122- updateRawResults := func (content string ) error {
123- url := urlAt (2 )
124- if url == "" {
125- return fmt .Errorf ("cannot update raw results in a ReadOnly hook" )
126- }
127- return c .fileClient .Upload (ctx , url , "" , []byte (content ))
128- }
129- updateFindings := func (findings []Finding ) error {
130- url := urlAt (3 )
131- if url == "" {
132- return fmt .Errorf ("cannot update findings in a ReadOnly hook" )
133- }
134- for index , finding := range findings {
135- if err := ValidateFinding (finding , index ); err != nil {
136- return err
137- }
138- }
139- body , err := json .Marshal (findings )
140- if err != nil {
141- return fmt .Errorf ("marshal findings: %w" , err )
142- }
143- if err := c .fileClient .Upload (ctx , url , "" , body ); err != nil {
144- return fmt .Errorf ("upload findings: %w" , err )
145- }
146- if err := c .k8sClient .PatchScanStatus (ctx , c .scanName , c .namespace , findings ); err != nil {
147- return fmt .Errorf ("update scan status: %w" , err )
148- }
149- return nil
150- }
151- request := & hookRequest {scan : scan , getRawResults : getRawResults , getFindings : getFindings , updateRawResults : updateRawResults , updateFindings : updateFindings }
151+ request := & hookRequest {k8sClient : c .k8sClient , fileClient : c .fileClient , scanName : c .scanName , namespace : c .namespace , urls : c .args [1 :]}
152152 if err := handler .Handle (ctx , request ); err != nil {
153153 return fmt .Errorf ("run hook handler: %w" , err )
154154 }
0 commit comments