Skip to content

Commit dd02397

Browse files
authored
Merge pull request #12 from Acmarr/main
NVSHAS-8057 fixed an issue with grpc error report handling
2 parents 336f7a9 + 5d01d0d commit dd02397

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

server/server.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const dataCheckInterval = 1.0
2929

3030
const rpcTimeout = time.Minute * 20
3131
const expirationTime = time.Minute * 25
32-
const pruneTime = time.Minute * 5
32+
const pruneTime = time.Minute * 60
3333

3434
var workloadID Counter
3535
var concurrentJobs Counter
@@ -57,10 +57,13 @@ func InitializeServer(config *config.ServerConfig) {
5757
defer http.DefaultClient.CloseIdleConnections()
5858
_, err := GetControllerServiceClient(serverConfig.ControllerIP, serverConfig.ControllerPort)
5959
if err != nil {
60-
log.WithFields(log.Fields{"error": err}).Error("Error retrieving rpc client")
60+
log.WithFields(log.Fields{"error": err}).Error("Error establishing grpc connection to controller")
61+
return
62+
}
63+
for nvScanner.Version == "" {
64+
time.Sleep(time.Second * 3)
65+
pollMaxConcurrent()
6166
}
62-
time.Sleep(time.Second * 3)
63-
pollMaxConcurrent()
6467
http.HandleFunc("/", unhandled)
6568
http.HandleFunc(metadataEndpoint, authenticateHarbor(metadata))
6669
http.HandleFunc(scanEndpoint, authenticateHarbor(scan))
@@ -195,7 +198,8 @@ func scan(w http.ResponseWriter, req *http.Request) {
195198
workloadID.Unlock()
196199

197200
reportCache.Lock()
198-
reportCache.ScanReports[scanId.ID] = ScanReport{Status: http.StatusFound, ExpirationTime: generateExpirationTime()}
201+
expirationTime := generateExpirationTime()
202+
reportCache.ScanReports[scanId.ID] = ScanReport{Status: http.StatusFound, ExpirationTime: expirationTime}
199203
reportCache.Unlock()
200204

201205
err = json.NewEncoder(w).Encode(scanId)
@@ -246,8 +250,10 @@ func processScanTask(scanRequest ScanRequest) {
246250
reportCache.Lock()
247251
report := reportCache.ScanReports[scanRequest.WorkloadID]
248252
report.Status = http.StatusInternalServerError
253+
reportCache.ScanReports[scanRequest.WorkloadID] = report
249254
reportCache.Unlock()
250-
log.WithFields(log.Fields{"error": err}).Error("Error retrieving rpc client")
255+
log.WithFields(log.Fields{"error": err}).Error("Error establishing grpc connection to controller")
256+
concurrentJobs.Decrement()
251257
return
252258
}
253259
request := share.AdapterScanImageRequest{
@@ -259,27 +265,27 @@ func processScanTask(scanRequest ScanRequest) {
259265
}
260266
ctx, cancel := context.WithTimeout(context.Background(), rpcTimeout)
261267
defer cancel()
268+
log.WithFields(log.Fields{"workloadId": scanRequest.WorkloadID, "artifact": scanRequest.Artifact, "registry": scanRequest.Registry}).Debug("Scan request forwarded to controller")
262269
result, err := client.ScanImage(ctx, &request)
263270
if err != nil {
264271
reportCache.Lock()
265272
report := reportCache.ScanReports[scanRequest.WorkloadID]
266273
report.Status = http.StatusInternalServerError
274+
reportCache.ScanReports[scanRequest.WorkloadID] = report
267275
reportCache.Unlock()
268276
log.WithFields(log.Fields{"error": err}).Error("Error sending scan request")
277+
concurrentJobs.Decrement()
269278
return
270279
}
271280
concurrentJobs.Decrement()
272-
273281
reportCache.Lock()
274-
log.WithFields(log.Fields{"workloadId": scanRequest.WorkloadID, "artifact": scanRequest.Artifact, "registry": scanRequest.Registry}).Debug("Scan request forwarded to controller")
275282
reportCache.ScanReports[scanRequest.WorkloadID] = convertRPCReportToScanReport(result)
276283
reportCache.Unlock()
277284
}
278285

279286
//convertRPCReportToScanReport converts the rpc results from the controller into a Harbor readable format.
280287
func convertRPCReportToScanReport(scanResult *share.ScanResult) ScanReport {
281288
var result ScanReport
282-
//TODO: Finish conversion/translation of Results
283289
result.Status = http.StatusOK
284290
result.Vulnerabilities = convertVulns(scanResult.Vuls)
285291
return result
@@ -315,7 +321,7 @@ func convertVulns(controllerVulns []*share.ScanVulnerability) []Vuln {
315321
func pollMaxConcurrent() (uint32, error) {
316322
client, err := GetControllerServiceClient(serverConfig.ControllerIP, serverConfig.ControllerPort)
317323
if err != nil {
318-
log.WithFields(log.Fields{"error": err}).Error("Error retrieving rpc client")
324+
log.WithFields(log.Fields{"error": err}).Error("Error establishing grpc connection to controller")
319325
return 0, err
320326
}
321327

@@ -390,7 +396,7 @@ func scanResult(w http.ResponseWriter, req *http.Request) {
390396
delete(reportCache.ScanReports, id)
391397
case http.StatusInternalServerError:
392398
log.WithFields(log.Fields{"id": id}).Debug("returned http 500 for workload id")
393-
http.Error(w, "NV Internal Server Error", http.StatusInternalServerError)
399+
w.WriteHeader(http.StatusInternalServerError)
394400
delete(reportCache.ScanReports, id)
395401
default:
396402
w.Header().Add("Location", req.URL.String())

0 commit comments

Comments
 (0)