Skip to content

Commit

Permalink
Merge pull request #149 from zeromath/main
Browse files Browse the repository at this point in the history
Allow attributes to be populated through S2A
  • Loading branch information
zeromath authored Aug 19, 2024
2 parents 0493c29 + a438b25 commit 3b283c9
Showing 1 changed file with 54 additions and 17 deletions.
71 changes: 54 additions & 17 deletions s2a_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ import (
"github.com/google/s2a-go/stream"
"google.golang.org/grpc/credentials"

s2apbv1 "github.com/google/s2a-go/internal/proto/common_go_proto"
s2av1pb "github.com/google/s2a-go/internal/proto/common_go_proto"
s2apb "github.com/google/s2a-go/internal/proto/v2/common_go_proto"
)

// Identity is the interface for S2A identities.
type Identity interface {
// Name returns the name of the identity.
Name() string
Attributes() map[string]string
}

type UnspecifiedID struct {
Attr map[string]string
}

func (u *UnspecifiedID) Name() string { return "" }

func (u *UnspecifiedID) Attributes() map[string]string {
return u.Attr
}

type spiffeID struct {
Expand All @@ -44,32 +55,32 @@ type spiffeID struct {

func (s *spiffeID) Name() string { return s.spiffeID }

func (spiffeID) Attributes() map[string]string { return nil }

// NewSpiffeID creates a SPIFFE ID from id.
func NewSpiffeID(id string) Identity {
return &spiffeID{spiffeID: id}
}
func NewSpiffeID(id string) Identity { return &spiffeID{spiffeID: id} }

type hostname struct {
hostname string
}

func (h *hostname) Name() string { return h.hostname }

func (hostname) Attributes() map[string]string { return nil }

// NewHostname creates a hostname from name.
func NewHostname(name string) Identity {
return &hostname{hostname: name}
}
func NewHostname(name string) Identity { return &hostname{hostname: name} }

type uid struct {
uid string
}

func (h *uid) Name() string { return h.uid }

func (uid) Attributes() map[string]string { return nil }

// NewUID creates a UID from name.
func NewUID(name string) Identity {
return &uid{uid: name}
}
func NewUID(name string) Identity { return &uid{uid: name} }

// VerificationModeType specifies the mode that S2A must use to verify the peer
// certificate chain.
Expand Down Expand Up @@ -203,17 +214,30 @@ func DefaultServerOptions(s2aAddress string) *ServerOptions {
}
}

func toProtoIdentity(identity Identity) (*s2apbv1.Identity, error) {
func toProtoIdentity(identity Identity) (*s2av1pb.Identity, error) {
if identity == nil {
return nil, nil
}
switch id := identity.(type) {
case *spiffeID:
return &s2apbv1.Identity{IdentityOneof: &s2apbv1.Identity_SpiffeId{SpiffeId: id.Name()}}, nil
return &s2av1pb.Identity{
IdentityOneof: &s2av1pb.Identity_SpiffeId{SpiffeId: id.Name()},
Attributes: id.Attributes(),
}, nil
case *hostname:
return &s2apbv1.Identity{IdentityOneof: &s2apbv1.Identity_Hostname{Hostname: id.Name()}}, nil
return &s2av1pb.Identity{
IdentityOneof: &s2av1pb.Identity_Hostname{Hostname: id.Name()},
Attributes: id.Attributes(),
}, nil
case *uid:
return &s2apbv1.Identity{IdentityOneof: &s2apbv1.Identity_Uid{Uid: id.Name()}}, nil
return &s2av1pb.Identity{
IdentityOneof: &s2av1pb.Identity_Uid{Uid: id.Name()},
Attributes: id.Attributes(),
}, nil
case *UnspecifiedID:
return &s2av1pb.Identity{
Attributes: id.Attributes(),
}, nil
default:
return nil, errors.New("unrecognized identity type")
}
Expand All @@ -225,11 +249,24 @@ func toV2ProtoIdentity(identity Identity) (*s2apb.Identity, error) {
}
switch id := identity.(type) {
case *spiffeID:
return &s2apb.Identity{IdentityOneof: &s2apb.Identity_SpiffeId{SpiffeId: id.Name()}}, nil
return &s2apb.Identity{
IdentityOneof: &s2apb.Identity_SpiffeId{SpiffeId: id.Name()},
Attributes: id.Attributes(),
}, nil
case *hostname:
return &s2apb.Identity{IdentityOneof: &s2apb.Identity_Hostname{Hostname: id.Name()}}, nil
return &s2apb.Identity{
IdentityOneof: &s2apb.Identity_Hostname{Hostname: id.Name()},
Attributes: id.Attributes(),
}, nil
case *uid:
return &s2apb.Identity{IdentityOneof: &s2apb.Identity_Uid{Uid: id.Name()}}, nil
return &s2apb.Identity{
IdentityOneof: &s2apb.Identity_Uid{Uid: id.Name()},
Attributes: id.Attributes(),
}, nil
case *UnspecifiedID:
return &s2apb.Identity{
Attributes: id.Attributes(),
}, nil
default:
return nil, errors.New("unrecognized identity type")
}
Expand Down

0 comments on commit 3b283c9

Please sign in to comment.