Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't de-reference pointers from ECS without checking #2918

Merged
merged 1 commit into from
Nov 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions probe/awsecs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,51 @@ func newClient(cluster string, cacheSize int, cacheExpiry time.Duration, cluster
}, nil
}

// Helper functions to map from the AWS practice of storing everything
// via a pointer to the Go practice of using zero values
func stringOrBlank(s *string) string {
if s == nil {
return ""
}
return *s
}

func timeOrZero(t *time.Time) time.Time {
if t == nil {
return time.Time{}
}
return *t
}

func int64OrZero(i *int64) int64 {
if i == nil {
return 0
}
return *i
}

func newECSTask(task *ecs.Task) EcsTask {
return EcsTask{
TaskARN: *task.TaskArn,
CreatedAt: *task.CreatedAt,
TaskDefinitionARN: *task.TaskDefinitionArn,
StartedAt: *task.StartedAt,
StartedBy: *task.StartedBy,
TaskARN: stringOrBlank(task.TaskArn),
CreatedAt: timeOrZero(task.CreatedAt),
TaskDefinitionARN: stringOrBlank(task.TaskDefinitionArn),
StartedAt: timeOrZero(task.StartedAt),
StartedBy: stringOrBlank(task.StartedBy),
}
}

func newECSService(service *ecs.Service) EcsService {
deploymentIDs := make([]string, len(service.Deployments))
for i, deployment := range service.Deployments {
deploymentIDs[i] = *deployment.Id
deploymentIDs[i] = stringOrBlank(deployment.Id)
}
return EcsService{
ServiceName: *service.ServiceName,
ServiceName: stringOrBlank(service.ServiceName),
DeploymentIDs: deploymentIDs,
DesiredCount: *service.DesiredCount,
PendingCount: *service.PendingCount,
RunningCount: *service.RunningCount,
TaskDefinitionARN: *service.TaskDefinition,
DesiredCount: int64OrZero(service.DesiredCount),
PendingCount: int64OrZero(service.PendingCount),
RunningCount: int64OrZero(service.RunningCount),
TaskDefinitionARN: stringOrBlank(service.TaskDefinition),
}
}

Expand Down Expand Up @@ -208,11 +231,11 @@ func (c ecsClientImpl) describeServicesBatch(names []string) {
}

for _, failure := range resp.Failures {
log.Warnf("Failed to describe ECS service %s, ECS service report may be incomplete: %s", *failure.Arn, failure.Reason)
log.Warnf("Failed to describe ECS service %s, ECS service report may be incomplete: %s", stringOrBlank(failure.Arn), stringOrBlank(failure.Reason))
}

for _, service := range resp.Services {
if service != nil {
if service != nil && service.ServiceName != nil {
c.serviceCache.Set(*service.ServiceName, newECSService(service))
}
}
Expand All @@ -239,7 +262,7 @@ func (c ecsClientImpl) getTasks(taskARNs []string) {
}

for _, failure := range resp.Failures {
log.Warnf("Failed to describe ECS task %s, ECS service report may be incomplete: %s", *failure.Arn, *failure.Reason)
log.Warnf("Failed to describe ECS task %s, ECS service report may be incomplete: %s", stringOrBlank(failure.Arn), stringOrBlank(failure.Reason))
}

for _, task := range resp.Tasks {
Expand Down