mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-31 18:07:17 +02:00
storage: add filter expressions, upgrade go to 1.18.1 (#3365)
* storage: add filter expressions * upgrade go
This commit is contained in:
parent
51e716ef54
commit
70f5d8b173
12 changed files with 517 additions and 63 deletions
65
pkg/storage/index.go
Normal file
65
pkg/storage/index.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
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 {
|
||||
any, ok := msg.(*anypb.Any)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
msg, _ = any.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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue