|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + sync "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/threatwinds/go-sdk/catcher" |
| 12 | + "github.com/threatwinds/go-sdk/plugins" |
| 13 | + "google.golang.org/grpc" |
| 14 | + codes "google.golang.org/grpc/codes" |
| 15 | + "google.golang.org/grpc/connectivity" |
| 16 | + "google.golang.org/grpc/credentials/insecure" |
| 17 | + "google.golang.org/grpc/metadata" |
| 18 | + "google.golang.org/grpc/status" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + reconnectDelay = 5 * time.Second |
| 23 | + maxMessageSize = 1024 * 1024 * 1024 |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + cnf *ConfigurationSection |
| 28 | + mu sync.Mutex |
| 29 | + |
| 30 | + internalKey string |
| 31 | + modulesConfigHost string |
| 32 | +) |
| 33 | + |
| 34 | +func GetConfig() *ConfigurationSection { |
| 35 | + mu.Lock() |
| 36 | + defer mu.Unlock() |
| 37 | + if cnf == nil { |
| 38 | + return &ConfigurationSection{} |
| 39 | + } |
| 40 | + return cnf |
| 41 | +} |
| 42 | + |
| 43 | +func StartConfigurationSystem() { |
| 44 | + for { |
| 45 | + pluginConfig := plugins.PluginCfg("com.utmstack", false) |
| 46 | + if !pluginConfig.Exists() { |
| 47 | + _ = catcher.Error("plugin configuration not found", nil, nil) |
| 48 | + time.Sleep(reconnectDelay) |
| 49 | + continue |
| 50 | + } |
| 51 | + internalKey = pluginConfig.Get("internalKey").String() |
| 52 | + modulesConfigHost = pluginConfig.Get("modulesConfig").String() |
| 53 | + |
| 54 | + if internalKey == "" || modulesConfigHost == "" { |
| 55 | + fmt.Println("Internal key or Modules Config Host is not set, skipping UTMStack plugin execution") |
| 56 | + time.Sleep(reconnectDelay) |
| 57 | + continue |
| 58 | + } |
| 59 | + break |
| 60 | + } |
| 61 | + |
| 62 | + for { |
| 63 | + ctx, cancel := context.WithCancel(context.Background()) |
| 64 | + defer cancel() |
| 65 | + ctx = metadata.AppendToOutgoingContext(ctx, "internal-key", internalKey) |
| 66 | + conn, err := grpc.NewClient( |
| 67 | + modulesConfigHost, |
| 68 | + grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 69 | + grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMessageSize)), |
| 70 | + ) |
| 71 | + |
| 72 | + if err != nil { |
| 73 | + catcher.Error("Failed to connect to server", err, nil) |
| 74 | + cancel() |
| 75 | + time.Sleep(reconnectDelay) |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + state := conn.GetState() |
| 80 | + if state == connectivity.Shutdown || state == connectivity.TransientFailure { |
| 81 | + catcher.Error("Connection is in shutdown or transient failure state", nil, nil) |
| 82 | + cancel() |
| 83 | + time.Sleep(reconnectDelay) |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + client := NewConfigServiceClient(conn) |
| 88 | + stream, err := client.StreamConfig(ctx) |
| 89 | + if err != nil { |
| 90 | + catcher.Error("Failed to create stream", err, nil) |
| 91 | + conn.Close() |
| 92 | + cancel() |
| 93 | + time.Sleep(reconnectDelay) |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + err = stream.Send(&BiDirectionalMessage{ |
| 98 | + Payload: &BiDirectionalMessage_PluginInit{ |
| 99 | + PluginInit: &PluginInit{Type: PluginType_CROWDSTRIKE}, |
| 100 | + }, |
| 101 | + }) |
| 102 | + if err != nil { |
| 103 | + catcher.Error("Failed to send PluginInit", err, nil) |
| 104 | + conn.Close() |
| 105 | + cancel() |
| 106 | + time.Sleep(reconnectDelay) |
| 107 | + continue |
| 108 | + } |
| 109 | + |
| 110 | + for { |
| 111 | + in, err := stream.Recv() |
| 112 | + if err != nil { |
| 113 | + if strings.Contains(err.Error(), "EOF") { |
| 114 | + catcher.Info("Stream closed by server, reconnecting...", nil) |
| 115 | + conn.Close() |
| 116 | + cancel() |
| 117 | + time.Sleep(reconnectDelay) |
| 118 | + break |
| 119 | + } |
| 120 | + st, ok := status.FromError(err) |
| 121 | + if ok && (st.Code() == codes.Unavailable || st.Code() == codes.Canceled) { |
| 122 | + catcher.Error("Stream error: "+st.Message(), err, nil) |
| 123 | + conn.Close() |
| 124 | + cancel() |
| 125 | + time.Sleep(reconnectDelay) |
| 126 | + break |
| 127 | + } else { |
| 128 | + catcher.Error("Stream receive error", err, nil) |
| 129 | + time.Sleep(reconnectDelay) |
| 130 | + continue |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + switch message := in.Payload.(type) { |
| 135 | + case *BiDirectionalMessage_Config: |
| 136 | + log.Printf("Received configuration update: %v", message.Config) |
| 137 | + cnf = message.Config |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments