Skip to content

Commit

Permalink
remove gogo from acl protobufs
Browse files Browse the repository at this point in the history
  • Loading branch information
erichaberkorn committed Mar 28, 2022
1 parent 4676960 commit 02d8a11
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 89 deletions.
10 changes: 5 additions & 5 deletions agent/consul/state/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (s *Store) CanBootstrapACLToken() (bool, uint64, error) {
// to update the name. Unlike the older functions to operate specifically on role or policy links
// this function does not itself handle the case where the id cannot be found. Instead the
// getName function should handle that and return an error if necessary
func resolveACLLinks(tx ReadTxn, links []pbacl.ACLLink, getName func(ReadTxn, string) (string, error)) (int, error) {
func resolveACLLinks(tx ReadTxn, links []*pbacl.ACLLink, getName func(ReadTxn, string) (string, error)) (int, error) {
var numValid int
for linkIndex, link := range links {
if link.ID != "" {
Expand All @@ -152,12 +152,12 @@ func resolveACLLinks(tx ReadTxn, links []pbacl.ACLLink, getName func(ReadTxn, st
// associated with the ID of the link. Ideally this will be a no-op if the names are already correct
// however if a linked resource was renamed it might be stale. This function will treat the incoming
// links with copy-on-write semantics and its output will indicate whether any modifications were made.
func fixupACLLinks(tx ReadTxn, original []pbacl.ACLLink, getName func(ReadTxn, string) (string, error)) ([]pbacl.ACLLink, bool, error) {
func fixupACLLinks(tx ReadTxn, original []*pbacl.ACLLink, getName func(ReadTxn, string) (string, error)) ([]*pbacl.ACLLink, bool, error) {
owned := false
links := original

cloneLinks := func(l []pbacl.ACLLink, copyNumLinks int) []pbacl.ACLLink {
clone := make([]pbacl.ACLLink, copyNumLinks)
cloneLinks := func(l []*pbacl.ACLLink, copyNumLinks int) []*pbacl.ACLLink {
clone := make([]*pbacl.ACLLink, copyNumLinks)
copy(clone, l[:copyNumLinks])
return clone
}
Expand All @@ -183,7 +183,7 @@ func fixupACLLinks(tx ReadTxn, original []pbacl.ACLLink, getName func(ReadTxn, s
}

// append the corrected link
links = append(links, pbacl.ACLLink{ID: link.ID, Name: name})
links = append(links, &pbacl.ACLLink{ID: link.ID, Name: name})
} else if owned {
links = append(links, link)
}
Expand Down
8 changes: 4 additions & 4 deletions agent/consul/state/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4110,7 +4110,7 @@ func TestStateStore_resolveACLLinks(t *testing.T) {
tx := s.db.Txn(false)
defer tx.Abort()

links := []pbacl.ACLLink{
links := []*pbacl.ACLLink{
{
Name: "foo",
},
Expand All @@ -4133,7 +4133,7 @@ func TestStateStore_resolveACLLinks(t *testing.T) {
tx := s.db.Txn(false)
defer tx.Abort()

links := []pbacl.ACLLink{
links := []*pbacl.ACLLink{
{
ID: "b985e082-25d3-45a9-9dd8-fd1a41b83b0d",
},
Expand Down Expand Up @@ -4166,7 +4166,7 @@ func TestStateStore_resolveACLLinks(t *testing.T) {
tx := s.db.Txn(false)
defer tx.Abort()

links := []pbacl.ACLLink{
links := []*pbacl.ACLLink{
{
ID: "b985e082-25d3-45a9-9dd8-fd1a41b83b0d",
},
Expand All @@ -4186,7 +4186,7 @@ func TestStateStore_resolveACLLinks(t *testing.T) {
func TestStateStore_fixupACLLinks(t *testing.T) {
t.Parallel()

links := []pbacl.ACLLink{
links := []*pbacl.ACLLink{
{
ID: "40b57f86-97ea-40e4-a99a-c399cc81f4dd",
Name: "foo",
Expand Down
36 changes: 32 additions & 4 deletions agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ func serveHandlerWithHeaders(h http.Handler, headers map[string]string) http.Han

// parseWait is used to parse the ?wait and ?index query params
// Returns true on error
func parseWait(resp http.ResponseWriter, req *http.Request, b structs.QueryOptionsCompat) bool {
func parseWait(resp http.ResponseWriter, req *http.Request, b QueryOptionsCompat) bool {
query := req.URL.Query()
if wait := query.Get("wait"); wait != "" {
dur, err := time.ParseDuration(wait)
Expand All @@ -868,7 +868,7 @@ func parseWait(resp http.ResponseWriter, req *http.Request, b structs.QueryOptio

// parseCacheControl parses the CacheControl HTTP header value. So far we only
// support maxage directive.
func parseCacheControl(resp http.ResponseWriter, req *http.Request, b structs.QueryOptionsCompat) bool {
func parseCacheControl(resp http.ResponseWriter, req *http.Request, b QueryOptionsCompat) bool {
raw := strings.ToLower(req.Header.Get("Cache-Control"))

if raw == "" {
Expand Down Expand Up @@ -926,7 +926,7 @@ func parseCacheControl(resp http.ResponseWriter, req *http.Request, b structs.Qu

// parseConsistency is used to parse the ?stale and ?consistent query params.
// Returns true on error
func (s *HTTPHandlers) parseConsistency(resp http.ResponseWriter, req *http.Request, b structs.QueryOptionsCompat) bool {
func (s *HTTPHandlers) parseConsistency(resp http.ResponseWriter, req *http.Request, b QueryOptionsCompat) bool {
query := req.URL.Query()
defaults := true
if _, ok := query["stale"]; ok {
Expand Down Expand Up @@ -1130,7 +1130,7 @@ func parseMetaPair(raw string) (string, string) {

// parse is a convenience method for endpoints that need to use both parseWait
// and parseDC.
func (s *HTTPHandlers) parse(resp http.ResponseWriter, req *http.Request, dc *string, b structs.QueryOptionsCompat) bool {
func (s *HTTPHandlers) parse(resp http.ResponseWriter, req *http.Request, dc *string, b QueryOptionsCompat) bool {
s.parseDC(req, dc)
var token string
s.parseTokenWithDefault(req, &token)
Expand Down Expand Up @@ -1190,3 +1190,31 @@ func getPathSuffixUnescaped(path string, prefixToTrim string) (string, error) {

return suffixUnescaped, nil
}

func setMetaProtobuf(resp http.ResponseWriter, queryMeta *pbcommon.QueryMeta) {
qm := new(structs.QueryMeta)
pbcommon.QueryMetaToStructs(queryMeta, qm)
setMeta(resp, qm)
}

type QueryOptionsCompat interface {
GetAllowStale() bool
SetAllowStale(bool)

GetRequireConsistent() bool
SetRequireConsistent(bool)

GetUseCache() bool
SetUseCache(bool)

SetFilter(string)
SetToken(string)

SetMustRevalidate(bool)
SetMaxAge(time.Duration)
SetMaxStaleDuration(time.Duration)
SetStaleIfError(time.Duration)

SetMaxQueryTime(time.Duration)
SetMinQueryIndex(uint64)
}
28 changes: 0 additions & 28 deletions agent/structs/protobuf_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,6 @@ import (
"time"
)

// QueryOptionsCompat is the interface that both the structs.QueryOptions
// and the proto/pbcommongogo.QueryOptions structs need to implement so that they
// can be operated on interchangeably
type QueryOptionsCompat interface {
GetToken() string
SetToken(string)
GetMinQueryIndex() uint64
SetMinQueryIndex(uint64)
GetMaxQueryTime() (time.Duration, error)
SetMaxQueryTime(time.Duration)
GetAllowStale() bool
SetAllowStale(bool)
GetRequireConsistent() bool
SetRequireConsistent(bool)
GetUseCache() bool
SetUseCache(bool)
GetMaxStaleDuration() (time.Duration, error)
SetMaxStaleDuration(time.Duration)
GetMaxAge() (time.Duration, error)
SetMaxAge(time.Duration)
GetMustRevalidate() bool
SetMustRevalidate(bool)
GetStaleIfError() (time.Duration, error)
SetStaleIfError(time.Duration)
GetFilter() string
SetFilter(string)
}

// GetToken helps implement the QueryOptionsCompat interface
// Copied from proto/pbcommongogo/common.pb.go
func (m *QueryOptions) GetToken() string {
Expand Down
18 changes: 0 additions & 18 deletions agent/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2575,26 +2575,13 @@ type ProtoMarshaller interface {
}

func EncodeProtoInterface(t MessageType, message interface{}) ([]byte, error) {
if marshaller, ok := message.(ProtoMarshaller); ok {
return EncodeProtoGogo(t, marshaller)
}

if marshaller, ok := message.(proto.Message); ok {
return EncodeProto(t, marshaller)
}

return nil, fmt.Errorf("message does not implement the ProtoMarshaller interface: %T", message)
}

func EncodeProtoGogo(t MessageType, message ProtoMarshaller) ([]byte, error) {
data := make([]byte, message.Size()+1)
data[0] = uint8(t)
if _, err := message.MarshalTo(data[1:]); err != nil {
return nil, err
}
return data, nil
}

func EncodeProto(t MessageType, pb proto.Message) ([]byte, error) {
data := make([]byte, proto.Size(pb)+1)
data[0] = uint8(t)
Expand All @@ -2612,11 +2599,6 @@ func DecodeProto(buf []byte, pb proto.Message) error {
return proto.Unmarshal(buf, pb)
}

func DecodeProtoGogo(buf []byte, out ProtoMarshaller) error {
// Note that this assumes the leading byte indicating the type as already been stripped off.
return out.Unmarshal(buf)
}

// CompoundResponse is an interface for gathering multiple responses. It is
// used in cross-datacenter RPC calls where more than 1 datacenter is
// expected to reply.
Expand Down
46 changes: 46 additions & 0 deletions api/namespace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"encoding/json"
"fmt"
"time"
)
Expand Down Expand Up @@ -38,6 +39,25 @@ type Namespace struct {
ModifyIndex uint64 `json:"ModifyIndex,omitempty"`
}

func (n *Namespace) UnmarshalJSON(data []byte) error {
type Alias Namespace
aux := struct {
DeletedAtSnake *time.Time `json:"deleted_at"`
*Alias
}{
Alias: (*Alias)(n),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}

if n.DeletedAt == nil && aux.DeletedAtSnake != nil {
n.DeletedAt = aux.DeletedAtSnake
}

return nil
}

// NamespaceACLConfig is the Namespace specific ACL configuration container
type NamespaceACLConfig struct {
// PolicyDefaults is the list of policies that should be used for the parent authorizer
Expand All @@ -48,6 +68,32 @@ type NamespaceACLConfig struct {
RoleDefaults []ACLLink `json:"RoleDefaults" alias:"role_defaults"`
}

func (n *NamespaceACLConfig) UnmarshalJSON(data []byte) error {
type Alias NamespaceACLConfig
aux := struct {
PolicyDefaultsSnake []ACLLink `json:"policy_defaults"`
RoleDefaultsSnake []ACLLink `json:"role_defaults"`
*Alias
}{
Alias: (*Alias)(n),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}

if n.PolicyDefaults == nil {
for _, pd := range aux.PolicyDefaultsSnake {
n.PolicyDefaults = append(n.PolicyDefaults, pd)
}
}
if n.RoleDefaults == nil {
for _, pd := range aux.RoleDefaultsSnake {
n.RoleDefaults = append(n.RoleDefaults, pd)
}
}
return nil
}

// Namespaces can be used to manage Namespaces in Consul Enterprise..
type Namespaces struct {
c *Client
Expand Down
19 changes: 19 additions & 0 deletions proto/pbacl/acl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pbacl

import (
"github.com/hashicorp/consul/api"
)

func (a *ACLLink) ToAPI() api.ACLLink {
return api.ACLLink{
ID: a.ID,
Name: a.Name,
}
}

func ACLLinkFromAPI(a api.ACLLink) *ACLLink {
return &ACLLink{
ID: a.ID,
Name: a.Name,
}
}
50 changes: 36 additions & 14 deletions proto/pbacl/acl.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 02d8a11

Please sign in to comment.