Skip to content

Conversation

@Lagovas
Copy link
Collaborator

@Lagovas Lagovas commented Feb 12, 2019

  • added error codes for all warning/error messages
  • synchronized some messages for mysql/postgresql
  • removed old/redundant messages
  • added killing processes in integrations tests via pidof to catch situations with sighup/restart on acra-server
  • change blocking method in main function for acra-server, swapped SIGHUP with SIGTERM handler. actually it doesn't change any logic but tried it to fix unknown sighup messages on circleci. thought that maybe it is some golang runtime special behavior with closing channel at end or something like that...
  • pass logger's with context to inner functions to log debug messages with related client/zone id
  • avoid error logs with decryption failures when it expected and are allowed (binary data but not acrastruct) and use warnings instead errors when it failure on acrastruct (to notify clients because may be situation when clients think that all data correctly encrypted but it's not)

Copy link
Collaborator

@vixentael vixentael left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have few suggestions about logs wording, but overall it looks great!

acraCensor.AddHandler(queryCaptureHandler)
default:
acraCensor.logger.Errorln("Unexpected handler in configuration")
acraCensor.logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCensorSetupError).Errorln("Unexpected handler in configuration")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
acraCensor.logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCensorSetupError).Errorln("Unexpected handler in configuration")
acraCensor.logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCensorSetupError).Errorln("Unexpected handler in configuration: probably AcraCensor configuration (acra-censor.yaml) is outdated.")

func (handler *DenyAllHandler) CheckQuery(sqlQuery string, parsedQuery sqlparser.Statement) (bool, error) {
// deny any query and stop further checks
handler.logger.Errorf("Query has been block by Denyall handler")
handler.logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCensorQueryIsNotAllowed).Errorf("Query has been block by Denyall handler")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds a bit weird, but we started using "deny" everywhere, so..

Suggested change
handler.logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCensorQueryIsNotAllowed).Errorf("Query has been block by Denyall handler")
handler.logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCensorQueryIsNotAllowed).Errorf("Query has been denied by Denyall handler")

spanContext, err := network.ReadTrace(wrappedConnection)
if err != nil {
log.WithError(err).Errorln("Can't read trace from Acra-Proxy")
log.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorTracingCantReadTrace).WithError(err).Errorln("Can't read trace from Acra-Proxy")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorTracingCantReadTrace).WithError(err).Errorln("Can't read trace from Acra-Proxy")
log.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorTracingCantReadTrace).WithError(err).Errorln("Can't read trace from AcraConnector")

err := server.Serve(listener)
if err != nil {
logrus.WithError(err).Errorln("Error from http server that process prometheus metrics")
logrus.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorPrometheusHTTPHandler).WithError(err).Errorln("Error from http server that process prometheus metrics")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that you prefer HTTP now :)

Suggested change
logrus.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorPrometheusHTTPHandler).WithError(err).Errorln("Error from http server that process prometheus metrics")
logrus.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorPrometheusHTTPHandler).WithError(err).Errorln("Error from HTTP server that process prometheus metrics")

span.AddAttributes(trace.BoolAttribute("failed_decryption", true))
base.AcrastructDecryptionCounter.WithLabelValues(base.DecryptionTypeFail).Inc()
logger.WithError(err).Warningln("Can't unwrap symmetric key")
logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorDecryptorCantDecryptBinary).WithError(err).Warningln("Can't decrypt AcraStruct")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imagine we get these logs from customer. How could we understand where exactly decryption failed.

Suggested change
logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorDecryptorCantDecryptBinary).WithError(err).Warningln("Can't decrypt AcraStruct")
logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorDecryptorCantDecryptBinary).WithError(err).Warningln("Can't decrypt AcraStruct: can't unwrap symmetric key")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im not agree. this info will be available in debug mode. but I removed logging as warnings such deep information where it was failed. now it's general warning that can't decrypt probably matched AcraStruct. and in common cases it should not be often logged

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, makes sense

