2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2023-08-17 18:43:21 +00:00
|
|
|
package response
|
2018-10-03 18:18:55 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-26 22:23:15 +00:00
|
|
|
envoy_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
|
|
|
envoy_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
|
|
|
|
envoy_matcher_v3 "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
|
2021-02-22 21:00:15 +00:00
|
|
|
|
2023-01-11 14:39:10 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
"google.golang.org/protobuf/types/known/wrapperspb"
|
2018-10-03 18:18:55 +00:00
|
|
|
)
|
|
|
|
|
2023-08-17 18:43:21 +00:00
|
|
|
func CreateResponse(typeURL string, version, nonce string, resources []proto.Message) (*envoy_discovery_v3.DiscoveryResponse, error) {
|
2023-01-11 14:39:10 +00:00
|
|
|
anys := make([]*anypb.Any, 0, len(resources))
|
2018-10-09 16:57:26 +00:00
|
|
|
for _, r := range resources {
|
2018-10-03 18:18:55 +00:00
|
|
|
if r == nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-01-11 14:39:10 +00:00
|
|
|
if any, ok := r.(*anypb.Any); ok {
|
2020-06-23 20:19:56 +00:00
|
|
|
anys = append(anys, any)
|
2018-10-03 18:18:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
data, err := proto.Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-11 14:39:10 +00:00
|
|
|
anys = append(anys, &anypb.Any{
|
2018-10-03 18:18:55 +00:00
|
|
|
TypeUrl: typeURL,
|
|
|
|
Value: data,
|
2018-10-09 16:57:26 +00:00
|
|
|
})
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2021-02-26 22:23:15 +00:00
|
|
|
resp := &envoy_discovery_v3.DiscoveryResponse{
|
2018-10-03 18:18:55 +00:00
|
|
|
VersionInfo: version,
|
|
|
|
Resources: anys,
|
|
|
|
TypeUrl: typeURL,
|
|
|
|
Nonce: nonce,
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2023-08-17 18:43:21 +00:00
|
|
|
func MakePipeAddress(path string, mode uint32) *envoy_core_v3.Address {
|
2021-03-26 20:00:44 +00:00
|
|
|
return &envoy_core_v3.Address{
|
|
|
|
Address: &envoy_core_v3.Address_Pipe{
|
|
|
|
Pipe: &envoy_core_v3.Pipe{
|
|
|
|
Path: path,
|
|
|
|
Mode: mode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 18:43:21 +00:00
|
|
|
func MakeAddress(ip string, port int) *envoy_core_v3.Address {
|
2024-06-04 21:55:53 +00:00
|
|
|
if port >= 0 && port <= 65535 {
|
|
|
|
return &envoy_core_v3.Address{
|
|
|
|
Address: &envoy_core_v3.Address_SocketAddress{
|
|
|
|
SocketAddress: &envoy_core_v3.SocketAddress{
|
|
|
|
Address: ip,
|
|
|
|
PortSpecifier: &envoy_core_v3.SocketAddress_PortValue{
|
|
|
|
PortValue: uint32(port),
|
|
|
|
},
|
2018-10-03 18:18:55 +00:00
|
|
|
},
|
|
|
|
},
|
2024-06-04 21:55:53 +00:00
|
|
|
}
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2024-06-04 21:55:53 +00:00
|
|
|
return nil
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
2023-08-17 18:43:21 +00:00
|
|
|
func MakeUint32Value(n int) *wrapperspb.UInt32Value {
|
2023-01-11 14:39:10 +00:00
|
|
|
return &wrapperspb.UInt32Value{Value: uint32(n)}
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2019-03-22 19:37:14 +00:00
|
|
|
|
2023-08-17 18:43:21 +00:00
|
|
|
func MakeBoolValue(n bool) *wrapperspb.BoolValue {
|
2023-01-11 14:39:10 +00:00
|
|
|
return &wrapperspb.BoolValue{Value: n}
|
2019-07-02 03:10:51 +00:00
|
|
|
}
|
2020-08-27 17:20:58 +00:00
|
|
|
|
2023-08-17 18:43:21 +00:00
|
|
|
func MakeEnvoyRegexMatch(patt string) *envoy_matcher_v3.RegexMatcher {
|
2021-02-26 22:23:15 +00:00
|
|
|
return &envoy_matcher_v3.RegexMatcher{
|
2020-08-27 17:20:58 +00:00
|
|
|
Regex: patt,
|
|
|
|
}
|
|
|
|
}
|