@@ -20,11 +20,11 @@ import (
2020)
2121
2222type PluginConfig struct {
23- Name string `yaml:"name"`
24- CustomerID string `yaml:"customer_id"`
25- SharedKey string `yaml:"shared_key"`
26- LogType string `yaml:"log_type"`
27- LogLevel * string `yaml:"log_level"`
23+ Name string `yaml:"name"`
24+ CustomerID string `yaml:"customer_id"`
25+ SharedKey string `yaml:"shared_key"`
26+ LogType string `yaml:"log_type"`
27+ LogLevel string `yaml:"log_level"`
2828}
2929
3030type SentinelPlugin struct {
@@ -39,43 +39,48 @@ var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
3939 JSONFormat : true ,
4040})
4141
42- func (s * SentinelPlugin ) getAuthorizationHeader (now string , length int , pluginName string ) (string , error ) {
42+ func (s * SentinelPlugin ) getAuthorizationHeader (now string , length int , name string ) (string , error ) {
4343 xHeaders := "X-Ms-Date:" + now
4444
45+ cfg := s .PluginConfigByName [name ]
46+
4547 stringToHash := fmt .Sprintf ("POST\n %d\n application/json\n %s\n /api/logs" , length , xHeaders )
46- decodedKey , _ := base64 .StdEncoding .DecodeString (s . PluginConfigByName [ pluginName ] .SharedKey )
48+ decodedKey , _ := base64 .StdEncoding .DecodeString (cfg .SharedKey )
4749
4850 h := hmac .New (sha256 .New , decodedKey )
4951 h .Write ([]byte (stringToHash ))
5052
5153 encodedHash := base64 .StdEncoding .EncodeToString (h .Sum (nil ))
52- authorization := "SharedKey " + s . PluginConfigByName [ pluginName ] .CustomerID + ":" + encodedHash
54+ authorization := "SharedKey " + cfg .CustomerID + ":" + encodedHash
5355
5456 logger .Trace ("authorization header" , "header" , authorization )
5557
5658 return authorization , nil
5759}
5860
5961func (s * SentinelPlugin ) Notify (ctx context.Context , notification * protobufs.Notification ) (* protobufs.Empty , error ) {
60- if _ , ok := s .PluginConfigByName [notification .GetName ()]; ! ok {
61- return nil , fmt .Errorf ("invalid plugin config name %s" , notification .GetName ())
62- }
62+ name := notification .GetName ()
63+ cfg , ok := s .PluginConfigByName [name ]
6364
64- cfg := s .PluginConfigByName [notification .GetName ()]
65+ if ! ok {
66+ return nil , fmt .Errorf ("invalid plugin config name %s" , name )
67+ }
6568
66- if cfg .LogLevel != nil && * cfg . LogLevel != "" {
67- logger .SetLevel (hclog .LevelFromString (* cfg .LogLevel ))
69+ if cfg .LogLevel != "" {
70+ logger .SetLevel (hclog .LevelFromString (cfg .LogLevel ))
6871 }
6972
70- logger .Info ("received notification for sentinel config" , "name" , notification .GetName ())
73+ logger .Info ("received notification for sentinel config" , "name" , name )
74+
75+ text := notification .GetText ()
7176
72- url := fmt .Sprintf ("https://%s.ods.opinsights.azure.com/api/logs?api-version=2016-04-01" , s . PluginConfigByName [ notification . GetName ()] .CustomerID )
73- body := strings .NewReader (notification . GetText () )
77+ url := fmt .Sprintf ("https://%s.ods.opinsights.azure.com/api/logs?api-version=2016-04-01" , cfg .CustomerID )
78+ body := strings .NewReader (text )
7479
7580 // Cannot use time.RFC1123 as azure wants GMT, not UTC
7681 now := time .Now ().UTC ().Format ("Mon, 02 Jan 2006 15:04:05 GMT" )
7782
78- authorization , err := s .getAuthorizationHeader (now , len (notification . GetText ()), notification . GetName () )
83+ authorization , err := s .getAuthorizationHeader (now , len (text ), name )
7984 if err != nil {
8085 return & protobufs.Empty {}, err
8186 }
@@ -87,7 +92,7 @@ func (s *SentinelPlugin) Notify(ctx context.Context, notification *protobufs.Not
8792 }
8893
8994 req .Header .Set ("Content-Type" , "application/json" )
90- req .Header .Set ("Log-Type" , s . PluginConfigByName [ notification . GetName ()] .LogType )
95+ req .Header .Set ("Log-Type" , cfg .LogType )
9196 req .Header .Set ("Authorization" , authorization )
9297 req .Header .Set ("X-Ms-Date" , now )
9398
0 commit comments