Skip to content

Commit

Permalink
apiclient/apiserver: lint/2 (#2741)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Jan 15, 2024
1 parent 75d8ad9 commit 48f011d
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pkg/apiclient/alerts_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *AlertsService) Add(ctx context.Context, alerts models.AddAlertsRequest)
return nil, nil, err
}

var addedIds models.AddAlertsResponse
addedIds := models.AddAlertsResponse{}

resp, err := s.client.Do(ctx, req, &addedIds)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/apiclient/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (h *HeartBeatService) StartHeartBeat(ctx context.Context, t *tomb.Tomb) {
log.Errorf("heartbeat unexpected return code: %d", resp.Response.StatusCode)
continue
}

if !ok {
log.Errorf("heartbeat returned false")
continue
Expand Down
1 change: 1 addition & 0 deletions pkg/apiserver/apic.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ func (a *apic) PullTop(forcePull bool) error {
}

addCounters, deleteCounters := makeAddAndDeleteCounters()

// process deleted decisions
nbDeleted, err := a.HandleDeletedDecisionsV3(data.Deleted, deleteCounters)
if err != nil {
Expand Down
14 changes: 11 additions & 3 deletions pkg/apiserver/controllers/v1/decisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ func FormatDecisions(decisions []*ent.Decision) []*models.Decision {
}

func (c *Controller) GetDecision(gctx *gin.Context) {
var err error
var results []*models.Decision
var data []*ent.Decision
var (
results []*models.Decision
data []*ent.Decision
)

bouncerInfo, err := getBouncerFromContext(gctx)
if err != nil {
Expand Down Expand Up @@ -89,6 +90,7 @@ func (c *Controller) DeleteDecisionById(gctx *gin.Context) {

return
}

nbDeleted, deletedFromDB, err := c.DBClient.SoftDeleteDecisionByID(decisionID)
if err != nil {
c.HandleDBErrors(gctx, err)
Expand Down Expand Up @@ -351,27 +353,33 @@ func (c *Controller) StreamDecisionNonChunked(gctx *gin.Context, bouncerInfo *en
if err != nil {
log.Errorf("unable to query expired decision for '%s' : %v", bouncerInfo.Name, err)
gctx.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()})

return err
}

ret["deleted"] = FormatDecisions(data)
gctx.JSON(http.StatusOK, ret)

return nil
}

func (c *Controller) StreamDecision(gctx *gin.Context) {
var err error

streamStartTime := time.Now().UTC()

bouncerInfo, err := getBouncerFromContext(gctx)
if err != nil {
gctx.JSON(http.StatusUnauthorized, gin.H{"message": "not allowed"})

return
}

if gctx.Request.Method == http.MethodHead {
//For HEAD, just return as the bouncer won't get a body anyway, so no need to query the db
//We also don't update the last pull time, as it would mess with the delta sent on the next request (if done without startup=true)
gctx.String(http.StatusOK, "")

return
}

Expand Down
1 change: 1 addition & 0 deletions pkg/apiserver/controllers/v1/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func PrometheusBouncersMiddleware() gin.HandlerFunc {
func PrometheusMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
startTime := time.Now()

LapiRouteHits.With(prometheus.Labels{
"route": c.Request.URL.Path,
"method": c.Request.Method}).Inc()
Expand Down
1 change: 0 additions & 1 deletion pkg/apiserver/middlewares/v1/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func (a *APIKey) MiddlewareFunc() gin.HandlerFunc {
}

c.Set(bouncerContextKey, bouncer)

c.Next()
}
}
24 changes: 19 additions & 5 deletions pkg/apiserver/middlewares/v1/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func PayloadFunc(data interface{}) jwt.MapClaims {
func IdentityHandler(c *gin.Context) interface{} {
claims := jwt.ExtractClaims(c)
machineID := claims[identityKey].(string)

return &models.WatcherAuthRequest{
MachineID: &machineID,
}
Expand Down Expand Up @@ -93,9 +94,12 @@ func (j *JWT) authTLS(c *gin.Context) (*authInput, error) {
"ip": c.ClientIP(),
"cn": extractedCN,
}).Errorf("error generating password: %s", err)

return nil, fmt.Errorf("error generating password")
}

password := strfmt.Password(pwd)

ret.clientMachine, err = j.DbClient.CreateMachine(&ret.machineID, &password, "", true, true, types.TlsAuthType)
if err != nil {
return nil, fmt.Errorf("while creating machine entry for %s: %w", ret.machineID, err)
Expand All @@ -114,27 +118,33 @@ func (j *JWT) authTLS(c *gin.Context) (*authInput, error) {
}{
Scenarios: []string{},
}

err = c.ShouldBindJSON(&loginInput)
if err != nil {
return nil, fmt.Errorf("missing scenarios list in login request for TLS auth: %w", err)
}

ret.scenariosInput = loginInput.Scenarios

return &ret, nil
}

func (j *JWT) authPlain(c *gin.Context) (*authInput, error) {
var loginInput models.WatcherAuthRequest
var err error
var (
loginInput models.WatcherAuthRequest
err error
)

ret := authInput{}

if err = c.ShouldBindJSON(&loginInput); err != nil {
return nil, fmt.Errorf("missing: %w", err)
}

if err = loginInput.Validate(strfmt.Default); err != nil {
return nil, err
}

ret.machineID = *loginInput.MachineID
password := *loginInput.Password
ret.scenariosInput = loginInput.Scenarios
Expand Down Expand Up @@ -168,8 +178,10 @@ func (j *JWT) authPlain(c *gin.Context) (*authInput, error) {
}

func (j *JWT) Authenticator(c *gin.Context) (interface{}, error) {
var err error
var auth *authInput
var (
err error
auth *authInput
)

if c.Request.TLS != nil && len(c.Request.TLS.PeerCertificates) > 0 {
auth, err = j.authTLS(c)
Expand All @@ -193,6 +205,7 @@ func (j *JWT) Authenticator(c *gin.Context) (interface{}, error) {
scenarios += "," + scenario
}
}

err = j.DbClient.UpdateMachineScenarios(scenarios, auth.clientMachine.ID)
if err != nil {
log.Errorf("Failed to update scenarios list for '%s': %s\n", auth.machineID, err)
Expand All @@ -210,6 +223,7 @@ func (j *JWT) Authenticator(c *gin.Context) (interface{}, error) {

if auth.clientMachine.IpAddress != c.ClientIP() && auth.clientMachine.IpAddress != "" {
log.Warningf("new IP address detected for machine '%s': %s (old: %s)", auth.clientMachine.MachineId, c.ClientIP(), auth.clientMachine.IpAddress)

err = j.DbClient.UpdateMachineIP(c.ClientIP(), auth.clientMachine.ID)
if err != nil {
log.Errorf("Failed to update ip address for '%s': %s\n", auth.clientMachine.MachineId, err)
Expand All @@ -228,10 +242,10 @@ func (j *JWT) Authenticator(c *gin.Context) (interface{}, error) {
log.Errorf("bad user agent from : %s", c.ClientIP())
return nil, jwt.ErrFailedAuthentication
}

return &models.WatcherAuthRequest{
MachineID: &auth.machineID,
}, nil

}

func Authorizator(data interface{}, c *gin.Context) bool {
Expand Down
1 change: 1 addition & 0 deletions pkg/apiserver/middlewares/v1/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ func NewMiddlewares(dbClient *database.Client) (*Middlewares, error) {
}

ret.APIKey = NewAPIKey(dbClient)

return ret, nil
}
Loading

0 comments on commit 48f011d

Please sign in to comment.