span.AddAttributes(trace.BoolAttribute("failed_decryption", true))
base.AcrastructDecryptionCounter.WithLabelValues(base.DecryptionTypeFail).Inc()
logger.WithError(err).Warningln("Can't decrypt data with unwrapped symmetric key")
logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorDecryptorCantDecryptBinary).WithError(err).Warningln("Can't decrypt data with unwrapped symmetric key")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we could improve this error message too?

Suggested change
logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorDecryptorCantDecryptBinary).WithError(err).Warningln("Can't decrypt data with unwrapped symmetric key")
logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorDecryptorCantDecryptBinary).WithError(err).Warningln("Can't decrypt AcraStruct: can't decrypt data with unwrapped symmetric key")


import "fmt"

// TODO: move to /logging and refactor everything
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@ilammy ilammy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no opinion on appropriateness of log levels (yet) but here are some general comments on readability at least 😃

acraCensor.AddHandler(queryCaptureHandler)
default:
acraCensor.logger.Errorln("Unexpected handler in configuration")
acraCensor.logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCensorSetupError).Errorln("Unexpected handler in configuration")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find long lines like this a bit hard to read. Something like this would be clearer, I think.

Suggested change
acraCensor.logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCensorSetupError).Errorln("Unexpected handler in configuration")
acraCensor.logger.
WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCensorSetupError).
Errorln("Unexpected handler in configuration")

Though I don't know if one can convince gofmt that this code style is okay.

go server.Start()
}

// on sighup we run callback that stop all listeners (that stop background goroutine of server.Start())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this comment line now belongs to line 426 where we install SIGHUP handler.

proxy.TLSCh <- true
return
}
// log message with debug level because onlt here we expect and can meet errors with closed connections .ioEOF
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

Suggested change
// log message with debug level because onlt here we expect and can meet errors with closed connections .ioEOF
// log message with debug level because only here we expect and can meet errors with closed connections .ioEOF

log.Errorln("Key store folder has an incorrect permissions")
const expectedPermission = "-rwx------"
if nil == err && runtime.GOOS == "linux" && fi.Mode().Perm().String() != expectedPermission {
log.Errorf("Key store folder has an incorrect permissions, expected: %s", expectedPermission)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be useful to log actual permissions as well. Like we do in utils/utils.go for files.

type loggerKey struct{}

// IsDebugLevel return true if logger configured to log debug messages
func IsDebugLevel(logger *log.Entry) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An idea of (possibly) more convenient interface for debug logging:

some.logger.OnlyForLevel(log.DebugLevel).
	WithField("machine_readable", data).
	Debugf("human-readable message")

No idea how to implement this tho (at the moment), but with this we should avoid one unnecessary nesting level of the if check before logging.

Though, I believe the current approach has its merits as well. We may use IsDebugLevel to avoid doing some heavy computations for debug logging if it is not actually enabled.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now this method used to check that logging configured with DebugLevel and we can add to logger's context more data (now used only for mode of decryption whole|inline) which will be used in inner functions but not unnecessary for default InfoLevel

return
output = output.strip().decode('utf-8').split(separator)
for pid in output:
os.kill(int(pid), signal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're using subprocess anyway then it may be more simple to call killall utility which sends a signal to processes by their name pattern. It's a pretty old BSD utility which should be generally available everywhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but killall is not installed on all systems by default. but pidof is posix utility that available in more OS's. it more useful for local running tests without requirement to install killall on local/docker machine.
p.s. debian which used in our tests hasn't installed killall by default

@Lagovas
Copy link
Collaborator Author

Lagovas commented Feb 12, 2019

return usage of sighup handler as last block function because it didn't fix sighup problem
applied your suggestions as one commit to avoid many commits for each suggestion

}
if n != 1 {
log.Debugln("readOctalData read 0 bytes")
decryptor.logger.Debugln("readOctalData read 0 bytes")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand, now we write logs from capital letter

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but I leaved readOctalData as is because it's name of method

@Lagovas Lagovas merged commit 47c33ac into cossacklabs:master Feb 13, 2019
@Lagovas Lagovas deleted the lagovas/T935-refactoring-of-log-messages branch February 20, 2019 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants