Procházet zdrojové kódy

deal with LAPI down : ensure client will reauthenticate (#527)

* to avoid keeping apiclient in broken state, reset the token on error
Thibault "bui" Koechlin před 4 roky
rodič
revize
bb679310c7
4 změnil soubory, kde provedl 12 přidání a 3 odebrání
  1. 0 1
      Makefile
  2. 4 0
      cmd/crowdsec/output.go
  3. 3 1
      pkg/apiclient/auth.go
  4. 5 1
      pkg/apiserver/apic.go

+ 0 - 1
Makefile

@@ -67,7 +67,6 @@ clean:
 	@rm -f $(CSCLI_BIN)
 	@rm -f *.log
 	@rm crowdsec-release.tgz
-	@rm -rf crowdsec-v*
 
 cscli:
 ifeq ($(lastword $(RESPECT_VERSION)), $(CURRENT_GOVERSION))

+ 4 - 0
cmd/crowdsec/output.go

@@ -110,6 +110,10 @@ LOOP:
 				cacheMutex.Unlock()
 				if err := PushAlerts(cachecopy, Client); err != nil {
 					log.Errorf("while pushing to api : %s", err)
+					//just push back the events to the queue
+					cacheMutex.Lock()
+					cache = append(cache, cachecopy...)
+					cacheMutex.Unlock()
 				}
 			}
 		case <-outputsTomb.Dying():

+ 3 - 1
pkg/apiclient/auth.go

@@ -97,7 +97,7 @@ func (t *JWTTransport) refreshJwtToken() error {
 		if err != nil {
 			return fmt.Errorf("can't update scenario list: %s", err)
 		}
-		log.Infof("scenarios list updated for '%s'", *t.MachineID)
+		log.Debugf("scenarios list updated for '%s'", *t.MachineID)
 	}
 
 	var auth = models.WatcherAuthRequest{
@@ -192,6 +192,8 @@ func (t *JWTTransport) RoundTrip(req *http.Request) (*http.Response, error) {
 		log.Tracef("resp-jwt: %s", string(dump))
 	}
 	if err != nil {
+		/*we had an error, reset the token ?*/
+		t.token = ""
 		return resp, errors.Wrapf(err, "performing jwt auth")
 	}
 	log.Debugf("resp-jwt: %d", resp.StatusCode)

+ 5 - 1
pkg/apiserver/apic.go

@@ -301,11 +301,15 @@ func (a *apic) Pull() error {
 	var err error
 
 	scenario := a.scenarioList
+	toldOnce := false
 	for {
 		if len(scenario) > 0 {
 			break
 		}
-		log.Warningf("scenario list is empty, will not pull yet")
+		if !toldOnce {
+			log.Warningf("scenario list is empty, will not pull yet")
+			toldOnce = true
+		}
 		time.Sleep(1 * time.Second)
 		scenario, err = a.FetchScenariosListFromDB()
 		if err != nil {