Skip to content

Commit

Permalink
Trim project from GCE cloud metadata (elastic#10987)
Browse files Browse the repository at this point in the history
This removes the project info from the cloud.machine.type and cloud.availability_zone fields
that are added to the add_cloud_metadata processor for Google Compute Engine (GCE).

Fixes elastic#10968, elastic#10775 (docs)

(cherry picked from commit f527cd5)
  • Loading branch information
andrewkroh committed Mar 3, 2019
1 parent 5831462 commit c57e35b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 54 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ https://github.com/elastic/beats/compare/v7.0.0-beta1...master[Check the HEAD di

*Affecting all Beats*

- On Google Cloud Engine (GCE) the add_cloud_metadata will now trim the project
info from the cloud.machine.type and cloud.availability_zone. {issue}10968[10968]

*Auditbeat*

*Filebeat*
Expand Down
90 changes: 38 additions & 52 deletions libbeat/docs/processors-using.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,12 @@ _EC2_
[source,json]
-------------------------------------------------------------------------------
{
"meta": {
"cloud": {
"availability_zone": "us-east-1c",
"instance_id": "i-4e123456",
"machine_type": "t2.medium",
"provider": "ec2",
"region": "us-east-1"
}
"cloud": {
"availability_zone": "us-east-1c",
"instance_id": "i-4e123456",
"machine_type": "t2.medium",
"provider": "ec2",
"region": "us-east-1"
}
}
-------------------------------------------------------------------------------
Expand All @@ -459,12 +457,10 @@ _Digital Ocean_
[source,json]
-------------------------------------------------------------------------------
{
"meta": {
"cloud": {
"instance_id": "1234567",
"provider": "digitalocean",
"region": "nyc2"
}
"cloud": {
"instance_id": "1234567",
"provider": "digitalocean",
"region": "nyc2"
}
}
-------------------------------------------------------------------------------
Expand All @@ -474,14 +470,12 @@ _GCE_
[source,json]
-------------------------------------------------------------------------------
{
"meta": {
"cloud": {
"availability_zone": "projects/1234567890/zones/us-east1-b",
"instance_id": "1234556778987654321",
"machine_type": "projects/1234567890/machineTypes/f1-micro",
"project_id": "my-dev",
"provider": "gce"
}
"cloud": {
"availability_zone": "us-east1-b",
"instance_id": "1234556778987654321",
"machine_type": "f1-micro",
"project_id": "my-dev",
"provider": "gce"
}
}
-------------------------------------------------------------------------------
Expand All @@ -491,13 +485,11 @@ _Tencent Cloud_
[source,json]
-------------------------------------------------------------------------------
{
"meta": {
"cloud": {
"availability_zone": "gz-azone2",
"instance_id": "ins-qcloudv5",
"provider": "qcloud",
"region": "china-south-gz"
}
"cloud": {
"availability_zone": "gz-azone2",
"instance_id": "ins-qcloudv5",
"provider": "qcloud",
"region": "china-south-gz"
}
}
-------------------------------------------------------------------------------
Expand All @@ -510,13 +502,11 @@ ECS instance.
[source,json]
-------------------------------------------------------------------------------
{
"meta": {
"cloud": {
"availability_zone": "cn-shenzhen",
"instance_id": "i-wz9g2hqiikg0aliyun2b",
"provider": "ecs",
"region": "cn-shenzhen-a"
}
"cloud": {
"availability_zone": "cn-shenzhen",
"instance_id": "i-wz9g2hqiikg0aliyun2b",
"provider": "ecs",
"region": "cn-shenzhen-a"
}
}
-------------------------------------------------------------------------------
Expand All @@ -526,14 +516,12 @@ _Azure Virtual Machine_
[source,json]
-------------------------------------------------------------------------------
{
"meta": {
"cloud": {
"provider": "az",
"instance_id": "04ab04c3-63de-4709-a9f9-9ab8c0411d5e",
"instance_name": "test-az-vm",
"machine_type": "Standard_D3_v2",
"region": "eastus2"
}
"cloud": {
"provider": "az",
"instance_id": "04ab04c3-63de-4709-a9f9-9ab8c0411d5e",
"instance_name": "test-az-vm",
"machine_type": "Standard_D3_v2",
"region": "eastus2"
}
}
-------------------------------------------------------------------------------
Expand All @@ -543,14 +531,12 @@ _Openstack Nova_
[source,json]
-------------------------------------------------------------------------------
{
"meta": {
"cloud": {
"provider": "openstack",
"instance_name": "test-998d932195.mycloud.tld",
"availability_zone": "xxxx-az-c",
"instance_id": "i-00011a84",
"machine_type": "m2.large"
}
"cloud": {
"provider": "openstack",
"instance_name": "test-998d932195.mycloud.tld",
"availability_zone": "xxxx-az-c",
"instance_id": "i-00011a84",
"machine_type": "m2.large"
}
}
-------------------------------------------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions libbeat/processors/add_cloud_metadata/provider_google_gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package add_cloud_metadata

import (
"path"

"github.com/elastic/beats/libbeat/common"
s "github.com/elastic/beats/libbeat/common/schema"
c "github.com/elastic/beats/libbeat/common/schema/mapstriface"
Expand All @@ -30,6 +32,18 @@ func newGceMetadataFetcher(config *common.Config) (*metadataFetcher, error) {
gceSchema := func(m map[string]interface{}) common.MapStr {
out := common.MapStr{}

trimLeadingPath := func(key string) {
v, err := out.GetValue(key)
if err != nil {
return
}
p, ok := v.(string)
if !ok {
return
}
out.Put(key, path.Base(p))
}

if instance, ok := m["instance"].(map[string]interface{}); ok {
s.Schema{
"instance": s.Object{
Expand All @@ -41,6 +55,8 @@ func newGceMetadataFetcher(config *common.Config) (*metadataFetcher, error) {
},
"availability_zone": c.Str("zone"),
}.ApplyTo(out, instance)
trimLeadingPath("machine.type")
trimLeadingPath("availability_zone")
}

if project, ok := m["project"].(map[string]interface{}); ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ func TestRetrieveGCEMetadata(t *testing.T) {
"name": "test-gce-dev",
},
"machine": common.MapStr{
"type": "projects/111111111111/machineTypes/f1-micro",
"type": "f1-micro",
},
"availability_zone": "projects/111111111111/zones/us-east1-b",
"availability_zone": "us-east1-b",
"project": common.MapStr{
"id": "test-dev",
},
Expand Down

0 comments on commit c57e35b

Please sign in to comment.