-
Notifications
You must be signed in to change notification settings - Fork 0
/
descriptorUtil.go
128 lines (105 loc) · 3.74 KB
/
descriptorUtil.go
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
// Copyright 2018 Istio Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"encoding/base64"
"fmt"
"io/ioutil"
"regexp"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
tmpl "istio.io/api/mixer/adapter/model/v1beta1"
)
const adapterCfgMsgName = "Params"
// GetTmplDescriptor find a template descriptor
func GetTmplDescriptor(base64Tmpl string) (*descriptor.FileDescriptorSet, *descriptor.FileDescriptorProto, string, tmpl.TemplateVariety, error) {
fds, err := decodeFds(base64Tmpl)
tmplVariety := tmpl.TEMPLATE_VARIETY_REPORT
if err != nil {
return nil, nil, "", tmplVariety, err
}
var tmplDescProto *descriptor.FileDescriptorProto
for _, fd := range fds.File {
if fd.GetOptions() == nil {
continue
}
var variety interface{}
if variety, err = proto.GetExtension(fd.GetOptions(), tmpl.E_TemplateVariety); err == nil {
tmplVariety = *(variety.(*tmpl.TemplateVariety))
if tmplDescProto != nil {
return nil, nil, "", tmplVariety, fmt.Errorf(
"proto files %s and %s, both have the option %s. Only one proto file is allowed with this options",
fd.GetName(), tmplDescProto.GetName(), tmpl.E_TemplateVariety.Name)
}
tmplDescProto = fd
}
}
if tmplDescProto == nil {
return nil, nil, "", tmplVariety, fmt.Errorf("there has to be one proto file that has the extension %s", tmpl.E_TemplateVariety.Name)
}
var nameExt interface{}
if nameExt, err = proto.GetExtension(tmplDescProto.GetOptions(), tmpl.E_TemplateName); err != nil {
return nil, nil, "", tmplVariety, fmt.Errorf(
"proto files %s is missing required template_name option", tmplDescProto.GetName())
}
if err = validateTmplName(*(nameExt.(*string))); err != nil {
return nil, nil, "", tmplVariety, err
}
return fds, tmplDescProto, *(nameExt.(*string)), tmplVariety, nil
}
// GetAdapterCfgDescriptor find an adapter configuration descriptor
func GetAdapterCfgDescriptor(base64Tmpl string) (*descriptor.FileDescriptorSet, *descriptor.FileDescriptorProto, error) {
if base64Tmpl == "" {
// no cfg is allowed
return &descriptor.FileDescriptorSet{}, &descriptor.FileDescriptorProto{}, nil
}
fds, err := decodeFds(base64Tmpl)
if err != nil {
return nil, nil, err
}
var cfgDesc *descriptor.FileDescriptorProto
for _, fd := range fds.File {
for _, msg := range fd.GetMessageType() {
if msg.GetName() == adapterCfgMsgName {
cfgDesc = fd
}
}
}
if cfgDesc == nil {
return nil, nil, fmt.Errorf("cannot find message named '%s' in the adapter configuration descriptor", adapterCfgMsgName)
}
return fds, cfgDesc, nil
}
var pkgLaskSegRegex = regexp.MustCompile("^[a-zA-Z]+$")
func validateTmplName(name string) error {
if !pkgLaskSegRegex.MatchString(name) {
return fmt.Errorf("the template name '%s' must match the regex '%s'", name, "^[a-zA-Z]+$")
}
return nil
}
func decodeFds(base64Fds string) (*descriptor.FileDescriptorSet, error) {
var err error
var bytes []byte
reader := strings.NewReader(base64Fds)
decoder := base64.NewDecoder(base64.StdEncoding, reader)
if bytes, err = ioutil.ReadAll(decoder); err != nil {
return nil, err
}
fds := &descriptor.FileDescriptorSet{}
if err = proto.Unmarshal(bytes, fds); err != nil {
return nil, err
}
return fds, nil
}