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

Add Azure VM support for add_cloud_metadata processor #5355

Merged
merged 5 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di
- Changed beat helper script to use `exec` when running the beat. {pull}5051[5051]
- Fix reloader error message to only print on actual error {pull}5066[5066]
- Add support for enabling TLS renegotiation. {issue}4386[4386]
- Add Azure VM support for add_cloud_metadata processor {pull}5355[5355]

*Auditbeat*

Expand Down
19 changes: 19 additions & 0 deletions libbeat/docs/processors-using.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ The following cloud providers are supported:
- Google Compute Engine (GCE)
- https://www.qcloud.com/?lang=en[Tencent Cloud] (QCloud)
- Alibaba Cloud (ECS)
- Azure Virtual Machine

The simple configuration below enables the processor.

Expand Down Expand Up @@ -365,6 +366,24 @@ ECS instance.
}
-------------------------------------------------------------------------------

_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"
}
}
}
-------------------------------------------------------------------------------


[[add-locale]]
=== Add the local time zone

Expand Down
5 changes: 5 additions & 0 deletions libbeat/processors/add_cloud_metadata/add_cloud_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,18 @@ func setupFetchers(c *common.Config) ([]*metadataFetcher, error) {
if err != nil {
return fetchers, err
}
azFetcher, err := newAzureVmMetadataFetcher(c)
if err != nil {
return fetchers, err
}

fetchers = []*metadataFetcher{
doFetcher,
ec2Fetcher,
gceFetcher,
qcloudFetcher,
ecsFetcher,
azFetcher,
}
return fetchers, nil
}
Expand Down
25 changes: 25 additions & 0 deletions libbeat/processors/add_cloud_metadata/provider_azure_vm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package add_cloud_metadata

import (
"github.com/elastic/beats/libbeat/common"
s "github.com/elastic/beats/libbeat/common/schema"
c "github.com/elastic/beats/libbeat/common/schema/mapstriface"
)

// Azure VM Metadata Service
func newAzureVmMetadataFetcher(config *common.Config) (*metadataFetcher, error) {
azMetadataURI := "/metadata/instance/compute?api-version=2017-04-02"
azHeaders := map[string]string{"Metadata": "true"}
azSchema := func(m map[string]interface{}) common.MapStr {
out, _ := s.Schema{
"instance_id": c.Str("vmId"),
"instance_name": c.Str("name"),
"machine_type": c.Str("vmSize"),
"region": c.Str("location"),
}.Apply(m)
return out
}

fetcher, err := newMetadataFetcher(config, "az", azHeaders, metadataHost, azSchema, azMetadataURI)
return fetcher, err
}
77 changes: 77 additions & 0 deletions libbeat/processors/add_cloud_metadata/provider_azure_vm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package add_cloud_metadata

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"

"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)

const azInstanceIdentityDocument = `{
"location": "eastus2",
"name": "test-az-vm",
"offer": "UbuntuServer",
"osType": "Linux",
"platformFaultDomain": "0",
"platformUpdateDomain": "0",
"publisher": "Canonical",
"sku": "14.04.4-LTS",
"version": "14.04.201605091",
"vmId": "04ab04c3-63de-4709-a9f9-9ab8c0411d5e",
"vmSize": "Standard_D3_v2"
}`

func initAzureTestServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/metadata/instance/compute?api-version=2017-04-02" && r.Header.Get("Metadata") == "true" {
w.Write([]byte(azInstanceIdentityDocument))
return
}

http.Error(w, "not found", http.StatusNotFound)
}))
}

func TestRetrieveAzureMetadata(t *testing.T) {
if testing.Verbose() {
logp.LogInit(logp.LOG_DEBUG, "", false, true, []string{"*"})
}

server := initAzureTestServer()
defer server.Close()

config, err := common.NewConfigFrom(map[string]interface{}{
"host": server.Listener.Addr().String(),
})
if err != nil {
t.Fatal(err)
}

p, err := newCloudMetadata(config)
if err != nil {
t.Fatal(err)
}

actual, err := p.Run(&beat.Event{Fields: common.MapStr{}})
if err != nil {
t.Fatal(err)
}

expected := common.MapStr{
"meta": common.MapStr{
"cloud": common.MapStr{
"provider": "az",
"instance_id": "04ab04c3-63de-4709-a9f9-9ab8c0411d5e",
"instance_name": "test-az-vm",
"machine_type": "Standard_D3_v2",
"region": "eastus2",
},
},
}
assert.Equal(t, expected, actual.Fields)
}