|
@@ -14,6 +14,7 @@ import (
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
+ "github.com/aws/aws-sdk-go/aws/credentials/endpointcreds"
|
|
"github.com/aws/aws-sdk-go/aws/ec2metadata"
|
|
"github.com/aws/aws-sdk-go/aws/ec2metadata"
|
|
"github.com/aws/aws-sdk-go/aws/request"
|
|
"github.com/aws/aws-sdk-go/aws/request"
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
@@ -26,16 +27,17 @@ import (
|
|
)
|
|
)
|
|
|
|
|
|
const (
|
|
const (
|
|
- name = "awslogs"
|
|
|
|
- regionKey = "awslogs-region"
|
|
|
|
- regionEnvKey = "AWS_REGION"
|
|
|
|
- logGroupKey = "awslogs-group"
|
|
|
|
- logStreamKey = "awslogs-stream"
|
|
|
|
- logCreateGroupKey = "awslogs-create-group"
|
|
|
|
- tagKey = "tag"
|
|
|
|
- datetimeFormatKey = "awslogs-datetime-format"
|
|
|
|
- multilinePatternKey = "awslogs-multiline-pattern"
|
|
|
|
- batchPublishFrequency = 5 * time.Second
|
|
|
|
|
|
+ name = "awslogs"
|
|
|
|
+ regionKey = "awslogs-region"
|
|
|
|
+ regionEnvKey = "AWS_REGION"
|
|
|
|
+ logGroupKey = "awslogs-group"
|
|
|
|
+ logStreamKey = "awslogs-stream"
|
|
|
|
+ logCreateGroupKey = "awslogs-create-group"
|
|
|
|
+ tagKey = "tag"
|
|
|
|
+ datetimeFormatKey = "awslogs-datetime-format"
|
|
|
|
+ multilinePatternKey = "awslogs-multiline-pattern"
|
|
|
|
+ credentialsEndpointKey = "awslogs-credentials-endpoint"
|
|
|
|
+ batchPublishFrequency = 5 * time.Second
|
|
|
|
|
|
// See: http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html
|
|
// See: http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html
|
|
perEventBytes = 26
|
|
perEventBytes = 26
|
|
@@ -50,6 +52,8 @@ const (
|
|
invalidSequenceTokenCode = "InvalidSequenceTokenException"
|
|
invalidSequenceTokenCode = "InvalidSequenceTokenException"
|
|
resourceNotFoundCode = "ResourceNotFoundException"
|
|
resourceNotFoundCode = "ResourceNotFoundException"
|
|
|
|
|
|
|
|
+ credentialsEndpoint = "http://169.254.170.2"
|
|
|
|
+
|
|
userAgentHeader = "User-Agent"
|
|
userAgentHeader = "User-Agent"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -198,6 +202,10 @@ var newRegionFinder = func() regionFinder {
|
|
return ec2metadata.New(session.New())
|
|
return ec2metadata.New(session.New())
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// newSDKEndpoint is a variable such that the implementation
|
|
|
|
+// can be swapped out for unit tests.
|
|
|
|
+var newSDKEndpoint = credentialsEndpoint
|
|
|
|
+
|
|
// newAWSLogsClient creates the service client for Amazon CloudWatch Logs.
|
|
// newAWSLogsClient creates the service client for Amazon CloudWatch Logs.
|
|
// Customizations to the default client from the SDK include a Docker-specific
|
|
// Customizations to the default client from the SDK include a Docker-specific
|
|
// User-Agent string and automatic region detection using the EC2 Instance
|
|
// User-Agent string and automatic region detection using the EC2 Instance
|
|
@@ -222,11 +230,33 @@ func newAWSLogsClient(info logger.Info) (api, error) {
|
|
}
|
|
}
|
|
region = &r
|
|
region = &r
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ sess, err := session.NewSession()
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, errors.New("Failed to create a service client session for for awslogs driver")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // attach region to cloudwatchlogs config
|
|
|
|
+ sess.Config.Region = region
|
|
|
|
+
|
|
|
|
+ if uri, ok := info.Config[credentialsEndpointKey]; ok {
|
|
|
|
+ logrus.Debugf("Trying to get credentials from awslogs-credentials-endpoint")
|
|
|
|
+
|
|
|
|
+ endpoint := fmt.Sprintf("%s%s", newSDKEndpoint, uri)
|
|
|
|
+ creds := endpointcreds.NewCredentialsClient(*sess.Config, sess.Handlers, endpoint,
|
|
|
|
+ func(p *endpointcreds.Provider) {
|
|
|
|
+ p.ExpiryWindow = 5 * time.Minute
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ // attach credentials to cloudwatchlogs config
|
|
|
|
+ sess.Config.Credentials = creds
|
|
|
|
+ }
|
|
|
|
+
|
|
logrus.WithFields(logrus.Fields{
|
|
logrus.WithFields(logrus.Fields{
|
|
"region": *region,
|
|
"region": *region,
|
|
}).Debug("Created awslogs client")
|
|
}).Debug("Created awslogs client")
|
|
|
|
|
|
- client := cloudwatchlogs.New(session.New(), aws.NewConfig().WithRegion(*region))
|
|
|
|
|
|
+ client := cloudwatchlogs.New(sess)
|
|
|
|
|
|
client.Handlers.Build.PushBackNamed(request.NamedHandler{
|
|
client.Handlers.Build.PushBackNamed(request.NamedHandler{
|
|
Name: "DockerUserAgentHandler",
|
|
Name: "DockerUserAgentHandler",
|
|
@@ -525,6 +555,7 @@ func ValidateLogOpt(cfg map[string]string) error {
|
|
case tagKey:
|
|
case tagKey:
|
|
case datetimeFormatKey:
|
|
case datetimeFormatKey:
|
|
case multilinePatternKey:
|
|
case multilinePatternKey:
|
|
|
|
+ case credentialsEndpointKey:
|
|
default:
|
|
default:
|
|
return fmt.Errorf("unknown log opt '%s' for %s log driver", key, name)
|
|
return fmt.Errorf("unknown log opt '%s' for %s log driver", key, name)
|
|
}
|
|
}
|