1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
package authenticatedwrapper
import (
"strings"
"github.com/docker/swarmkit/protobuf/plugin"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
)
type authenticatedWrapperGen struct {
gen *generator.Generator
}
func init() {
generator.RegisterPlugin(new(authenticatedWrapperGen))
}
func (g *authenticatedWrapperGen) Init(gen *generator.Generator) {
g.gen = gen
}
func (g *authenticatedWrapperGen) Name() string {
return "authenticatedwrapper"
}
func (g *authenticatedWrapperGen) genAuthenticatedStruct(s *descriptor.ServiceDescriptorProto) {
g.gen.P("type " + serviceTypeName(s) + " struct {")
g.gen.P(" local " + s.GetName() + "Server")
g.gen.P(" authorize func(context.Context, []string) error")
g.gen.P("}")
}
func (g *authenticatedWrapperGen) genAuthenticatedConstructor(s *descriptor.ServiceDescriptorProto) {
g.gen.P("func NewAuthenticatedWrapper" + s.GetName() + "Server(local " + s.GetName() + "Server, authorize func(context.Context, []string) error)" + s.GetName() + "Server {")
g.gen.P("return &" + serviceTypeName(s) + `{
local: local,
authorize: authorize,
}`)
g.gen.P("}")
}
func getInputTypeName(m *descriptor.MethodDescriptorProto) string {
parts := strings.Split(m.GetInputType(), ".")
return parts[len(parts)-1]
}
func getOutputTypeName(m *descriptor.MethodDescriptorProto) string {
parts := strings.Split(m.GetOutputType(), ".")
return parts[len(parts)-1]
}
func serviceTypeName(s *descriptor.ServiceDescriptorProto) string {
return "authenticatedWrapper" + s.GetName() + "Server"
}
func sigPrefix(s *descriptor.ServiceDescriptorProto, m *descriptor.MethodDescriptorProto) string {
return "func (p *" + serviceTypeName(s) + ") " + m.GetName() + "("
}
func genRoles(auth *plugin.TLSAuthorization) string {
rolesSlice := "[]string{"
first := true
for _, role := range auth.Roles {
if !first {
rolesSlice += ","
}
first = false
rolesSlice += `"` + role + `"`
}
rolesSlice += "}"
return rolesSlice
}
func (g *authenticatedWrapperGen) genServerStreamingMethod(s *descriptor.ServiceDescriptorProto, m *descriptor.MethodDescriptorProto) {
g.gen.P(sigPrefix(s, m) + "r *" + getInputTypeName(m) + ", stream " + s.GetName() + "_" + m.GetName() + "Server) error {")
authIntf, err := proto.GetExtension(m.Options, plugin.E_TlsAuthorization)
if err != nil {
g.gen.P(`
panic("no authorization information in protobuf")`)
g.gen.P(`}`)
return
}
auth := authIntf.(*plugin.TLSAuthorization)
if auth.Insecure != nil && *auth.Insecure {
if len(auth.Roles) != 0 {
panic("Roles and Insecure cannot both be specified")
}
g.gen.P(`
return p.local.` + m.GetName() + `(r, stream)`)
g.gen.P(`}`)
return
}
g.gen.P(`
if err := p.authorize(stream.Context(),` + genRoles(auth) + `); err != nil {
return err
}
return p.local.` + m.GetName() + `(r, stream)`)
g.gen.P("}")
}
func (g *authenticatedWrapperGen) genClientServerStreamingMethod(s *descriptor.ServiceDescriptorProto, m *descriptor.MethodDescriptorProto) {
g.gen.P(sigPrefix(s, m) + "stream " + s.GetName() + "_" + m.GetName() + "Server) error {")
authIntf, err := proto.GetExtension(m.Options, plugin.E_TlsAuthorization)
if err != nil {
g.gen.P(`
panic("no authorization information in protobuf")`)
g.gen.P(`}`)
return
}
auth := authIntf.(*plugin.TLSAuthorization)
if auth.Insecure != nil && *auth.Insecure {
if len(auth.Roles) != 0 {
panic("Roles and Insecure cannot both be specified")
}
g.gen.P(`
return p.local.` + m.GetName() + `(stream)`)
g.gen.P(`}`)
return
}
g.gen.P(`
if err := p.authorize(stream.Context(), ` + genRoles(auth) + `); err != nil {
return err
}
return p.local.` + m.GetName() + `(stream)`)
g.gen.P("}")
}
func (g *authenticatedWrapperGen) genSimpleMethod(s *descriptor.ServiceDescriptorProto, m *descriptor.MethodDescriptorProto) {
g.gen.P(sigPrefix(s, m) + "ctx context.Context, r *" + getInputTypeName(m) + ") (*" + getOutputTypeName(m) + ", error) {")
authIntf, err := proto.GetExtension(m.Options, plugin.E_TlsAuthorization)
if err != nil {
g.gen.P(`
panic("no authorization information in protobuf")`)
g.gen.P(`}`)
return
}
auth := authIntf.(*plugin.TLSAuthorization)
if auth.Insecure != nil && *auth.Insecure {
if len(auth.Roles) != 0 {
panic("Roles and Insecure cannot both be specified")
}
g.gen.P(`
return p.local.` + m.GetName() + `(ctx, r)`)
g.gen.P(`}`)
return
}
g.gen.P(`
if err := p.authorize(ctx, ` + genRoles(auth) + `); err != nil {
return nil, err
}
return p.local.` + m.GetName() + `(ctx, r)`)
g.gen.P("}")
}
func (g *authenticatedWrapperGen) genAuthenticatedMethod(s *descriptor.ServiceDescriptorProto, m *descriptor.MethodDescriptorProto) {
g.gen.P()
switch {
case m.GetClientStreaming():
g.genClientServerStreamingMethod(s, m)
case m.GetServerStreaming():
g.genServerStreamingMethod(s, m)
default:
g.genSimpleMethod(s, m)
}
g.gen.P()
}
func (g *authenticatedWrapperGen) Generate(file *generator.FileDescriptor) {
g.gen.P()
for _, s := range file.Service {
g.genAuthenticatedStruct(s)
g.genAuthenticatedConstructor(s)
for _, m := range s.Method {
g.genAuthenticatedMethod(s, m)
}
}
g.gen.P()
}
func (g *authenticatedWrapperGen) GenerateImports(file *generator.FileDescriptor) {
}
|