pomerium/pkg/storage/index.go
Caleb Doxsey bbed421cd8
config: remove source, remove deadcode, fix linting issues (#4118)
* remove source, remove deadcode, fix linting issues

* use github action for lint

* fix missing envoy
2023-04-21 17:25:11 -06:00

65 lines
1.1 KiB
Go

package storage
import (
"net/netip"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/structpb"
)
const (
indexField = "$index"
cidrField = "cidr"
)
// GetRecordIndex gets a record's index. If there is no index, nil is returned.
func GetRecordIndex(msg proto.Message) *structpb.Struct {
for {
data, ok := msg.(*anypb.Any)
if !ok {
break
}
msg, _ = data.UnmarshalNew()
}
var s *structpb.Struct
if sv, ok := msg.(*structpb.Value); ok {
s = sv.GetStructValue()
} else {
s, _ = msg.(*structpb.Struct)
}
if s == nil {
return nil
}
f, ok := s.Fields[indexField]
if !ok {
return nil
}
return f.GetStructValue()
}
// GetRecordIndexCIDR returns the $index.cidr for a record's data. If none is available nil is returned.
func GetRecordIndexCIDR(msg proto.Message) *netip.Prefix {
obj := GetRecordIndex(msg)
if obj == nil {
return nil
}
cf, ok := obj.Fields[cidrField]
if !ok {
return nil
}
c := cf.GetStringValue()
if c == "" {
return nil
}
prefix, err := netip.ParsePrefix(c)
if err != nil {
return nil
}
return &prefix
}