Skip to content

Commit

Permalink
feat: add huaweicloud metadata support. (elastic#27607)
Browse files Browse the repository at this point in the history
Signed-off-by: colstuwjx <colstuwjx@gmail.com>
Co-authored-by: Jaime Soriano Pastor <jaime.soriano@elastic.co>
  • Loading branch information
2 people authored and wiwen committed Nov 1, 2021
1 parent f6174e8 commit 3bf0875
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Allow non-padded base64 data to be decoded by decode_base64_field {pull}27311[27311], {issue}27021[27021]
- The Kafka support library Sarama has been updated to 1.29.1. {pull}27717[27717]
- Kafka is now supported up to version 2.8.0. {pull}27720[27720]
- Add Huawei Cloud provider to add_cloud_metadata. {pull}27607[27607]
- Add default seccomp policy for linux arm64. {pull}27955[27955]

*Auditbeat*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The following cloud providers are supported:
- Google Compute Engine (GCE)
- https://www.qcloud.com/?lang=en[Tencent Cloud] (QCloud)
- Alibaba Cloud (ECS)
- Huawei Cloud (ECS)
- Azure Virtual Machine
- Openstack Nova

Expand Down Expand Up @@ -54,6 +55,7 @@ List of names the `providers` setting supports:
- "openstack", or "nova" for Openstack Nova (enabled by default).
- "openstack-ssl", or "nova-ssl" for Openstack Nova when SSL metadata APIs are enabled (enabled by default).
- "tencent", or "qcloud" for Tencent Cloud (disabled by default).
- "huawei" for Huawei Cloud (enabled by default).

The third optional configuration setting is `overwrite`. When `overwrite` is
`true`, `add_cloud_metadata` overwrites existing `cloud.*` fields (`false` by
Expand Down Expand Up @@ -124,6 +126,20 @@ _Tencent Cloud_
}
-------------------------------------------------------------------------------

_Huawei Cloud_

[source,json]
-------------------------------------------------------------------------------
{
"cloud": {
"availability_zone": "cn-east-2b",
"instance.id": "37da9890-8289-4c58-ba34-a8271c4a8216",
"provider": "huawei",
"region": "cn-east-2"
}
}
-------------------------------------------------------------------------------

_Alibaba Cloud_

This metadata is only available when VPC is selected as the network type of the
Expand Down
80 changes: 80 additions & 0 deletions libbeat/processors/add_cloud_metadata/provider_huawei_cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package add_cloud_metadata

import (
"encoding/json"

"github.com/elastic/beats/v7/libbeat/common"
)

type hwMeta struct {
ImageName string `json:"image_name"`
VpcID string `json:"vpc_id"`
}

type hwMetadata struct {
UUID string `json:"uuid"`
AvailabilityZone string `json:"availability_zone"`
RegionID string `json:"region_id"`
Meta *hwMeta `json:"meta"`
ProjectID string `json:"project_id"`
Name string `json:"name"`
}

// Huawei Cloud Metadata Service
// Document https://support.huaweicloud.com/usermanual-ecs/ecs_03_0166.html
var huaweiMetadataFetcher = provider{
Name: "huawei-cloud",

Local: true,

Create: func(_ string, c *common.Config) (metadataFetcher, error) {
metadataHost := "169.254.169.254"
huaweiCloudMetadataJSONURI := "/openstack/latest/meta_data.json"

huaweiCloudSchema := func(m map[string]interface{}) common.MapStr {
m["service"] = common.MapStr{
"name": "ECS",
}
return common.MapStr{"cloud": m}
}

urls, err := getMetadataURLs(c, metadataHost, []string{
huaweiCloudMetadataJSONURI,
})
if err != nil {
return nil, err
}
responseHandlers := map[string]responseHandler{
urls[0]: func(all []byte, result *result) error {
data := new(hwMetadata)
err := json.Unmarshal(all, data)
if err != nil {
return err
}
result.metadata.Put("instance.id", data.UUID)
result.metadata.Put("region", data.RegionID)
result.metadata.Put("availability_zone", data.AvailabilityZone)
return nil
},
}
fetcher := &httpMetadataFetcher{"huawei", nil, responseHandlers, huaweiCloudSchema}
return fetcher, nil
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package add_cloud_metadata

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

"github.com/stretchr/testify/assert"

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

func initHuaweiCloudTestServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/openstack/latest/meta_data.json" {
w.Write([]byte(`{
"random_seed": "CWIZtYK4y5pzMtShTtCKx16qB1DsA/2kL0US4u1fHxedODNr7gos4RgdE/z9eHucnltnlJfDY1remfGL60yzTsvEIWPdECOpPaJm1edIYQaUvQzdeQwKcOQAHjUP5wLQzGA3j3Pw10p7u+M7glHEwNRoEY1WsbVYwzyOOkBnqb+MJ1aOhiRnfNtHOxjLNBSDvjHaQZzoHL+1YNAxDYFezE83nE2m3ciVwZO7xWpdKDQ+W5hYBUsYAWODRMOYqIR/5ZLsfAfxE2DhK+NvuMyJ5yjO+ObQf0DN5nRUSrM5ajs84UVMr9ylJuT78ckh83CLSttsjzXJ+sr07ZFsB6/6NABzziFL7Xn8z/mEBVmFXBiBgg7KcWSoH756w42VSdUezwTy9lW0spRmdvNBKV/PzrYyy0FMiGXXZwMOCyBD05CBRJlsPorwxZLlfRVmNvsTuMYB8TG3UUbFhoR8Bd5en+EC3ncH3QIUDWn0oVg28BVjWe5rADVQLX1h83ti6GD08YUGaxoNPXnJLZfiaucSacby2mG31xysxd8Tg0qPRq7744a1HPVryuauWR9pF0+qDmtskhenxK0FR+TQ4w0fRxTigteBsXx1pQu0iz+B8rP68uokU2faCC2IMHY2Tf9RPCe6Eef0/DdQhBft88PuJLwq52o/0qZ/n9HFL6LdgCU=",
"uuid": "37da9890-8289-4c58-ba34-a8271c4a8216",
"availability_zone": "cn-east-2b",
"enterprise_project_id": "0",
"launch_index": 0,
"instance_type": "c3.large.2",
"meta": {
"os_bit": "64",
"image_name": "CentOS 7.4",
"vpc_id": "6dad7f50-db1d-4cce-b095-d27bc837d4bb"
},
"region_id": "cn-east-2",
"project_id": "c09b8baf28b845a9b53ed37575cfd61f",
"name": "hwdev-test-1"
}`))
return
}

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

func TestRetrieveHuaweiCloudMetadata(t *testing.T) {
logp.TestingSetup()

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

config, err := common.NewConfigFrom(map[string]interface{}{
"providers": []string{"huawei"},
"host": server.Listener.Addr().String(),
})

if err != nil {
t.Fatal(err)
}

p, err := New(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{
"cloud": common.MapStr{
"provider": "huawei",
"instance": common.MapStr{
"id": "37da9890-8289-4c58-ba34-a8271c4a8216",
},
"region": "cn-east-2",
"availability_zone": "cn-east-2b",
"service": common.MapStr{
"name": "ECS",
},
},
}
assert.Equal(t, expected, actual.Fields)
}
1 change: 1 addition & 0 deletions libbeat/processors/add_cloud_metadata/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var cloudMetaProviders = map[string]provider{
"nova-ssl": openstackNovaSSLMetadataFetcher,
"qcloud": qcloudMetadataFetcher,
"tencent": qcloudMetadataFetcher,
"huawei": huaweiMetadataFetcher,
}

func selectProviders(configList providerList, providers map[string]provider) map[string]provider {
Expand Down

0 comments on commit 3bf0875

Please sign in to comment.