Skip to content

Commit

Permalink
QA-235: pipeline: Switch to static checks by golangci-lint
Browse files Browse the repository at this point in the history
And fix the code accordingly.

Changelog: None

Signed-off-by: Lluis Campos <[email protected]>
  • Loading branch information
lluiscampos committed Dec 1, 2021
1 parent 1ab1ba6 commit 48ef2dd
Show file tree
Hide file tree
Showing 48 changed files with 881 additions and 399 deletions.
5 changes: 3 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ stages:
- trigger

include:
- project: 'Northern.tech/Mender/mendertesting'
file: '.gitlab-ci-check-golang-lint.yml'
- project: 'Northern.tech/Mender/mendertesting'
file: '.gitlab-ci-github-status-updates.yml'
- project: 'Northern.tech/Mender/mendertesting'
Expand All @@ -17,10 +19,9 @@ test:
image: golang:1.17
before_script:
- apt-get update && apt-get install -yyq liblzma-dev libssl-dev libglib2.0-dev dbus clang-format-9
- make get-tools
- GO111MODULE=off go get -u github.com/jstemmer/go-junit-report
script:
- git ls-tree -r --name-only HEAD | grep -v vendor/ | grep '\.[ch]$' | xargs clang-format-9 -i
- make extracheck
- make coverage
- make
- mkdir -p tests/unit-coverage && find . -name 'coverage.txt' -exec cp --parents {} ./tests/unit-coverage \;
Expand Down
131 changes: 78 additions & 53 deletions app/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ func maybeInvalidateCachedAuthorizationToken(
}
err = txn.WriteAll(datastore.AuthTokenCacheInvalidatorName, dbValue)
if err != nil {
return fmt.Errorf("Failed to cache the currently used tenant token to the DB. Error %s", err.Error())
return fmt.Errorf(
"Failed to cache the currently used tenant token to the DB. Error %s",
err.Error(),
)
}
return nil
}
Expand All @@ -249,7 +252,10 @@ func maybeInvalidateCachedAuthorizationToken(
// Remove works even if there is no authorization token cached
err = txn.Remove(datastore.AuthTokenName)
if err != nil {
return fmt.Errorf("Failed to remove the cached tenant token from the database. Error %s", err.Error())
return fmt.Errorf(
"Failed to remove the cached tenant token from the database. Error %s",
err.Error(),
)
}
err = txn.WriteAll(datastore.AuthTokenCacheInvalidatorName, dbValue)
if err != nil {
Expand Down Expand Up @@ -290,41 +296,59 @@ func (m *MenderAuthManager) GetBroadcastMessageChan(name string) <-chan AuthMana

func (m *menderAuthManagerService) registerDBusCallbacks() (unregisterFunc func()) {
// GetJwtToken
m.dbus.RegisterMethodCallCallback(AuthManagerDBusPath, AuthManagerDBusInterfaceName, "GetJwtToken", func(objectPath, interfaceName, methodName string, parameters string) (interface{}, error) {
respChan := make(chan AuthManagerResponse)
m.inChan <- AuthManagerRequest{
Action: ActionGetAuthToken,
ResponseChannel: respChan,
}
select {
case message := <-respChan:
tokenAndServerURL := dbus.TokenAndServerURL{
Token: string(message.AuthToken),
ServerURL: m.serverURL,
m.dbus.RegisterMethodCallCallback(
AuthManagerDBusPath,
AuthManagerDBusInterfaceName,
"GetJwtToken",
func(objectPath, interfaceName, methodName string, parameters string) (interface{}, error) {
respChan := make(chan AuthManagerResponse)
m.inChan <- AuthManagerRequest{
Action: ActionGetAuthToken,
ResponseChannel: respChan,
}
return tokenAndServerURL, message.Error
case <-time.After(5 * time.Second):
}
return string(noAuthToken), errors.New("timeout when calling GetJwtToken")
})
select {
case message := <-respChan:
tokenAndServerURL := dbus.TokenAndServerURL{
Token: string(message.AuthToken),
ServerURL: m.serverURL,
}
return tokenAndServerURL, message.Error
case <-time.After(5 * time.Second):
}
return string(noAuthToken), errors.New("timeout when calling GetJwtToken")
},
)
// FetchJwtToken
m.dbus.RegisterMethodCallCallback(AuthManagerDBusPath, AuthManagerDBusInterfaceName, "FetchJwtToken", func(objectPath, interfaceName, methodName string, parameters string) (interface{}, error) {
respChan := make(chan AuthManagerResponse)
m.inChan <- AuthManagerRequest{
Action: ActionFetchAuthToken,
ResponseChannel: respChan,
}
select {
case message := <-respChan:
return message.Event == EventFetchAuthToken, message.Error
case <-time.After(5 * time.Second):
}
return false, errors.New("timeout when calling FetchJwtToken")
})
m.dbus.RegisterMethodCallCallback(
AuthManagerDBusPath,
AuthManagerDBusInterfaceName,
"FetchJwtToken",
func(objectPath, interfaceName, methodName string, parameters string) (interface{}, error) {
respChan := make(chan AuthManagerResponse)
m.inChan <- AuthManagerRequest{
Action: ActionFetchAuthToken,
ResponseChannel: respChan,
}
select {
case message := <-respChan:
return message.Event == EventFetchAuthToken, message.Error
case <-time.After(5 * time.Second):
}
return false, errors.New("timeout when calling FetchJwtToken")
},
)

return func() {
m.dbus.UnregisterMethodCallCallback(AuthManagerDBusPath, AuthManagerDBusInterfaceName, "FetchJwtToken")
m.dbus.UnregisterMethodCallCallback(AuthManagerDBusPath, AuthManagerDBusInterfaceName, "GetJwtToken")
m.dbus.UnregisterMethodCallCallback(
AuthManagerDBusPath,
AuthManagerDBusInterfaceName,
"FetchJwtToken",
)
m.dbus.UnregisterMethodCallCallback(
AuthManagerDBusPath,
AuthManagerDBusInterfaceName,
"GetJwtToken",
)
}
}

Expand Down Expand Up @@ -355,22 +379,27 @@ func (m *menderAuthManagerService) run() {
}()

// run the DBus interface, if available
dbusConn := dbus.Handle(nil)
dbusLoop := dbus.MainLoop(nil)
if m.dbus != nil {
var err error
if dbusConn, err = m.dbus.BusGet(dbus.GBusTypeSystem); err == nil {
if dbusConn, err := m.dbus.BusGet(dbus.GBusTypeSystem); err == nil {
m.dbusConn = dbusConn

nameGid, err := m.dbus.BusOwnNameOnConnection(dbusConn, AuthManagerDBusObjectName,
dbus.DBusNameOwnerFlagsAllowReplacement|dbus.DBusNameOwnerFlagsReplace)
if err != nil {
log.Errorf("Could not own DBus name '%s': %s", AuthManagerDBusObjectName, err.Error())
log.Errorf(
"Could not own DBus name '%s': %s",
AuthManagerDBusObjectName,
err.Error(),
)
goto mainloop
}
defer m.dbus.BusUnownName(nameGid)

intGid, err := m.dbus.BusRegisterInterface(dbusConn, AuthManagerDBusPath, AuthManagerDBusInterface)
intGid, err := m.dbus.BusRegisterInterface(
dbusConn,
AuthManagerDBusPath,
AuthManagerDBusInterface,
)
if err != nil {
log.Errorf("Could register DBus interface name '%s' at path '%s': %s",
AuthManagerDBusInterface, AuthManagerDBusPath, err.Error())
Expand All @@ -381,7 +410,7 @@ func (m *menderAuthManagerService) run() {
unregisterFunc := m.registerDBusCallbacks()
defer unregisterFunc()

dbusLoop = m.dbus.MainLoopNew()
dbusLoop := m.dbus.MainLoopNew()
go m.dbus.MainLoopRun(dbusLoop)
defer m.dbus.MainLoopQuit(dbusLoop)
}
Expand Down Expand Up @@ -427,24 +456,21 @@ mainloop:
case <-m.quitReq:
running = false
m.workerChan <- AuthManagerRequest{}
break
}
}
}

// This is a helper to the main loop, for tasks that may take a long time. It's
// running in a separate Go routine.
func (m *menderAuthManagerService) longRunningWorkerLoop() {
for {
select {
case msg := <-m.workerChan:
switch msg.Action {
case ActionFetchAuthToken:
m.fetchAuthToken()
case "":
// Quit loop.
return
}
for msg := range m.workerChan {
switch msg.Action {
case ActionFetchAuthToken:
m.fetchAuthToken()
case "":
// Quit loop.
return

}
}
}
Expand Down Expand Up @@ -492,7 +518,7 @@ func (m *menderAuthManagerService) broadcast(message AuthManagerResponse) {
Token: string(message.AuthToken),
ServerURL: m.serverURL,
}
m.dbus.EmitSignal(m.dbusConn, "", AuthManagerDBusPath,
_ = m.dbus.EmitSignal(m.dbusConn, "", AuthManagerDBusPath,
AuthManagerDBusInterfaceName, AuthManagerDBusSignalJwtTokenStateChange,
tokenAndServerURL)
}
Expand Down Expand Up @@ -588,7 +614,6 @@ func (m *menderAuthManagerService) fetchAuthToken() {
m.serverURL = serverURL

log.Info("successfully received new authorization data")
return
}

// ForceBootstrap forces the bootstrap
Expand Down
3 changes: 2 additions & 1 deletion app/client_closures.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package app

import (
log "github.com/sirupsen/logrus"

"github.com/mendersoftware/mender/client"
"github.com/mendersoftware/mender/conf"
log "github.com/sirupsen/logrus"
)

// see client.go: ApiRequest.Do()
Expand Down
5 changes: 3 additions & 2 deletions app/deployment_logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Northern.tech AS
// Copyright 2021 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -26,8 +26,9 @@ import (
"strings"
"syscall"

"github.com/mendersoftware/mender/conf"
log "github.com/sirupsen/logrus"

"github.com/mendersoftware/mender/conf"
)

// error messages
Expand Down
Loading

0 comments on commit 48ef2dd

Please sign in to comment.