@@ -7,6 +7,7 @@ package instance
77
88import (
99 "context"
10+ "errors"
1011 "log/slog"
1112 "os"
1213 "strings"
@@ -26,72 +27,71 @@ const (
2627)
2728
2829type (
29- NginxAppProtectProcessParser struct {
30+ NginxAppProtectParser struct {
3031 versionFilePath string
3132 releaseFilePath string
3233 attackSignatureVersionFilePath string
3334 threatCampaignVersionFilePath string
3435 }
3536)
3637
37- var _ processParser = (* NginxAppProtectProcessParser )(nil )
38-
39- func NewNginxAppProtectProcessParser () * NginxAppProtectProcessParser {
40- return & NginxAppProtectProcessParser {
38+ func NewNginxAppProtectParser () * NginxAppProtectParser {
39+ return & NginxAppProtectParser {
4140 versionFilePath : versionFilePath ,
4241 releaseFilePath : releaseFilePath ,
4342 attackSignatureVersionFilePath : attackSignatureVersionFilePath ,
4443 threatCampaignVersionFilePath : threatCampaignVersionFilePath ,
4544 }
4645}
4746
48- func (n NginxAppProtectProcessParser ) Parse (
47+ func (n NginxAppProtectParser ) Parse (
4948 ctx context.Context ,
50- processes []* nginxprocess.Process ,
5149) map [string ]* mpi.Instance {
5250 instanceMap := make (map [string ]* mpi.Instance ) // key is instanceID
5351
54- for _ , process := range processes {
55- if process .Name == processName {
56- instanceID := n .instanceID (process )
57-
58- binaryPath := process .Exe
59- if binaryPath == "" {
60- binaryPath = strings .Split (process .Cmd , " " )[0 ]
61- }
62-
63- instanceMap [instanceID ] = & mpi.Instance {
64- InstanceMeta : & mpi.InstanceMeta {
65- InstanceId : instanceID ,
66- InstanceType : mpi .InstanceMeta_INSTANCE_TYPE_NGINX_APP_PROTECT ,
67- Version : n .instanceVersion (ctx ),
68- },
69- InstanceConfig : & mpi.InstanceConfig {},
70- InstanceRuntime : & mpi.InstanceRuntime {
71- ProcessId : process .PID ,
72- BinaryPath : binaryPath ,
73- ConfigPath : "" ,
74- Details : & mpi.InstanceRuntime_NginxAppProtectRuntimeInfo {
75- NginxAppProtectRuntimeInfo : & mpi.NGINXAppProtectRuntimeInfo {
76- Release : n .release (ctx ),
77- AttackSignatureVersion : n .attackSignatureVersion (ctx ),
78- ThreatCampaignVersion : n .threatCampaignVersion (ctx ),
79- },
52+ if n .isNAPInstance () {
53+ instanceID := id .Generate ("" )
54+
55+ instanceMap [instanceID ] = & mpi.Instance {
56+ InstanceMeta : & mpi.InstanceMeta {
57+ InstanceId : instanceID ,
58+ InstanceType : mpi .InstanceMeta_INSTANCE_TYPE_NGINX_APP_PROTECT ,
59+ Version : n .instanceVersion (ctx ),
60+ },
61+ InstanceConfig : & mpi.InstanceConfig {},
62+ InstanceRuntime : & mpi.InstanceRuntime {
63+ ProcessId : 0 ,
64+ BinaryPath : "" ,
65+ ConfigPath : "" ,
66+ Details : & mpi.InstanceRuntime_NginxAppProtectRuntimeInfo {
67+ NginxAppProtectRuntimeInfo : & mpi.NGINXAppProtectRuntimeInfo {
68+ Release : n .release (ctx ),
69+ AttackSignatureVersion : n .attackSignatureVersion (ctx ),
70+ ThreatCampaignVersion : n .threatCampaignVersion (ctx ),
8071 },
81- InstanceChildren : make ([]* mpi.InstanceChild , 0 ),
8272 },
83- }
73+ InstanceChildren : make ([]* mpi.InstanceChild , 0 ),
74+ },
8475 }
8576 }
8677
8778 return instanceMap
8879}
8980
90- func (n NginxAppProtectProcessParser ) instanceID (process * nginxprocess.Process ) string {
81+ func (n NginxAppProtectParser ) isNAPInstance () bool {
82+ _ , errVersion := os .Stat (n .versionFilePath )
83+ _ , errRelease := os .Stat (n .releaseFilePath )
84+ if errors .Is (errVersion , os .ErrNotExist ) || errors .Is (errRelease , os .ErrNotExist ) {
85+ return false
86+ }
87+ return true
88+ }
89+
90+ func (n NginxAppProtectParser ) instanceID (process * nginxprocess.Process ) string {
9191 return id .Generate ("%s" , process .Exe )
9292}
9393
94- func (n NginxAppProtectProcessParser ) instanceVersion (ctx context.Context ) string {
94+ func (n NginxAppProtectParser ) instanceVersion (ctx context.Context ) string {
9595 version , err := os .ReadFile (n .versionFilePath )
9696 if err != nil {
9797 slog .WarnContext (ctx , "Unable to read NAP version file" , "file_path" , n .versionFilePath , "error" , err )
@@ -101,7 +101,7 @@ func (n NginxAppProtectProcessParser) instanceVersion(ctx context.Context) strin
101101 return strings .TrimSuffix (string (version ), "\n " )
102102}
103103
104- func (n NginxAppProtectProcessParser ) release (ctx context.Context ) string {
104+ func (n NginxAppProtectParser ) release (ctx context.Context ) string {
105105 release , err := os .ReadFile (n .releaseFilePath )
106106 if err != nil {
107107 slog .WarnContext (ctx , "Unable to read NAP release file" , "file_path" , n .releaseFilePath , "error" , err )
@@ -111,7 +111,7 @@ func (n NginxAppProtectProcessParser) release(ctx context.Context) string {
111111 return strings .TrimSuffix (string (release ), "\n " )
112112}
113113
114- func (n NginxAppProtectProcessParser ) attackSignatureVersion (ctx context.Context ) string {
114+ func (n NginxAppProtectParser ) attackSignatureVersion (ctx context.Context ) string {
115115 attackSignatureVersion , err := os .ReadFile (n .attackSignatureVersionFilePath )
116116 if err != nil {
117117 slog .WarnContext (
@@ -127,7 +127,7 @@ func (n NginxAppProtectProcessParser) attackSignatureVersion(ctx context.Context
127127 return string (attackSignatureVersion )
128128}
129129
130- func (n NginxAppProtectProcessParser ) threatCampaignVersion (ctx context.Context ) string {
130+ func (n NginxAppProtectParser ) threatCampaignVersion (ctx context.Context ) string {
131131 threatCampaignVersion , err := os .ReadFile (n .threatCampaignVersionFilePath )
132132 if err != nil {
133133 slog .WarnContext (
0 commit comments