forked from mweagle/Sparta
/
session.go
79 lines (70 loc) · 2.52 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package aws
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/sirupsen/logrus"
)
type logrusProxy struct {
logger *logrus.Logger
}
// Log is a utility function to comply with the AWS signature
func (proxy *logrusProxy) Log(args ...interface{}) {
proxy.logger.Info(args...)
}
// NewSessionWithConfig returns an awsSession that includes the user supplied
// configuration information
func NewSessionWithConfig(awsConfig *aws.Config, logger *logrus.Logger) *session.Session {
return NewSessionWithConfigLevel(awsConfig, aws.LogDebugWithRequestErrors, logger)
}
// NewSession that attaches a debug level handler to all AWS requests from services
// sharing the session value.
func NewSession(logger *logrus.Logger) *session.Session {
return NewSessionWithLevel(aws.LogDebugWithRequestErrors, logger)
}
// NewSessionWithLevel returns an AWS Session (https://github.com/aws/aws-sdk-go/wiki/Getting-Started-Configuration)
// object that attaches a debug level handler to all AWS requests from services
// sharing the session value.
func NewSessionWithLevel(level aws.LogLevelType, logger *logrus.Logger) *session.Session {
awsConfig := &aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
}
return NewSessionWithConfigLevel(awsConfig, level, logger)
}
// NewSessionWithConfigLevel returns an AWS Session (https://github.com/aws/aws-sdk-go/wiki/Getting-Started-Configuration)
// object that attaches a debug level handler to all AWS requests from services
// sharing the session value.
func NewSessionWithConfigLevel(awsConfig *aws.Config,
level aws.LogLevelType,
logger *logrus.Logger) *session.Session {
if nil == awsConfig {
awsConfig = &aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
}
}
// Log AWS calls if needed
switch logger.Level {
case logrus.DebugLevel:
awsConfig.LogLevel = aws.LogLevel(level)
}
awsConfig.Logger = &logrusProxy{logger}
sess, sessErr := session.NewSession(awsConfig)
if sessErr != nil {
logger.WithField("Error", sessErr).Warn("Failed to create AWS Session")
} else {
sess.Handlers.Send.PushFront(func(r *request.Request) {
logger.WithFields(logrus.Fields{
"Service": r.ClientInfo.ServiceName,
"Operation": r.Operation.Name,
"Method": r.Operation.HTTPMethod,
"Path": r.Operation.HTTPPath,
"Payload": r.Params,
}).Debug("AWS Request")
})
}
logger.WithFields(logrus.Fields{
"Name": aws.SDKName,
"Version": aws.SDKVersion,
}).Debug("AWS SDK Info")
return sess
}