github.com/Axway/agent-sdk@v1.1.101/pkg/compliance/job.go (about) 1 package compliance 2 3 import ( 4 "time" 5 6 "github.com/Axway/agent-sdk/pkg/agent" 7 "github.com/Axway/agent-sdk/pkg/apic" 8 v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" 9 management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" 10 "github.com/Axway/agent-sdk/pkg/util/log" 11 ) 12 13 const ( 14 SourceCompliancePath = "/source/compliance" 15 ) 16 17 type Processor interface { 18 CollectRuntimeResult(RuntimeResults) error 19 } 20 21 type runtimeComplianceJob struct { 22 logger log.FieldLogger 23 id string 24 processor Processor 25 } 26 27 func (j *runtimeComplianceJob) Status() error { 28 return nil 29 } 30 31 func (j *runtimeComplianceJob) Ready() bool { 32 return true 33 } 34 35 func (j *runtimeComplianceJob) Execute() error { 36 if j.processor != nil { 37 results := &runtimeResults{ 38 logger: j.logger, 39 } 40 j.logger.Info("starting runtime compliance processing") 41 j.processor.CollectRuntimeResult(results) 42 j.publishResources(results) 43 j.logger.Info("completed runtime compliance processing") 44 } 45 return nil 46 } 47 48 func (j *runtimeComplianceJob) publishResources(results *runtimeResults) { 49 cacheManager := agent.GetCacheManager() 50 for instanceName, result := range results.items { 51 ri, err := cacheManager.GetAPIServiceInstanceByName(instanceName) 52 if err != nil { 53 j.logger.WithError(err).WithField("instanceName", instanceName).Warn("skipping instance") 54 continue 55 } 56 57 instance := &management.APIServiceInstance{} 58 instance.FromInstance(ri) 59 if instance.Source != nil { 60 compliance := management.ApiServiceInstanceSourceCompliance{ 61 Runtime: management.ApiServiceInstanceSourceRuntimeStatus{ 62 Result: management.ApiServiceInstanceSourceRuntimeStatusResult{ 63 Timestamp: v1.Time(time.Now()), 64 RiskScore: result.RiskScore, 65 }, 66 }, 67 } 68 69 patches := make([]map[string]interface{}, 0) 70 patches = append(patches, map[string]interface{}{ 71 apic.PatchOperation: apic.PatchOpAdd, 72 apic.PatchPath: SourceCompliancePath, 73 apic.PatchValue: compliance, 74 }) 75 76 logger := j.logger. 77 WithField("instanceId", ri.Metadata.ID). 78 WithField("instanceName", instanceName). 79 WithField("riskScore", result.RiskScore) 80 81 logger.Debug("updating runtime compliance result") 82 _, err := agent.GetCentralClient().PatchSubResource(instance, management.ApiServiceInstanceSourceSubResourceName, patches) 83 if err != nil { 84 logger.WithError(err).Error("failed to updated runtime compliance result") 85 } 86 } 87 } 88 }